jariseon / audioworklet-polyfill Goto Github PK
View Code? Open in Web Editor NEWstrictly unofficial polyfill for Web Audio API AudioWorklet
License: MIT License
strictly unofficial polyfill for Web Audio API AudioWorklet
License: MIT License
Looks like with correct headers SharedArrayBuffer should work on latest Firefox:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer
According to the spec AudioWorkletNodeOptions has outputChannelCount
but no inputChannelCount
. This causes an error to be thrown as they mismatch if inputChannelCount
is not specified.
https://webaudio.github.io/web-audio-api/#dictdef-audioworkletnodeoptions
Hi,
I'm facing an issue with using this polyfill. Let me first tell you why I am looking to use this. I have implemented an AudioWorket processor its working fine on chrome, I also want it to work on ios and android browsers. The issue is that the polyfill that I'm using - https://github.com/GoogleChromeLabs/audioworklet-polyfill - uses the main thread for audio rendering and that is why I am getting a glitchy audio.
Therefore I wanted to use this implementation. To explain my method of implementation. I am first going to show the default code that works without a polyfill when worklet api is present.
public/
|___ index.html
|___ audioprocessor.js
index.html
<!DOCTYPE html>
<head></head>
<body>
<script>
(async function () {
// audioworker.js should also reside at root
const audioContext = new AudioContext();
// Initialize module and create a node for processing.
await audioContext.audioWorklet.addModule("audioprocessor.js");
const pn = new AudioWorkletNode(audioContext, "audioprocessor");
// Get stream and create audio graph source
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const mic = audioContext.createMediaStreamSource(stream);
// Connect the graph.
mic.connect(pn);
pn.connect(audioContext.destination);
// Wait for 10 seconds and stop the stream as well as
// the audio processor.
setTimeout(() => {
stream.getTracks().forEach(track => {
track.stop();
});
}, 10000);
})();
</script>
</body>
</html>
audioprocessor.js
class Processor extends AudioWorkletProcessor {
process(inputs, outputs, parameters) {
// necessary code
}
}
registerProcessor('audioprocessor', Processor);
From the documentation that I came accross and from the understanding of the usage I attempted to migrate this but Its not working. Here's the details
public/
|___ index.html
|___ audioprocessor.js
|___ audioworker.js
|___ audioworklet.js
index.html
<!DOCTYPE html>
<head>
<script src="audioworklet.js"></script>
</head>
<body>
<script>
// audioworker.js should also reside at root
const audioContext = new AudioContext();
// -- buflenSPN defines ScriptProcessorNode buffer length in samples
// -- default is 512. use larger values if there are audible glitches
AWPF.polyfill(audioContext, { buflenSPN:512 }).then(async () => {
// Initialize module and create a node for processing.
await audioContext.audioWorklet.addModule("audioprocessor.js");
const pn = new AudioWorkletNode(audioContext, "audioprocessor");
// Get stream and create audio graph source
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const mic = audioContext.createMediaStreamSource(stream);
// Connect the graph.
mic.connect(pn);
pn.connect(audioContext.destination);
// Wait for 10 seconds and stop the stream as well as
// the audio processor.
setTimeout(() => {
stream.getTracks().forEach(track => {
track.stop();
});
}, 10000);
});
</script>
</body>
</html>
audioprocessor.js
class Processor extends AudioWorkletProcessor {
process(inputs, outputs, parameters) {
// necessary code
}
}
registerProcessor('audioprocessor', Processor);
I'd be grateful if someone can help me in filling the gaps. I think this is going to have much better performance on mobile browsers.
On Safari 11.1.2 (OSX/Sierra), I am getting the following error:
Unhandled Promise Rejection: TypeError: undefined is not an object (evaluating 'AWPF.worker.onmessage')
I think this happens because in the following code:
fetch(AWPF.origin + "/audioworker.js").then(function (resp) {
resp.text().then(function (s) {
var u = window.URL.createObjectURL(new Blob([s]));
AWPF.worker = new Worker(u);
AWPF.worker.postMessage({ type:"init", sampleRate:scope.sampleRate });
})
})
console.warn('Using Worker polyfill of AudioWorklet, audio will not be performance isolated.');
AWPF.isAudioWorkletPolyfilled = true;
resolve();
The promise may be resolved before the fetch is done. In that case, AWPF.worker may be accessed before it is set.
This is the code I am using:
this.context = new (window.AudioContext || window.webkitAudioContext)();
AWPF.polyfill(this.context).then(() => {
this.context.audioWorklet.addModule('js/empty-processor.js').then(() => {
// ...
});
});
I've been reading up this project's description (as well as the code itself), and first I'd like to say I appreciate the effort behind it.
However, I saw that it uses dedicated workers for audio processing, and passes audio data back and forth. Which is async.
So my question is: how does this work with OfflineAudioContext, in which the onaudioprocess
handler can be called faster than time progresses? Does it work at all?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.