Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(794)

Unified Diff: media/audio/simple_sources_unittest.cc

Issue 10832285: Switch OnMoreData() to use AudioBus. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Comments. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « media/audio/simple_sources.cc ('k') | media/audio/win/audio_low_latency_output_win.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/audio/simple_sources_unittest.cc
diff --git a/media/audio/simple_sources_unittest.cc b/media/audio/simple_sources_unittest.cc
index 74d19f23fae60000526245b0f8fb8433006fe504..47f18b494c961618b04c7b663be71d8fa76b4140 100644
--- a/media/audio/simple_sources_unittest.cc
+++ b/media/audio/simple_sources_unittest.cc
@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include <algorithm> // std::min
+#include <limits>
#include "base/logging.h"
#include "base/basictypes.h"
@@ -11,66 +12,18 @@
#include "media/audio/audio_manager.h"
#include "media/audio/fake_audio_output_stream.h"
#include "media/audio/simple_sources.h"
+#include "media/base/audio_bus.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace media {
-static void GenerateRandomData(char* buffer, uint32 len) {
- static bool called = false;
- if (!called) {
- called = true;
- int seed = static_cast<int>(base::Time::Now().ToInternalValue());
- srand(seed);
- VLOG(1) << "Random seed: " << seed;
- }
-
- for (uint32 i = 0; i < len; i++)
- buffer[i] = static_cast<char>(rand()); // NOLINT
-}
-
-// To test write size smaller than read size.
-TEST(SimpleSourcesTest, PushSourceSmallerWrite) {
- const uint32 kDataSize = 40960;
- scoped_array<char> data(new char[kDataSize]);
- GenerateRandomData(data.get(), kDataSize);
-
- // Choose two prime numbers for read and write sizes.
- const uint32 kWriteSize = 283;
- const uint32 kReadSize = 293;
- scoped_array<uint8> read_data(new uint8[kReadSize]);
-
- // Create a PushSource.
- PushSource push_source;
- EXPECT_EQ(0u, push_source.UnProcessedBytes());
-
- // Write everything into this push source.
- for (uint32 i = 0; i < kDataSize; i += kWriteSize) {
- uint32 size = std::min(kDataSize - i, kWriteSize);
- EXPECT_TRUE(push_source.Write(data.get() + i, size));
- }
- EXPECT_EQ(kDataSize, push_source.UnProcessedBytes());
-
- // Read everything from the push source.
- for (uint32 i = 0; i < kDataSize; i += kReadSize) {
- uint32 size = std::min(kDataSize - i , kReadSize);
- EXPECT_EQ(size, push_source.OnMoreData(read_data.get(), size,
- AudioBuffersState()));
- EXPECT_EQ(0, memcmp(data.get() + i, read_data.get(), size));
- }
- EXPECT_EQ(0u, push_source.UnProcessedBytes());
-}
-
-// Validate that the SineWaveAudioSource writes the expected values for
-// the FORMAT_16BIT_MONO. The values are carefully selected so rounding issues
-// do not affect the result. We also test that AudioManager::GetLastMockBuffer
-// works.
-TEST(SimpleSources, SineWaveAudio16MonoTest) {
+// Validate that the SineWaveAudioSource writes the expected values.
+TEST(SimpleSources, SineWaveAudioSource) {
const uint32 samples = 1024;
const uint32 bytes_per_sample = 2;
const int freq = 200;
- SineWaveAudioSource source(SineWaveAudioSource::FORMAT_16BIT_LINEAR_PCM, 1,
- freq, AudioParameters::kTelephoneSampleRate);
+ SineWaveAudioSource source(1, freq, AudioParameters::kTelephoneSampleRate);
scoped_ptr<AudioManager> audio_man(AudioManager::Create());
AudioParameters params(
@@ -84,24 +37,43 @@ TEST(SimpleSources, SineWaveAudio16MonoTest) {
oas->Stop();
ASSERT_TRUE(FakeAudioOutputStream::GetCurrentFakeStream());
- const int16* last_buffer =
- reinterpret_cast<int16*>(
- FakeAudioOutputStream::GetCurrentFakeStream()->buffer());
- ASSERT_TRUE(NULL != last_buffer);
+ const AudioBus* last_audio_bus =
+ FakeAudioOutputStream::GetCurrentFakeStream()->audio_bus();
+ ASSERT_TRUE(NULL != last_audio_bus);
uint32 half_period = AudioParameters::kTelephoneSampleRate / (freq * 2);
// Spot test positive incursion of sine wave.
- EXPECT_EQ(0, last_buffer[0]);
- EXPECT_EQ(5126, last_buffer[1]);
- EXPECT_TRUE(last_buffer[1] < last_buffer[2]);
- EXPECT_TRUE(last_buffer[2] < last_buffer[3]);
+ EXPECT_NEAR(0, last_audio_bus->channel(0)[0],
+ std::numeric_limits<float>::epsilon());
+ EXPECT_FLOAT_EQ(0.15643446f, last_audio_bus->channel(0)[1]);
+ EXPECT_LT(last_audio_bus->channel(0)[1], last_audio_bus->channel(0)[2]);
+ EXPECT_LT(last_audio_bus->channel(0)[2], last_audio_bus->channel(0)[3]);
// Spot test negative incursion of sine wave.
- EXPECT_EQ(0, last_buffer[half_period]);
- EXPECT_EQ(-5126, last_buffer[half_period + 1]);
- EXPECT_TRUE(last_buffer[half_period + 1] > last_buffer[half_period + 2]);
- EXPECT_TRUE(last_buffer[half_period + 2] > last_buffer[half_period + 3]);
+ EXPECT_NEAR(0, last_audio_bus->channel(0)[half_period],
+ std::numeric_limits<float>::epsilon());
+ EXPECT_FLOAT_EQ(-0.15643446f, last_audio_bus->channel(0)[half_period + 1]);
+ EXPECT_GT(last_audio_bus->channel(0)[half_period + 1],
+ last_audio_bus->channel(0)[half_period + 2]);
+ EXPECT_GT(last_audio_bus->channel(0)[half_period + 2],
+ last_audio_bus->channel(0)[half_period + 3]);
+
oas->Close();
}
+TEST(SimpleSources, SineWaveAudioCapped) {
+ SineWaveAudioSource source(1, 200, AudioParameters::kTelephoneSampleRate);
+
+ static const int kSampleCap = 100;
+ source.CapSamples(kSampleCap);
+
+ scoped_ptr<AudioBus> audio_bus = AudioBus::Create(1, 2 * kSampleCap);
+ EXPECT_EQ(source.OnMoreData(
+ audio_bus.get(), AudioBuffersState()), kSampleCap);
+ EXPECT_EQ(source.OnMoreData(audio_bus.get(), AudioBuffersState()), 0);
+ source.Reset();
+ EXPECT_EQ(source.OnMoreData(
+ audio_bus.get(), AudioBuffersState()), kSampleCap);
+}
+
} // namespace media
« no previous file with comments | « media/audio/simple_sources.cc ('k') | media/audio/win/audio_low_latency_output_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698