Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ target_link_libraries(${UTF8_TEST} PRIVATE common)
add_test(NAME ${UTF8_TEST} COMMAND ${UTF8_TEST})
set_tests_properties(${UTF8_TEST} PROPERTIES LABELS "unit")

# Audio byte-buffer decode regression test
set(AUDIO_BUFFER_TEST test-read-audio-data-buffer)
add_executable(${AUDIO_BUFFER_TEST} ${AUDIO_BUFFER_TEST}.cpp)
target_include_directories(${AUDIO_BUFFER_TEST} PRIVATE ../examples)
target_link_libraries(${AUDIO_BUFFER_TEST} PRIVATE common)
target_compile_definitions(${AUDIO_BUFFER_TEST} PRIVATE
SAMPLE_PATH="${PROJECT_SOURCE_DIR}/samples/jfk.wav")
add_test(NAME ${AUDIO_BUFFER_TEST} COMMAND ${AUDIO_BUFFER_TEST})
set_tests_properties(${AUDIO_BUFFER_TEST} PROPERTIES LABELS "unit")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be run in CI? (it would need to have the gh label if so):

Suggested change
set_tests_properties(${AUDIO_BUFFER_TEST} PROPERTIES LABELS "unit")
set_tests_properties(${AUDIO_BUFFER_TEST} PROPERTIES LABELS "unit;gh")

With that change it should have both labels:

$ cmake --workflow --preset cpu-debug
$ ctest --test-dir build-cpu-debug/ -R test-read-audio-data-buffer --output-on-failure --print-labels
Internal ctest changing into directory: /home/danbev/work/ai/whisper-work/build-cpu-debug
Test project /home/danbev/work/ai/whisper-work/build-cpu-debug
All Labels:
  gh
  unit


# VAD test tests VAD in isolation
set(VAD_TEST test-vad)
add_executable(${VAD_TEST} ${VAD_TEST}.cpp)
Expand Down
51 changes: 51 additions & 0 deletions tests/test-read-audio-data-buffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "common-whisper.h"

#include <cmath>
#include <cstdio>
#include <fstream>
#include <iterator>
#include <string>
#include <vector>

#ifdef NDEBUG
#undef NDEBUG
#endif
#include <cassert>

static std::vector<char> read_file_bytes(const std::string & path) {
std::ifstream input(path, std::ios::binary);
assert(input.good());
return std::vector<char>(
std::istreambuf_iterator<char>(input),
std::istreambuf_iterator<char>());
}

static void assert_pcm_nearly_equal(const std::vector<float> & expected, const std::vector<float> & actual) {
assert(expected.size() == actual.size());
for (size_t i = 0; i < expected.size(); ++i) {
assert(std::fabs(expected[i] - actual[i]) < 1e-6f);
}
}

int main() {
const std::string sample_path = SAMPLE_PATH;
const std::vector<char> wav_data = read_file_bytes(sample_path);
assert(!wav_data.empty());

std::vector<float> pcm_from_file;
std::vector<std::vector<float>> stereo_from_file;
assert(read_audio_data(sample_path, pcm_from_file, stereo_from_file, false));
assert(!pcm_from_file.empty());
assert(stereo_from_file.empty());

std::vector<float> pcm_from_memory;
std::vector<std::vector<float>> stereo_from_memory;
assert(read_audio_data(wav_data.data(), wav_data.size(), pcm_from_memory, stereo_from_memory, false));
assert(!pcm_from_memory.empty());
assert(stereo_from_memory.empty());

assert_pcm_nearly_equal(pcm_from_file, pcm_from_memory);

printf("Decoded %zu bytes from memory into %zu PCM samples\n", wav_data.size(), pcm_from_memory.size());
return 0;
}
Loading