Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: ppapi/examples/audio/audio.cc

Issue 8676043: Pepper: Make the sample rate settable in the audio (output) example plugin. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | ppapi/examples/audio/audio.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <math.h>
6 #include <stdlib.h>
7 #include <string.h>
8
6 #include <limits> 9 #include <limits>
7 10
8 #include "ppapi/c/pp_errors.h" 11 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/cpp/audio.h" 12 #include "ppapi/cpp/audio.h"
10 #include "ppapi/cpp/audio_config.h" 13 #include "ppapi/cpp/audio_config.h"
11 #include "ppapi/cpp/completion_callback.h" 14 #include "ppapi/cpp/completion_callback.h"
12 #include "ppapi/cpp/instance.h" 15 #include "ppapi/cpp/instance.h"
13 #include "ppapi/cpp/module.h" 16 #include "ppapi/cpp/module.h"
14 17
15 // Separate left and right frequency to make sure we didn't swap L & R. 18 // Separate left and right frequency to make sure we didn't swap L & R.
16 // Sounds pretty horrible, though... 19 // Sounds pretty horrible, though...
17 const double frequency_l = 400; 20 const double frequency_l = 400;
viettrungluu 2011/11/29 21:22:07 Maybe I should fix these too....
18 const double frequency_r = 1000; 21 const double frequency_r = 1000;
19 22
20 // This sample frequency is guaranteed to work. 23 // This sample frequency is guaranteed to work.
21 const PP_AudioSampleRate sample_frequency = PP_AUDIOSAMPLERATE_44100; 24 const PP_AudioSampleRate kDefaultSampleRate = PP_AUDIOSAMPLERATE_44100;
22 const uint32_t sample_count = 4096; 25 const uint32_t kDefaultSampleCount = 4096;
23 uint32_t obtained_sample_count = 0;
24 26
25 const double kPi = 3.141592653589; 27 const char kSampleRateAttributeName[] = "samplerate";
26 const double kTwoPi = 2.0 * kPi;
27 28
28 class MyInstance : public pp::Instance { 29 class MyInstance : public pp::Instance {
29 public: 30 public:
30 explicit MyInstance(PP_Instance instance) 31 explicit MyInstance(PP_Instance instance)
31 : pp::Instance(instance), 32 : pp::Instance(instance),
33 sample_rate_(kDefaultSampleRate),
34 sample_count_(0),
32 audio_wave_l_(0.0), 35 audio_wave_l_(0.0),
33 audio_wave_r_(0.0) { 36 audio_wave_r_(0.0) {
34 } 37 }
35 38
36 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) { 39 virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
40 for (uint32_t i = 0; i < argc; i++) {
41 if (strcmp(kSampleRateAttributeName, argn[i]) == 0) {
42 int value = atoi(argv[i]);
43 if (value > 0 && value <= 1000000)
44 sample_rate_ = static_cast<PP_AudioSampleRate>(value);
45 else
46 return false;
47 }
48 }
49
37 pp::AudioConfig config; 50 pp::AudioConfig config;
38 obtained_sample_count = pp::AudioConfig::RecommendSampleFrameCount( 51 sample_count_ = pp::AudioConfig::RecommendSampleFrameCount(
39 sample_frequency, sample_count); 52 sample_rate_, kDefaultSampleCount);
40 config = pp::AudioConfig(this, sample_frequency, obtained_sample_count); 53 config = pp::AudioConfig(this, sample_rate_, sample_count_);
41 audio_ = pp::Audio(this, config, SineWaveCallback, this); 54 audio_ = pp::Audio(this, config, SineWaveCallbackTrampoline, this);
42 return audio_.StartPlayback(); 55 return audio_.StartPlayback();
43 } 56 }
44 57
45 private: 58 private:
46 static void SineWaveCallback(void* samples, uint32_t num_bytes, void* thiz) { 59 static void SineWaveCallbackTrampoline(void* samples,
47 const double delta_l = kTwoPi * frequency_l / sample_frequency; 60 uint32_t num_bytes,
48 const double delta_r = kTwoPi * frequency_r / sample_frequency; 61 void* thiz) {
62 static_cast<MyInstance*>(thiz)->SineWaveCallback(samples, num_bytes);
63 }
64
65 void SineWaveCallback(void* samples, uint32_t num_bytes) {
66 double delta_l = 2.0 * M_PI * frequency_l / sample_rate_;
67 double delta_r = 2.0 * M_PI * frequency_r / sample_rate_;
49 68
50 // Use per channel audio wave value to avoid clicks on buffer boundries. 69 // Use per channel audio wave value to avoid clicks on buffer boundries.
51 double wave_l = reinterpret_cast<MyInstance*>(thiz)->audio_wave_l_; 70 double wave_l = audio_wave_l_;
52 double wave_r = reinterpret_cast<MyInstance*>(thiz)->audio_wave_r_; 71 double wave_r = audio_wave_r_;
53 const int16_t max_int16 = std::numeric_limits<int16_t>::max(); 72 const int16_t max_int16 = std::numeric_limits<int16_t>::max();
54 int16_t* buf = reinterpret_cast<int16_t*>(samples); 73 int16_t* buf = reinterpret_cast<int16_t*>(samples);
55 for (size_t sample = 0; sample < obtained_sample_count; ++sample) { 74 for (size_t sample = 0; sample < sample_count_; ++sample) {
56 *buf++ = static_cast<int16_t>(sin(wave_l) * max_int16); 75 *buf++ = static_cast<int16_t>(sin(wave_l) * max_int16);
57 *buf++ = static_cast<int16_t>(sin(wave_r) * max_int16); 76 *buf++ = static_cast<int16_t>(sin(wave_r) * max_int16);
58 // Add delta, keep within -kTwoPi..kTwoPi to preserve precision. 77 // Add delta, keep within -2 * M_PI .. 2 * M_PI to preserve precision.
59 wave_l += delta_l; 78 wave_l += delta_l;
60 if (wave_l > kTwoPi) 79 if (wave_l > 2.0 * M_PI)
61 wave_l -= kTwoPi * 2.0; 80 wave_l -= 2.0 * M_PI;
62 wave_r += delta_r; 81 wave_r += delta_r;
63 if (wave_r > kTwoPi) 82 if (wave_r > 2.0 * M_PI)
64 wave_r -= kTwoPi * 2.0; 83 wave_r -= 2.0 * M_PI;
65 } 84 }
66 // Store current value to use as starting point for next callback. 85 // Store current value to use as starting point for next callback.
67 reinterpret_cast<MyInstance*>(thiz)->audio_wave_l_ = wave_l; 86 audio_wave_l_ = wave_l;
68 reinterpret_cast<MyInstance*>(thiz)->audio_wave_r_ = wave_r; 87 audio_wave_r_ = wave_r;
69 } 88 }
70 89
71 // Audio resource. Allocated in Init(), freed on destruction. 90 PP_AudioSampleRate sample_rate_;
91 uint32_t sample_count_;
92
72 pp::Audio audio_; 93 pp::Audio audio_;
73 94
74 // Current audio wave position, used to prevent sine wave skips 95 // Current audio wave position, used to prevent sine wave skips
75 // on buffer boundaries. 96 // on buffer boundaries.
76 double audio_wave_l_; 97 double audio_wave_l_;
77 double audio_wave_r_; 98 double audio_wave_r_;
78 }; 99 };
79 100
80 class MyModule : public pp::Module { 101 class MyModule : public pp::Module {
81 public: 102 public:
82 // Override CreateInstance to create your customized Instance object. 103 // Override CreateInstance to create your customized Instance object.
83 virtual pp::Instance* CreateInstance(PP_Instance instance) { 104 virtual pp::Instance* CreateInstance(PP_Instance instance) {
84 return new MyInstance(instance); 105 return new MyInstance(instance);
85 } 106 }
86 }; 107 };
87 108
88 namespace pp { 109 namespace pp {
89 110
90 // Factory function for your specialization of the Module object. 111 // Factory function for your specialization of the Module object.
91 Module* CreateModule() { 112 Module* CreateModule() {
92 return new MyModule(); 113 return new MyModule();
93 } 114 }
94 115
95 } // namespace pp 116 } // namespace pp
OLDNEW
« no previous file with comments | « no previous file | ppapi/examples/audio/audio.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698