From 096012b8fd8d059723be036c42d37bfbb7a51afe Mon Sep 17 00:00:00 2001 From: Gabriel Clark Date: Mon, 27 Jul 2026 16:55:10 -0700 Subject: [PATCH] fix: make Windows voice cloning reliable --- .github/workflows/build.yml | 32 ++++ CMakeLists.txt | 14 ++ src/spk-encoder.cpp | 320 ++++++++++++++++++++++++++++++-- src/spk-encoder.h | 4 +- tests/fake-ffmpeg.cpp | 40 ++++ tests/ffmpeg-decode-smoke.cpp | 28 +++ tests/ui-speaker-upload.test.js | 70 +++++++ web/tts_ui.html | 14 +- 8 files changed, 501 insertions(+), 21 deletions(-) create mode 100644 tests/fake-ffmpeg.cpp create mode 100644 tests/ffmpeg-decode-smoke.cpp create mode 100644 tests/ui-speaker-upload.test.js diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c80db78..e4be7a4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -370,6 +370,38 @@ jobs: -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache - name: Build run: cmake --build build -j + - name: Build voice-cloning regression tests + run: cmake --build build --target fake-ffmpeg ffmpeg-decode-smoke -j + - name: Windows ffmpeg process-launch test + shell: pwsh + run: | + $fake = Get-ChildItem build -Recurse -Filter fake-ffmpeg.exe | Select-Object -First 1 + $smoke = Get-ChildItem build -Recurse -Filter ffmpeg-decode-smoke.exe | Select-Object -First 1 + if (-not $fake -or -not $smoke) { throw "voice-cloning test binaries not found" } + + $testDir = Join-Path $env:RUNNER_TEMP 'zonos2 ffmpeg % & smoke' + New-Item -ItemType Directory -Force -Path $testDir | Out-Null + $input = Join-Path $testDir 'reference voice % &.wav' + [System.IO.File]::WriteAllText($input, 'fake audio input') + + # The packaged launcher sets an absolute executable path. Both that path + # and the uploaded audio path deliberately contain cmd.exe metacharacters. + $envFfmpeg = Join-Path $testDir 'ffmpeg % & helper.exe' + Copy-Item $fake.FullName $envFfmpeg + $env:ZONOS2_FFMPEG = $envFfmpeg + & $smoke.FullName $input + if ($LASTEXITCODE -ne 0) { throw "ZONOS2_FFMPEG launch test failed" } + + # The standalone CLI also promises to discover a system ffmpeg on PATH. + $pathDir = Join-Path $testDir 'path fallback' + New-Item -ItemType Directory -Force -Path $pathDir | Out-Null + Copy-Item $fake.FullName (Join-Path $pathDir 'ffmpeg.exe') + Remove-Item Env:ZONOS2_FFMPEG + $env:PATH = "$pathDir;$env:PATH" + & $smoke.FullName $input + if ($LASTEXITCODE -ne 0) { throw "PATH ffmpeg launch test failed" } + - name: Web UI speaker-upload regression test + run: node --test tests/ui-speaker-upload.test.js - name: App smoke test shell: bash run: | diff --git a/CMakeLists.txt b/CMakeLists.txt index 3fb7005..e6d8538 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,6 +87,20 @@ add_executable(spk-encoder-cli src/spk-encoder-cli.cpp src/spk-encoder.cpp) target_include_directories(spk-encoder-cli PRIVATE src ggml/include) target_link_libraries(spk-encoder-cli PRIVATE ggml) +# Decoder-only smoke test. EXCLUDE_FROM_ALL keeps release builds unchanged; CI builds +# it explicitly to exercise ffmpeg process launch and binary stdout without a model. +add_executable(ffmpeg-decode-smoke EXCLUDE_FROM_ALL + tests/ffmpeg-decode-smoke.cpp + src/spk-encoder.cpp +) +target_include_directories(ffmpeg-decode-smoke PRIVATE src ggml/include) +target_link_libraries(ffmpeg-decode-smoke PRIVATE ggml) + +if (WIN32) + # Deterministic stand-in for ffmpeg in the Windows process-launch regression test. + add_executable(fake-ffmpeg EXCLUDE_FROM_ALL tests/fake-ffmpeg.cpp) +endif() + # --- DAC vocoder decoder CLI (codes -> wav) --- add_executable(dac-cli src/dac-cli.cpp src/dac.cpp) target_include_directories(dac-cli PRIVATE src ggml/include) diff --git a/src/spk-encoder.cpp b/src/spk-encoder.cpp index 33a5ae4..17c5e97 100644 --- a/src/spk-encoder.cpp +++ b/src/spk-encoder.cpp @@ -2,6 +2,14 @@ // (marksverdhei/Qwen3-Voice-Embedding-12Hz-1.7B, actually a ~6M-param ECAPA-TDNN). // log-mel [128,T] -> 2048-d x-vector. Optional in-graph mel frontend from a 24 kHz // mono waveform (STFT via DFT matmul + slaney mel + log). CPU backend. +#ifdef _WIN32 +// STARTUPINFOEX and PROC_THREAD_ATTRIBUTE_HANDLE_LIST are available on Vista+. +// Define the floor before any project or system header can include windows.h. +#ifndef _WIN32_WINNT +#define _WIN32_WINNT 0x0600 +#endif +#endif + #include "compat.h" #include "spk-encoder.h" #include "exe-path.h" @@ -11,12 +19,17 @@ #include "ggml-backend.h" #include "gguf.h" +#include +#include #include #include #include #include #include +#include +#include #include +#include #include bool spk_load(spk_model & m, const char * path) { @@ -288,45 +301,321 @@ std::vector spk_embed_from_pcm24k(const spk_model & m, const float * wav, } static std::string shell_quote_path(const char * path) { -#ifdef _WIN32 - return "\"" + std::string(path) + "\""; -#else std::string q = "'"; for (const char * p = path; *p; ++p) q += (*p == '\'') ? "'\\''" : std::string(1, *p); return q + "'"; -#endif } // Locate the ffmpeg used to decode reference audio for cloning. The launcher and the // desktop app download an ffmpeg on demand and point us at it via ZONOS2_FFMPEG; we // also look next to the executable (dist layout / macOS bundle Resources) before // falling back to a bare "ffmpeg" on PATH. -static std::string resolve_ffmpeg() { - if (const char * e = getenv("ZONOS2_FFMPEG"); e && *e) return e; +static std::filesystem::path resolve_ffmpeg() { namespace fs = std::filesystem; #ifdef _WIN32 - const char * name = "ffmpeg.exe"; + if (const wchar_t * e = _wgetenv(L"ZONOS2_FFMPEG"); e && *e) return fs::path(e); + const fs::path name = L"ffmpeg.exe"; #else - const char * name = "ffmpeg"; + if (const char * e = getenv("ZONOS2_FFMPEG"); e && *e) return fs::path(e); + const fs::path name = "ffmpeg"; #endif std::error_code ec; const fs::path dir = zonos2_exe_dir(); for (const fs::path c : { dir / name, dir / "bin" / name, dir / ".." / "Resources" / name }) - if (fs::exists(c, ec)) return c.lexically_normal().string(); + if (fs::exists(c, ec)) return c.lexically_normal(); return name; // rely on PATH } +#ifdef _WIN32 +namespace { + +// A small owning wrapper keeps every process/pipe handle on one cleanup path. +class win_handle { +public: + win_handle() = default; + explicit win_handle(HANDLE handle) : handle_(handle) {} + ~win_handle() { reset(); } + + win_handle(const win_handle &) = delete; + win_handle & operator=(const win_handle &) = delete; + + HANDLE get() const { return handle_; } + bool valid() const { return handle_ != nullptr && handle_ != INVALID_HANDLE_VALUE; } + + void reset(HANDLE handle = INVALID_HANDLE_VALUE) { + if (valid()) CloseHandle(handle_); + handle_ = handle; + } + +private: + HANDLE handle_ = INVALID_HANDLE_VALUE; +}; + +static void log_windows_error(const char * what, DWORD error) { + fprintf(stderr, "spk: %s (Windows error %lu)\n", what, (unsigned long) error); +} + +// CreateProcessW does not search PATH when lpApplicationName is supplied as a bare +// filename. Resolve that case ourselves, while keeping explicit paths explicit. +static std::filesystem::path resolve_windows_executable(const std::filesystem::path & requested) { + namespace fs = std::filesystem; + if (requested.has_parent_path()) { + std::error_code ec; + const fs::path absolute = fs::absolute(requested, ec); + return ec ? requested : absolute.lexically_normal(); + } + + std::vector buffer(32768); + const DWORD n = SearchPathW( + nullptr, + requested.c_str(), + nullptr, + (DWORD) buffer.size(), + buffer.data(), + nullptr); + if (n == 0 || n >= buffer.size()) return {}; + return fs::path(buffer.data(), buffer.data() + n); +} + +// Quote one argv element according to the parsing rules used by the Microsoft C +// runtime. This is argument quoting only; no command shell is involved. +static std::wstring quote_windows_argument(const std::wstring & argument) { + const bool needs_quotes = argument.empty() || std::any_of( + argument.begin(), + argument.end(), + [](wchar_t c) { return std::iswspace(c) || c == L'"'; }); + if (!needs_quotes) return argument; + + std::wstring quoted(1, L'"'); + size_t backslashes = 0; + for (const wchar_t c : argument) { + if (c == L'\\') { + ++backslashes; + } else if (c == L'"') { + quoted.append(backslashes * 2 + 1, L'\\'); + quoted.push_back(L'"'); + backslashes = 0; + } else { + quoted.append(backslashes, L'\\'); + backslashes = 0; + quoted.push_back(c); + } + } + quoted.append(backslashes * 2, L'\\'); + quoted.push_back(L'"'); + return quoted; +} + +static win_handle inheritable_stderr(SECURITY_ATTRIBUTES & security) { + HANDLE duplicate = INVALID_HANDLE_VALUE; + const HANDLE current = GetStdHandle(STD_ERROR_HANDLE); + if (current != nullptr && current != INVALID_HANDLE_VALUE && + DuplicateHandle( + GetCurrentProcess(), + current, + GetCurrentProcess(), + &duplicate, + 0, + TRUE, + DUPLICATE_SAME_ACCESS)) { + return win_handle(duplicate); + } + + return win_handle(CreateFileW( + L"NUL", + GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, + &security, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + nullptr)); +} + +static std::vector decode_audio_windows(const spk_model & m, const char * path) { + namespace fs = std::filesystem; + + const fs::path ffmpeg = resolve_windows_executable(resolve_ffmpeg()); + if (ffmpeg.empty()) { + fprintf(stderr, "spk: cannot find ffmpeg.exe\n"); + return {}; + } + + const fs::path input = fs::u8path(path); + std::wstring command = + quote_windows_argument(ffmpeg.native()) + + L" -nostdin -v error -i " + + quote_windows_argument(input.native()) + + L" -ac 1 -ar " + + std::to_wstring(m.sr) + + L" -f f32le -"; + + SECURITY_ATTRIBUTES security{}; + security.nLength = sizeof(security); + security.bInheritHandle = TRUE; + + HANDLE stdout_read_raw = INVALID_HANDLE_VALUE; + HANDLE stdout_write_raw = INVALID_HANDLE_VALUE; + if (!CreatePipe(&stdout_read_raw, &stdout_write_raw, &security, 0)) { + log_windows_error("cannot create ffmpeg output pipe", GetLastError()); + return {}; + } + win_handle stdout_read(stdout_read_raw); + win_handle stdout_write(stdout_write_raw); + if (!SetHandleInformation(stdout_read.get(), HANDLE_FLAG_INHERIT, 0)) { + log_windows_error("cannot protect ffmpeg output pipe", GetLastError()); + return {}; + } + + win_handle stdin_null(CreateFileW( + L"NUL", + GENERIC_READ, + FILE_SHARE_READ | FILE_SHARE_WRITE, + &security, + OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, + nullptr)); + win_handle stderr_handle = inheritable_stderr(security); + if (!stdin_null.valid() || !stderr_handle.valid()) { + log_windows_error("cannot prepare ffmpeg standard handles", GetLastError()); + return {}; + } + + std::array inherited_handles = { + stdin_null.get(), + stdout_write.get(), + stderr_handle.get(), + }; + SIZE_T attribute_bytes = 0; + InitializeProcThreadAttributeList(nullptr, 1, 0, &attribute_bytes); + if (attribute_bytes == 0) { + log_windows_error("cannot size ffmpeg process attributes", GetLastError()); + return {}; + } + + std::vector attribute_storage(attribute_bytes); + auto * attributes = + reinterpret_cast(attribute_storage.data()); + if (!InitializeProcThreadAttributeList(attributes, 1, 0, &attribute_bytes)) { + log_windows_error("cannot initialize ffmpeg process attributes", GetLastError()); + return {}; + } + + const bool handles_updated = UpdateProcThreadAttribute( + attributes, + 0, + PROC_THREAD_ATTRIBUTE_HANDLE_LIST, + inherited_handles.data(), + sizeof(inherited_handles), + nullptr, + nullptr) != FALSE; + if (!handles_updated) { + const DWORD error = GetLastError(); + DeleteProcThreadAttributeList(attributes); + log_windows_error("cannot restrict ffmpeg inherited handles", error); + return {}; + } + + STARTUPINFOEXW startup{}; + startup.StartupInfo.cb = sizeof(startup); + startup.StartupInfo.dwFlags = STARTF_USESTDHANDLES; + startup.StartupInfo.hStdInput = stdin_null.get(); + startup.StartupInfo.hStdOutput = stdout_write.get(); + startup.StartupInfo.hStdError = stderr_handle.get(); + startup.lpAttributeList = attributes; + + PROCESS_INFORMATION process_info{}; + const BOOL created = CreateProcessW( + ffmpeg.c_str(), + command.data(), + nullptr, + nullptr, + TRUE, + EXTENDED_STARTUPINFO_PRESENT | CREATE_NO_WINDOW, + nullptr, + nullptr, + &startup.StartupInfo, + &process_info); + const DWORD create_error = created ? ERROR_SUCCESS : GetLastError(); + DeleteProcThreadAttributeList(attributes); + if (!created) { + log_windows_error("cannot start ffmpeg", create_error); + return {}; + } + + win_handle process(process_info.hProcess); + win_handle thread(process_info.hThread); + // Only the child may retain these ends while the parent drains stdout. + stdout_write.reset(); + stdin_null.reset(); + stderr_handle.reset(); + + std::vector raw; + std::array buffer{}; + DWORD read_error = ERROR_SUCCESS; + for (;;) { + DWORD bytes_read = 0; + if (!ReadFile( + stdout_read.get(), + buffer.data(), + (DWORD) buffer.size(), + &bytes_read, + nullptr)) { + const DWORD error = GetLastError(); + if (error != ERROR_BROKEN_PIPE) read_error = error; + break; + } + if (bytes_read == 0) break; + raw.insert(raw.end(), buffer.begin(), buffer.begin() + bytes_read); + } + + // If the pipe itself failed, close our read end before waiting so a child + // that is still writing receives a broken pipe instead of blocking forever. + if (read_error != ERROR_SUCCESS) stdout_read.reset(); + const DWORD wait_result = WaitForSingleObject(process.get(), INFINITE); + DWORD exit_code = 1; + const bool got_exit_code = GetExitCodeProcess(process.get(), &exit_code) != FALSE; + if (read_error != ERROR_SUCCESS) { + log_windows_error("cannot read ffmpeg output", read_error); + return {}; + } + if (wait_result != WAIT_OBJECT_0) { + log_windows_error("cannot wait for ffmpeg", GetLastError()); + return {}; + } + if (!got_exit_code) { + log_windows_error("cannot read ffmpeg exit code", GetLastError()); + return {}; + } + if (exit_code != 0 || raw.empty() || raw.size() % sizeof(float) != 0) { + fprintf( + stderr, + "spk: ffmpeg failed (rc=%lu, %zu bytes) for %s\n", + (unsigned long) exit_code, + raw.size(), + path); + return {}; + } + + std::vector wav(raw.size() / sizeof(float)); + memcpy(wav.data(), raw.data(), raw.size()); + return wav; +} + +} // namespace +#endif + std::vector spk_decode_audio_file(const spk_model & m, const char * path) { // decode any audio via ffmpeg -> 24 kHz mono f32 - std::string cmd = shell_quote_path(resolve_ffmpeg().c_str()) + " -v error -i " + shell_quote_path(path) + - " -ac 1 -ar " + std::to_string(m.sr) + " -f f32le -"; #ifdef _WIN32 - // binary mode: text mode eats 0x1A as EOF and mangles CRLF in the raw f32 - // stream. POSIX popen rejects "rb" (EINVAL on macOS/BSD), so Windows-only. - FILE * pp = popen(cmd.c_str(), "rb"); + // Do not use _popen here: it delegates to cmd.exe, whose /c quote stripping + // breaks an absolute executable path followed by another quoted input path. + return decode_audio_windows(m, path); #else + const std::string ffmpeg = resolve_ffmpeg().string(); + std::string cmd = shell_quote_path(ffmpeg.c_str()) + " -nostdin -v error -i " + + shell_quote_path(path) + " -ac 1 -ar " + std::to_string(m.sr) + + " -f f32le -"; FILE * pp = popen(cmd.c_str(), "r"); -#endif if (!pp) { fprintf(stderr, "spk: cannot run ffmpeg\n"); return {}; } std::vector wav; float buf[8192]; size_t n; @@ -337,6 +626,7 @@ std::vector spk_decode_audio_file(const spk_model & m, const char * path) return {}; } return wav; +#endif } std::vector spk_embed_from_file(const spk_model & m, const char * path) { diff --git a/src/spk-encoder.h b/src/spk-encoder.h index 153ee3d..ae7d0e0 100644 --- a/src/spk-encoder.h +++ b/src/spk-encoder.h @@ -46,8 +46,8 @@ std::vector spk_embed_from_mel(const spk_model & m, const float * mel, in std::vector spk_embed_from_pcm24k(const spk_model & m, const float * wav, int n); // Decode any audio file to sample_rate (24 kHz) mono f32 via ffmpeg. Returns empty on failure. -// Requires ffmpeg on PATH. Useful to keep the decoded waveform (e.g. for a preview) alongside -// the embedding from spk_embed_from_pcm24k. +// Resolves ffmpeg from ZONOS2_FFMPEG, next to the executable, or PATH. Useful to keep the +// decoded waveform (e.g. for a preview) alongside the embedding from spk_embed_from_pcm24k. std::vector spk_decode_audio_file(const spk_model & m, const char * path); // Embed from any audio file: spk_decode_audio_file() then spk_embed_from_pcm24k(). diff --git a/tests/fake-ffmpeg.cpp b/tests/fake-ffmpeg.cpp new file mode 100644 index 0000000..60746e8 --- /dev/null +++ b/tests/fake-ffmpeg.cpp @@ -0,0 +1,40 @@ +#include +#include + +#include +#include +#include +#include + +int main(int argc, char ** argv) { + const std::vector expected = { + "-nostdin", "-v", "error", "-i", "", "-ac", "1", + "-ar", "24000", "-f", "f32le", "-", + }; + if (argc != (int) expected.size() + 1) { + fprintf(stderr, "fake-ffmpeg: expected %zu arguments, got %d\n", expected.size(), argc - 1); + return 2; + } + for (size_t i = 0; i < expected.size(); ++i) { + if (i == 4) continue; + if (argv[i + 1] != expected[i]) { + fprintf(stderr, "fake-ffmpeg: unexpected argument %zu: %s\n", i + 1, argv[i + 1]); + return 2; + } + } + if (!std::filesystem::exists(std::filesystem::u8path(argv[5]))) { + fprintf(stderr, "fake-ffmpeg: input path was not preserved: %s\n", argv[5]); + return 2; + } + if (_setmode(_fileno(stdout), _O_BINARY) == -1) { + fprintf(stderr, "fake-ffmpeg: cannot set binary stdout\n"); + return 2; + } + + const std::vector pcm(12000, 0.25f); + if (fwrite(pcm.data(), sizeof(float), pcm.size(), stdout) != pcm.size()) { + fprintf(stderr, "fake-ffmpeg: cannot write output\n"); + return 2; + } + return 0; +} diff --git a/tests/ffmpeg-decode-smoke.cpp b/tests/ffmpeg-decode-smoke.cpp new file mode 100644 index 0000000..7e76457 --- /dev/null +++ b/tests/ffmpeg-decode-smoke.cpp @@ -0,0 +1,28 @@ +#include "spk-encoder.h" + +#include +#include + +int main(int argc, char ** argv) { + if (argc != 2) { + fprintf(stderr, "usage: ffmpeg-decode-smoke AUDIO\n"); + return 2; + } + + spk_model model; + model.sr = 24000; + const std::vector pcm = spk_decode_audio_file(model, argv[1]); + if (pcm.size() != 12000) { + fprintf(stderr, "expected 12000 decoded samples, got %zu\n", pcm.size()); + return 1; + } + for (const float sample : pcm) { + if (!std::isfinite(sample)) { + fprintf(stderr, "decoded output contains a non-finite sample\n"); + return 1; + } + } + + fprintf(stderr, "decoded %zu samples\n", pcm.size()); + return 0; +} diff --git a/tests/ui-speaker-upload.test.js b/tests/ui-speaker-upload.test.js new file mode 100644 index 0000000..b429a43 --- /dev/null +++ b/tests/ui-speaker-upload.test.js @@ -0,0 +1,70 @@ +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const test = require('node:test'); + +const uiPath = path.join(__dirname, '..', 'web', 'tts_ui.html'); +const uiSource = fs.readFileSync(uiPath, 'utf8'); + +function extractFunction(name) { + const pattern = new RegExp(` function ${name}\\([^)]*\\) \\{[\\s\\S]*?\\n \\}`); + const match = uiSource.match(pattern); + assert.ok(match, `could not find ${name} in ${uiPath}`); + return match[0]; +} + +class FakeSelect { + constructor(optionIds) { + this.optionIds = new Set(optionIds); + this.selectedValue = ''; + } + + setOptions(optionIds) { + this.optionIds = new Set(optionIds); + if (!this.optionIds.has(this.selectedValue)) this.selectedValue = ''; + } + + get value() { + return this.selectedValue; + } + + set value(nextValue) { + // HTMLSelectElement.value clears the selection when no option matches. + this.selectedValue = this.optionIds.has(nextValue) ? nextValue : ''; + } +} + +test('an uploaded speaker is selected only after its option is rendered', () => { + const applyUploadedSpeakers = new Function( + 'renderSpeakerSelectOptions', + 'autoAssignSpeakers', + 'renderSpeakerLibrary', + 'updateSpeakerBlendUI', + `${extractFunction('applyUploadedSpeakers')}; return applyUploadedSpeakers;`, + )( + renderSpeakerSelectOptions, + autoAssignSpeakers, + () => calls.push('library'), + () => calls.push('blend'), + ); + + const uploadedSpeaker = { id: 'spk_uploaded' }; + const speakerASelect = new FakeSelect(['']); + const calls = []; + + function renderSpeakerSelectOptions() { + calls.push('options'); + speakerASelect.setOptions(['', uploadedSpeaker.id]); + } + + function autoAssignSpeakers(addedSpeakers) { + calls.push('assign'); + speakerASelect.value = addedSpeakers[addedSpeakers.length - 1].id; + } + + applyUploadedSpeakers([uploadedSpeaker]); + + assert.equal(speakerASelect.value, uploadedSpeaker.id); + assert.deepEqual(calls, ['options', 'assign', 'library', 'blend']); + assert.match(uiSource, /applyUploadedSpeakers\(addedSpeakers\);/); +}); diff --git a/web/tts_ui.html b/web/tts_ui.html index fb1b981..93e1a16 100644 --- a/web/tts_ui.html +++ b/web/tts_ui.html @@ -2098,6 +2098,15 @@ state.speakerInitialSelectionApplied = true; } + function applyUploadedSpeakers(addedSpeakers) { + // Populate the