OLD | NEW |
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 // | 4 // |
5 | 5 |
6 #include <nacl/nacl_log.h> | 6 #include <nacl/nacl_log.h> |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <stdlib.h> | 10 #include <stdlib.h> |
(...skipping 17 matching lines...) Expand all Loading... |
28 | 28 |
29 | 29 |
30 // Most of this example is borrowed from ppapi/examples/audio/audio.cc | 30 // Most of this example is borrowed from ppapi/examples/audio/audio.cc |
31 | 31 |
32 // Separate left and right frequency to make sure we didn't swap L & R. | 32 // Separate left and right frequency to make sure we didn't swap L & R. |
33 // Sounds pretty horrible, though... | 33 // Sounds pretty horrible, though... |
34 const double kDefaultFrequencyLeft = 400.0; | 34 const double kDefaultFrequencyLeft = 400.0; |
35 const double kDefaultFrequencyRight = 1000.0; | 35 const double kDefaultFrequencyRight = 1000.0; |
36 const uint32_t kDefaultDuration = 10000; | 36 const uint32_t kDefaultDuration = 10000; |
37 | 37 |
38 // This sample frequency is guaranteed to work. | |
39 const PP_AudioSampleRate kSampleFrequency = PP_AUDIOSAMPLERATE_44100; | |
40 // Buffer size in units of sample frames. | |
41 // 4096 is a conservative size that should avoid underruns on most systems. | |
42 const uint32_t kSampleFrameCount = 4096; | |
43 | |
44 const double kPi = 3.141592653589; | 38 const double kPi = 3.141592653589; |
45 const double kTwoPi = 2.0 * kPi; | 39 const double kTwoPi = 2.0 * kPi; |
46 | 40 |
47 void LogFailure(const char* msg) { | 41 void LogFailure(const char* msg) { |
48 NaClLog(LOG_ERROR, "\n*** FAILURE **** example: %s", msg); | 42 NaClLog(LOG_ERROR, "\n*** FAILURE **** example: %s", msg); |
49 } | 43 } |
50 | 44 |
51 class MyInstance : public pp::Instance { | 45 class MyInstance : public pp::Instance { |
52 private: | 46 private: |
53 void ParseArgs(uint32_t argc, const char* argn[], const char* argv[]) { | 47 void ParseArgs(uint32_t argc, const char* argn[], const char* argv[]) { |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 } | 185 } |
192 | 186 |
193 void TestSuite() { | 187 void TestSuite() { |
194 if (basic_tests_) | 188 if (basic_tests_) |
195 BasicTests(); | 189 BasicTests(); |
196 if (stress_tests_) | 190 if (stress_tests_) |
197 StressTests(); | 191 StressTests(); |
198 } | 192 } |
199 | 193 |
200 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { | 194 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { |
| 195 const int32_t kSampleFrameCount = 2048; |
201 ParseArgs(argc, argn, argv); | 196 ParseArgs(argc, argn, argv); |
| 197 obtained_sample_rate_ = pp::AudioConfig::RecommendSampleRate(this); |
202 obtained_sample_frame_count_ = pp::AudioConfig::RecommendSampleFrameCount( | 198 obtained_sample_frame_count_ = pp::AudioConfig::RecommendSampleFrameCount( |
203 kSampleFrequency, kSampleFrameCount); | 199 this, obtained_sample_rate_, kSampleFrameCount); |
204 config_ = new | 200 config_ = new pp::AudioConfig( |
205 pp::AudioConfig(this, kSampleFrequency, obtained_sample_frame_count_); | 201 this, obtained_sample_rate_, obtained_sample_frame_count_); |
206 CHECK(NULL != config_); | 202 CHECK(NULL != config_); |
207 audio_ = new pp::Audio(this, *config_, SineWaveCallback, this); | 203 audio_ = new pp::Audio(this, *config_, SineWaveCallback, this); |
208 CHECK(NULL != audio_); | 204 CHECK(NULL != audio_); |
209 // Run through test suite before attempting real playback. | 205 // Run through test suite before attempting real playback. |
210 TestSuite(); | 206 TestSuite(); |
211 return true; | 207 return true; |
212 } | 208 } |
213 | 209 |
214 private: | 210 private: |
215 static void SineWaveCallback(void* samples, uint32_t num_bytes, void* thiz) { | 211 static void SineWaveCallback(void* samples, uint32_t num_bytes, void* thiz) { |
216 MyInstance* instance = reinterpret_cast<MyInstance*>(thiz); | 212 MyInstance* instance = reinterpret_cast<MyInstance*>(thiz); |
217 const double delta_l = kTwoPi * instance->frequency_l_ / kSampleFrequency; | 213 const double delta_l = kTwoPi * instance->frequency_l_ / |
218 const double delta_r = kTwoPi * instance->frequency_r_ / kSampleFrequency; | 214 instance->obtained_sample_rate_; |
| 215 const double delta_r = kTwoPi * instance->frequency_r_ / |
| 216 instance->obtained_sample_rate_; |
219 | 217 |
220 // Verify num_bytes and obtained_sample_frame_count match up. | 218 // Verify num_bytes and obtained_sample_frame_count match up. |
221 const int kNumChannelsForStereo = 2; | 219 const int kNumChannelsForStereo = 2; |
222 const int kSizeOfSample = sizeof(int16_t); | 220 const int kSizeOfSample = sizeof(int16_t); |
223 const size_t single_sample = kNumChannelsForStereo * kSizeOfSample; | 221 const size_t single_sample = kNumChannelsForStereo * kSizeOfSample; |
224 | 222 |
225 // CHECK inside callback is only for testing purposes. | 223 // CHECK inside callback is only for testing purposes. |
226 CHECK(instance->obtained_sample_frame_count_ * single_sample == num_bytes); | 224 CHECK(instance->obtained_sample_frame_count_ * single_sample == num_bytes); |
227 | 225 |
228 // Use per channel audio wave value to avoid clicks on buffer boundries. | 226 // Use per channel audio wave value to avoid clicks on buffer boundries. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 | 268 |
271 double amplitude_l_; | 269 double amplitude_l_; |
272 double amplitude_r_; | 270 double amplitude_r_; |
273 | 271 |
274 bool headless_; | 272 bool headless_; |
275 | 273 |
276 bool basic_tests_; | 274 bool basic_tests_; |
277 bool stress_tests_; | 275 bool stress_tests_; |
278 | 276 |
279 uint32_t duration_msec_; | 277 uint32_t duration_msec_; |
| 278 PP_AudioSampleRate obtained_sample_rate_; |
280 uint32_t obtained_sample_frame_count_; | 279 uint32_t obtained_sample_frame_count_; |
281 | 280 |
282 int callback_count_; | 281 int callback_count_; |
283 }; | 282 }; |
284 | 283 |
285 class MyModule : public pp::Module { | 284 class MyModule : public pp::Module { |
286 public: | 285 public: |
287 // Override CreateInstance to create your customized Instance object. | 286 // Override CreateInstance to create your customized Instance object. |
288 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 287 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
289 return new MyInstance(instance); | 288 return new MyInstance(instance); |
290 } | 289 } |
291 }; | 290 }; |
292 | 291 |
293 namespace pp { | 292 namespace pp { |
294 | 293 |
295 // Factory function for your specialization of the Module object. | 294 // Factory function for your specialization of the Module object. |
296 Module* CreateModule() { | 295 Module* CreateModule() { |
297 NaClLogModuleInit(); | 296 NaClLogModuleInit(); |
298 return new MyModule(); | 297 return new MyModule(); |
299 } | 298 } |
300 | 299 |
301 } // namespace pp | 300 } // namespace pp |
OLD | NEW |