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