OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // MSVC++ requires this to be set before any other includes to get M_PI. | 5 // MSVC++ requires this to be set before any other includes to get M_PI. |
6 #define _USE_MATH_DEFINES | 6 #define _USE_MATH_DEFINES |
7 | 7 |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "media/base/audio_timestamp_helper.h" | 10 #include "media/base/audio_timestamp_helper.h" |
11 #include "media/base/fake_audio_render_callback.h" | 11 #include "media/base/fake_audio_render_callback.h" |
12 | 12 |
13 namespace media { | 13 namespace media { |
14 | 14 |
15 FakeAudioRenderCallback::FakeAudioRenderCallback(double step) | 15 FakeAudioRenderCallback::FakeAudioRenderCallback(double step, int sample_rate) |
16 : half_fill_(false), | 16 : half_fill_(false), |
17 step_(step), | 17 step_(step), |
18 last_frames_delayed_(-1), | 18 last_delay_(base::TimeDelta::Max()), |
19 last_channel_count_(-1), | 19 last_channel_count_(-1), |
20 volume_(1) { | 20 volume_(1), |
| 21 sample_rate_(sample_rate) { |
21 reset(); | 22 reset(); |
22 } | 23 } |
23 | 24 |
24 FakeAudioRenderCallback::~FakeAudioRenderCallback() {} | 25 FakeAudioRenderCallback::~FakeAudioRenderCallback() {} |
25 | 26 |
26 int FakeAudioRenderCallback::Render(base::TimeDelta delay, | 27 int FakeAudioRenderCallback::Render(base::TimeDelta delay, |
27 base::TimeTicks delay_timestamp, | 28 base::TimeTicks delay_timestamp, |
28 int prior_frames_skipped, | 29 int prior_frames_skipped, |
29 AudioBus* audio_bus) { | 30 AudioBus* audio_bus) { |
30 const int kSampleRate = 48000; | 31 return RenderInternal(audio_bus, delay, volume_); |
31 auto frames_delayed = AudioTimestampHelper::TimeToFrames(delay, kSampleRate); | |
32 return RenderInternal(audio_bus, frames_delayed, volume_); | |
33 } | 32 } |
34 | 33 |
35 double FakeAudioRenderCallback::ProvideInput(AudioBus* audio_bus, | 34 double FakeAudioRenderCallback::ProvideInput(AudioBus* audio_bus, |
36 uint32_t frames_delayed) { | 35 uint32_t frames_delayed) { |
37 // Volume should only be applied by the caller to ProvideInput, so don't bake | 36 // Volume should only be applied by the caller to ProvideInput, so don't bake |
38 // it into the rendered audio. | 37 // it into the rendered audio. |
39 RenderInternal(audio_bus, frames_delayed, 1.0); | 38 auto delay = AudioTimestampHelper::FramesToTime(frames_delayed, sample_rate_); |
| 39 RenderInternal(audio_bus, delay, 1.0); |
40 return volume_; | 40 return volume_; |
41 } | 41 } |
42 | 42 |
43 int FakeAudioRenderCallback::RenderInternal(AudioBus* audio_bus, | 43 int FakeAudioRenderCallback::RenderInternal(AudioBus* audio_bus, |
44 uint32_t frames_delayed, | 44 base::TimeDelta delay, |
45 double volume) { | 45 double volume) { |
46 DCHECK_LE(frames_delayed, static_cast<uint32_t>(INT_MAX)); | 46 DCHECK_LE(delay, base::TimeDelta::Max()); |
47 last_frames_delayed_ = static_cast<int>(frames_delayed); | 47 last_delay_ = delay; |
48 last_channel_count_ = audio_bus->channels(); | 48 last_channel_count_ = audio_bus->channels(); |
49 | 49 |
50 int number_of_frames = audio_bus->frames(); | 50 int number_of_frames = audio_bus->frames(); |
51 if (half_fill_) | 51 if (half_fill_) |
52 number_of_frames /= 2; | 52 number_of_frames /= 2; |
53 | 53 |
54 // Fill first channel with a sine wave. | 54 // Fill first channel with a sine wave. |
55 for (int i = 0; i < number_of_frames; ++i) | 55 for (int i = 0; i < number_of_frames; ++i) |
56 audio_bus->channel(0)[i] = sin(2 * M_PI * (x_ + step_ * i)) * volume; | 56 audio_bus->channel(0)[i] = sin(2 * M_PI * (x_ + step_ * i)) * volume; |
57 x_ += number_of_frames * step_; | 57 x_ += number_of_frames * step_; |
58 | 58 |
59 // Copy first channel into the rest of the channels. | 59 // Copy first channel into the rest of the channels. |
60 for (int i = 1; i < audio_bus->channels(); ++i) { | 60 for (int i = 1; i < audio_bus->channels(); ++i) { |
61 memcpy(audio_bus->channel(i), audio_bus->channel(0), | 61 memcpy(audio_bus->channel(i), audio_bus->channel(0), |
62 number_of_frames * sizeof(*audio_bus->channel(i))); | 62 number_of_frames * sizeof(*audio_bus->channel(i))); |
63 } | 63 } |
64 | 64 |
65 return number_of_frames; | 65 return number_of_frames; |
66 } | 66 } |
67 | 67 |
68 } // namespace media | 68 } // namespace media |
OLD | NEW |