// --------------------------------------------------------------------------- // Ende-zu-Ende-Test der ffmpeg-Anbindung. // // Der Selbsttest (sc_selftest) prueft nur die DSP-Schicht gegen die // Python-Referenz -- er fasst ffmpeg nicht an. Hier laeuft die komplette // Kette auf einer echten Audiodatei: dekodieren, analysieren, Nulldurchgangs- // Referenz holen, "Stille entfernt" exportieren, Chops exportieren, und die // Ergebnisse anschliessend nachmessen. // // Aufruf: sc_e2e // --------------------------------------------------------------------------- #include "core/Analyze.h" #include "core/AudioIO.h" #include "core/Dsp.h" #include "core/Exporter.h" #include #include #include #include #include #include #include namespace { int g_failures = 0; void check(bool ok, const QString& what) { std::printf("%s %s\n", ok ? "[ok] " : "[FEHL]", qPrintable(what)); if (!ok) ++g_failures; } /// Misst die Dauer einer Audiodatei ueber ffprobe/ffmpeg. double probeDuration(const QString& path) { QProcess proc; sc::configureChildProcess(proc); proc.start(sc::ffmpegPath(), {QStringLiteral("-v"), QStringLiteral("error"), QStringLiteral("-i"), path, QStringLiteral("-f"), QStringLiteral("f32le"), QStringLiteral("-ac"), QStringLiteral("1"), QStringLiteral("-ar"), QStringLiteral("1000"), QStringLiteral("-")}); if (!proc.waitForStarted(10000)) return -1.0; QByteArray out; while (proc.state() != QProcess::NotRunning) { proc.waitForReadyRead(50); out += proc.readAllStandardOutput(); proc.readAllStandardError(); } out += proc.readAllStandardOutput(); return double(out.size() / 4) / 1000.0; // 1000 float32-Samples je Sekunde } } // namespace int main(int argc, char** argv) { QCoreApplication app(argc, argv); if (argc < 3) { std::fprintf(stderr, "Aufruf: sc_e2e \n"); return 2; } const QString src = QString::fromLocal8Bit(argv[1]); const QString outDir = QString::fromLocal8Bit(argv[2]); std::printf("Quelle: %s\nZiel: %s\n\n", qPrintable(src), qPrintable(outDir)); check(sc::ffmpegAvailable(), QStringLiteral("ffmpeg im PATH gefunden")); check(sc::ffplayAvailable(), QStringLiteral("ffplay im PATH gefunden")); check(QFileInfo::exists(src), QStringLiteral("Quelldatei vorhanden")); if (g_failures > 0) return 1; QDir().mkpath(outDir); // ---- 1. Dekodieren --------------------------------------------------- sc::DecodedPtr audio; try { audio = sc::loadAudio(src); } catch (const std::exception& e) { std::printf("[FEHL] Dekodieren: %s\n", e.what()); return 1; } std::printf("dekodiert: %zu Frames, %.3f s, %zu Huellkurven-Stuetzstellen\n", audio->frames(), audio->dur, audio->rms.size()); check(audio->dur > 1.0, QStringLiteral("Dekodierung liefert > 1 s Audio")); check(audio->combined.size() == audio->left.size() && audio->left.size() == audio->right.size(), QStringLiteral("linker/rechter/kombinierter Kanal gleich lang")); // ---- 2. Analysieren -------------------------------------------------- const sc::Curve flux = sc::fluxEnvelope(audio->combined, sc::ANALYSIS_SR); const sc::PitchCurve pitch = sc::pitchTrack(audio->combined, sc::ANALYSIS_SR); sc::Series thr; sc::Series onsets; sc::onsetsFromFlux(flux, 0.5, sc::ANALYSIS_SR, thr, onsets); std::printf("Flux: %zu Werte, Onsets (Intensitaet 0.5): %zu\n", flux.values.size(), onsets.size()); sc::DetectorSet mode; mode.set(sc::Detector::Transient); mode.set(sc::Detector::Onset); const sc::Curve envelope{audio->times, audio->rms}; const sc::Series cuts = sc::detectCuts(audio->combined, sc::ANALYSIS_SR, mode, 0.5, -40.0, 200.0, audio->dur, 40.0, &flux, &envelope, nullptr); std::printf("Chop-Punkte: %zu\n", cuts.size()); check(!cuts.empty(), QStringLiteral("Chop-Erkennung findet etwas")); // Auto-Analyse ueber die echte Datei sc::MaterialInfo info; const sc::FileSettings settings = sc::analyzeMaterial( audio->combined, sc::ANALYSIS_SR, audio->times, audio->rms, audio->dur, QStringLiteral("phrases"), &flux, &pitch, &info); std::printf("Auto: Modus=%s, Schwelle=%.1f dB, min_len=%.0f ms, Rand=%.0f ms\n", qPrintable(sc::modeToStr(settings.mode)), settings.thresh, settings.minLenMs, settings.pad); check(!settings.mode.empty(), QStringLiteral("Auto-Analyse waehlt einen Detektor")); // ---- 3. Nulldurchgangs-Referenz + Zero-Crossing ---------------------- sc::Samples zcRef; try { zcRef = sc::decodeMono(src, sc::ZC_SR); } catch (const std::exception& e) { std::printf("[FEHL] Mono-Dekodierung: %s\n", e.what()); return 1; } std::printf("zc-Referenz: %zu Samples bei %d Hz\n", zcRef.size(), sc::ZC_SR); check(!zcRef.empty(), QStringLiteral("Mono-Dekodierung fuer Zero-Crossing")); const double t0 = audio->dur * 0.5; const double snapped = sc::snapToZeroCrossing(zcRef, sc::ZC_SR, t0, 10.0); std::printf("Zero-Crossing: %.6f -> %.6f (Verschiebung %.3f ms)\n", t0, snapped, (snapped - t0) * 1000.0); check(std::abs(snapped - t0) <= 0.0101, QStringLiteral("Zero-Crossing bleibt im Suchfenster von +-10 ms")); sc::ExportOptions options; options.bits = 24; options.rate = QStringLiteral("44100"); options.channelMode = QStringLiteral("stereo"); options.zeroCrossing = true; // ---- 4. "Stille entfernt" exportieren -------------------------------- const std::vector keeps = sc::keepSegments(audio->times, audio->rms, audio->dur, settings.thresh, settings.minsil, settings.pad); std::printf("Keep-Segmente: %zu\n", keeps.size()); const QString trimmed = QDir(outDir).filePath(QStringLiteral("trimmed.wav")); try { sc::exportSilenceRemoved(src, trimmed, keeps, options, &zcRef, sc::ZC_SR); } catch (const std::exception& e) { std::printf("[FEHL] Export 'Stille entfernt': %s\n", e.what()); return 1; } check(QFileInfo::exists(trimmed), QStringLiteral("trimmed.wav angelegt")); double keptSeconds = 0.0; for (const sc::Segment& s : keeps) keptSeconds += s.end - s.start; const double trimmedDur = probeDuration(trimmed); std::printf("trimmed.wav: %.3f s (erwartet ~%.3f s)\n", trimmedDur, keptSeconds); check(std::abs(trimmedDur - keptSeconds) < 0.25, QStringLiteral("Laenge von trimmed.wav passt zur Summe der Keep-Segmente")); // ---- 5. Chops exportieren (mit Normalisierung, Monokanal) ------------ sc::ExportOptions chopOptions = options; chopOptions.bits = 16; chopOptions.rate = QStringLiteral("Original"); chopOptions.channelMode = QStringLiteral("combined"); chopOptions.normalize = true; const QString chopDir = QDir(outDir).filePath(QStringLiteral("chops")); int chopCount = 0; try { chopCount = sc::exportChops(src, chopDir, cuts, audio->dur, chopOptions, &zcRef, sc::ZC_SR, 10.0, &audio->left, &audio->right, sc::ANALYSIS_SR, QStringLiteral("Test"), 5.0); } catch (const std::exception& e) { std::printf("[FEHL] Chop-Export: %s\n", e.what()); return 1; } std::printf("Chops exportiert: %d\n", chopCount); check(chopCount > 0, QStringLiteral("mindestens ein Chop geschrieben")); const QString firstChop = QDir(chopDir).filePath(QStringLiteral("Test_001.wav")); check(QFileInfo::exists(firstChop), QStringLiteral("Chop-Titel wird als Dateiname verwendet (Test_001.wav)")); // Alle Chops muessen eine messbare Laenge haben. int measured = 0; for (int i = 1; i <= chopCount; ++i) { const QString path = QDir(chopDir).filePath(QStringLiteral("Test_%1.wav").arg(i, 3, 10, QLatin1Char('0'))); if (!QFileInfo::exists(path)) continue; if (probeDuration(path) > 0.01) ++measured; } check(measured == chopCount, QStringLiteral("alle %1 Chops haben eine messbare Laenge").arg(chopCount)); std::printf("\n%s (%d Fehler)\n", g_failures == 0 ? "E2E OK" : "E2E FEHLGESCHLAGEN", g_failures); return g_failures == 0 ? 0 : 1; }