Portierung der Python/Tkinter-Vorlage nach C++17/Qt6
Die fuer Debian 13 geschriebene Fassung (Python + Tkinter + matplotlib)
laeuft jetzt als Windows-Anwendung mit Qt 6. Oberflaeche, Bedienlogik und
alle DSP-Verfahren sind unveraendert uebernommen.
- core/ DSP, Audio-IO und Export ohne GUI-Abhaengigkeit
- ui/ Hauptfenster, eigene Wellenform-Anzeige, Uebersichtsleiste,
ffplay-Wiedergabe, Hintergrund-Worker
- tools/ Selbsttest gegen die Python-Vorlage, Ende-zu-Ende-Test der
ffmpeg-Kette, Messwerkzeuge fuer den Geschwindigkeitsvergleich
- chopper/ unveraenderte Vorlage als Referenz fuer die Pruefungen
Verifikation: verify.ps1 vergleicht den Auslieferungsbau gegen die Vorlage
(alle diskreten Ergebnisse identisch) und zusaetzlich einen double-Bau
gegen eine float64-Referenz (260 Werte innerhalb von 1e-9).
Die 14 Commits der Python-Vorlage liegen als chopper-history.bundle bei
(chopper/ war unter Debian ein eigenes Repository).
This commit is contained in:
@@ -0,0 +1,382 @@
|
||||
// ---------------------------------------------------------------------------
|
||||
// Selbsttest: rechnet die DSP-Kette auf einem deterministischen Testsignal
|
||||
// und gibt alle relevanten Werte als "schluessel<TAB>wert" aus.
|
||||
//
|
||||
// tools/reference_dump.py erzeugt dieselben Zeilen mit den Originalfunktionen
|
||||
// aus sample_chopper4.py; tools/compare.py vergleicht beide Ausgaben. Damit
|
||||
// ist belegbar, dass die Portierung nach C++ rechnet wie die Vorlage.
|
||||
//
|
||||
// Aufruf: sc_selftest [ausgabe.txt]
|
||||
// ---------------------------------------------------------------------------
|
||||
#include "core/Analyze.h"
|
||||
#include "core/Dsp.h"
|
||||
#include "core/Fft.h"
|
||||
#include "core/Stats.h"
|
||||
|
||||
#include <QFile>
|
||||
#include <QString>
|
||||
#include <QTextStream>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr double kPi = 3.14159265358979323846;
|
||||
constexpr int kSr = 22050;
|
||||
constexpr double kSeconds = 4.0;
|
||||
|
||||
QTextStream* g_out = nullptr;
|
||||
|
||||
void emitLine(const QString& line) {
|
||||
*g_out << line << '\n';
|
||||
// Sofort schreiben: bei einem Absturz ist sonst nicht zu sehen, wie weit
|
||||
// der Test gekommen ist.
|
||||
g_out->flush();
|
||||
}
|
||||
|
||||
/// %.10g wie in tools/reference_dump.py -- so sind beide Ausgaben direkt
|
||||
/// vergleichbar, auch ohne die Zahlen vorher zu parsen.
|
||||
void emitValue(const QString& key, double value) {
|
||||
char buffer[64];
|
||||
std::snprintf(buffer, sizeof(buffer), "%.10g", value);
|
||||
*g_out << key << '\t' << QLatin1String(buffer) << '\n';
|
||||
g_out->flush();
|
||||
}
|
||||
|
||||
void emitInt(const QString& key, long long value) {
|
||||
*g_out << key << '\t' << value << '\n';
|
||||
g_out->flush();
|
||||
}
|
||||
|
||||
/// Volle Genauigkeit -- fuer die Diagnose einzelner Zwischenwerte.
|
||||
void emitPrecise(const QString& key, double value) {
|
||||
char buffer[64];
|
||||
std::snprintf(buffer, sizeof(buffer), "%.17g", value);
|
||||
*g_out << key << '\t' << QLatin1String(buffer) << '\n';
|
||||
g_out->flush();
|
||||
}
|
||||
|
||||
/// Deterministisches Testsignal -- identisch in tools/reference_dump.py.
|
||||
///
|
||||
/// Drei Toene (220 / 440 / 330 Hz) mit zwei echten Pausen dazwischen und einem
|
||||
/// ausklingenden Ende, plus sehr leises Rauschen aus einem LCG. Damit sind
|
||||
/// alle Detektoren adressiert: Pausen (Einsaetze), Tonwechsel (Pitch),
|
||||
/// Anschlaege (Transienten) und ein Rauschboden fuer die Auto-Schwelle.
|
||||
///
|
||||
/// Die Werte werden auf ein 1e-4-Raster gerundet. Ohne das unterscheiden sich
|
||||
/// die Signale diesseits und jenseits der Sprachgrenze um ein bis zwei
|
||||
/// float32-ULPs, weil sin() in libm und in numpy nicht bitgleich ist. Der Flux
|
||||
/// ist eine Summe fast gleich grosser Differenzen und verstaerkt so einen
|
||||
/// 1e-7-Unterschied im Sample auf ~1e-6 im Ergebnis -- genug, um den Vergleich
|
||||
/// der beiden Implementierungen unbrauchbar zu machen. Das Raster ist fein
|
||||
/// genug, dass es die Analyse nicht beeinflusst (Amplitude 0.5 -> 5000 Stufen).
|
||||
sc::Samples makeSignal() {
|
||||
const std::size_t n = std::size_t(kSeconds * kSr);
|
||||
sc::Samples x(n);
|
||||
std::uint32_t state = 12345u;
|
||||
for (std::size_t i = 0; i < n; ++i) {
|
||||
const double t = double(i) / double(kSr);
|
||||
double f = 0.0;
|
||||
double a = 0.0;
|
||||
if (t < 0.8) {
|
||||
f = 220.0;
|
||||
a = 0.5;
|
||||
} else if (t < 1.4) {
|
||||
a = 0.0;
|
||||
} else if (t < 2.2) {
|
||||
f = 440.0;
|
||||
a = 0.5;
|
||||
} else if (t < 2.6) {
|
||||
a = 0.0;
|
||||
} else {
|
||||
f = 330.0;
|
||||
a = 0.5 * std::exp(-(t - 2.6) * 0.5);
|
||||
}
|
||||
state = state * 1664525u + 1013904223u;
|
||||
const double noise = (double(state) / 4294967296.0) * 2.0 - 1.0;
|
||||
const double raw = a * std::sin(2.0 * kPi * f * t) + 0.001 * noise;
|
||||
// floor(x+0.5) statt round(): gleiche Rundung in beiden Sprachen.
|
||||
x[i] = float(std::floor(raw * 10000.0 + 0.5) / 10000.0);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
void dumpCurveStats(const QString& prefix, const sc::Series& values) {
|
||||
double sum = 0.0;
|
||||
double mx = 0.0;
|
||||
for (double v : values) {
|
||||
sum += v;
|
||||
mx = std::max(mx, v);
|
||||
}
|
||||
emitInt(prefix + "_n", (long long)values.size());
|
||||
emitValue(prefix + "_sum", sum);
|
||||
emitValue(prefix + "_max", mx);
|
||||
if (!values.empty()) emitValue(prefix + "_mid", values[values.size() / 2]);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
QFile file;
|
||||
QTextStream fileStream;
|
||||
QTextStream console(stdout);
|
||||
QTextStream* out = &console;
|
||||
|
||||
if (argc > 1) {
|
||||
file.setFileName(QString::fromLocal8Bit(argv[1]));
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
std::fprintf(stderr, "kann %s nicht schreiben\n", argv[1]);
|
||||
return 2;
|
||||
}
|
||||
fileStream.setDevice(&file);
|
||||
out = &fileStream;
|
||||
}
|
||||
g_out = out;
|
||||
|
||||
const sc::Samples signal = makeSignal();
|
||||
emitInt("samples_n", (long long)signal.size());
|
||||
|
||||
// ---- frame_rms -------------------------------------------------------
|
||||
const sc::Curve rms = sc::frameRms(signal, kSr);
|
||||
dumpCurveStats("rms", rms.values);
|
||||
emitValue("rms_t0", rms.times.empty() ? 0.0 : rms.times.front());
|
||||
emitValue("rms_t1", rms.times.empty() ? 0.0 : rms.times.back());
|
||||
|
||||
// ---- spectral_flux / flux_envelope -----------------------------------
|
||||
const sc::Curve flux = sc::spectralFlux(signal, kSr);
|
||||
dumpCurveStats("flux", flux.values);
|
||||
|
||||
const sc::Curve env = sc::fluxEnvelope(signal, kSr);
|
||||
dumpCurveStats("env", env.values);
|
||||
|
||||
{ // Diagnose: woher kommt der Unterschied bei env_mid?
|
||||
sc::Series logFlux = flux.values;
|
||||
for (double& v : logFlux) v = std::log1p(v);
|
||||
const double base = sc::median(logFlux);
|
||||
const double top = sc::percentile(logFlux, 98.0);
|
||||
emitPrecise("diag_base", base);
|
||||
emitPrecise("diag_top", top);
|
||||
emitPrecise("diag_span", top - base);
|
||||
for (std::size_t idx : {std::size_t(0), std::size_t(1), std::size_t(100),
|
||||
std::size_t(399), std::size_t(400), std::size_t(700)}) {
|
||||
if (idx < flux.values.size()) {
|
||||
emitPrecise(QStringLiteral("diag_rawflux_%1").arg(idx), flux.values[idx]);
|
||||
emitPrecise(QStringLiteral("diag_logflux_%1").arg(idx), logFlux[idx]);
|
||||
emitPrecise(QStringLiteral("diag_env_%1").arg(idx), env.values[idx]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{ // Diagnose: Genauigkeit der eigenen FFT gegen numpy fuer EINEN Frame.
|
||||
constexpr int kFrame = 170;
|
||||
constexpr int kFftFrame = 1024;
|
||||
constexpr int kFftHop = 256;
|
||||
sc::Fft fft(kFftFrame);
|
||||
const sc::Series win = sc::hanning(kFftFrame);
|
||||
std::vector<double> buf(kFftFrame);
|
||||
for (int j = 0; j < kFftFrame; ++j)
|
||||
buf[std::size_t(j)] =
|
||||
double(signal[std::size_t(kFrame * kFftHop + j)]) * win[std::size_t(j)];
|
||||
std::vector<double> mag(std::size_t(kFftFrame / 2 + 1));
|
||||
fft.rfftMagnitude(buf.data(), mag.data());
|
||||
for (int k : {0, 1, 2, 10, 100, 256, 512})
|
||||
emitPrecise(QStringLiteral("diag_mag_%1").arg(k), mag[std::size_t(k)]);
|
||||
}
|
||||
|
||||
// ---- onsets_from_flux bei mehreren Empfindlichkeiten -----------------
|
||||
for (double sens : {0.15, 0.35, 0.5, 0.65, 0.9}) {
|
||||
sc::Series thr;
|
||||
sc::Series onsets;
|
||||
sc::onsetsFromFlux(env, sens, kSr, thr, onsets);
|
||||
const QString tag = QStringLiteral("onsets_%1").arg(sens, 0, 'f', 2);
|
||||
emitInt(tag + "_n", (long long)onsets.size());
|
||||
for (std::size_t i = 0; i < onsets.size() && i < 24; ++i)
|
||||
emitValue(QStringLiteral("%1_%2").arg(tag).arg(i), onsets[i]);
|
||||
double thrSum = 0.0;
|
||||
for (double v : thr) thrSum += v;
|
||||
emitValue(tag + "_thrsum", thrSum);
|
||||
}
|
||||
|
||||
// ---- Region-Starts / silence_runs ------------------------------------
|
||||
const std::vector<sc::Segment> regions =
|
||||
sc::keepSegments(rms.times, rms.values, kSeconds, -40.0, 200.0, 20.0);
|
||||
emitInt("keep_n", (long long)regions.size());
|
||||
for (std::size_t i = 0; i < regions.size(); ++i) {
|
||||
emitValue(QStringLiteral("keep_%1_start").arg(i), regions[i].start);
|
||||
emitValue(QStringLiteral("keep_%1_end").arg(i), regions[i].end);
|
||||
}
|
||||
|
||||
const sc::Series runs = sc::silenceRuns(rms.times, rms.values, -40.0);
|
||||
emitInt("silence_runs_n", (long long)runs.size());
|
||||
for (std::size_t i = 0; i < runs.size() && i < 12; ++i)
|
||||
emitValue(QStringLiteral("silence_run_%1").arg(i), runs[i]);
|
||||
|
||||
const sc::Series regionStarts =
|
||||
sc::detectRegionStarts(rms.times, rms.values, kSeconds, -40.0, 200.0);
|
||||
emitInt("region_starts_n", (long long)regionStarts.size());
|
||||
for (std::size_t i = 0; i < regionStarts.size() && i < 12; ++i)
|
||||
emitValue(QStringLiteral("region_start_%1").arg(i), regionStarts[i]);
|
||||
|
||||
// ---- pitch_track / detect_pitch_changes ------------------------------
|
||||
const sc::PitchCurve pitch = sc::pitchTrack(signal, kSr);
|
||||
emitInt("pitch_n", (long long)pitch.times.size());
|
||||
for (std::size_t i = 0; i < pitch.times.size(); i += 20) {
|
||||
emitValue(QStringLiteral("pitch_f0_%1").arg(i), pitch.f0[i]);
|
||||
emitValue(QStringLiteral("pitch_conf_%1").arg(i), pitch.conf[i]);
|
||||
}
|
||||
const sc::Series pitchChanges =
|
||||
sc::detectPitchChanges(pitch, rms.times, rms.values, -40.0);
|
||||
emitInt("pitch_changes_n", (long long)pitchChanges.size());
|
||||
for (std::size_t i = 0; i < pitchChanges.size(); ++i)
|
||||
emitValue(QStringLiteral("pitch_change_%1").arg(i), pitchChanges[i]);
|
||||
|
||||
// ---- Cut-Aufbereitung ------------------------------------------------
|
||||
const sc::Series cutsIn{0.10, 0.42, 0.44, 0.80, 1.05, 1.62, 2.00, 2.05,
|
||||
2.60, 3.10, 3.11, 3.55};
|
||||
const sc::Series fluxCuts = sc::detectCuts(signal, kSr,
|
||||
[] {
|
||||
sc::DetectorSet s;
|
||||
s.set(sc::Detector::Transient);
|
||||
s.set(sc::Detector::Onset);
|
||||
s.set(sc::Detector::Pitch);
|
||||
return s;
|
||||
}(),
|
||||
0.5, -40.0, 200.0, kSeconds);
|
||||
|
||||
auto dumpSeries = [&](const QString& tag, const sc::Series& v, std::size_t limit) {
|
||||
emitInt(tag + "_n", (long long)v.size());
|
||||
for (std::size_t i = 0; i < v.size() && i < limit; ++i)
|
||||
emitValue(QStringLiteral("%1_%2").arg(tag).arg(i), v[i]);
|
||||
};
|
||||
|
||||
dumpSeries("detect_cuts", fluxCuts, 64);
|
||||
|
||||
const sc::Series minLen = sc::enforceLengthConstraints(cutsIn, kSeconds, 0.35, 0.0);
|
||||
dumpSeries("enforce_min", minLen, 64);
|
||||
const sc::Series maxLen = sc::enforceLengthConstraints(cutsIn, kSeconds, 0.0, 0.5);
|
||||
dumpSeries("enforce_max", maxLen, 64);
|
||||
|
||||
const sc::Series cap = sc::capByStrength(fluxCuts, env.times, env.values, 5);
|
||||
dumpSeries("cap5", cap, 64);
|
||||
|
||||
const sc::Series pause = sc::pauseBeforeCuts(cutsIn, rms.times, rms.values, -40.0);
|
||||
dumpSeries("pause", pause, 64);
|
||||
|
||||
const sc::Series scores =
|
||||
sc::cutScores(cutsIn, env.times, env.values, rms.times, rms.values, -40.0);
|
||||
dumpSeries("scores", scores, 64);
|
||||
|
||||
const sc::Series phrase = sc::selectPhraseCuts(cutsIn, scores, kSeconds, 0.6);
|
||||
dumpSeries("phrase", phrase, 64);
|
||||
|
||||
sc::FluxProvider provider = [&] { return std::make_pair(env.times, env.values); };
|
||||
const sc::Series finalized = sc::finalizeCuts(cutsIn, kSeconds, 0.5, 0.0, 0, true,
|
||||
provider, &rms.times, &rms.values, -40.0);
|
||||
dumpSeries("finalized_phrase", finalized, 64);
|
||||
|
||||
const sc::Series finalizedHits =
|
||||
sc::finalizeCuts(cutsIn, kSeconds, 0.3, 0.4, 9, false, provider, &rms.times,
|
||||
&rms.values, -40.0);
|
||||
dumpSeries("finalized_hits", finalizedHits, 64);
|
||||
|
||||
const sc::Series dropped = sc::dropExcluded(cutsIn, sc::Series{0.44, 2.05}, 0.01);
|
||||
dumpSeries("drop_excluded", dropped, 64);
|
||||
|
||||
// ---- rise_times / stable_sensitivity ---------------------------------
|
||||
const sc::Series rises = sc::riseTimes(regionStarts, rms.times, rms.values);
|
||||
dumpSeries("rises", rises, 16);
|
||||
emitValue("rise_median_ms",
|
||||
rises.empty() ? 0.0 : sc::median(rises) * 1000.0);
|
||||
|
||||
sc::Series counts;
|
||||
const double stableSens =
|
||||
sc::stableSensitivity(env.times, env.values, kSr, counts);
|
||||
emitValue("stable_sens", stableSens);
|
||||
dumpSeries("stable_counts", counts, 32);
|
||||
|
||||
// ---- analyze_material ------------------------------------------------
|
||||
sc::MaterialInfo info;
|
||||
const sc::FileSettings analysed =
|
||||
sc::analyzeMaterial(signal, kSr, rms.times, rms.values, kSeconds,
|
||||
QStringLiteral("phrases"), &env, &pitch, &info);
|
||||
emitValue("auto_thresh", analysed.thresh);
|
||||
emitValue("auto_minsil", analysed.minsil);
|
||||
emitValue("auto_pad", analysed.pad);
|
||||
emitValue("auto_sens", analysed.sens);
|
||||
emitLine("auto_mode\t" + sc::modeToStr(analysed.mode));
|
||||
emitValue("auto_minlen_ms", analysed.minLenMs);
|
||||
emitValue("auto_maxlen_ms", analysed.maxLenMs);
|
||||
emitInt("auto_phrase", analysed.phrase ? 1 : 0);
|
||||
emitValue("auto_semitones", analysed.semitones);
|
||||
emitValue("info_floor_db", info.floorDb);
|
||||
emitValue("info_loud_db", info.loudDb);
|
||||
emitValue("info_rise_ms", info.riseMs);
|
||||
emitValue("info_silence_frac", info.silenceFrac);
|
||||
emitValue("info_onsets_per_s", info.onsetsPerS);
|
||||
emitValue("info_voiced_frac", info.voicedFrac);
|
||||
emitValue("info_region_gap", info.regionGap);
|
||||
emitValue("info_ioi", info.ioi);
|
||||
emitInt("info_regions", info.regions);
|
||||
emitInt("info_onsets", info.onsets);
|
||||
emitInt("info_pitch_changes", info.pitchChanges);
|
||||
emitValue("info_min_len_s", info.minLenS);
|
||||
dumpSeries("info_counts", info.counts, 32);
|
||||
|
||||
const sc::FileSettings hits =
|
||||
sc::analyzeMaterial(signal, kSr, rms.times, rms.values, kSeconds,
|
||||
QStringLiteral("hits"), &env, &pitch, nullptr);
|
||||
emitValue("hits_minlen_ms", hits.minLenMs);
|
||||
emitInt("hits_phrase", hits.phrase ? 1 : 0);
|
||||
emitLine("hits_mode\t" + sc::modeToStr(hits.mode));
|
||||
|
||||
// ---- Anzeige / Export-Helfer -----------------------------------------
|
||||
sc::Series px, pmins, pmaxs;
|
||||
sc::waveformPeaksRange(signal, kSr, 0.5, 2.5, 400, px, pmins, pmaxs);
|
||||
emitInt("peaks_n", (long long)px.size());
|
||||
if (!px.empty()) {
|
||||
emitValue("peaks_x0", px.front());
|
||||
emitValue("peaks_x1", px.back());
|
||||
}
|
||||
{ // Pruefsumme ueber alle Buckets -- empfindlich gegen Verschiebungen
|
||||
double sumMin = 0.0;
|
||||
double sumMax = 0.0;
|
||||
for (double v : pmins) sumMin += v;
|
||||
for (double v : pmaxs) sumMax += v;
|
||||
emitValue("peaks_minsum", sumMin);
|
||||
emitValue("peaks_maxsum", sumMax);
|
||||
}
|
||||
|
||||
// Nulldurchgangs-Referenz: 220-Hz-Sinus, wie ihn decode_mono() liefern
|
||||
// wuerde (die echte Dekodierung haengt an ffmpeg und gehoert nicht hierher).
|
||||
sc::Samples zcRef(std::size_t(kSeconds * kSr));
|
||||
for (std::size_t i = 0; i < zcRef.size(); ++i)
|
||||
zcRef[i] = float(0.5 * std::sin(2.0 * kPi * 220.0 * double(i) / kSr));
|
||||
for (double t : {0.5, 1.0, 2.0, 3.0}) {
|
||||
emitValue(QStringLiteral("snap_%1").arg(t, 0, 'f', 1),
|
||||
sc::snapToZeroCrossing(zcRef, kSr, t, 10.0));
|
||||
}
|
||||
|
||||
const double peak = sc::measurePeak(&signal, &signal, QStringLiteral("stereo"),
|
||||
1.4, 2.2, kSr);
|
||||
emitValue("measure_peak_stereo", peak);
|
||||
const double peakL = sc::measurePeak(&signal, &signal, QStringLiteral("l"),
|
||||
1.4, 2.2, kSr);
|
||||
emitValue("measure_peak_l", peakL);
|
||||
|
||||
// ---- Modus-Persistenz ------------------------------------------------
|
||||
emitLine("mode_roundtrip\t" + sc::modeToStr(sc::parseMode(QStringLiteral("both"))));
|
||||
emitLine("mode_legacy\t" + sc::modeToStr(sc::parseMode(QStringLiteral("pitch+onset"))));
|
||||
emitLine("mode_off\t" + sc::modeToStr(sc::parseMode(QStringLiteral(""))));
|
||||
|
||||
if (file.isOpen()) {
|
||||
out->flush();
|
||||
file.close();
|
||||
} else {
|
||||
out->flush();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user