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

Side by Side Diff: media/audio/simple_sources.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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « media/audio/simple_sources.h ('k') | media/audio/simple_sources_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // MSVC++ requires this to be set before any other includes to get M_PI.
5 #define _USE_MATH_DEFINES
6 #include <cmath>
4 7
5 #include "media/audio/simple_sources.h" 8 #include "media/audio/simple_sources.h"
6 9
7 #include <math.h>
8 #include <algorithm> 10 #include <algorithm>
9 11
10 #include "base/basictypes.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/memory/ref_counted.h" 13 #include "media/audio/audio_util.h"
13 #include "media/audio/audio_io.h"
14 #include "media/base/data_buffer.h"
15 14
16 namespace media { 15 namespace media {
17 16
18 ////////////////////////////////////////////////////////////////////////////// 17 //////////////////////////////////////////////////////////////////////////////
19 // SineWaveAudioSource implementation. 18 // SineWaveAudioSource implementation.
20 19
21 SineWaveAudioSource::SineWaveAudioSource(Format format, int channels, 20 SineWaveAudioSource::SineWaveAudioSource(int channels,
22 double freq, double sample_freq) 21 double freq, double sample_freq)
23 : format_(format), 22 : channels_(channels),
24 channels_(channels), 23 f_(freq / sample_freq),
25 freq_(freq), 24 time_state_(0),
26 sample_freq_(sample_freq), 25 cap_(0) {
27 time_state_(0) {
28 // TODO(cpu): support other formats.
29 DCHECK((format_ == FORMAT_16BIT_LINEAR_PCM) && (channels_ == 1));
30 } 26 }
31 27
32 // The implementation could be more efficient if a lookup table is constructed 28 // The implementation could be more efficient if a lookup table is constructed
33 // but it is efficient enough for our simple needs. 29 // but it is efficient enough for our simple needs.
34 uint32 SineWaveAudioSource::OnMoreData( 30 int SineWaveAudioSource::OnMoreData(AudioBus* audio_bus,
35 uint8* dest, uint32 max_size, AudioBuffersState audio_buffers) { 31 AudioBuffersState audio_buffers) {
36 const double kTwoPi = 2.0 * 3.141592653589; 32 base::AutoLock auto_lock(time_lock_);
37 double f = freq_ / sample_freq_;
38 int16* sin_tbl = reinterpret_cast<int16*>(dest);
39 uint32 len = max_size / sizeof(int16);
40 33
41 // The table is filled with s(t) = kint16max*sin(Theta*t), 34 // The table is filled with s(t) = kint16max*sin(Theta*t),
42 // where Theta = 2*PI*fs. 35 // where Theta = 2*PI*fs.
43 // We store the discrete time value |t| in a member to ensure that the 36 // We store the discrete time value |t| in a member to ensure that the
44 // next pass starts at a correct state. 37 // next pass starts at a correct state.
45 for (uint32 n = 0; n < len; ++n) { 38 int max_frames = cap_ > 0 ?
46 double theta = kTwoPi * f; 39 std::min(audio_bus->frames(), cap_ - time_state_) : audio_bus->frames();
47 double ksinx = kint16max * sin(theta * time_state_); 40 for (int i = 0; i < max_frames; ++i)
48 sin_tbl[n] = (ksinx > 0.0f) ? static_cast<int16>(ksinx + 0.5) : 41 audio_bus->channel(0)[i] = sin(2.0 * M_PI * f_ * time_state_++);
49 static_cast<int16>(ksinx - 0.5); 42 for (int i = 1; i < audio_bus->channels(); ++i) {
50 ++time_state_; 43 memcpy(audio_bus->channel(i), audio_bus->channel(0),
44 max_frames * sizeof(*audio_bus->channel(i)));
51 } 45 }
52 return max_size; 46 return max_frames;
53 } 47 }
54 48
55 void SineWaveAudioSource::OnError(AudioOutputStream* stream, int code) { 49 void SineWaveAudioSource::OnError(AudioOutputStream* stream, int code) {
56 NOTREACHED(); 50 NOTREACHED();
57 } 51 }
58 52
59 ////////////////////////////////////////////////////////////////////////////// 53 void SineWaveAudioSource::CapSamples(int cap) {
60 // PushSource implementation. 54 base::AutoLock auto_lock(time_lock_);
61 55 DCHECK_GT(cap, 0);
62 PushSource::PushSource() 56 cap_ = cap;
63 : buffer_(0, 0) {
64 } 57 }
65 58
66 PushSource::~PushSource() { } 59 void SineWaveAudioSource::Reset() {
67 60 base::AutoLock auto_lock(time_lock_);
68 uint32 PushSource::OnMoreData( 61 time_state_ = 0;
69 uint8* dest, uint32 max_size, AudioBuffersState buffers_state) {
70 return buffer_.Read(dest, max_size);
71 }
72
73 void PushSource::OnError(AudioOutputStream* stream, int code) {
74 NOTREACHED();
75 }
76
77 // TODO(cpu): Manage arbitrary large sizes.
78 bool PushSource::Write(const void *data, uint32 len) {
79 if (len == 0) {
80 NOTREACHED();
81 return false;
82 }
83 buffer_.Append(static_cast<const uint8*>(data), len);
84 return true;
85 }
86
87 uint32 PushSource::UnProcessedBytes() {
88 return buffer_.forward_bytes();
89 }
90
91 void PushSource::CleanUp() {
92 buffer_.Clear();
93 } 62 }
94 63
95 } // namespace media 64 } // namespace media
OLDNEW
« no previous file with comments | « media/audio/simple_sources.h ('k') | media/audio/simple_sources_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698