-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
1 changed file
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <emscripten/em_js.h> | ||
#include <emscripten/webaudio.h> | ||
|
||
#define AUDIO_STACK_SIZE 2048 | ||
|
||
EM_JS(bool, addAudio, (EMSCRIPTEN_WEBAUDIO_T ctxHnd, const char* url, const char* label), { | ||
var context = emscriptenGetAudioObject(ctxHnd); | ||
if (context) { | ||
var audio = document.createElement('audio'); | ||
audio.src = UTF8ToString(url); | ||
audio.loop = true; | ||
var track = context.createMediaElementSource(audio); | ||
track.connect(context.destination); | ||
|
||
var button = document.createElement('button'); | ||
button.innerHTML = UTF8ToString(label); | ||
button.onclick = () => { | ||
if (context.state == 'suspended') { | ||
context.resume(); | ||
} | ||
audio.play(); | ||
}; | ||
|
||
document.body.appendChild(button); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
|
||
int main() { | ||
EMSCRIPTEN_WEBAUDIO_T context = emscripten_create_audio_context(NULL); | ||
|
||
addAudio(context, "audio_files/emscripten-beat.mp3", "Play Beat"); | ||
addAudio(context, "audio_files/emscripten-bass.mp3", "Play Bass"); | ||
} |