openFrameworks icon indicating copy to clipboard operation
openFrameworks copied to clipboard

Emscripten 3.1.9 audio worklet

Open Jonathhhan opened this issue 3 years ago • 1 comments

Maybe wait with the pull until audioWorklets are part of the main Emscripten branch. Until then it can be tested and improved... Here is some additional info: https://forum.openframeworks.cc/t/audioworklet-and-emscripten-work-with-of/38824/9

Jonathhhan avatar Sep 14 '22 16:09 Jonathhhan

The audioWorklet gets generated if ofxEmscriptenSoundStream::setup() is called:

uint8_t wasmAudioWorkletStack[4096];

// This callback will fire after the Audio Worklet Processor has finished being added to the Worklet global scope.
void AudioWorkletProcessorCreated(EMSCRIPTEN_WEBAUDIO_T audioContext, EM_BOOL success, int inputChannels, int outputChannels, int inbuffer, int outbuffer, int stream_callback, int userData) 
{
  if (!success) return; 
  // Specify the input and output node configurations for the Wasm Audio Worklet. A simple setup with single mono output channel here, and no inputs.
  int outputChannelCounts[1] = { outputChannels };

  EmscriptenAudioWorkletNodeCreateOptions options = {
    .numberOfInputs = 1,
    .numberOfOutputs = 1,
    .outputChannelCounts = outputChannelCounts
  };

  // Instantiate the noise-generator Audio Worklet Processor.
  EMSCRIPTEN_AUDIO_WORKLET_NODE_T audioWorklet = emscripten_create_wasm_audio_worklet_node(audioContext, "noise-generator", &options, inputChannels, outputChannels, inbuffer, outbuffer, stream_callback, userData);
  html5audio_stream_connect(audioContext, audioWorklet, inputChannels);
}

// This callback will fire when the Wasm Module has been shared to the AudioWorklet global scope, and is now ready to begin adding Audio Worklet Processors.
void WebAudioWorkletThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, EM_BOOL success, int inputChannels, int outputChannels, int inbuffer, int outbuffer, int stream_callback, int userData)
{
  if (!success) return;
  WebAudioWorkletProcessorCreateOptions opts = {
    .name = "noise-generator",
  };
  emscripten_create_wasm_audio_worklet_processor_async(audioContext, &opts, AudioWorkletProcessorCreated, inputChannels, outputChannels, inbuffer, outbuffer, stream_callback, userData);
}

using namespace std;

int ofxEmscriptenAudioContext();

ofxEmscriptenSoundStream::ofxEmscriptenSoundStream()
:context(ofxEmscriptenAudioContext())
,stream(-1)
,tickCount(0)
{

}

ofxEmscriptenSoundStream::~ofxEmscriptenSoundStream() {
	close();
}

std::vector<ofSoundDevice> ofxEmscriptenSoundStream::getDeviceList(ofSoundDevice::Api api) const{
	ofLogWarning() << "ofSoundStream::getDeviceList() not supported in emscripten";
	return vector<ofSoundDevice>();
}

bool ofxEmscriptenSoundStream::setup(const ofSoundStreamSettings & settings) {
	inbuffer.allocate(settings.bufferSize, settings.numInputChannels);
	outbuffer.allocate(settings.bufferSize, settings.numOutputChannels);
	this->settings = settings;
	emscripten_start_wasm_audio_worklet_thread_async(context, wasmAudioWorkletStack, sizeof(wasmAudioWorkletStack), WebAudioWorkletThreadInitialized, settings.numInputChannels, settings.numOutputChannels, inbuffer.getBuffer().data(), outbuffer.getBuffer().data(), &audio_cb, this);
	stream = html5audio_stream_create();
	return true;
}

Jonathhhan avatar Sep 17 '22 03:09 Jonathhhan

Jobs failed with this error

/src/addons/ofxEmscripten/src/ofxEmscriptenSoundStream.h:13:10: fatal error: 'emscripten/webaudio.h' file not found
[45](https://github.com/openframeworks/openFrameworks/actions/runs/3086823624/jobs/4991568588#step:6:46)
#include <emscripten/webaudio.h>

dimitre avatar Sep 28 '22 01:09 dimitre

@dimitre thanks. I needed to copy this file: https://github.com/Jonathhhan/openFrameworks/blob/emscripten_3.1.9_audioWorklet/addons/ofxEmscripten/libs/html5audio/include/html5audio.h into this folder: https://github.com/juj/emscripten/tree/audio_worklets/system/include/emscripten (could probably linked in a better way...) and it uses this Emscripten branch: https://github.com/juj/emscripten/tree/audio_worklets I hope that helps.

Jonathhhan avatar Sep 28 '22 14:09 Jonathhhan

I close this, because ofxEmscripten changed a lot since I opened this pull request. Basically only the audioWorklet stuff in this pr is still relevant.

Jonathhhan avatar Dec 13 '22 21:12 Jonathhhan