|
| 1 | +#include "common-whisper.h" |
| 2 | + |
| 3 | +#include <cmath> |
| 4 | +#include <cstdio> |
| 5 | +#include <fstream> |
| 6 | +#include <iterator> |
| 7 | +#include <string> |
| 8 | +#include <vector> |
| 9 | + |
| 10 | +#ifdef NDEBUG |
| 11 | +#undef NDEBUG |
| 12 | +#endif |
| 13 | +#include <cassert> |
| 14 | + |
| 15 | +static std::vector<char> read_file_bytes(const std::string & path) { |
| 16 | + std::ifstream input(path, std::ios::binary); |
| 17 | + assert(input.good()); |
| 18 | + return std::vector<char>( |
| 19 | + std::istreambuf_iterator<char>(input), |
| 20 | + std::istreambuf_iterator<char>()); |
| 21 | +} |
| 22 | + |
| 23 | +static void assert_pcm_nearly_equal(const std::vector<float> & expected, const std::vector<float> & actual) { |
| 24 | + assert(expected.size() == actual.size()); |
| 25 | + for (size_t i = 0; i < expected.size(); ++i) { |
| 26 | + assert(std::fabs(expected[i] - actual[i]) < 1e-6f); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +int main() { |
| 31 | + const std::string sample_path = SAMPLE_PATH; |
| 32 | + const std::vector<char> wav_data = read_file_bytes(sample_path); |
| 33 | + assert(!wav_data.empty()); |
| 34 | + |
| 35 | + std::vector<float> pcm_from_file; |
| 36 | + std::vector<std::vector<float>> stereo_from_file; |
| 37 | + assert(read_audio_data(sample_path, pcm_from_file, stereo_from_file, false)); |
| 38 | + assert(!pcm_from_file.empty()); |
| 39 | + assert(stereo_from_file.empty()); |
| 40 | + |
| 41 | + std::vector<float> pcm_from_memory; |
| 42 | + std::vector<std::vector<float>> stereo_from_memory; |
| 43 | + assert(read_audio_data(wav_data.data(), wav_data.size(), pcm_from_memory, stereo_from_memory, false)); |
| 44 | + assert(!pcm_from_memory.empty()); |
| 45 | + assert(stereo_from_memory.empty()); |
| 46 | + |
| 47 | + assert_pcm_nearly_equal(pcm_from_file, pcm_from_memory); |
| 48 | + |
| 49 | + printf("Decoded %zu bytes from memory into %zu PCM samples\n", wav_data.size(), pcm_from_memory.size()); |
| 50 | + return 0; |
| 51 | +} |
0 commit comments