21 #include "../../SDL_internal.h" 23 #if SDL_AUDIO_DRIVER_EMSCRIPTEN 27 #include "../SDL_audio_c.h" 30 #include <emscripten/emscripten.h> 37 if (this->hidden->write_off + this->convert.len_cvt > this->hidden->mixlen) {
38 if (this->hidden->write_off > this->hidden->read_off) {
40 this->hidden->mixbuf + this->hidden->read_off,
41 this->hidden->mixlen - this->hidden->read_off);
42 this->hidden->write_off = this->hidden->write_off - this->hidden->read_off;
44 this->hidden->write_off = 0;
46 this->hidden->read_off = 0;
49 SDL_memcpy(this->hidden->mixbuf + this->hidden->write_off,
51 this->convert.len_cvt);
52 this->hidden->write_off += this->convert.len_cvt;
53 byte_len = this->hidden->write_off - this->hidden->read_off;
59 HandleAudioProcess(
_THIS)
73 if (this->convert.needed) {
74 if (this->hidden->conv_in_len != 0) {
75 this->convert.len = this->hidden->conv_in_len * bytes_in * this->
spec.
channels;
82 buf = this->convert.buf;
83 byte_len = this->convert.len_cvt;
87 if (!this->hidden->mixbuf) {
88 this->hidden->mixlen = this->
spec.
size > byte_len ? this->
spec.
size * 2 : byte_len * 2;
89 this->hidden->mixbuf =
SDL_malloc(this->hidden->mixlen);
93 byte_len = copyData(
this);
101 byte_len = copyData(
this);
105 buf = this->hidden->mixbuf + this->hidden->read_off;
106 this->hidden->read_off += byte_len;
110 if (!this->hidden->mixbuf) {
111 this->hidden->mixlen = this->
spec.
size;
112 this->hidden->mixbuf =
SDL_malloc(this->hidden->mixlen);
115 this->hidden->mixbuf,
116 this->hidden->mixlen);
117 buf = this->hidden->mixbuf;
118 byte_len = this->hidden->mixlen;
123 var numChannels = SDL2.audio.currentOutputBuffer[
'numberOfChannels'];
124 for (var
c = 0;
c < numChannels; ++
c) {
125 var channelData = SDL2.audio.currentOutputBuffer[
'getChannelData'](
c);
126 if (channelData.length != $1) {
127 throw 'Web Audio output buffer length mismatch! Destination size: ' + channelData.length +
' samples vs expected ' + $1 +
' samples!';
130 for (var
j = 0;
j < $1; ++
j) {
131 channelData[
j] = getValue($0 + (
j*numChannels +
c)*4,
'float');
139 Emscripten_CloseDevice(
_THIS)
141 if (this->hidden !=
NULL) {
142 if (this->hidden->mixbuf !=
NULL) {
145 this->hidden->mixbuf =
NULL;
154 Emscripten_OpenDevice(
_THIS,
void *handle,
const char *devname,
int iscapture)
162 while ((!valid_format) && (test_format)) {
163 switch (test_format) {
181 if (this->hidden ==
NULL) {
184 SDL_memset(this->hidden, 0, (
sizeof *this->hidden));
189 result = EM_ASM_INT_V({
190 if(typeof(SDL2) ===
'undefined')
193 if(typeof(SDL2.audio) ===
'undefined')
196 if (!SDL2.audioContext) {
197 if (typeof(AudioContext) !==
'undefined') {
198 SDL2.audioContext = new AudioContext();
199 }
else if (typeof(webkitAudioContext) !==
'undefined') {
200 SDL2.audioContext =
new webkitAudioContext();
212 int sampleRate = EM_ASM_INT_V({
213 return SDL2.audioContext[
'sampleRate'];
218 f = (float)i / (
float)sampleRate * (float)this->
spec.
freq;
220 this->hidden->conv_in_len =
SDL_floor(f);
232 SDL2.audio.scriptProcessorNode = SDL2.audioContext[
'createScriptProcessor']($1, 0, $0);
233 SDL2.audio.scriptProcessorNode[
'onaudioprocess'] =
function (
e) {
234 SDL2.audio.currentOutputBuffer =
e[
'outputBuffer'];
235 Runtime.dynCall(
'vi', $2, [$3]);
237 SDL2.audio.scriptProcessorNode[
'connect'](SDL2.audioContext[
'destination']);
257 int available = EM_ASM_INT_V({
258 if (typeof(AudioContext) !==
'undefined') {
260 }
else if (typeof(webkitAudioContext) !==
'undefined') {
274 "emscripten",
"SDL emscripten audio driver", Emscripten_Init, 0
SDL_AudioFormat SDL_FirstAudioFormat(SDL_AudioFormat format)
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 * e
int ProvidesOwnCallbackThread
Uint16 SDL_AudioFormat
Audio format flags.
SDL_AudioFormat SDL_NextAudioFormat(void)
int OnlyHasDefaultOutputDevice
uint8_t Uint8
An unsigned 8-bit integer type.
#define SDL_AUDIO_BITSIZE(x)
void SDL_CalculateAudioSpec(SDL_AudioSpec *spec)
SDL_AudioCallback callback
GLenum GLuint GLenum GLsizei const GLchar * buf
GLenum GLenum GLsizei const GLuint GLboolean enabled
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
int(* OpenDevice)(_THIS, void *handle, const char *devname, int iscapture)
#define SDL_OutOfMemory()
void(* CloseDevice)(_THIS)
AudioBootStrap EmscriptenAudio_bootstrap
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)