Skip to content

Commit

Permalink
at least it logs something
Browse files Browse the repository at this point in the history
  • Loading branch information
BlinkyStitt committed Apr 20, 2024
1 parent f9a565c commit e286c01
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
39 changes: 19 additions & 20 deletions musical-leptos/src/my-wasm-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,33 @@ class MyWasmProcessor extends AudioWorkletProcessor {
constructor(options) {
super();

this.wasmInstance = null;

console.log("options.processorOptions:", options.processorOptions);

let [module, foobar] = options.processorOptions;

// Fetch, compile, and instantiate the WebAssembly module
WebAssembly.instantiateStreaming(module)
.then(results => {
// `results` is an object with both the module and its instance
console.log('WebAssembly module instantiated successfully');

// Exported functions can now be called
const exports = results.instance.exports;
// const result = exports.yourExportedFunction();
// console.log('Result from your function:', result);

console.log("exports:", exports);
WebAssembly.instantiate(module)
.then(obj => {
this.wasmInstance = obj.instance;
console.log('WASM loaded in worklet');
})
.catch(error => {
console.error('Error instantiating WebAssembly module:', error);
});
.catch(err => console.error('Error instantiating WASM module in worklet:', err));
}
process(inputs, outputs, parameters) {
// TODO: send it over a channel to a regular worker? i'm stumped and just want it working even if it isn't real time

// TODO: this seems to be ignored
process(inputs, outputs, parameters) {
if (this.wasmInstance) {
// TODO: Call your WASM functions here to process audio. Then send it over this.port.postMessage()
} else {
let sum = inputs[0][0].reduce((acc, val) => acc + val, 0);
console.log("sum:", sum);
}

// browsers all handle this differently
// chrome, return true or it stops immediatly
// firefox, return true or it stops when there is no more input
// false SHOULD be fine, but
return true;

// return this.processor.process(inputs[0][0]);
}
}

Expand Down
3 changes: 2 additions & 1 deletion musical-leptos/src/wasm_audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ pub async fn wasm_audio(media_stream: &MediaStream) -> Result<AudioContext, JsVa
pub fn wasm_audio_worklet(ctx: &AudioContext) -> Result<AudioWorkletNode, JsValue> {
let mut audio_worklet_node = AudioWorkletNodeOptions::new();

// TODO: one example passed wasm_bindgen::memory() here, but I don't think that is needed anymore
// TODO: one example passed wasm_bindgen::memory() here, but I don't think that is needed anymore. it also gave errors
// TODO: instead of the main module, i think we need a sub-module specifically for audio processing
let options = audio_worklet_node.processor_options(Some(&js_sys::Array::of2(
&wasm_bindgen::module(),
&"foobar".into(),
Expand Down

0 comments on commit e286c01

Please sign in to comment.