| 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 // Needed on Windows to get |M_PI| from math.h. |
| 6 #ifdef _WIN32 |
| 7 #define _USE_MATH_DEFINES |
| 8 #endif |
| 9 |
| 10 #include <math.h> |
| 11 #include <stdlib.h> |
| 12 #include <string.h> |
| 13 |
| 6 #include <limits> | 14 #include <limits> |
| 7 | 15 |
| 8 #include "ppapi/c/pp_errors.h" | 16 #include "ppapi/c/pp_errors.h" |
| 9 #include "ppapi/cpp/audio.h" | 17 #include "ppapi/cpp/audio.h" |
| 10 #include "ppapi/cpp/audio_config.h" | 18 #include "ppapi/cpp/audio_config.h" |
| 11 #include "ppapi/cpp/completion_callback.h" | 19 #include "ppapi/cpp/completion_callback.h" |
| 12 #include "ppapi/cpp/instance.h" | 20 #include "ppapi/cpp/instance.h" |
| 13 #include "ppapi/cpp/module.h" | 21 #include "ppapi/cpp/module.h" |
| 14 | 22 |
| 15 // Separate left and right frequency to make sure we didn't swap L & R. | 23 // Separate left and right frequency to make sure we didn't swap L & R. |
| 16 // Sounds pretty horrible, though... | 24 // Sounds pretty horrible, though... |
| 17 const double frequency_l = 400; | 25 const double kLeftFrequency = 400; |
| 18 const double frequency_r = 1000; | 26 const double kRightFrequency = 1000; |
| 19 | 27 |
| 20 // This sample frequency is guaranteed to work. | 28 // This sample frequency is guaranteed to work. |
| 21 const PP_AudioSampleRate sample_frequency = PP_AUDIOSAMPLERATE_44100; | 29 const PP_AudioSampleRate kDefaultSampleRate = PP_AUDIOSAMPLERATE_44100; |
| 22 const uint32_t sample_count = 4096; | 30 const uint32_t kDefaultSampleCount = 4096; |
| 23 uint32_t obtained_sample_count = 0; | |
| 24 | 31 |
| 25 const double kPi = 3.141592653589; | 32 const char kSampleRateAttributeName[] = "samplerate"; |
| 26 const double kTwoPi = 2.0 * kPi; | |
| 27 | 33 |
| 28 class MyInstance : public pp::Instance { | 34 class MyInstance : public pp::Instance { |
| 29 public: | 35 public: |
| 30 explicit MyInstance(PP_Instance instance) | 36 explicit MyInstance(PP_Instance instance) |
| 31 : pp::Instance(instance), | 37 : pp::Instance(instance), |
| 38 sample_rate_(kDefaultSampleRate), |
| 39 sample_count_(0), |
| 32 audio_wave_l_(0.0), | 40 audio_wave_l_(0.0), |
| 33 audio_wave_r_(0.0) { | 41 audio_wave_r_(0.0) { |
| 34 } | 42 } |
| 35 | 43 |
| 36 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { | 44 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
| 45 for (uint32_t i = 0; i < argc; i++) { |
| 46 if (strcmp(kSampleRateAttributeName, argn[i]) == 0) { |
| 47 int value = atoi(argv[i]); |
| 48 if (value > 0 && value <= 1000000) |
| 49 sample_rate_ = static_cast<PP_AudioSampleRate>(value); |
| 50 else |
| 51 return false; |
| 52 } |
| 53 } |
| 54 |
| 37 pp::AudioConfig config; | 55 pp::AudioConfig config; |
| 38 obtained_sample_count = pp::AudioConfig::RecommendSampleFrameCount( | 56 sample_count_ = pp::AudioConfig::RecommendSampleFrameCount( |
| 39 sample_frequency, sample_count); | 57 sample_rate_, kDefaultSampleCount); |
| 40 config = pp::AudioConfig(this, sample_frequency, obtained_sample_count); | 58 config = pp::AudioConfig(this, sample_rate_, sample_count_); |
| 41 audio_ = pp::Audio(this, config, SineWaveCallback, this); | 59 audio_ = pp::Audio(this, config, SineWaveCallbackTrampoline, this); |
| 42 return audio_.StartPlayback(); | 60 return audio_.StartPlayback(); |
| 43 } | 61 } |
| 44 | 62 |
| 45 private: | 63 private: |
| 46 static void SineWaveCallback(void* samples, uint32_t num_bytes, void* thiz) { | 64 static void SineWaveCallbackTrampoline(void* samples, |
| 47 const double delta_l = kTwoPi * frequency_l / sample_frequency; | 65 uint32_t num_bytes, |
| 48 const double delta_r = kTwoPi * frequency_r / sample_frequency; | 66 void* thiz) { |
| 67 static_cast<MyInstance*>(thiz)->SineWaveCallback(samples, num_bytes); |
| 68 } |
| 69 |
| 70 void SineWaveCallback(void* samples, uint32_t num_bytes) { |
| 71 double delta_l = 2.0 * M_PI * kLeftFrequency / sample_rate_; |
| 72 double delta_r = 2.0 * M_PI * kRightFrequency / sample_rate_; |
| 49 | 73 |
| 50 // Use per channel audio wave value to avoid clicks on buffer boundries. | 74 // Use per channel audio wave value to avoid clicks on buffer boundries. |
| 51 double wave_l = reinterpret_cast<MyInstance*>(thiz)->audio_wave_l_; | 75 double wave_l = audio_wave_l_; |
| 52 double wave_r = reinterpret_cast<MyInstance*>(thiz)->audio_wave_r_; | 76 double wave_r = audio_wave_r_; |
| 53 const int16_t max_int16 = std::numeric_limits<int16_t>::max(); | 77 const int16_t max_int16 = std::numeric_limits<int16_t>::max(); |
| 54 int16_t* buf = reinterpret_cast<int16_t*>(samples); | 78 int16_t* buf = reinterpret_cast<int16_t*>(samples); |
| 55 for (size_t sample = 0; sample < obtained_sample_count; ++sample) { | 79 for (size_t sample = 0; sample < sample_count_; ++sample) { |
| 56 *buf++ = static_cast<int16_t>(sin(wave_l) * max_int16); | 80 *buf++ = static_cast<int16_t>(sin(wave_l) * max_int16); |
| 57 *buf++ = static_cast<int16_t>(sin(wave_r) * max_int16); | 81 *buf++ = static_cast<int16_t>(sin(wave_r) * max_int16); |
| 58 // Add delta, keep within -kTwoPi..kTwoPi to preserve precision. | 82 // Add delta, keep within -2 * M_PI .. 2 * M_PI to preserve precision. |
| 59 wave_l += delta_l; | 83 wave_l += delta_l; |
| 60 if (wave_l > kTwoPi) | 84 if (wave_l > 2.0 * M_PI) |
| 61 wave_l -= kTwoPi * 2.0; | 85 wave_l -= 2.0 * M_PI; |
| 62 wave_r += delta_r; | 86 wave_r += delta_r; |
| 63 if (wave_r > kTwoPi) | 87 if (wave_r > 2.0 * M_PI) |
| 64 wave_r -= kTwoPi * 2.0; | 88 wave_r -= 2.0 * M_PI; |
| 65 } | 89 } |
| 66 // Store current value to use as starting point for next callback. | 90 // Store current value to use as starting point for next callback. |
| 67 reinterpret_cast<MyInstance*>(thiz)->audio_wave_l_ = wave_l; | 91 audio_wave_l_ = wave_l; |
| 68 reinterpret_cast<MyInstance*>(thiz)->audio_wave_r_ = wave_r; | 92 audio_wave_r_ = wave_r; |
| 69 } | 93 } |
| 70 | 94 |
| 71 // Audio resource. Allocated in Init(), freed on destruction. | 95 PP_AudioSampleRate sample_rate_; |
| 96 uint32_t sample_count_; |
| 97 |
| 72 pp::Audio audio_; | 98 pp::Audio audio_; |
| 73 | 99 |
| 74 // Current audio wave position, used to prevent sine wave skips | 100 // Current audio wave position, used to prevent sine wave skips |
| 75 // on buffer boundaries. | 101 // on buffer boundaries. |
| 76 double audio_wave_l_; | 102 double audio_wave_l_; |
| 77 double audio_wave_r_; | 103 double audio_wave_r_; |
| 78 }; | 104 }; |
| 79 | 105 |
| 80 class MyModule : public pp::Module { | 106 class MyModule : public pp::Module { |
| 81 public: | 107 public: |
| 82 // Override CreateInstance to create your customized Instance object. | 108 // Override CreateInstance to create your customized Instance object. |
| 83 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 109 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 84 return new MyInstance(instance); | 110 return new MyInstance(instance); |
| 85 } | 111 } |
| 86 }; | 112 }; |
| 87 | 113 |
| 88 namespace pp { | 114 namespace pp { |
| 89 | 115 |
| 90 // Factory function for your specialization of the Module object. | 116 // Factory function for your specialization of the Module object. |
| 91 Module* CreateModule() { | 117 Module* CreateModule() { |
| 92 return new MyModule(); | 118 return new MyModule(); |
| 93 } | 119 } |
| 94 | 120 |
| 95 } // namespace pp | 121 } // namespace pp |
| OLD | NEW |