1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include <limits> 6 7 #include "base/logging.h" 8 #include "base/basictypes.h" 9 #include "base/memory/scoped_ptr.h" 10 #include "media/audio/audio_parameters.h" 11 #include "media/audio/simple_sources.h" 12 #include "media/base/audio_bus.h" 13 #include "testing/gtest/include/gtest/gtest.h" 14 15 namespace media { 16 17 // Validate that the SineWaveAudioSource writes the expected values. 18 TEST(SimpleSources, SineWaveAudioSource) { 19 static const uint32 samples = 1024; 20 static const uint32 bytes_per_sample = 2; 21 static const int freq = 200; 22 23 AudioParameters params( 24 AudioParameters::AUDIO_PCM_LINEAR, CHANNEL_LAYOUT_MONO, 25 AudioParameters::kTelephoneSampleRate, bytes_per_sample * 8, samples); 26 27 SineWaveAudioSource source(1, freq, params.sample_rate()); 28 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(params); 29 source.OnMoreData(audio_bus.get(), AudioBuffersState()); 30 EXPECT_EQ(1, source.callbacks()); 31 EXPECT_EQ(0, source.errors()); 32 33 uint32 half_period = AudioParameters::kTelephoneSampleRate / (freq * 2); 34 35 // Spot test positive incursion of sine wave. 36 EXPECT_NEAR(0, audio_bus->channel(0)[0], 37 std::numeric_limits<float>::epsilon()); 38 EXPECT_FLOAT_EQ(0.15643446f, audio_bus->channel(0)[1]); 39 EXPECT_LT(audio_bus->channel(0)[1], audio_bus->channel(0)[2]); 40 EXPECT_LT(audio_bus->channel(0)[2], audio_bus->channel(0)[3]); 41 // Spot test negative incursion of sine wave. 42 EXPECT_NEAR(0, audio_bus->channel(0)[half_period], 43 std::numeric_limits<float>::epsilon()); 44 EXPECT_FLOAT_EQ(-0.15643446f, audio_bus->channel(0)[half_period + 1]); 45 EXPECT_GT(audio_bus->channel(0)[half_period + 1], 46 audio_bus->channel(0)[half_period + 2]); 47 EXPECT_GT(audio_bus->channel(0)[half_period + 2], 48 audio_bus->channel(0)[half_period + 3]); 49 } 50 51 TEST(SimpleSources, SineWaveAudioCapped) { 52 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); 53 54 static const int kSampleCap = 100; 55 source.CapSamples(kSampleCap); 56 57 scoped_ptr<AudioBus> audio_bus = AudioBus::Create(1, 2 * kSampleCap); 58 EXPECT_EQ(source.OnMoreData( 59 audio_bus.get(), AudioBuffersState()), kSampleCap); 60 EXPECT_EQ(1, source.callbacks()); 61 EXPECT_EQ(source.OnMoreData(audio_bus.get(), AudioBuffersState()), 0); 62 EXPECT_EQ(2, source.callbacks()); 63 source.Reset(); 64 EXPECT_EQ(source.OnMoreData( 65 audio_bus.get(), AudioBuffersState()), kSampleCap); 66 EXPECT_EQ(3, source.callbacks()); 67 EXPECT_EQ(0, source.errors()); 68 } 69 70 TEST(SimpleSources, OnError) { 71 SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate); 72 source.OnError(NULL); 73 EXPECT_EQ(1, source.errors()); 74 source.OnError(NULL); 75 EXPECT_EQ(2, source.errors()); 76 } 77 78 } // namespace media 79