TestAudioBuffer.cpp (1630B)
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 "MediaData.h" 8 #include "gtest/gtest.h" 9 10 using mozilla::AlignedFloatBuffer; 11 using mozilla::AudioDataValue; 12 using mozilla::FloatToAudioSample; 13 using mozilla::InflatableShortBuffer; 14 15 void FillSine(InflatableShortBuffer& aBuf, AlignedFloatBuffer& aFloatBuf) { 16 // Write a constant-pitch sine wave in both the integer and float buffers. 17 float phase = 0; 18 float phaseIncrement = 2 * M_PI * 440. / 44100.f; 19 for (uint32_t i = 0; i < aBuf.Length(); i++) { 20 aBuf.get()[i] = FloatToAudioSample<int16_t>(sin(phase)); 21 aFloatBuf.get()[i] = sin(phase); 22 phase += phaseIncrement; 23 if (phase >= 2 * M_PI) { 24 phase -= 2 * M_PI; 25 } 26 } 27 } 28 29 TEST(InflatableAudioBuffer, Test) 30 { 31 for (uint32_t i = 1; i < 10000; i++) { 32 InflatableShortBuffer buf(i); 33 AlignedFloatBuffer bufFloat(i); 34 FillSine(buf, bufFloat); 35 AlignedFloatBuffer inflated = buf.Inflate(); 36 for (uint32_t j = 0; j < buf.Length(); j++) { 37 // Accept a very small difference because floats are floored in the 38 // conversion to integer. 39 if (std::abs(bufFloat.get()[j] - inflated.get()[j]) * 32767. > 1.0) { 40 fprintf(stderr, "%f != %f (size: %u, index: %u)\n", bufFloat.get()[j], 41 inflated.get()[j], i, j); 42 ASSERT_TRUE(false); 43 } 44 } 45 } 46 47 } // namespace audio_mixer