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

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

Issue 5202002: changes for proxy audio (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 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
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 <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[]) {
29 audio_ = pp::Audio_Dev( 31 pp::AudioConfig_Dev config;
30 *this, pp::AudioConfig_Dev(sample_frequency, sample_count), 32 obtained_sample_count = pp::AudioConfig_Dev::RecommendSampleFrameCount(
31 SineWaveCallback, this); 33 sample_count);
34 config = pp::AudioConfig_Dev(sample_frequency, obtained_sample_count);
35 audio_ = pp::Audio_Dev(*this, config, SineWaveCallback, this);
32 return audio_.StartPlayback(); 36 return audio_.StartPlayback();
33 } 37 }
34 38
35 private: 39 private:
36 static void SineWaveCallback(void* samples, 40 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; 41 const double th_l = 2 * 3.141592653589 * frequency_l / sample_frequency;
40 const double th_r = 2 * 3.141592653589 * frequency_r / sample_frequency; 42 const double th_r = 2 * 3.141592653589 * frequency_r / sample_frequency;
41 43
42 // Store time value to avoid clicks on buffer boundries. 44 // Store time value to avoid clicks on buffer boundries.
43 size_t t = reinterpret_cast<MyInstance*>(thiz)->audio_time_; 45 size_t t = reinterpret_cast<MyInstance*>(thiz)->audio_time_;
44 46
45 uint16_t* buf = reinterpret_cast<uint16_t*>(samples); 47 uint16_t* buf = reinterpret_cast<uint16_t*>(samples);
46 for (size_t buffer_index = 0u; 48 for (size_t sample = 0; sample < obtained_sample_count; ++sample) {
47 buffer_index < buffer_size_in_bytes; 49 *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()); 50 * std::numeric_limits<uint16_t>::max());
51 *buf++ = static_cast<uint16_t>(std::sin(th_r * t++) 51 *buf++ = static_cast<uint16_t>(sin(th_r * t++)
52 * std::numeric_limits<uint16_t>::max()); 52 * std::numeric_limits<uint16_t>::max());
53 } 53 }
54 reinterpret_cast<MyInstance*>(thiz)->audio_time_ = t; 54 reinterpret_cast<MyInstance*>(thiz)->audio_time_ = t;
55 } 55 }
56 56
57 // Audio resource. Allocated in Init(), freed on destruction. 57 // Audio resource. Allocated in Init(), freed on destruction.
58 pp::Audio_Dev audio_; 58 pp::Audio_Dev audio_;
59 59
60 // Audio buffer time. Used to make prevent sine wave skips on buffer 60 // Audio buffer time. Used to make prevent sine wave skips on buffer
61 // boundaries. 61 // boundaries.
62 size_t audio_time_; 62 size_t audio_time_;
63 }; 63 };
64 64
65 class MyModule : public pp::Module { 65 class MyModule : public pp::Module {
66 public: 66 public:
67 // Override CreateInstance to create your customized Instance object. 67 // Override CreateInstance to create your customized Instance object.
68 virtual pp::Instance* CreateInstance(PP_Instance instance) { 68 virtual pp::Instance* CreateInstance(PP_Instance instance) {
69 return new MyInstance(instance); 69 return new MyInstance(instance);
70 } 70 }
71 }; 71 };
72 72
73 namespace pp { 73 namespace pp {
74 74
75 // Factory function for your specialization of the Module object. 75 // Factory function for your specialization of the Module object.
76 Module* CreateModule() { 76 Module* CreateModule() {
77 return new MyModule(); 77 return new MyModule();
78 } 78 }
79 79
80 } // namespace pp 80 } // namespace pp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698