TestAudioBuffers.cpp (2131B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include <stdint.h> 8 9 #include <vector> 10 11 #include "AudioBufferUtils.h" 12 #include "gtest/gtest.h" 13 14 const uint32_t FRAMES = 256; 15 16 void test_for_number_of_channels(const uint32_t channels) { 17 const uint32_t samples = channels * FRAMES; 18 19 mozilla::AudioCallbackBufferWrapper<float> mBuffer(channels); 20 mozilla::SpillBuffer<float, 128> b(channels); 21 std::vector<float> fromCallback(samples, 0.0); 22 std::vector<float> other(samples, 1.0); 23 mozilla::AudioChunk chunk; 24 chunk.mBufferFormat = mozilla::AUDIO_FORMAT_FLOAT32; 25 chunk.mChannelData.SetLength(channels); 26 for (uint32_t i = 0; i < channels; ++i) { 27 chunk.mChannelData[i] = other.data() + i * channels; 28 } 29 30 // Set the buffer in the wrapper from the callback 31 mBuffer.SetBuffer(fromCallback.data(), FRAMES); 32 33 // Fill the SpillBuffer with data. 34 chunk.mDuration = 15; 35 ASSERT_TRUE(b.Fill(chunk) == 15); 36 chunk.mDuration = 17; 37 ASSERT_TRUE(b.Fill(chunk) == 17); 38 for (uint32_t i = 0; i < 32 * channels; i++) { 39 other[i] = 0.0; 40 } 41 42 // Empty it in the AudioCallbackBufferWrapper 43 ASSERT_TRUE(b.Empty(mBuffer) == 32); 44 45 // Check available return something reasonnable 46 ASSERT_TRUE(mBuffer.Available() == FRAMES - 32); 47 48 // Fill the buffer with the rest of the data 49 mBuffer.WriteFrames(other.data() + 32 * channels, FRAMES - 32); 50 51 // Check the buffer is now full 52 ASSERT_TRUE(mBuffer.Available() == 0); 53 54 for (uint32_t i = 0; i < samples; i++) { 55 ASSERT_TRUE(fromCallback[i] == 1.0) 56 << "Difference at " << i << " (" << fromCallback[i] << " != " << 1.0 57 << ")\n"; 58 } 59 60 chunk.mDuration = FRAMES; 61 ASSERT_TRUE(b.Fill(chunk) == 128); 62 ASSERT_TRUE(b.Fill(chunk) == 0); 63 ASSERT_TRUE(b.Empty(mBuffer) == 0); 64 } 65 66 TEST(AudioBuffers, Test) 67 { 68 for (uint32_t ch = 1; ch <= 8; ++ch) { 69 test_for_number_of_channels(ch); 70 } 71 }