OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <cmath> | 5 #include <cmath> |
6 #include <limits> | 6 #include <limits> |
7 | 7 |
8 #include "ppapi/cpp/dev/audio_dev.h" | 8 #include "ppapi/cpp/dev/audio_dev.h" |
9 #include "ppapi/cpp/dev/audio_config_dev.h" | 9 #include "ppapi/cpp/dev/audio_config_dev.h" |
10 #include "ppapi/cpp/instance.h" | 10 #include "ppapi/cpp/instance.h" |
11 #include "ppapi/cpp/module.h" | 11 #include "ppapi/cpp/module.h" |
12 | 12 |
13 // Separate left and right frequency to make sure we didn't swap L & R. | 13 // Separate left and right frequency to make sure we didn't swap L & R. |
14 // Sounds pretty horrible, though... | 14 // Sounds pretty horrible, though... |
15 const double frequency_l = 200; | 15 const double frequency_l = 400; |
16 const double frequency_r = 1000; | 16 const double frequency_r = 1000; |
17 | 17 |
18 // This sample frequency is guaranteed to work. | 18 // This sample frequency is guaranteed to work. |
19 const PP_AudioSampleRate_Dev sample_frequency = PP_AUDIOSAMPLERATE_44100; | 19 const PP_AudioSampleRate_Dev sample_frequency = PP_AUDIOSAMPLERATE_44100; |
20 const uint32_t sample_count = 4096; | 20 const uint32_t sample_count = 4096; |
21 uint32_t obtained_sample_count = 0; | 21 uint32_t obtained_sample_count = 0; |
22 | 22 |
| 23 const double kPi = 3.141592653589; |
| 24 const double kTwoPi = 2.0 * kPi; |
| 25 |
23 class MyInstance : public pp::Instance { | 26 class MyInstance : public pp::Instance { |
24 public: | 27 public: |
25 explicit MyInstance(PP_Instance instance) | 28 explicit MyInstance(PP_Instance instance) |
26 : pp::Instance(instance), | 29 : pp::Instance(instance), |
27 audio_time_(0) { | 30 audio_wave_l_(0.0), |
| 31 audio_wave_r_(0.0) { |
28 } | 32 } |
29 | 33 |
30 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { | 34 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
31 pp::AudioConfig_Dev config; | 35 pp::AudioConfig_Dev config; |
32 obtained_sample_count = pp::AudioConfig_Dev::RecommendSampleFrameCount( | 36 obtained_sample_count = pp::AudioConfig_Dev::RecommendSampleFrameCount( |
33 sample_count); | 37 sample_count); |
34 config = pp::AudioConfig_Dev(sample_frequency, obtained_sample_count); | 38 config = pp::AudioConfig_Dev(this, sample_frequency, obtained_sample_count); |
35 audio_ = pp::Audio_Dev(*this, config, SineWaveCallback, this); | 39 audio_ = pp::Audio_Dev(this, config, SineWaveCallback, this); |
36 return audio_.StartPlayback(); | 40 return audio_.StartPlayback(); |
37 } | 41 } |
38 | 42 |
39 private: | 43 private: |
40 static void SineWaveCallback(void* samples, size_t num_bytes, void* thiz) { | 44 static void SineWaveCallback(void* samples, size_t num_bytes, void* thiz) { |
41 const double th_l = 2 * 3.141592653589 * frequency_l / sample_frequency; | 45 const double delta_l = kTwoPi * frequency_l / sample_frequency; |
42 const double th_r = 2 * 3.141592653589 * frequency_r / sample_frequency; | 46 const double delta_r = kTwoPi * frequency_r / sample_frequency; |
43 | 47 |
44 // Store time value to avoid clicks on buffer boundries. | 48 // Use per channel audio wave value to avoid clicks on buffer boundries. |
45 size_t t = reinterpret_cast<MyInstance*>(thiz)->audio_time_; | 49 double wave_l = reinterpret_cast<MyInstance*>(thiz)->audio_wave_l_; |
46 | 50 double wave_r = reinterpret_cast<MyInstance*>(thiz)->audio_wave_r_; |
47 uint16_t* buf = reinterpret_cast<uint16_t*>(samples); | 51 const int16_t max_int16 = std::numeric_limits<int16_t>::max(); |
| 52 int16_t* buf = reinterpret_cast<int16_t*>(samples); |
48 for (size_t sample = 0; sample < obtained_sample_count; ++sample) { | 53 for (size_t sample = 0; sample < obtained_sample_count; ++sample) { |
49 *buf++ = static_cast<uint16_t>(sin(th_l * t) | 54 *buf++ = static_cast<int16_t>(sin(wave_l) * max_int16); |
50 * std::numeric_limits<uint16_t>::max()); | 55 *buf++ = static_cast<int16_t>(sin(wave_r) * max_int16); |
51 *buf++ = static_cast<uint16_t>(sin(th_r * t++) | 56 // Add delta, keep within -kTwoPi..kTwoPi to preserve precision. |
52 * std::numeric_limits<uint16_t>::max()); | 57 wave_l += delta_l; |
| 58 if (wave_l > kTwoPi) |
| 59 wave_l -= kTwoPi * 2.0; |
| 60 wave_r += delta_r; |
| 61 if (wave_r > kTwoPi) |
| 62 wave_r -= kTwoPi * 2.0; |
53 } | 63 } |
54 reinterpret_cast<MyInstance*>(thiz)->audio_time_ = t; | 64 // Store current value to use as starting point for next callback. |
| 65 reinterpret_cast<MyInstance*>(thiz)->audio_wave_l_ = wave_l; |
| 66 reinterpret_cast<MyInstance*>(thiz)->audio_wave_r_ = wave_r; |
55 } | 67 } |
56 | 68 |
57 // Audio resource. Allocated in Init(), freed on destruction. | 69 // Audio resource. Allocated in Init(), freed on destruction. |
58 pp::Audio_Dev audio_; | 70 pp::Audio_Dev audio_; |
59 | 71 |
60 // Audio buffer time. Used to make prevent sine wave skips on buffer | 72 // Current audio wave position, used to prevent sine wave skips |
61 // boundaries. | 73 // on buffer boundaries. |
62 size_t audio_time_; | 74 double audio_wave_l_; |
| 75 double audio_wave_r_; |
63 }; | 76 }; |
64 | 77 |
65 class MyModule : public pp::Module { | 78 class MyModule : public pp::Module { |
66 public: | 79 public: |
67 // Override CreateInstance to create your customized Instance object. | 80 // Override CreateInstance to create your customized Instance object. |
68 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 81 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
69 return new MyInstance(instance); | 82 return new MyInstance(instance); |
70 } | 83 } |
71 }; | 84 }; |
72 | 85 |
73 namespace pp { | 86 namespace pp { |
74 | 87 |
75 // Factory function for your specialization of the Module object. | 88 // Factory function for your specialization of the Module object. |
76 Module* CreateModule() { | 89 Module* CreateModule() { |
77 return new MyModule(); | 90 return new MyModule(); |
78 } | 91 } |
79 | 92 |
80 } // namespace pp | 93 } // namespace pp |
OLD | NEW |