Add test for stdin/pipe PCM streaming

This commit is contained in:
Ross Morsali 2026-02-05 09:40:44 +01:00
parent f6c8faff54
commit c8f7a8aa51
2 changed files with 73 additions and 0 deletions

View File

@ -110,3 +110,13 @@ target_compile_definitions(${VAD_TEST} PRIVATE
SAMPLE_PATH="${PROJECT_SOURCE_DIR}/samples/jfk.wav")
add_test(NAME ${VAD_TEST} COMMAND ${VAD_TEST})
set_tests_properties(${VAD_TEST} PROPERTIES LABELS "base;en")
if (WHISPER_BUILD_EXAMPLES)
set(TEST_TARGET test-stream-pcm)
add_executable(${TEST_TARGET} ${TEST_TARGET}.cpp)
target_compile_definitions(${TEST_TARGET} PRIVATE
WHISPER_STREAM_PCM_PATH="${CMAKE_BINARY_DIR}/bin/whisper-stream-pcm"
WHISPER_TEST_MODEL_PATH="${PROJECT_SOURCE_DIR}/models/for-tests-ggml-tiny.en.bin")
add_test(NAME ${TEST_TARGET} COMMAND ${TEST_TARGET})
set_tests_properties(${TEST_TARGET} PROPERTIES LABELS "tiny;stream")
endif()

63
tests/test-stream-pcm.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <cstdlib>
#include <cstdio>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
#ifndef WHISPER_STREAM_PCM_PATH
#error "WHISPER_STREAM_PCM_PATH is not defined"
#endif
#ifndef WHISPER_TEST_MODEL_PATH
#error "WHISPER_TEST_MODEL_PATH is not defined"
#endif
static std::filesystem::path temp_pcm_path() {
std::error_code ec;
auto dir = std::filesystem::temp_directory_path(ec);
if (ec) {
return std::filesystem::path("whisper_stream_pcm_test.raw");
}
return dir / "whisper_stream_pcm_test.raw";
}
int main() {
const int sample_rate = 16000;
const int seconds = 2;
const size_t n_samples = (size_t) sample_rate * seconds;
std::vector<float> zeros(n_samples, 0.0f);
const auto pcm_path = temp_pcm_path();
std::ofstream out(pcm_path, std::ios::binary);
if (!out.is_open()) {
fprintf(stderr, "failed to open temp PCM path: %s\n", pcm_path.string().c_str());
return 1;
}
out.write(reinterpret_cast<const char *>(zeros.data()), zeros.size() * sizeof(float));
out.close();
const std::string stream_bin = WHISPER_STREAM_PCM_PATH;
const std::string model_path = WHISPER_TEST_MODEL_PATH;
std::string cmd;
cmd.reserve(1024);
cmd += "\"" + stream_bin + "\"";
cmd += " -m \"" + model_path + "\"";
cmd += " --input \"" + pcm_path.string() + "\"";
cmd += " --format f32 --sample-rate 16000 --step 500 --length 2000 -t 1 -ng";
const int rc = std::system(cmd.c_str());
std::error_code ec;
std::filesystem::remove(pcm_path, ec);
if (rc != 0) {
fprintf(stderr, "whisper-stream-pcm exited with code %d\n", rc);
return 1;
}
return 0;
}