OLD | NEW |
1 {{+bindTo:partials.standard_nacl_article}} | 1 {{+bindTo:partials.standard_nacl_article}} |
2 | 2 |
3 <section id="audio"> | 3 <section id="audio"> |
4 <span id="devguide-coding-audio"></span><h1 id="audio"><span id="devguide-coding
-audio"></span>Audio</h1> | 4 <span id="devguide-coding-audio"></span><h1 id="audio"><span id="devguide-coding
-audio"></span>Audio</h1> |
5 <div class="contents local" id="contents" style="display: none"> | 5 <div class="contents local" id="contents" style="display: none"> |
6 <ul class="small-gap"> | 6 <ul class="small-gap"> |
7 <li><a class="reference internal" href="#reference-information" id="id1">Referen
ce information</a></li> | 7 <li><a class="reference internal" href="#reference-information" id="id1">Referen
ce information</a></li> |
8 <li><a class="reference internal" href="#about-the-pepper-audio-api" id="id2">Ab
out the Pepper audio API</a></li> | 8 <li><a class="reference internal" href="#about-the-pepper-audio-api" id="id2">Ab
out the Pepper audio API</a></li> |
9 <li><a class="reference internal" href="#digital-audio-concepts" id="id3">Digita
l audio concepts</a></li> | 9 <li><a class="reference internal" href="#digital-audio-concepts" id="id3">Digita
l audio concepts</a></li> |
10 <li><a class="reference internal" href="#setting-up-the-module" id="id4">Setting
up the module</a></li> | 10 <li><a class="reference internal" href="#setting-up-the-module" id="id4">Setting
up the module</a></li> |
(...skipping 20 matching lines...) Expand all Loading... |
31 audio stream, and tells the browser to start or stop playing the audio | 31 audio stream, and tells the browser to start or stop playing the audio |
32 resource. The browser calls a function in the Native Client module to fill a | 32 resource. The browser calls a function in the Native Client module to fill a |
33 buffer with audio samples every time it needs data to play from the audio | 33 buffer with audio samples every time it needs data to play from the audio |
34 stream.</p> | 34 stream.</p> |
35 <p>The code examples in this chapter describe a simple Native Client module that | 35 <p>The code examples in this chapter describe a simple Native Client module that |
36 generates audio samples using a sine wave with a frequency of 440 Hz. The module | 36 generates audio samples using a sine wave with a frequency of 440 Hz. The module |
37 starts playing the audio samples as soon as it is loaded into the browser. For a | 37 starts playing the audio samples as soon as it is loaded into the browser. For a |
38 slightly more sophisticated example, see the <code>audio</code> example (source
code in | 38 slightly more sophisticated example, see the <code>audio</code> example (source
code in |
39 the SDK directory <code>examples/api/audio</code>), which lets users specify a f
requency | 39 the SDK directory <code>examples/api/audio</code>), which lets users specify a f
requency |
40 for the sine wave and click buttons to start and stop audio playback.</p> | 40 for the sine wave and click buttons to start and stop audio playback.</p> |
41 <section id="reference-information"> | |
42 <h2 id="reference-information">Reference information</h2> | 41 <h2 id="reference-information">Reference information</h2> |
43 <p>For reference information related to the Pepper audio API, see the following | 42 <p>For reference information related to the Pepper audio API, see the following |
44 documentation:</p> | 43 documentation:</p> |
45 <ul class="small-gap"> | 44 <ul class="small-gap"> |
46 <li><a class="reference external" href="/native-client/pepper_stable/cpp/classpp
_1_1_audio_config">pp::AudioConfig class</a></li> | 45 <li><a class="reference external" href="/native-client/pepper_stable/cpp/classpp
_1_1_audio_config">pp::AudioConfig class</a></li> |
47 <li><a class="reference external" href="/native-client/pepper_stable/cpp/classpp
_1_1_audio">pp::Audio class</a></li> | 46 <li><a class="reference external" href="/native-client/pepper_stable/cpp/classpp
_1_1_audio">pp::Audio class</a></li> |
48 <li><a class="reference external" href="/native-client/pepper_cpp/audio__config_
8h">audio_config.h</a></li> | 47 <li><a class="reference external" href="/native-client/pepper_cpp/audio__config_
8h">audio_config.h</a></li> |
49 <li><a class="reference external" href="/native-client/pepper_stable/cpp/audio_8
h">audio.h</a></li> | 48 <li><a class="reference external" href="/native-client/pepper_stable/cpp/audio_8
h">audio.h</a></li> |
50 <li><a class="reference external" href="/native-client/pepper_stable/c/group___e
nums#gaee750c350655f2fb0fe04c04029e0ff8">PP_AudioSampleRate</a></li> | 49 <li><a class="reference external" href="/native-client/pepper_stable/c/group___e
nums#gaee750c350655f2fb0fe04c04029e0ff8">PP_AudioSampleRate</a></li> |
51 </ul> | 50 </ul> |
52 </section><section id="about-the-pepper-audio-api"> | |
53 <h2 id="about-the-pepper-audio-api">About the Pepper audio API</h2> | 51 <h2 id="about-the-pepper-audio-api">About the Pepper audio API</h2> |
54 <p>The Pepper audio API lets Native Client modules play audio streams in a | 52 <p>The Pepper audio API lets Native Client modules play audio streams in a |
55 browser. To play an audio stream, a module generates audio samples and writes | 53 browser. To play an audio stream, a module generates audio samples and writes |
56 them into a buffer. The browser reads the audio samples from the buffer and | 54 them into a buffer. The browser reads the audio samples from the buffer and |
57 plays them using an audio device on the client computer.</p> | 55 plays them using an audio device on the client computer.</p> |
58 <img alt="/native-client/images/pepper-audio-buffer.png" src="/native-client/ima
ges/pepper-audio-buffer.png" /> | 56 <img alt="/native-client/images/pepper-audio-buffer.png" src="/native-client/ima
ges/pepper-audio-buffer.png" /> |
59 <p>This mechanism is simple but low-level. If you want to play plain sound files
in | 57 <p>This mechanism is simple but low-level. If you want to play plain sound files
in |
60 a web application, you may want to consider higher-level alternatives such as | 58 a web application, you may want to consider higher-level alternatives such as |
61 using the HTML <code><audio></code> tag, JavaScript, or the new <a class="
reference external" href="http://chromium.googlecode.com/svn/trunk/samples/audio
/index.html">Web Audio API</a>.</p> | 59 using the HTML <code><audio></code> tag, JavaScript, or the new <a class="
reference external" href="http://chromium.googlecode.com/svn/trunk/samples/audio
/index.html">Web Audio API</a>.</p> |
62 <p>The Pepper audio API is a good option for playing audio data if you want to d
o | 60 <p>The Pepper audio API is a good option for playing audio data if you want to d
o |
(...skipping 11 matching lines...) Expand all Loading... |
74 <li>Your module invokes the StartPlayback and StopPlayback methods of the audio | 72 <li>Your module invokes the StartPlayback and StopPlayback methods of the audio |
75 resource (e.g., when certain events occur).</li> | 73 resource (e.g., when certain events occur).</li> |
76 <li>The browser invokes your callback function whenever it needs audio data to | 74 <li>The browser invokes your callback function whenever it needs audio data to |
77 play. Your callback function can generate the audio data in a number of | 75 play. Your callback function can generate the audio data in a number of |
78 ways—e.g., it can generate new data, or it can copy pre-mixed data into th
e | 76 ways—e.g., it can generate new data, or it can copy pre-mixed data into th
e |
79 audio buffer.</li> | 77 audio buffer.</li> |
80 </ol> | 78 </ol> |
81 <p>This basic interaction is illustrated below, and described in detail in the | 79 <p>This basic interaction is illustrated below, and described in detail in the |
82 sections that follow.</p> | 80 sections that follow.</p> |
83 <img alt="/native-client/images/pepper-audio-api.png" src="/native-client/images
/pepper-audio-api.png" /> | 81 <img alt="/native-client/images/pepper-audio-api.png" src="/native-client/images
/pepper-audio-api.png" /> |
84 </section><section id="digital-audio-concepts"> | |
85 <h2 id="digital-audio-concepts">Digital audio concepts</h2> | 82 <h2 id="digital-audio-concepts">Digital audio concepts</h2> |
86 <p>Before you use the Pepper audio API, it’s helpful to understand a few c
oncepts | 83 <p>Before you use the Pepper audio API, it’s helpful to understand a few c
oncepts |
87 that are fundamental to how digital audio is recorded and played back:</p> | 84 that are fundamental to how digital audio is recorded and played back:</p> |
88 <dl class="docutils"> | 85 <dl class="docutils"> |
89 <dt>sample rate</dt> | 86 <dt>sample rate</dt> |
90 <dd>the number of times an input sound source is sampled per second; | 87 <dd>the number of times an input sound source is sampled per second; |
91 correspondingly, the number of samples that are played back per second</dd> | 88 correspondingly, the number of samples that are played back per second</dd> |
92 <dt>bit depth</dt> | 89 <dt>bit depth</dt> |
93 <dd>the number of bits used to represent a sample</dd> | 90 <dd>the number of bits used to represent a sample</dd> |
94 <dt>channels</dt> | 91 <dt>channels</dt> |
95 <dd>the number of input sources recorded in each sampling interval; | 92 <dd>the number of input sources recorded in each sampling interval; |
96 correspondingly, the number of outputs that are played back simultaneously | 93 correspondingly, the number of outputs that are played back simultaneously |
97 (typically using different speakers)</dd> | 94 (typically using different speakers)</dd> |
98 </dl> | 95 </dl> |
99 <p>The higher the sample rate and bit depth used to record a sound wave, the mor
e | 96 <p>The higher the sample rate and bit depth used to record a sound wave, the mor
e |
100 accurately the sound wave can be reproduced, since it will have been sampled | 97 accurately the sound wave can be reproduced, since it will have been sampled |
101 more frequently and stored using a higher level of quantization. Common sampling | 98 more frequently and stored using a higher level of quantization. Common sampling |
102 rates include 44,100 Hz (44,100 samples/second, the sample rate used on CDs), | 99 rates include 44,100 Hz (44,100 samples/second, the sample rate used on CDs), |
103 and 48,000 Hz (the sample rate used on DVDs and Digital Audio Tapes). A common | 100 and 48,000 Hz (the sample rate used on DVDs and Digital Audio Tapes). A common |
104 bit depth is 16 bits per sample, and a common number of channels is 2 (left and | 101 bit depth is 16 bits per sample, and a common number of channels is 2 (left and |
105 right channels for stereo sound).</p> | 102 right channels for stereo sound).</p> |
106 <p id="pepper-audio-configurations">The Pepper audio API currently lets Native C
lient modules play audio streams | 103 <p id="pepper-audio-configurations">The Pepper audio API currently lets Native C
lient modules play audio streams |
107 with the following configurations:</p> | 104 with the following configurations:</p> |
108 <ul class="small-gap"> | 105 <ul class="small-gap"> |
109 <li><strong>sample rate</strong>: 44,100 Hz or 48,000 Hz</li> | 106 <li><strong>sample rate</strong>: 44,100 Hz or 48,000 Hz</li> |
110 <li><strong>bit depth</strong>: 16</li> | 107 <li><strong>bit depth</strong>: 16</li> |
111 <li><strong>channels</strong>: 2 (stereo)</li> | 108 <li><strong>channels</strong>: 2 (stereo)</li> |
112 </ul> | 109 </ul> |
113 </section><section id="setting-up-the-module"> | |
114 <h2 id="setting-up-the-module">Setting up the module</h2> | 110 <h2 id="setting-up-the-module">Setting up the module</h2> |
115 <p>The code examples below describe a simple Native Client module that generates | 111 <p>The code examples below describe a simple Native Client module that generates |
116 audio samples using a sine wave with a frequency of 440 Hz. The module starts | 112 audio samples using a sine wave with a frequency of 440 Hz. The module starts |
117 playing the audio samples as soon as it is loaded into the browser.</p> | 113 playing the audio samples as soon as it is loaded into the browser.</p> |
118 <p>The Native Client module is set up by implementing subclasses of the | 114 <p>The Native Client module is set up by implementing subclasses of the |
119 <code>pp::Module</code> and <code>pp::Instance</code> classes, as normal.</p> | 115 <code>pp::Module</code> and <code>pp::Instance</code> classes, as normal.</p> |
120 <pre class="prettyprint"> | 116 <pre class="prettyprint"> |
121 class SineSynthInstance : public pp::Instance { | 117 class SineSynthInstance : public pp::Instance { |
122 public: | 118 public: |
123 explicit SineSynthInstance(PP_Instance instance); | 119 explicit SineSynthInstance(PP_Instance instance); |
(...skipping 22 matching lines...) Expand all Loading... |
146 public: | 142 public: |
147 SineSynthModule() : pp::Module() {} | 143 SineSynthModule() : pp::Module() {} |
148 ~SineSynthModule() {} | 144 ~SineSynthModule() {} |
149 | 145 |
150 // Create and return a SineSynthInstance object. | 146 // Create and return a SineSynthInstance object. |
151 virtual pp::Instance* CreateInstance(PP_Instance instance) { | 147 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
152 return new SineSynthInstance(instance); | 148 return new SineSynthInstance(instance); |
153 } | 149 } |
154 }; | 150 }; |
155 </pre> | 151 </pre> |
156 </section><section id="creating-an-audio-configuration-resource"> | |
157 <h2 id="creating-an-audio-configuration-resource">Creating an audio configuratio
n resource</h2> | 152 <h2 id="creating-an-audio-configuration-resource">Creating an audio configuratio
n resource</h2> |
158 <section id="resources"> | |
159 <h3 id="resources">Resources</h3> | 153 <h3 id="resources">Resources</h3> |
160 <p>Before the module can play an audio stream, it must create two resources: an | 154 <p>Before the module can play an audio stream, it must create two resources: an |
161 audio configuration resource and an audio resource. Resources are handles to | 155 audio configuration resource and an audio resource. Resources are handles to |
162 objects that the browser provides to module instances. An audio resource is an | 156 objects that the browser provides to module instances. An audio resource is an |
163 object that represents the state of an audio stream, including whether the | 157 object that represents the state of an audio stream, including whether the |
164 stream is paused or being played back, and which callback function to invoke | 158 stream is paused or being played back, and which callback function to invoke |
165 when the samples in the stream’s buffer run out. An audio configuration re
source | 159 when the samples in the stream’s buffer run out. An audio configuration re
source |
166 is an object that stores configuration data for an audio resource, including the | 160 is an object that stores configuration data for an audio resource, including the |
167 sampling frequency of the audio samples, and the number of samples that the | 161 sampling frequency of the audio samples, and the number of samples that the |
168 callback function must provide when the browser invokes it.</p> | 162 callback function must provide when the browser invokes it.</p> |
169 </section><section id="sample-frame-count"> | |
170 <h3 id="sample-frame-count">Sample frame count</h3> | 163 <h3 id="sample-frame-count">Sample frame count</h3> |
171 <p>Prior to creating an audio configuration resource, the module should call | 164 <p>Prior to creating an audio configuration resource, the module should call |
172 <code>RecommendSampleFrameCount</code> to obtain a <em>sample frame count</em> f
rom the | 165 <code>RecommendSampleFrameCount</code> to obtain a <em>sample frame count</em> f
rom the |
173 browser. The sample frame count is the number of samples that the callback | 166 browser. The sample frame count is the number of samples that the callback |
174 function must provide per channel each time the browser invokes the callback | 167 function must provide per channel each time the browser invokes the callback |
175 function. For example, if the sample frame count is 4096 for a stereo audio | 168 function. For example, if the sample frame count is 4096 for a stereo audio |
176 stream, the callback function must provide a 8192 samples (4096 for the left | 169 stream, the callback function must provide a 8192 samples (4096 for the left |
177 channel and 4096 for the right channel).</p> | 170 channel and 4096 for the right channel).</p> |
178 <p>The module can request a specific sample frame count, but the browser may ret
urn | 171 <p>The module can request a specific sample frame count, but the browser may ret
urn |
179 a different sample frame count depending on the capabilities of the client | 172 a different sample frame count depending on the capabilities of the client |
180 device. At present, <code>RecommendSampleFrameCount</code> simply bound-checks t
he | 173 device. At present, <code>RecommendSampleFrameCount</code> simply bound-checks t
he |
181 requested sample frame count (see <code>include/ppapi/c/ppb_audio_config.h</code
> for the | 174 requested sample frame count (see <code>include/ppapi/c/ppb_audio_config.h</code
> for the |
182 minimum and maximum sample frame counts, currently 64 and 32768). In the future, | 175 minimum and maximum sample frame counts, currently 64 and 32768). In the future, |
183 <code>RecommendSampleFrameCount</code> may perform a more sophisticated calculat
ion, | 176 <code>RecommendSampleFrameCount</code> may perform a more sophisticated calculat
ion, |
184 particularly if there is an intrinsic buffer size for the client device.</p> | 177 particularly if there is an intrinsic buffer size for the client device.</p> |
185 <p>Selecting a sample frame count for an audio stream involves a tradeoff betwee
n | 178 <p>Selecting a sample frame count for an audio stream involves a tradeoff betwee
n |
186 latency and CPU usage. If you want your module to have short audio latency so | 179 latency and CPU usage. If you want your module to have short audio latency so |
187 that it can rapidly change what’s playing in the audio stream, you should | 180 that it can rapidly change what’s playing in the audio stream, you should |
188 request a small sample frame count. That could be useful in gaming applications, | 181 request a small sample frame count. That could be useful in gaming applications, |
189 for example, where sounds have to change frequently in response to game | 182 for example, where sounds have to change frequently in response to game |
190 action. However, a small sample frame count results in higher CPU usage, since | 183 action. However, a small sample frame count results in higher CPU usage, since |
191 the browser must invoke the callback function frequently to refill the audio | 184 the browser must invoke the callback function frequently to refill the audio |
192 buffer. Conversely, a large sample frame count results in higher latency but | 185 buffer. Conversely, a large sample frame count results in higher latency but |
193 lower CPU usage. You should request a large sample frame count if your module | 186 lower CPU usage. You should request a large sample frame count if your module |
194 will play long, uninterrupted audio segments.</p> | 187 will play long, uninterrupted audio segments.</p> |
195 </section><section id="supported-audio-configurations"> | |
196 <h3 id="supported-audio-configurations">Supported audio configurations</h3> | 188 <h3 id="supported-audio-configurations">Supported audio configurations</h3> |
197 <p>After the module obtains a sample frame count, it can create an audio | 189 <p>After the module obtains a sample frame count, it can create an audio |
198 configuration resource. Currently the Pepper audio API supports audio streams | 190 configuration resource. Currently the Pepper audio API supports audio streams |
199 with the configuration settings shown <a class="reference internal" href="#peppe
r-audio-configurations"><em>above</em></a>. | 191 with the configuration settings shown <a class="reference internal" href="#peppe
r-audio-configurations"><em>above</em></a>. |
200 C++ modules can create a configuration resource by instantiating a | 192 C++ modules can create a configuration resource by instantiating a |
201 <code>pp::AudioConfig</code> object. Check <code>audio_config.h</code> for the l
atest | 193 <code>pp::AudioConfig</code> object. Check <code>audio_config.h</code> for the l
atest |
202 configurations that are supported.</p> | 194 configurations that are supported.</p> |
203 <pre class="prettyprint"> | 195 <pre class="prettyprint"> |
204 bool SineSynthInstance::Init(uint32_t argc, | 196 bool SineSynthInstance::Init(uint32_t argc, |
205 const char* argn[], | 197 const char* argn[], |
(...skipping 12 matching lines...) Expand all Loading... |
218 // Create an audio resource. | 210 // Create an audio resource. |
219 audio_ = pp::Audio(this, | 211 audio_ = pp::Audio(this, |
220 audio_config, | 212 audio_config, |
221 SineWaveCallback, | 213 SineWaveCallback, |
222 this); | 214 this); |
223 | 215 |
224 // Start playback when the module instance is initialized. | 216 // Start playback when the module instance is initialized. |
225 return audio_.StartPlayback(); | 217 return audio_.StartPlayback(); |
226 } | 218 } |
227 </pre> | 219 </pre> |
228 </section></section><section id="creating-an-audio-resource"> | |
229 <h2 id="creating-an-audio-resource">Creating an audio resource</h2> | 220 <h2 id="creating-an-audio-resource">Creating an audio resource</h2> |
230 <p>Once the module has created an audio configuration resource, it can create an | 221 <p>Once the module has created an audio configuration resource, it can create an |
231 audio resource. To do so, it instantiates a <code>pp::Audio</code> object, passi
ng in a | 222 audio resource. To do so, it instantiates a <code>pp::Audio</code> object, passi
ng in a |
232 pointer to the module instance, the audio configuration resource, a callback | 223 pointer to the module instance, the audio configuration resource, a callback |
233 function, and a pointer to user data (data that is used in the callback | 224 function, and a pointer to user data (data that is used in the callback |
234 function). See the example above.</p> | 225 function). See the example above.</p> |
235 </section><section id="implementing-a-callback-function"> | |
236 <h2 id="implementing-a-callback-function">Implementing a callback function</h2> | 226 <h2 id="implementing-a-callback-function">Implementing a callback function</h2> |
237 <p>The browser calls the callback function associated with an audio resource eve
ry | 227 <p>The browser calls the callback function associated with an audio resource eve
ry |
238 time it needs more samples to play. The callback function can generate new | 228 time it needs more samples to play. The callback function can generate new |
239 samples (e.g., by applying sound effects), or copy pre-mixed samples into the | 229 samples (e.g., by applying sound effects), or copy pre-mixed samples into the |
240 audio buffer. The example below generates new samples by computing values of a | 230 audio buffer. The example below generates new samples by computing values of a |
241 sine wave.</p> | 231 sine wave.</p> |
242 <p>The last parameter passed to the callback function is generic user data that
the | 232 <p>The last parameter passed to the callback function is generic user data that
the |
243 function can use in processing samples. In the example below, the user data is a | 233 function can use in processing samples. In the example below, the user data is a |
244 pointer to the module instance, which includes member variables | 234 pointer to the module instance, which includes member variables |
245 <code>sample_frame_count_</code> (the sample frame count obtained from the brows
er) and | 235 <code>sample_frame_count_</code> (the sample frame count obtained from the brows
er) and |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 int16_t scaled_value = static_cast<int16_t>(sin_value * max_int16); | 276 int16_t scaled_value = static_cast<int16_t>(sin_value * max_int16); |
287 for (size_t channel = 0; channel < kChannels; ++channel) { | 277 for (size_t channel = 0; channel < kChannels; ++channel) { |
288 *buff++ = scaled_value; | 278 *buff++ = scaled_value; |
289 } | 279 } |
290 } | 280 } |
291 } | 281 } |
292 | 282 |
293 ... | 283 ... |
294 }; | 284 }; |
295 </pre> | 285 </pre> |
296 <section id="application-threads-and-real-time-requirements"> | |
297 <h3 id="application-threads-and-real-time-requirements">Application threads and
real-time requirements</h3> | 286 <h3 id="application-threads-and-real-time-requirements">Application threads and
real-time requirements</h3> |
298 <p>The callback function runs in a background application thread. This allows au
dio | 287 <p>The callback function runs in a background application thread. This allows au
dio |
299 processing to continue even when the application is busy doing something | 288 processing to continue even when the application is busy doing something |
300 else. If the main application thread and the callback thread access the same | 289 else. If the main application thread and the callback thread access the same |
301 data, you may be tempted to use a lock to control access to that data. You | 290 data, you may be tempted to use a lock to control access to that data. You |
302 should avoid the use of locks in the callback thread, however, as attempting to | 291 should avoid the use of locks in the callback thread, however, as attempting to |
303 acquire a lock may cause the thread to get swapped out, resulting in audio | 292 acquire a lock may cause the thread to get swapped out, resulting in audio |
304 dropouts.</p> | 293 dropouts.</p> |
305 <p>In general, you must program the callback thread carefully, as the Pepper aud
io | 294 <p>In general, you must program the callback thread carefully, as the Pepper aud
io |
306 API is a very low level API that needs to meet hard real-time requirements. If | 295 API is a very low level API that needs to meet hard real-time requirements. If |
(...skipping 11 matching lines...) Expand all Loading... |
318 avoid making system/Pepper calls in the first place. In short, the audio | 307 avoid making system/Pepper calls in the first place. In short, the audio |
319 (callback) thread should use “lock-free” techniques and avoid making
CRT library | 308 (callback) thread should use “lock-free” techniques and avoid making
CRT library |
320 calls.</p> | 309 calls.</p> |
321 <p>One other issue to be aware of is that the <code>StartPlayback</code> functio
n (discussed | 310 <p>One other issue to be aware of is that the <code>StartPlayback</code> functio
n (discussed |
322 below) is an asynchronous RPC; i.e., it does not block. That means that the | 311 below) is an asynchronous RPC; i.e., it does not block. That means that the |
323 callback function may not be called immediately after the call to | 312 callback function may not be called immediately after the call to |
324 <code>StartPlayback</code>. If it’s important to synchronize the callback
thread with | 313 <code>StartPlayback</code>. If it’s important to synchronize the callback
thread with |
325 another thread so that the audio stream starts playing simultaneously with | 314 another thread so that the audio stream starts playing simultaneously with |
326 another action in your application, you must handle such synchronization | 315 another action in your application, you must handle such synchronization |
327 manually.</p> | 316 manually.</p> |
328 </section></section><section id="starting-and-stopping-playback"> | |
329 <h2 id="starting-and-stopping-playback">Starting and stopping playback</h2> | 317 <h2 id="starting-and-stopping-playback">Starting and stopping playback</h2> |
330 <p>To start and stop audio playback, the module simply reacts to JavaScript | 318 <p>To start and stop audio playback, the module simply reacts to JavaScript |
331 messages.</p> | 319 messages.</p> |
332 <pre class="prettyprint"> | 320 <pre class="prettyprint"> |
333 const char* const kPlaySoundId = "playSound"; | 321 const char* const kPlaySoundId = "playSound"; |
334 const char* const kStopSoundId = "stopSound"; | 322 const char* const kStopSoundId = "stopSound"; |
335 | 323 |
336 void SineSynthInstance::HandleMessage(const pp::Var& var_message) { | 324 void SineSynthInstance::HandleMessage(const pp::Var& var_message) { |
337 if (!var_message.is_string()) { | 325 if (!var_message.is_string()) { |
338 return; | 326 return; |
339 } | 327 } |
340 std::string message = var_message.AsString(); | 328 std::string message = var_message.AsString(); |
341 if (message == kPlaySoundId) { | 329 if (message == kPlaySoundId) { |
342 audio_.StartPlayback(); | 330 audio_.StartPlayback(); |
343 } else if (message == kStopSoundId) { | 331 } else if (message == kStopSoundId) { |
344 audio_.StopPlayback(); | 332 audio_.StopPlayback(); |
345 } else if (...) { | 333 } else if (...) { |
346 ... | 334 ... |
347 } | 335 } |
348 } | 336 } |
349 </pre> | 337 </pre> |
350 </section></section> | 338 </section> |
351 | 339 |
352 {{/partials.standard_nacl_article}} | 340 {{/partials.standard_nacl_article}} |
OLD | NEW |