| 60 | } // namespace |
| 61 | |
| 62 | TEST(AudioDecoderTest, WavDecoderTest) { |
| 63 | using DataType = short; // NOLINT |
| 64 | auto decoder = make_generic_audio_decoder(); |
| 65 | |
| 66 | // Contains wav file to be decoded |
| 67 | std::string wav_path = make_string(audio_data_root, "dziendobry.wav"); |
| 68 | // Contains raw PCM data decoded offline |
| 69 | std::string decoded_path = make_string(audio_data_root, "dziendobry.txt"); |
| 70 | |
| 71 | constexpr int expected_frequency = 44100; |
| 72 | constexpr int expected_nchannels = 2; |
| 73 | std::vector<DataType> vec; |
| 74 | std::vector<char> bytes; |
| 75 | try { |
| 76 | vec = ReadTxt<DataType>(decoded_path); |
| 77 | bytes = ReadBytes(wav_path); |
| 78 | } catch (const std::bad_alloc &e) { |
| 79 | FAIL() << "Test data hasn't been provided: Expected `" << wav_path << "` and `" << decoded_path |
| 80 | << "` to exist"; |
| 81 | } |
| 82 | |
| 83 | { |
| 84 | auto meta = decoder->Open(make_cspan(bytes)); |
| 85 | EXPECT_EQ(meta.channels, expected_nchannels); |
| 86 | EXPECT_EQ(meta.channels_interleaved, true); |
| 87 | EXPECT_EQ(meta.sample_rate, expected_frequency); |
| 88 | decoder->Close(); |
| 89 | } |
| 90 | |
| 91 | { |
| 92 | auto meta = decoder->Open(make_cspan(bytes)); |
| 93 | std::vector<DataType> output(meta.length * meta.channels); |
| 94 | decoder->Decode(make_span(output)); |
| 95 | EXPECT_PRED3(CheckBuffers<DataType>, output.data(), vec.data(), vec.size()); |
| 96 | } |
| 97 | |
| 98 | { |
| 99 | auto meta = decoder->Open(make_cspan(bytes)); |
| 100 | std::vector<DataType> output(meta.length * meta.channels); |
| 101 | decoder->DecodeFrames(output.data(), meta.length); |
| 102 | EXPECT_PRED3(CheckBuffers<DataType>, output.data(), vec.data(), vec.size()); |
| 103 | } |
| 104 | |
| 105 | { |
| 106 | auto meta = decoder->Open(make_cspan(bytes)); |
| 107 | int64_t offset = meta.length / 2; |
| 108 | int64_t length = meta.length - offset; |
| 109 | // allocating a bigger buffer in purpose |
| 110 | std::vector<DataType> output(meta.length * meta.channels, 0xBE); |
| 111 | decoder->SeekFrames(offset, SEEK_CUR); |
| 112 | decoder->DecodeFrames(output.data(), length); |
| 113 | EXPECT_PRED3(CheckBuffers<DataType>, output.data(), vec.data() + offset * meta.channels, |
| 114 | length * meta.channels); |
| 115 | // Verifying that we didn't read more than we should |
| 116 | for (size_t i = length * meta.channels; i < output.size(); i++) { |
| 117 | ASSERT_EQ(0xBE, output[i]); |
| 118 | } |
| 119 | } |
nothing calls this directly
no test coverage detected