forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_webaudio.c
More file actions
34 lines (28 loc) · 1.04 KB
/
create_webaudio.c
File metadata and controls
34 lines (28 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <emscripten/webaudio.h>
// This code shows a simple example of how to create a Web Audio context from C/C++ code using the webaudio.h API,
// and how to add a pure sine wave tone generator to it.
int main()
{
EMSCRIPTEN_WEBAUDIO_T context = emscripten_create_audio_context(0 /* use default constructor options */);
// Illustrate how this handle can be passed to JS code (e.g. to a JS library function, EM_ASM or a EM_JS block)
EM_ASM({
var audioContext = emscriptenGetAudioObject($0);
var oscillator = audioContext.createOscillator();
oscillator.connect(audioContext.destination);
oscillator.start();
// Add a button on the page to toggle playback as a response to user click.
var startButton = document.createElement('button');
startButton.innerHTML = 'Toggle playback';
document.body.appendChild(startButton);
startButton.onclick = () => {
if (audioContext.state != 'running') {
audioContext.resume();
#ifdef REPORT_RESULT
__ReportResult(0, 0);
#endif
} else {
audioContext.suspend();
}
};
}, context);
}