diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 09e77ea8..2d3aaa3a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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() diff --git a/tests/test-stream-pcm.cpp b/tests/test-stream-pcm.cpp new file mode 100644 index 00000000..3c29e94a --- /dev/null +++ b/tests/test-stream-pcm.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include + +#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 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(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; +}