File: create_webaudio.c

package info (click to toggle)
emscripten 3.1.69%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 121,872 kB
  • sloc: ansic: 636,110; cpp: 425,974; javascript: 78,401; python: 58,404; sh: 49,154; pascal: 5,237; makefile: 3,365; asm: 2,415; lisp: 1,869
file content (34 lines) | stat: -rw-r--r-- 1,064 bytes parent folder | download | duplicates (2)
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);
#endif
			} else {
				audioContext.suspend();
			}
		};
	}, context);
}