From 74af401f22c4c3ad8a7e12251590ebe5728c3ef1 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 00:04:33 +0200 Subject: [PATCH 01/41] Update ofxEmscriptenSoundStream.cpp --- .../src/ofxEmscriptenSoundStream.cpp | 59 +++++++++++++++++-- 1 file changed, 53 insertions(+), 6 deletions(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp index 208400abbab..836aeaec550 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp @@ -9,9 +9,60 @@ #include "html5audio.h" #include "ofBaseApp.h" #include "ofLog.h" +#include "ofSoundBuffer.h" using namespace std; +int stream_callback; +ofSoundBuffer inbuffer; +ofSoundBuffer outbuffer; +uint8_t wasmAudioWorkletStack[4096]; + +EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs, AudioSampleFrame *outputs, int numParams, const AudioParamFrame *params, void *userData) { + ofxEmscriptenSoundStream* stream = (ofxEmscriptenSoundStream*)stream_callback; + stream->ofxEmscriptenSoundStream::audioCB(stream->settings.bufferSize, stream->settings.numInputChannels, stream->settings.numOutputChannels); + if (stream->settings.numInputChannels > 0) { + for (int o = 0; o < numInputs; ++o) { + for (int i = 0; i < 128; ++i) { + for (int ch = 0; ch < inputs[o].numberOfChannels; ++ch) { + inbuffer[i * inputs[o].numberOfChannels + ch] = inputs[o].data[ch * 128 + i]; + } + } + } + } + if (stream->settings.numOutputChannels > 0) { + for (int o = 0; o < numOutputs; ++o) { + for (int i = 0; i < 128; ++i) { + for (int ch = 0; ch < stream->settings.numOutputChannels; ++ch) { + outputs[o].data[ch * 128 + i] = outbuffer[i * outputs[o].numberOfChannels + ch]; + } + } + } + } + return EM_TRUE; +} + +void AudioWorkletProcessorCreated(EMSCRIPTEN_WEBAUDIO_T audioContext, EM_BOOL success, void *userData) { + if (!success) return; + ofxEmscriptenSoundStream* stream = (ofxEmscriptenSoundStream*)stream_callback; + int outputChannelCounts[1] = { static_cast(stream->settings.numOutputChannels) }; + EmscriptenAudioWorkletNodeCreateOptions options = { + .numberOfInputs = 1, + .numberOfOutputs = 1, + .outputChannelCounts = outputChannelCounts + }; + EMSCRIPTEN_AUDIO_WORKLET_NODE_T audioWorklet = emscripten_create_wasm_audio_worklet_node(audioContext, "audio-processor", &options, &ProcessAudio, 0); + html5audio_stream_create(audioWorklet, static_cast(stream->settings.numInputChannels)); +} + +void WebAudioWorkletThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, EM_BOOL success, void *userData) { + if (!success) return; + WebAudioWorkletProcessorCreateOptions opts = { + .name = "audio-processor", + }; + emscripten_create_wasm_audio_worklet_processor_async(audioContext, &opts, AudioWorkletProcessorCreated, userData); +} + int ofxEmscriptenAudioContext(); ofxEmscriptenSoundStream::ofxEmscriptenSoundStream() @@ -34,7 +85,8 @@ bool ofxEmscriptenSoundStream::setup(const ofSoundStreamSettings & settings) { inbuffer.allocate(settings.bufferSize, settings.numInputChannels); outbuffer.allocate(settings.bufferSize, settings.numOutputChannels); this->settings = settings; - html5audio_stream_create(settings.bufferSize,settings.numInputChannels,settings.numOutputChannels,inbuffer.getBuffer().data(),outbuffer.getBuffer().data(),&audio_cb,this); + stream_callback = reinterpret_cast(this); + emscripten_start_wasm_audio_worklet_thread_async(context, wasmAudioWorkletStack, sizeof(wasmAudioWorkletStack), WebAudioWorkletThreadInitialized, 0); return true; } @@ -86,11 +138,6 @@ int ofxEmscriptenSoundStream::getBufferSize() const{ return settings.bufferSize; } -void ofxEmscriptenSoundStream::audio_cb( int bufferSize, int inputChannels, int outputChannels, void * userData){ - ofxEmscriptenSoundStream * stream = (ofxEmscriptenSoundStream*) userData; - stream->audioCB(bufferSize,inputChannels,outputChannels); -} - void ofxEmscriptenSoundStream::audioCB(int bufferSize, int inputChannels, int outputChannels){ if(inputChannels>0 && settings.inCallback) settings.inCallback(inbuffer); if(outputChannels>0 && settings.outCallback) settings.outCallback(outbuffer); From 5eb1151d1323c35dc7af056c1ab6873f4c6fe450 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 00:41:09 +0200 Subject: [PATCH 02/41] Update ofxEmscriptenSoundStream.h --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h index 73943c8a14d..61e038b3cce 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h @@ -32,13 +32,10 @@ class ofxEmscriptenSoundStream: public ofBaseSoundStream { int getNumOutputChannels() const; int getSampleRate() const; int getBufferSize() const; + ofSoundStreamSettings settings; + void audioCB(int bufferSize, int inputChannels, int outputChannels); private: - static void audio_cb(int bufferSize, int inputChannels, int outputChannels, void * userData); - void audioCB(int bufferSize, int inputChannels, int outputChannels); int context; unsigned long long tickCount; - ofSoundStreamSettings settings; - ofSoundBuffer inbuffer; - ofSoundBuffer outbuffer; }; From 32dd2fb3bf699033420d41be0acbea28f49a079e Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 00:55:58 +0200 Subject: [PATCH 03/41] Update ofxEmscriptenSoundStream.h --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h index 61e038b3cce..5ce7d97bb88 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h @@ -8,7 +8,7 @@ #pragma once #include "ofSoundBaseTypes.h" -#include "ofSoundBuffer.h" +#include "emscripten/webaudio.h" class ofxEmscriptenSoundStream: public ofBaseSoundStream { public: From 87a82b9cfbe53bca191edcf75f65ea5ef3467d58 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 01:10:52 +0200 Subject: [PATCH 04/41] Update library_html5audio.js --- .../lib/emscripten/library_html5audio.js | 66 ++++++++----------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js index 81a20818ec9..04802a256ad 100644 --- a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js +++ b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js @@ -29,6 +29,7 @@ var LibraryHTML5Audio = { // Fix up for prefixing window.AudioContext = window.AudioContext || window.webkitAudioContext; var context = new AudioContext({}); + var id = emscriptenRegisterAudioObject(context); // Fix issue with chrome autoplay policy document.addEventListener('mousedown', function cb(event) { @@ -44,7 +45,7 @@ var LibraryHTML5Audio = { fft.maxDecibels = 0; fft.minDecibels = -100; AUDIO.fft = fft; - return 0; + return id; } catch (e) { console.log('Web Audio API is not supported in this browser', e); return -1; @@ -201,48 +202,35 @@ var LibraryHTML5Audio = { } }, - html5audio_stream_create: function(bufferSize, inputChannels, outputChannels, inbuffer, outbuffer, callback, userData) { - var stream = AUDIO.context.createScriptProcessor(bufferSize, inputChannels, outputChannels); - var inbufferArray = Module.HEAPF32.subarray(inbuffer >> 2, (inbuffer >> 2) + bufferSize * inputChannels); - var outbufferArray = Module.HEAPF32.subarray(outbuffer >> 2, (outbuffer >> 2) + bufferSize * outputChannels); - - stream.onaudioprocess = function(event) { - var i, j, c; - if (inputChannels > 0) { - for (c = 0; c < inputChannels; ++c) { - var inChannel = event.inputBuffer.getChannelData(c); - for (i = 0, j = c; i < bufferSize; ++i, j += inputChannels) { - inbufferArray[j] = inChannel[i]; + html5audio_stream_create__deps: ['$emscriptenGetAudioObject'], + html5audio_stream_create: function(audioWorkletNode, numInputChannels){ + var audioWorkletNode = emscriptenGetAudioObject(audioWorkletNode); + if(numInputChannels>0){ + navigator.getUserMedia = navigator.getUserMedia || + navigator.webkitGetUserMedia || + navigator.mozGetUserMedia || + navigator.msGetUserMedia; + if(navigator.getUserMedia){ + navigator.getUserMedia({ + audio: { + autoGainControl: false, + echoCancellation: false, + noiseSuppression: false } - } - } - - {{{ makeDynCall('viiii', 'callback') }}}(bufferSize, inputChannels, outputChannels, userData); - - if (outputChannels > 0) { - for (c = 0; c < outputChannels; ++c) { - var outChannel = event.outputBuffer.getChannelData(c); - for (i = 0, j = c; i < bufferSize; ++i, j += outputChannels) { - outChannel[i] = outbufferArray[j]; - } - } - } - }; - - if (inputChannels > 0) { - navigator.mediaDevices.getUserMedia({ audio: true }) - .then(function (audioIn) { + }, + function(audioIn) { var mediaElement = AUDIO.context.createMediaStreamSource(audioIn); - mediaElement.connect(stream); + mediaElement.connect(audioWorkletNode); AUDIO.mediaElement = mediaElement; - }) - .catch(function (error) { - console.log("Error creating audio in", error); - }); + }, + function(error){ + console.log("error creating audio in",error); + } + ); } - - stream.connect(AUDIO.fft); - }, + } + audioWorkletNode.connect(AUDIO.fft); + }, html5audio_stream_free: function () { From 9f46e2239886929c8ffaa43e354e6caa5963b2ca Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 01:20:26 +0200 Subject: [PATCH 05/41] Update html5audio.h --- addons/ofxEmscripten/libs/html5audio/include/html5audio.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/ofxEmscripten/libs/html5audio/include/html5audio.h b/addons/ofxEmscripten/libs/html5audio/include/html5audio.h index 48831961100..9dd55eaa608 100644 --- a/addons/ofxEmscripten/libs/html5audio/include/html5audio.h +++ b/addons/ofxEmscripten/libs/html5audio/include/html5audio.h @@ -28,7 +28,7 @@ extern "C"{ extern void html5audio_sound_set_pan(int sound, double pan); extern void html5audio_sound_free(int sound); - extern void html5audio_stream_create(int bufferSize, int inputChannels, int outputChannels, float * inbuffer, float * outbuffer, html5audio_stream_callback callback, void * userData); + extern void html5audio_stream_create(int audioWorkletNode, int numInputChannels); extern void html5audio_stream_free(); extern bool html5audio_sound_is_loaded(int sound); } From daed4fc3c4a08336850a97084969b114c67e6557 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 01:21:46 +0200 Subject: [PATCH 06/41] Update ofxEmscriptenSoundStream.cpp --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp index 836aeaec550..73153639155 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp @@ -9,7 +9,6 @@ #include "html5audio.h" #include "ofBaseApp.h" #include "ofLog.h" -#include "ofSoundBuffer.h" using namespace std; @@ -52,7 +51,7 @@ void AudioWorkletProcessorCreated(EMSCRIPTEN_WEBAUDIO_T audioContext, EM_BOOL su .outputChannelCounts = outputChannelCounts }; EMSCRIPTEN_AUDIO_WORKLET_NODE_T audioWorklet = emscripten_create_wasm_audio_worklet_node(audioContext, "audio-processor", &options, &ProcessAudio, 0); - html5audio_stream_create(audioWorklet, static_cast(stream->settings.numInputChannels)); + html5audio_stream_create(audioWorklet, stream->settings.numInputChannels); } void WebAudioWorkletThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, EM_BOOL success, void *userData) { From 3f724d755b799dcd04fff96e068b67066c030909 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 01:22:01 +0200 Subject: [PATCH 07/41] Update ofxEmscriptenSoundStream.h --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h | 1 + 1 file changed, 1 insertion(+) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h index 5ce7d97bb88..1aaedb4cb3f 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h @@ -8,6 +8,7 @@ #pragma once #include "ofSoundBaseTypes.h" +#include "ofSoundBuffer.h" #include "emscripten/webaudio.h" class ofxEmscriptenSoundStream: public ofBaseSoundStream { From 63205a59851e55f9dbf0256d46f61cc157c1dd42 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 01:24:02 +0200 Subject: [PATCH 08/41] Update config.emscripten.default.mk --- .../project/emscripten/config.emscripten.default.mk | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libs/openFrameworksCompiled/project/emscripten/config.emscripten.default.mk b/libs/openFrameworksCompiled/project/emscripten/config.emscripten.default.mk index a5521323215..9ea24af0bef 100644 --- a/libs/openFrameworksCompiled/project/emscripten/config.emscripten.default.mk +++ b/libs/openFrameworksCompiled/project/emscripten/config.emscripten.default.mk @@ -64,8 +64,8 @@ PLATFORM_REQUIRED_ADDONS = ofxEmscripten ################################################################################ # Code Generation Option Flags (http://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html) -PLATFORM_CFLAGS = -PLATFORM_CXXFLAGS = -Wall -std=c++17 -Wno-warn-absolute-paths +PLATFORM_CFLAGS = -s USE_PTHREADS=1 +PLATFORM_CXXFLAGS = -Wall -std=c++17 -Wno-warn-absolute-paths -s USE_PTHREADS=1 ################################################################################ # PLATFORM LDFLAGS @@ -93,7 +93,7 @@ ifdef USE_CCACHE endif endif -PLATFORM_LDFLAGS = -Wl --gc-sections --preload-file bin/data@data --emrun --bind --profiling-funcs -s USE_FREETYPE=1 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -s WEBGL2_BACKWARDS_COMPATIBILITY_EMULATION=1 -s FULL_ES2 -sFULL_ES3=1 +PLATFORM_LDFLAGS = -Wl --gc-sections --preload-file bin/data@data --emrun --bind --profiling-funcs -s USE_FREETYPE=1 -s ALLOW_MEMORY_GROWTH=1 -s MAX_WEBGL_VERSION=2 -s WEBGL2_BACKWARDS_COMPATIBILITY_EMULATION=1 -s FULL_ES2 -sFULL_ES3=1 -s USE_PTHREADS=1 -s AUDIO_WORKLET=1 -s WASM_WORKERS=1 -sENVIRONMENT="web,worker" -s WEBAUDIO_DEBUG=1 PLATFORM_LDFLAGS += --js-library $(OF_ADDONS_PATH)/ofxEmscripten/libs/html5video/lib/emscripten/library_html5video.js PLATFORM_LDFLAGS += --js-library $(OF_ADDONS_PATH)/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js From 53d4523b5d9c34eeab8c06f6d87f92d1b5a3a3a8 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 01:42:48 +0200 Subject: [PATCH 09/41] Update library_html5audio.js --- .../libs/html5audio/lib/emscripten/library_html5audio.js | 1 + 1 file changed, 1 insertion(+) diff --git a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js index 04802a256ad..eb79ba6df74 100644 --- a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js +++ b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js @@ -24,6 +24,7 @@ var LibraryHTML5Audio = { } }, + html5audio_context_create__deps: ['$emscriptenRegisterAudioObject'], html5audio_context_create: function () { try { // Fix up for prefixing From 9771f0bfdcd0780837586f04fd22f4d49597143c Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 01:56:10 +0200 Subject: [PATCH 10/41] Update ofxEmscriptenSoundStream.cpp --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp index 73153639155..c633976a6b2 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp @@ -6,9 +6,11 @@ */ #include "ofxEmscriptenSoundStream.h" -#include "html5audio.h" +#include "ofSoundBuffer.h" #include "ofBaseApp.h" #include "ofLog.h" +#include "html5audio.h" +#include "emscripten/webaudio.h" using namespace std; From 6dbd42bffb1152f511a89da40b11b5e310fa175e Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 01:56:26 +0200 Subject: [PATCH 11/41] Update ofxEmscriptenSoundStream.h --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h index 1aaedb4cb3f..0fd6f24e498 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h @@ -8,8 +8,6 @@ #pragma once #include "ofSoundBaseTypes.h" -#include "ofSoundBuffer.h" -#include "emscripten/webaudio.h" class ofxEmscriptenSoundStream: public ofBaseSoundStream { public: From 4df5929a22f4fb620b7e7fd76ff22fe2cce8c943 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 01:59:40 +0200 Subject: [PATCH 12/41] Update ofxEmscriptenSoundStream.cpp --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp index c633976a6b2..33c3fad426f 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp @@ -17,7 +17,6 @@ using namespace std; int stream_callback; ofSoundBuffer inbuffer; ofSoundBuffer outbuffer; -uint8_t wasmAudioWorkletStack[4096]; EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs, AudioSampleFrame *outputs, int numParams, const AudioParamFrame *params, void *userData) { ofxEmscriptenSoundStream* stream = (ofxEmscriptenSoundStream*)stream_callback; @@ -87,6 +86,7 @@ bool ofxEmscriptenSoundStream::setup(const ofSoundStreamSettings & settings) { outbuffer.allocate(settings.bufferSize, settings.numOutputChannels); this->settings = settings; stream_callback = reinterpret_cast(this); + uint8_t wasmAudioWorkletStack[4096]; emscripten_start_wasm_audio_worklet_thread_async(context, wasmAudioWorkletStack, sizeof(wasmAudioWorkletStack), WebAudioWorkletThreadInitialized, 0); return true; } From 741f3321593c43a232e19af14ed3dd39fd570a0d Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 02:10:48 +0200 Subject: [PATCH 13/41] Update ofxEmscriptenSoundStream.cpp --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp index 33c3fad426f..30de3e37688 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp @@ -86,7 +86,6 @@ bool ofxEmscriptenSoundStream::setup(const ofSoundStreamSettings & settings) { outbuffer.allocate(settings.bufferSize, settings.numOutputChannels); this->settings = settings; stream_callback = reinterpret_cast(this); - uint8_t wasmAudioWorkletStack[4096]; emscripten_start_wasm_audio_worklet_thread_async(context, wasmAudioWorkletStack, sizeof(wasmAudioWorkletStack), WebAudioWorkletThreadInitialized, 0); return true; } From 8583bbddde0a0e3672ca59451d6e8ff7863c9330 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 02:11:17 +0200 Subject: [PATCH 14/41] Update ofxEmscriptenSoundStream.h --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h | 1 + 1 file changed, 1 insertion(+) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h index 0fd6f24e498..dfffd8ca23b 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h @@ -33,6 +33,7 @@ class ofxEmscriptenSoundStream: public ofBaseSoundStream { int getBufferSize() const; ofSoundStreamSettings settings; void audioCB(int bufferSize, int inputChannels, int outputChannels); + uint8_t wasmAudioWorkletStack[4096]; private: int context; From d559af87cad8acbd28d42ef4f5cbe13e8268ef58 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 02:39:56 +0200 Subject: [PATCH 15/41] Update ofxEmscriptenSoundStream.cpp --- .../src/ofxEmscriptenSoundStream.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp index 30de3e37688..896d7436d8c 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp @@ -18,10 +18,13 @@ int stream_callback; ofSoundBuffer inbuffer; ofSoundBuffer outbuffer; +ofxEmscriptenSoundStream * stream; +ofSoundBuffer inbuffer; +ofSoundBuffer outbuffer; + EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs, AudioSampleFrame *outputs, int numParams, const AudioParamFrame *params, void *userData) { - ofxEmscriptenSoundStream* stream = (ofxEmscriptenSoundStream*)stream_callback; - stream->ofxEmscriptenSoundStream::audioCB(stream->settings.bufferSize, stream->settings.numInputChannels, stream->settings.numOutputChannels); if (stream->settings.numInputChannels > 0) { + stream->settings.inCallback(inbuffer); for (int o = 0; o < numInputs; ++o) { for (int i = 0; i < 128; ++i) { for (int ch = 0; ch < inputs[o].numberOfChannels; ++ch) { @@ -31,6 +34,7 @@ EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutpu } } if (stream->settings.numOutputChannels > 0) { + stream->settings.outCallback(outbuffer); for (int o = 0; o < numOutputs; ++o) { for (int i = 0; i < 128; ++i) { for (int ch = 0; ch < stream->settings.numOutputChannels; ++ch) { @@ -44,7 +48,6 @@ EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutpu void AudioWorkletProcessorCreated(EMSCRIPTEN_WEBAUDIO_T audioContext, EM_BOOL success, void *userData) { if (!success) return; - ofxEmscriptenSoundStream* stream = (ofxEmscriptenSoundStream*)stream_callback; int outputChannelCounts[1] = { static_cast(stream->settings.numOutputChannels) }; EmscriptenAudioWorkletNodeCreateOptions options = { .numberOfInputs = 1, @@ -85,7 +88,7 @@ bool ofxEmscriptenSoundStream::setup(const ofSoundStreamSettings & settings) { inbuffer.allocate(settings.bufferSize, settings.numInputChannels); outbuffer.allocate(settings.bufferSize, settings.numOutputChannels); this->settings = settings; - stream_callback = reinterpret_cast(this); + stream = this; emscripten_start_wasm_audio_worklet_thread_async(context, wasmAudioWorkletStack, sizeof(wasmAudioWorkletStack), WebAudioWorkletThreadInitialized, 0); return true; } @@ -137,9 +140,3 @@ int ofxEmscriptenSoundStream::getSampleRate() const{ int ofxEmscriptenSoundStream::getBufferSize() const{ return settings.bufferSize; } - -void ofxEmscriptenSoundStream::audioCB(int bufferSize, int inputChannels, int outputChannels){ - if(inputChannels>0 && settings.inCallback) settings.inCallback(inbuffer); - if(outputChannels>0 && settings.outCallback) settings.outCallback(outbuffer); - tickCount++; -} From 6c10af0b40e33b0dd2946f34b773932c55a40450 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 02:40:20 +0200 Subject: [PATCH 16/41] Update ofxEmscriptenSoundStream.h --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h | 1 - 1 file changed, 1 deletion(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h index dfffd8ca23b..3b64477b2c3 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h @@ -32,7 +32,6 @@ class ofxEmscriptenSoundStream: public ofBaseSoundStream { int getSampleRate() const; int getBufferSize() const; ofSoundStreamSettings settings; - void audioCB(int bufferSize, int inputChannels, int outputChannels); uint8_t wasmAudioWorkletStack[4096]; private: From d100aa30afe9d6b376b0ca21e230fe89dd24e1a4 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 03:03:55 +0200 Subject: [PATCH 17/41] Update ofxEmscriptenSoundStream.cpp --- .../ofxEmscripten/src/ofxEmscriptenSoundStream.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp index 896d7436d8c..30e6b2b5066 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp @@ -14,17 +14,13 @@ using namespace std; -int stream_callback; -ofSoundBuffer inbuffer; -ofSoundBuffer outbuffer; - ofxEmscriptenSoundStream * stream; ofSoundBuffer inbuffer; ofSoundBuffer outbuffer; EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs, AudioSampleFrame *outputs, int numParams, const AudioParamFrame *params, void *userData) { + stream->ofxEmscriptenSoundStream::audioCB(stream->settings.bufferSize, stream->settings.numInputChannels, stream->settings.numOutputChannels); if (stream->settings.numInputChannels > 0) { - stream->settings.inCallback(inbuffer); for (int o = 0; o < numInputs; ++o) { for (int i = 0; i < 128; ++i) { for (int ch = 0; ch < inputs[o].numberOfChannels; ++ch) { @@ -34,7 +30,6 @@ EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutpu } } if (stream->settings.numOutputChannels > 0) { - stream->settings.outCallback(outbuffer); for (int o = 0; o < numOutputs; ++o) { for (int i = 0; i < 128; ++i) { for (int ch = 0; ch < stream->settings.numOutputChannels; ++ch) { @@ -140,3 +135,9 @@ int ofxEmscriptenSoundStream::getSampleRate() const{ int ofxEmscriptenSoundStream::getBufferSize() const{ return settings.bufferSize; } + +void ofxEmscriptenSoundStream::audioCB(int bufferSize, int inputChannels, int outputChannels) { + if (inputChannels > 0 && settings.inCallback) settings.inCallback(inbuffer); + if (outputChannels > 0 && settings.outCallback) settings.outCallback(outbuffer); + tickCount++; +} From 9f4f74aaf0c6fceac4864a76ddc46b4849cf58d9 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 03:24:41 +0200 Subject: [PATCH 18/41] Update ofxEmscriptenSoundStream.cpp --- .../src/ofxEmscriptenSoundStream.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp index 30e6b2b5066..40e5d64e91c 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp @@ -17,10 +17,12 @@ using namespace std; ofxEmscriptenSoundStream * stream; ofSoundBuffer inbuffer; ofSoundBuffer outbuffer; +int audioProcessedCount = 0; EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs, AudioSampleFrame *outputs, int numParams, const AudioParamFrame *params, void *userData) { - stream->ofxEmscriptenSoundStream::audioCB(stream->settings.bufferSize, stream->settings.numInputChannels, stream->settings.numOutputChannels); - if (stream->settings.numInputChannels > 0) { + ++audioProcessedCount; + if (stream->settings.numInputChannels > 0 && stream->settings.inCallback) { + stream->settings.inCallback(inbuffer); for (int o = 0; o < numInputs; ++o) { for (int i = 0; i < 128; ++i) { for (int ch = 0; ch < inputs[o].numberOfChannels; ++ch) { @@ -29,7 +31,8 @@ EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutpu } } } - if (stream->settings.numOutputChannels > 0) { + if (stream->settings.numOutputChannels > 0 && stream->settings.outCallback) { + stream->settings.outCallback(outbuffer); for (int o = 0; o < numOutputs; ++o) { for (int i = 0; i < 128; ++i) { for (int ch = 0; ch < stream->settings.numOutputChannels; ++ch) { @@ -65,7 +68,6 @@ int ofxEmscriptenAudioContext(); ofxEmscriptenSoundStream::ofxEmscriptenSoundStream() :context(ofxEmscriptenAudioContext()) -,tickCount(0) { } @@ -117,7 +119,7 @@ void ofxEmscriptenSoundStream::close() { } uint64_t ofxEmscriptenSoundStream::getTickCount() const{ - return tickCount; + return audioProcessedCount; } int ofxEmscriptenSoundStream::getNumInputChannels() const{ @@ -135,9 +137,3 @@ int ofxEmscriptenSoundStream::getSampleRate() const{ int ofxEmscriptenSoundStream::getBufferSize() const{ return settings.bufferSize; } - -void ofxEmscriptenSoundStream::audioCB(int bufferSize, int inputChannels, int outputChannels) { - if (inputChannels > 0 && settings.inCallback) settings.inCallback(inbuffer); - if (outputChannels > 0 && settings.outCallback) settings.outCallback(outbuffer); - tickCount++; -} From 545ac97bb4d305dc208a828097280ad751b1b021 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 03:25:06 +0200 Subject: [PATCH 19/41] Update ofxEmscriptenSoundStream.h --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h | 1 - 1 file changed, 1 deletion(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h index 3b64477b2c3..b3a57e869bb 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h @@ -36,5 +36,4 @@ class ofxEmscriptenSoundStream: public ofBaseSoundStream { private: int context; - unsigned long long tickCount; }; From 3dd871778611b16395dc55dab9287b867e52b785 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 03:42:36 +0200 Subject: [PATCH 20/41] Update ofxEmscriptenSoundStream.h --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h index b3a57e869bb..4e9631705b2 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h @@ -32,8 +32,8 @@ class ofxEmscriptenSoundStream: public ofBaseSoundStream { int getSampleRate() const; int getBufferSize() const; ofSoundStreamSettings settings; - uint8_t wasmAudioWorkletStack[4096]; private: int context; + uint8_t wasmAudioWorkletStack[4096]; }; From 0f60ca5e50eb734e68fbeb7ad555e64f54e56648 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 04:23:58 +0200 Subject: [PATCH 21/41] Update library_html5audio.js --- .../libs/html5audio/lib/emscripten/library_html5audio.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js index eb79ba6df74..c3c9e5e3c4d 100644 --- a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js +++ b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js @@ -29,7 +29,7 @@ var LibraryHTML5Audio = { try { // Fix up for prefixing window.AudioContext = window.AudioContext || window.webkitAudioContext; - var context = new AudioContext({}); + var context = new AudioContext({ sampleRate: 44100 }); var id = emscriptenRegisterAudioObject(context); // Fix issue with chrome autoplay policy From cbaeab6127f48400bbafdfc53d2ead9748915727 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 04:40:43 +0200 Subject: [PATCH 22/41] Update ofxEmscriptenSoundStream.cpp --- .../ofxEmscripten/src/ofxEmscriptenSoundStream.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp index 40e5d64e91c..ba317824eb1 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp @@ -6,7 +6,6 @@ */ #include "ofxEmscriptenSoundStream.h" -#include "ofSoundBuffer.h" #include "ofBaseApp.h" #include "ofLog.h" #include "html5audio.h" @@ -15,28 +14,25 @@ using namespace std; ofxEmscriptenSoundStream * stream; -ofSoundBuffer inbuffer; -ofSoundBuffer outbuffer; -int audioProcessedCount = 0; EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs, AudioSampleFrame *outputs, int numParams, const AudioParamFrame *params, void *userData) { - ++audioProcessedCount; + ++stream->audioProcessedCount; if (stream->settings.numInputChannels > 0 && stream->settings.inCallback) { - stream->settings.inCallback(inbuffer); + stream->settings.inCallback(stream->inbuffer); for (int o = 0; o < numInputs; ++o) { for (int i = 0; i < 128; ++i) { for (int ch = 0; ch < inputs[o].numberOfChannels; ++ch) { - inbuffer[i * inputs[o].numberOfChannels + ch] = inputs[o].data[ch * 128 + i]; + stream->inbuffer[i * inputs[o].numberOfChannels + ch] = inputs[o].data[ch * 128 + i]; } } } } if (stream->settings.numOutputChannels > 0 && stream->settings.outCallback) { - stream->settings.outCallback(outbuffer); + stream->settings.outCallback(stream->outbuffer); for (int o = 0; o < numOutputs; ++o) { for (int i = 0; i < 128; ++i) { for (int ch = 0; ch < stream->settings.numOutputChannels; ++ch) { - outputs[o].data[ch * 128 + i] = outbuffer[i * outputs[o].numberOfChannels + ch]; + outputs[o].data[ch * 128 + i] = stream->outbuffer[i * outputs[o].numberOfChannels + ch]; } } } From 0043d8a89b826560ce6922f51c25699c8be2bcb8 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 04:45:25 +0200 Subject: [PATCH 23/41] Update ofxEmscriptenSoundStream.cpp --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp index ba317824eb1..2f47acd89ee 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp @@ -80,6 +80,7 @@ std::vector ofxEmscriptenSoundStream::getDeviceList(ofSoundDevice bool ofxEmscriptenSoundStream::setup(const ofSoundStreamSettings & settings) { inbuffer.allocate(settings.bufferSize, settings.numInputChannels); outbuffer.allocate(settings.bufferSize, settings.numOutputChannels); + audioProcessedCount = 0; this->settings = settings; stream = this; emscripten_start_wasm_audio_worklet_thread_async(context, wasmAudioWorkletStack, sizeof(wasmAudioWorkletStack), WebAudioWorkletThreadInitialized, 0); From 848a22c1fedd7d67f3913e1b20b62bfccb979dc9 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 04:45:44 +0200 Subject: [PATCH 24/41] Update ofxEmscriptenSoundStream.h --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h index 4e9631705b2..d97eb81a71c 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h @@ -8,6 +8,7 @@ #pragma once #include "ofSoundBaseTypes.h" +#include "ofSoundBuffer.h" class ofxEmscriptenSoundStream: public ofBaseSoundStream { public: @@ -32,8 +33,11 @@ class ofxEmscriptenSoundStream: public ofBaseSoundStream { int getSampleRate() const; int getBufferSize() const; ofSoundStreamSettings settings; + ofSoundBuffer inbuffer; + ofSoundBuffer outbuffer; private: int context; + int audioProcessedCount; uint8_t wasmAudioWorkletStack[4096]; }; From a3b8caa6aa05416654aadee24949ae1564fd72c2 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 04:47:47 +0200 Subject: [PATCH 25/41] Update ofxEmscriptenSoundStream.h --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h index d97eb81a71c..29cd13d8c69 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h @@ -35,9 +35,9 @@ class ofxEmscriptenSoundStream: public ofBaseSoundStream { ofSoundStreamSettings settings; ofSoundBuffer inbuffer; ofSoundBuffer outbuffer; + int audioProcessedCount; private: int context; - int audioProcessedCount; uint8_t wasmAudioWorkletStack[4096]; }; From 80faa5641a34b49cf25801e3c575db0434b3e686 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 04:56:15 +0200 Subject: [PATCH 26/41] Update ofxEmscriptenSoundStream.cpp --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp index 2f47acd89ee..84ff0d22ca1 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp @@ -31,7 +31,7 @@ EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutpu stream->settings.outCallback(stream->outbuffer); for (int o = 0; o < numOutputs; ++o) { for (int i = 0; i < 128; ++i) { - for (int ch = 0; ch < stream->settings.numOutputChannels; ++ch) { + for (int ch = 0; ch < outputs[o].numberOfChannels; ++ch) { outputs[o].data[ch * 128 + i] = stream->outbuffer[i * outputs[o].numberOfChannels + ch]; } } From d5c9d5f6a9fe30e45b27180862a6e04e8758e8ce Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 05:10:24 +0200 Subject: [PATCH 27/41] Update library_html5audio.js --- .../lib/emscripten/library_html5audio.js | 30 +++++-------------- 1 file changed, 8 insertions(+), 22 deletions(-) diff --git a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js index c3c9e5e3c4d..892238d891d 100644 --- a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js +++ b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js @@ -207,28 +207,14 @@ var LibraryHTML5Audio = { html5audio_stream_create: function(audioWorkletNode, numInputChannels){ var audioWorkletNode = emscriptenGetAudioObject(audioWorkletNode); if(numInputChannels>0){ - navigator.getUserMedia = navigator.getUserMedia || - navigator.webkitGetUserMedia || - navigator.mozGetUserMedia || - navigator.msGetUserMedia; - if(navigator.getUserMedia){ - navigator.getUserMedia({ - audio: { - autoGainControl: false, - echoCancellation: false, - noiseSuppression: false - } - }, - function(audioIn) { - var mediaElement = AUDIO.context.createMediaStreamSource(audioIn); - mediaElement.connect(audioWorkletNode); - AUDIO.mediaElement = mediaElement; - }, - function(error){ - console.log("error creating audio in",error); - } - ); - } + navigator.mediaDevices.getUserMedia({ audio: true }) + .then(function (audioIn) { + var mediaElement = AUDIO.context.createMediaStreamSource(audioIn); + mediaElement.connect(audioWorkletNode); + }) + .catch(function (error) { + console.log("Error creating audio in", error); + }); } audioWorkletNode.connect(AUDIO.fft); }, From af6499847f243f32ef362414441dfc05501c503f Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 05:12:41 +0200 Subject: [PATCH 28/41] Update library_html5audio.js --- .../libs/html5audio/lib/emscripten/library_html5audio.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js index 892238d891d..b88cb9406ff 100644 --- a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js +++ b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js @@ -206,7 +206,7 @@ var LibraryHTML5Audio = { html5audio_stream_create__deps: ['$emscriptenGetAudioObject'], html5audio_stream_create: function(audioWorkletNode, numInputChannels){ var audioWorkletNode = emscriptenGetAudioObject(audioWorkletNode); - if(numInputChannels>0){ + if(numInputChannels > 0){ navigator.mediaDevices.getUserMedia({ audio: true }) .then(function (audioIn) { var mediaElement = AUDIO.context.createMediaStreamSource(audioIn); From 93b9e619fa9eedc2e68fe4a02affd45b99e932d3 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 05:28:30 +0200 Subject: [PATCH 29/41] Update library_html5audio.js --- .../lib/emscripten/library_html5audio.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js index b88cb9406ff..e07ba0a22d3 100644 --- a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js +++ b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js @@ -208,15 +208,14 @@ var LibraryHTML5Audio = { var audioWorkletNode = emscriptenGetAudioObject(audioWorkletNode); if(numInputChannels > 0){ navigator.mediaDevices.getUserMedia({ audio: true }) - .then(function (audioIn) { - var mediaElement = AUDIO.context.createMediaStreamSource(audioIn); - mediaElement.connect(audioWorkletNode); - }) - .catch(function (error) { - console.log("Error creating audio in", error); - }); + .then(function (audioIn) { + var mediaElement = AUDIO.context.createMediaStreamSource(audioIn); + mediaElement.connect(audioWorkletNode).connect(AUDIO.fft); + }) + .catch(function (error) { + console.log("Error creating audio in", error); + }); } - audioWorkletNode.connect(AUDIO.fft); }, html5audio_stream_free: function () { From e985d3c93d91b274ccab79a1044c857564fd521f Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 05:35:47 +0200 Subject: [PATCH 30/41] Update library_html5audio.js --- .../libs/html5audio/lib/emscripten/library_html5audio.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js index e07ba0a22d3..47f1125cda6 100644 --- a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js +++ b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js @@ -210,12 +210,13 @@ var LibraryHTML5Audio = { navigator.mediaDevices.getUserMedia({ audio: true }) .then(function (audioIn) { var mediaElement = AUDIO.context.createMediaStreamSource(audioIn); - mediaElement.connect(audioWorkletNode).connect(AUDIO.fft); + mediaElement.connect(audioWorkletNode); }) .catch(function (error) { console.log("Error creating audio in", error); }); } + audioWorkletNode.connect(AUDIO.fft); }, html5audio_stream_free: function () { From a88ea797b814805b93680a043e4fad5afcec930d Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 13:56:16 +0200 Subject: [PATCH 31/41] Update ofApp.cpp --- examples/sound/audioInputExample/src/ofApp.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/sound/audioInputExample/src/ofApp.cpp b/examples/sound/audioInputExample/src/ofApp.cpp index fef39b0d9f5..4546a7ed342 100644 --- a/examples/sound/audioInputExample/src/ofApp.cpp +++ b/examples/sound/audioInputExample/src/ofApp.cpp @@ -9,7 +9,7 @@ void ofApp::setup(){ soundStream.printDeviceList(); - int bufferSize = 256; + int bufferSize = 128; left.assign(bufferSize, 0.0); right.assign(bufferSize, 0.0); @@ -44,10 +44,11 @@ void ofApp::setup(){ settings.sampleRate = 44100; #ifdef TARGET_EMSCRIPTEN settings.numOutputChannels = 2; + settings.numInputChannels = 2; #else settings.numOutputChannels = 0; + settings.numInputChannels = 1; #endif - settings.numInputChannels = 1; settings.bufferSize = bufferSize; soundStream.setup(settings); @@ -92,7 +93,7 @@ void ofApp::draw(){ ofBeginShape(); for (unsigned int i = 0; i < left.size(); i++){ - ofVertex(i*2, 100 -left[i]*180.0f); + ofVertex(i*4, 100 -left[i]*180.0f); } ofEndShape(false); @@ -115,7 +116,7 @@ void ofApp::draw(){ ofBeginShape(); for (unsigned int i = 0; i < right.size(); i++){ - ofVertex(i*2, 100 -right[i]*180.0f); + ofVertex(i*4, 100 -right[i]*180.0f); } ofEndShape(false); From d64206db7375a8fd4abdde5a5dd7b51769e74305 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 13:57:14 +0200 Subject: [PATCH 32/41] Update ofApp.cpp --- examples/sound/audioOutputExample/src/ofApp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/sound/audioOutputExample/src/ofApp.cpp b/examples/sound/audioOutputExample/src/ofApp.cpp index ecd903b8587..d957e2263bd 100644 --- a/examples/sound/audioOutputExample/src/ofApp.cpp +++ b/examples/sound/audioOutputExample/src/ofApp.cpp @@ -5,7 +5,7 @@ void ofApp::setup(){ ofBackground(34, 34, 34); - int bufferSize = 512; + int bufferSize = 128; sampleRate = 44100; phase = 0; phaseAdder = 0.0f; From 45cb44af92adeda65adaadd64310a3ddd174cf56 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 13:58:03 +0200 Subject: [PATCH 33/41] Update ofApp.cpp --- examples/sound/soundBufferExample/src/ofApp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/sound/soundBufferExample/src/ofApp.cpp b/examples/sound/soundBufferExample/src/ofApp.cpp index 6f61b138173..a618d66b790 100644 --- a/examples/sound/soundBufferExample/src/ofApp.cpp +++ b/examples/sound/soundBufferExample/src/ofApp.cpp @@ -12,7 +12,7 @@ void ofApp::setup(){ ofSoundStreamSettings settings; settings.numOutputChannels = 2; settings.sampleRate = 44100; - settings.bufferSize = 512; + settings.bufferSize = 128; settings.numBuffers = 4; settings.setOutListener(this); soundStream.setup(settings); From 771f41fdf14c93e6008b787b61666fb7ae70dc3d Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 14:50:07 +0200 Subject: [PATCH 34/41] Update ofApp.cpp --- examples/sound/soundBufferExample/src/ofApp.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/sound/soundBufferExample/src/ofApp.cpp b/examples/sound/soundBufferExample/src/ofApp.cpp index a618d66b790..5df14af22a9 100644 --- a/examples/sound/soundBufferExample/src/ofApp.cpp +++ b/examples/sound/soundBufferExample/src/ofApp.cpp @@ -24,7 +24,7 @@ void ofApp::update(){ // "lastBuffer" is shared between update() and audioOut(), which are called // on two different threads. This lock makes sure we don't use lastBuffer // from both threads simultaneously (see the corresponding lock in audioOut()) - unique_lock lock(audioMutex); + std::unique_lock lock(audioMutex); // this loop is building up a polyline representing the audio contained in // the left channel of the buffer @@ -92,7 +92,7 @@ void ofApp::audioOut(ofSoundBuffer &outBuffer) { pulsePhase += pulsePhaseStep; } - unique_lock lock(audioMutex); + std::unique_lock lock(audioMutex); lastBuffer = outBuffer; } From e3aea3ddc516f2bb25f93eba159b61a61337758c Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 18:10:41 +0200 Subject: [PATCH 35/41] Update library_html5audio.js --- .../libs/html5audio/lib/emscripten/library_html5audio.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js index 47f1125cda6..18f252a9e6c 100644 --- a/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js +++ b/addons/ofxEmscripten/libs/html5audio/lib/emscripten/library_html5audio.js @@ -64,7 +64,11 @@ var LibraryHTML5Audio = { html5audio_context_spectrum: function (bands, spectrum) { AUDIO.fft.fftSize = bands * 2; var spectrumArray = Module.HEAPF32.subarray(spectrum >> 2, (spectrum >> 2) + bands); - AUDIO.fft.getFloatFrequencyData(spectrumArray); + var spectrumArrayCopy = new Float32Array (spectrumArray); + AUDIO.fft.getFloatFrequencyData(spectrumArrayCopy); + for (let i = 0; i < spectrumArrayCopy.length; i++) { + spectrumArray[i] = spectrumArrayCopy[i]; + } }, html5audio_context_samplerate: function () { @@ -99,7 +103,7 @@ var LibraryHTML5Audio = { var fileSizeInBytes = stats.size; var tag = ext; //this covers most types - if( ext == mp3 ){ + if( ext == 'mp3'){ tag = 'mpeg'; }else if( ext == 'oga'){ tag = 'ogg'; From e5ec1da4f7353c4cde60726ddadd40542361620b Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 18:51:32 +0200 Subject: [PATCH 36/41] Update ofxEmscriptenSoundStream.h From 409feb20504493f70befee2db7d7e6f140e04651 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 21:44:25 +0200 Subject: [PATCH 37/41] Update ofxEmscriptenSoundStream.cpp --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp index 84ff0d22ca1..7aee8fcd476 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.cpp @@ -13,9 +13,8 @@ using namespace std; -ofxEmscriptenSoundStream * stream; - EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutputs, AudioSampleFrame *outputs, int numParams, const AudioParamFrame *params, void *userData) { + ofxEmscriptenSoundStream * stream = (ofxEmscriptenSoundStream *)userData; ++stream->audioProcessedCount; if (stream->settings.numInputChannels > 0 && stream->settings.inCallback) { stream->settings.inCallback(stream->inbuffer); @@ -42,13 +41,14 @@ EM_BOOL ProcessAudio(int numInputs, const AudioSampleFrame *inputs, int numOutpu void AudioWorkletProcessorCreated(EMSCRIPTEN_WEBAUDIO_T audioContext, EM_BOOL success, void *userData) { if (!success) return; + ofxEmscriptenSoundStream * stream = (ofxEmscriptenSoundStream *)userData; int outputChannelCounts[1] = { static_cast(stream->settings.numOutputChannels) }; EmscriptenAudioWorkletNodeCreateOptions options = { .numberOfInputs = 1, .numberOfOutputs = 1, .outputChannelCounts = outputChannelCounts }; - EMSCRIPTEN_AUDIO_WORKLET_NODE_T audioWorklet = emscripten_create_wasm_audio_worklet_node(audioContext, "audio-processor", &options, &ProcessAudio, 0); + EMSCRIPTEN_AUDIO_WORKLET_NODE_T audioWorklet = emscripten_create_wasm_audio_worklet_node(audioContext, "audio-processor", &options, &ProcessAudio, userData); html5audio_stream_create(audioWorklet, stream->settings.numInputChannels); } @@ -82,8 +82,7 @@ bool ofxEmscriptenSoundStream::setup(const ofSoundStreamSettings & settings) { outbuffer.allocate(settings.bufferSize, settings.numOutputChannels); audioProcessedCount = 0; this->settings = settings; - stream = this; - emscripten_start_wasm_audio_worklet_thread_async(context, wasmAudioWorkletStack, sizeof(wasmAudioWorkletStack), WebAudioWorkletThreadInitialized, 0); + emscripten_start_wasm_audio_worklet_thread_async(context, wasmAudioWorkletStack, sizeof(wasmAudioWorkletStack), WebAudioWorkletThreadInitialized, this); return true; } From ddbcfc0277e5d903ce10711e523736250686414a Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Fri, 20 Oct 2023 21:51:01 +0200 Subject: [PATCH 38/41] Update ofxEmscriptenSoundStream.cpp From 0cc7b2af94500b0ccc46c13ef0095795d831cef9 Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:22:50 +0200 Subject: [PATCH 39/41] Update ofxEmscriptenSoundStream.h --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h index 29cd13d8c69..e53c618d077 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h @@ -39,5 +39,5 @@ class ofxEmscriptenSoundStream: public ofBaseSoundStream { private: int context; - uint8_t wasmAudioWorkletStack[4096]; + uint8_t wasmAudioWorkletStack[8192]; }; From 6204d8b34a84b9f7e592a642ae4407a52747493f Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:24:22 +0200 Subject: [PATCH 40/41] Update ofxEmscriptenSoundStream.h --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h index e53c618d077..aa6b390febe 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h @@ -39,5 +39,5 @@ class ofxEmscriptenSoundStream: public ofBaseSoundStream { private: int context; - uint8_t wasmAudioWorkletStack[8192]; + uint8_t wasmAudioWorkletStack[16384]; }; From 030fe772e5f06807dd50bba1ac0760464586004f Mon Sep 17 00:00:00 2001 From: Jonathan Frank <41275844+Jonathhhan@users.noreply.github.com> Date: Mon, 23 Oct 2023 00:26:41 +0200 Subject: [PATCH 41/41] Update ofxEmscriptenSoundStream.h --- addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h index aa6b390febe..8d0976e92a5 100644 --- a/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h +++ b/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h @@ -39,5 +39,5 @@ class ofxEmscriptenSoundStream: public ofBaseSoundStream { private: int context; - uint8_t wasmAudioWorkletStack[16384]; + uint8_t wasmAudioWorkletStack[4096 * 4]; };