| 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 #include "media/audio/simple_sources.h" | 5 #include "media/audio/simple_sources.h" |
| 6 | 6 |
| 7 #include <math.h> |
| 7 #include <algorithm> | 8 #include <algorithm> |
| 8 #include <math.h> | |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "media/audio/audio_io.h" | 13 #include "media/audio/audio_io.h" |
| 14 #include "media/base/data_buffer.h" | 14 #include "media/base/data_buffer.h" |
| 15 | 15 |
| 16 namespace media { | 16 namespace media { |
| 17 | 17 |
| 18 ////////////////////////////////////////////////////////////////////////////// | 18 ////////////////////////////////////////////////////////////////////////////// |
| 19 // SineWaveAudioSource implementation. | 19 // SineWaveAudioSource implementation. |
| 20 | 20 |
| 21 SineWaveAudioSource::SineWaveAudioSource(Format format, int channels, | 21 SineWaveAudioSource::SineWaveAudioSource(Format format, int channels, |
| 22 double freq, double sample_freq) | 22 double freq, double sample_freq) |
| 23 : format_(format), | 23 : format_(format), |
| 24 channels_(channels), | 24 channels_(channels), |
| 25 freq_(freq), | 25 freq_(freq), |
| 26 sample_freq_(sample_freq), | 26 sample_freq_(sample_freq), |
| 27 time_state_(0) { | 27 time_state_(0) { |
| 28 // TODO(cpu): support other formats. | 28 // TODO(cpu): support other formats. |
| 29 DCHECK((format_ == FORMAT_16BIT_LINEAR_PCM) && (channels_ == 1)); | 29 DCHECK((format_ == FORMAT_16BIT_LINEAR_PCM) && (channels_ == 1)); |
| 30 } | 30 } |
| 31 | 31 |
| 32 // The implementation could be more efficient if a lookup table is constructed | 32 // The implementation could be more efficient if a lookup table is constructed |
| 33 // but it is efficient enough for our simple needs. | 33 // but it is efficient enough for our simple needs. |
| 34 uint32 SineWaveAudioSource::OnMoreData( | 34 uint32 SineWaveAudioSource::OnMoreData( |
| 35 AudioOutputStream* stream, uint8* dest, uint32 max_size, | 35 uint8* dest, uint32 max_size, AudioBuffersState audio_buffers) { |
| 36 AudioBuffersState audio_buffers) { | |
| 37 const double kTwoPi = 2.0 * 3.141592653589; | 36 const double kTwoPi = 2.0 * 3.141592653589; |
| 38 double f = freq_ / sample_freq_; | 37 double f = freq_ / sample_freq_; |
| 39 int16* sin_tbl = reinterpret_cast<int16*>(dest); | 38 int16* sin_tbl = reinterpret_cast<int16*>(dest); |
| 40 uint32 len = max_size / sizeof(int16); | 39 uint32 len = max_size / sizeof(int16); |
| 41 | 40 |
| 42 // The table is filled with s(t) = kint16max*sin(Theta*t), | 41 // The table is filled with s(t) = kint16max*sin(Theta*t), |
| 43 // where Theta = 2*PI*fs. | 42 // where Theta = 2*PI*fs. |
| 44 // We store the discrete time value |t| in a member to ensure that the | 43 // We store the discrete time value |t| in a member to ensure that the |
| 45 // next pass starts at a correct state. | 44 // next pass starts at a correct state. |
| 46 for (uint32 n = 0; n < len; ++n) { | 45 for (uint32 n = 0; n < len; ++n) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 60 ////////////////////////////////////////////////////////////////////////////// | 59 ////////////////////////////////////////////////////////////////////////////// |
| 61 // PushSource implementation. | 60 // PushSource implementation. |
| 62 | 61 |
| 63 PushSource::PushSource() | 62 PushSource::PushSource() |
| 64 : buffer_(0, 0) { | 63 : buffer_(0, 0) { |
| 65 } | 64 } |
| 66 | 65 |
| 67 PushSource::~PushSource() { } | 66 PushSource::~PushSource() { } |
| 68 | 67 |
| 69 uint32 PushSource::OnMoreData( | 68 uint32 PushSource::OnMoreData( |
| 70 AudioOutputStream* stream, uint8* dest, uint32 max_size, | 69 uint8* dest, uint32 max_size, AudioBuffersState buffers_state) { |
| 71 AudioBuffersState buffers_state) { | |
| 72 return buffer_.Read(dest, max_size); | 70 return buffer_.Read(dest, max_size); |
| 73 } | 71 } |
| 74 | 72 |
| 75 void PushSource::OnError(AudioOutputStream* stream, int code) { | 73 void PushSource::OnError(AudioOutputStream* stream, int code) { |
| 76 NOTREACHED(); | 74 NOTREACHED(); |
| 77 } | 75 } |
| 78 | 76 |
| 79 // TODO(cpu): Manage arbitrary large sizes. | 77 // TODO(cpu): Manage arbitrary large sizes. |
| 80 bool PushSource::Write(const void *data, uint32 len) { | 78 bool PushSource::Write(const void *data, uint32 len) { |
| 81 if (len == 0) { | 79 if (len == 0) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 93 void PushSource::ClearAll() { | 91 void PushSource::ClearAll() { |
| 94 // Cleanup() will discard all the data. | 92 // Cleanup() will discard all the data. |
| 95 CleanUp(); | 93 CleanUp(); |
| 96 } | 94 } |
| 97 | 95 |
| 98 void PushSource::CleanUp() { | 96 void PushSource::CleanUp() { |
| 99 buffer_.Clear(); | 97 buffer_.Clear(); |
| 100 } | 98 } |
| 101 | 99 |
| 102 } // namespace media | 100 } // namespace media |
| OLD | NEW |