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/instance.h" | 10 #include "ppapi/cpp/instance.h" |
10 #include "ppapi/cpp/module.h" | 11 #include "ppapi/cpp/module.h" |
11 | 12 |
12 // 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. |
13 // Sounds pretty horrible, though... | 14 // Sounds pretty horrible, though... |
14 const double frequency_l = 200; | 15 const double frequency_l = 200; |
15 const double frequency_r = 1000; | 16 const double frequency_r = 1000; |
16 | 17 |
17 // This sample frequency is guaranteed to work. | 18 // This sample frequency is guaranteed to work. |
18 const PP_AudioSampleRate_Dev sample_frequency = PP_AUDIOSAMPLERATE_44100; | 19 const PP_AudioSampleRate_Dev sample_frequency = PP_AUDIOSAMPLERATE_44100; |
19 const uint32_t sample_count = 4096; | 20 const uint32_t sample_count = 4096; |
21 uint32_t obtained_sample_count = 0; | |
20 | 22 |
21 class MyInstance : public pp::Instance { | 23 class MyInstance : public pp::Instance { |
22 public: | 24 public: |
23 explicit MyInstance(PP_Instance instance) | 25 explicit MyInstance(PP_Instance instance) |
24 : pp::Instance(instance), | 26 : pp::Instance(instance), |
25 audio_time_(0) { | 27 audio_time_(0) { |
26 } | 28 } |
27 | 29 |
28 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { | 30 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
31 pp::AudioConfig_Dev config; | |
32 obtained_sample_count = pp::AudioConfig_Dev::RecommendSampleFrameCount( | |
33 sample_count); | |
34 config = pp::AudioConfig_Dev(sample_frequency, | |
35 obtained_sample_count); | |
brettw
2010/11/22 18:22:09
This can be on the previous line.
nfullagar
2010/11/23 02:48:58
Done.
| |
29 audio_ = pp::Audio_Dev( | 36 audio_ = pp::Audio_Dev( |
brettw
2010/11/22 18:22:09
This can all be on one line now.
nfullagar
2010/11/23 02:48:58
Done.
| |
30 *this, pp::AudioConfig_Dev(sample_frequency, sample_count), | 37 *this, config, |
31 SineWaveCallback, this); | 38 SineWaveCallback, this); |
32 return audio_.StartPlayback(); | 39 return audio_.StartPlayback(); |
33 } | 40 } |
34 | 41 |
35 private: | 42 private: |
36 static void SineWaveCallback(void* samples, | 43 static void SineWaveCallback(void* samples, size_t num_bytes, void* thiz) { |
37 size_t buffer_size_in_bytes, | |
38 void* thiz) { | |
39 const double th_l = 2 * 3.141592653589 * frequency_l / sample_frequency; | 44 const double th_l = 2 * 3.141592653589 * frequency_l / sample_frequency; |
40 const double th_r = 2 * 3.141592653589 * frequency_r / sample_frequency; | 45 const double th_r = 2 * 3.141592653589 * frequency_r / sample_frequency; |
41 | 46 |
42 // Store time value to avoid clicks on buffer boundries. | 47 // Store time value to avoid clicks on buffer boundries. |
43 size_t t = reinterpret_cast<MyInstance*>(thiz)->audio_time_; | 48 size_t t = reinterpret_cast<MyInstance*>(thiz)->audio_time_; |
44 | 49 |
45 uint16_t* buf = reinterpret_cast<uint16_t*>(samples); | 50 uint16_t* buf = reinterpret_cast<uint16_t*>(samples); |
46 for (size_t buffer_index = 0u; | 51 for (size_t sample = 0; sample < obtained_sample_count; ++sample) { |
47 buffer_index < buffer_size_in_bytes; | 52 *buf++ = static_cast<uint16_t>(sin(th_l * t) |
48 buffer_index += 2) { | |
49 *buf++ = static_cast<uint16_t>(std::sin(th_l * t) | |
50 * std::numeric_limits<uint16_t>::max()); | 53 * std::numeric_limits<uint16_t>::max()); |
51 *buf++ = static_cast<uint16_t>(std::sin(th_r * t++) | 54 *buf++ = static_cast<uint16_t>(sin(th_r * t++) |
52 * std::numeric_limits<uint16_t>::max()); | 55 * std::numeric_limits<uint16_t>::max()); |
53 } | 56 } |
54 reinterpret_cast<MyInstance*>(thiz)->audio_time_ = t; | 57 reinterpret_cast<MyInstance*>(thiz)->audio_time_ = t; |
55 } | 58 } |
56 | 59 |
57 // Audio resource. Allocated in Init(), freed on destruction. | 60 // Audio resource. Allocated in Init(), freed on destruction. |
58 pp::Audio_Dev audio_; | 61 pp::Audio_Dev audio_; |
59 | 62 |
60 // Audio buffer time. Used to make prevent sine wave skips on buffer | 63 // Audio buffer time. Used to make prevent sine wave skips on buffer |
61 // boundaries. | 64 // boundaries. |
62 size_t audio_time_; | 65 size_t audio_time_; |
63 }; | 66 }; |
64 | 67 |
65 class MyModule : public pp::Module { | 68 class MyModule : public pp::Module { |
66 public: | 69 public: |
67 // Override CreateInstance to create your customized Instance object. | 70 // Override CreateInstance to create your customized Instance object. |
68 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 71 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
69 return new MyInstance(instance); | 72 return new MyInstance(instance); |
70 } | 73 } |
71 }; | 74 }; |
72 | 75 |
73 namespace pp { | 76 namespace pp { |
74 | 77 |
75 // Factory function for your specialization of the Module object. | 78 // Factory function for your specialization of the Module object. |
76 Module* CreateModule() { | 79 Module* CreateModule() { |
77 return new MyModule(); | 80 return new MyModule(); |
78 } | 81 } |
79 | 82 |
80 } // namespace pp | 83 } // namespace pp |
OLD | NEW |