Passing a function from global scope to AudioWorkletProcessor
See original GitHub issueApologies if this question is unclear or overly basic, I’m new to using audio worklets and the syntax is quite unfamiliar to me. I managed to get a simple example working of an audioWorkletNode firing up an audioWorkletProcessor.
HTML:
<!doctype html>
<html>
<script>
const audio = new AudioContext();
// Loads module script via AudioWorklet.
audio.audioWorklet.addModule('audio-processor.js').then(() => {
let sound = new AudioWorkletNode(audio, 'audio-processor');
sound.connect(audio.destination);
});
</script>
</html>
Javascript:
class AudioProcessor extends AudioWorkletProcessor {
constructor(options) {
super(options);
}
process(inputs, outputs, parameters) {
let input = inputs[0][0];
let output = outputs[0][0];
let numSamples = output.length;
loop(input, output, numSamples);
return true;
}
}
registerProcessor('audio-processor', AudioProcessor);
function loop(input, output, numSamples) {
let amplitude = 0.1;
for (let i = 0; i < output.length; ++i) {
output[i] = 2 * (Math.random() - 0.5) * amplitude;
}
}
Now I’d like to define a function in the global scope that can be passed to / accessed by the audioWorkletProcessor. e.g. I’d like to define the loop function in my main javascript code instead, and pass it to the audioWorklet. How can I do this?
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
AudioWorkletGlobalScope - Web APIs - MDN Web Docs
In this example we output all global properties into the console in the constructor of a custom AudioWorkletProcessor . First we need to...
Read more >How do I access global/window scope from inside a worklet ...
I get my data and I send it to the worker through the port ... class TestProcessor extends AudioWorkletProcessor { constructor(options){ ...
Read more >2. Real-time analysis
First we define the global variables needed for audio, as well as for UI (in ... ScriptNodeProcessor callback function to calculate RMS using...
Read more >AudioWorklet: The future of web audio - Hongchan Choi
global scope and functions like a regular AudioNode. Audio- ... and the AudioWorkletProcessor takes the data to send it to the audio device....
Read more >global scope, nested functions - YouTube
global scope, nested functions. Watch later. Share. Copy link. Info. Shopping. Tap to unmute. If playback doesn't begin shortly, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If that satisfies your purpose, nothing stops you from doing that. However, I would not say it is a pretty thing to look at.
By using async/await, you can precisely sequence the module loading with better readability.
Thanks, this is very helpful! I’ll give it a try.
After some digging online, I managed to get things working using template literals, but it felt a bit hacky and I’m not sure if this was the right way to go about out: