martincameron / micromod

Music player libraries for MOD, S3M and XM formats.
BSD 3-Clause "New" or "Revised" License
161 stars 29 forks source link

[JS Library] Audio playback has sound issues when using the Browser Audio Worklet API #19

Closed jamsinclair closed 2 years ago

jamsinclair commented 2 years ago

Firstly thanks for all the hard work and this neat library!

I've been playing around and testing the JS port with the Audio Worklet API. Due to the fact that the Script Processor API used in this project, and many other Audio projects, has been deprecated for some years now.

The good news is that the library works with the Worklet API! As to be expected, there were some audio regressions. I've put this down to the buffer size on each getAudio call is considerably smaller at 128 samples. The Script Processsor fetches a buffer size of usually 4096 samples.

I've put together an example project comparing the two:

@martincameron I'm sure it's been a long time since you've been familiar with the code, but does anything come to mind where I should be investigating further with the JS code? Or what method is the likely culprit?

martincameron commented 2 years ago

The specs aren't very clear but here's what you have to do...

// One output, 2 channels(stereo)

Player.createNode() { return new AudioWorkletNode(this.context, PROCESSOR_MODULE_NAME, { numberOfInputs: 0, numberOfOutputs: 1, outputChannelCount: [2] }); }

ModProcessor.process(_inputs, outputs, _parameters) { if ( outputs.length !== 1 || !this.audioSource || !this.module || !this.isPlaying ) { return true; }

const leftBuf = outputs[0][0];
const rightBuf = outputs[0][1];
const bufferSize = Math.min(leftBuf.length, rightBuf.length);
this.audioSource.getAudio(leftBuf, rightBuf, bufferSize);

return true;

}

Hope this helps, Cheers, Martin

On Tue, 26 Jul 2022 at 00:31, Martin Cameron @.***> wrote:

Hi, I don't think it's a problem with the buffer-size or you would hear a lot of noise. The worklet version seems to be in mono rather than stereo, and one thing I did notice is that you appear to be passing the first element of each output array to getAudio() rather than the whole array. The sandbox didn't work when I tried to test it, but try with output[0] and output[1] for the left and right channels...

Thanks, Martin

On Thu, 21 Jul 2022 at 17:29, Jamie @.***> wrote:

Firstly thanks for all the hard work and this neat library!

I've been playing around and testing the JS port with the Audio Worklet API https://developer.mozilla.org/en-US/docs/Web/API/AudioWorklet. Due to the fact that the Script Processor API used in this project, and many other Audio projects, has been deprecated for some years now.

The good news is that the library works with the Worklet API! As to be expected, there were some audio regressions. I've put this down to the buffer size on each getAudio call is considerably smaller at 128 samples. The Script Processsor fetches a buffer size of usually 4096 samples.

I've put together an example project comparing the two:

@martincameron https://github.com/martincameron I'm sure it's been a long time since you've been familiar with the code, but does anything come to mind where I should be investigating further with the JS code? Or what method is the likely culprit?

— Reply to this email directly, view it on GitHub https://github.com/martincameron/micromod/issues/19, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACYU5UUI2QHNPZGUGIXKQM3VVF3F3ANCNFSM54IIJVUA . You are receiving this because you were mentioned.Message ID: @.***>

jamsinclair commented 2 years ago

@martincameron many thanks!

Silly me, I was conflating the outputs with channels. That's makes a lot of sense and fixes my issues. Cheers!

martincameron commented 2 years ago

Hi, I don't think it's a problem with the buffer-size or you would hear a lot of noise. The worklet version seems to be in mono rather than stereo, and one thing I did notice is that you appear to be passing the first element of each output array to getAudio() rather than the whole array. The sandbox didn't work when I tried to test it, but try with output[0] and output[1] for the left and right channels...

Thanks, Martin

On Thu, 21 Jul 2022 at 17:29, Jamie @.***> wrote:

Firstly thanks for all the hard work and this neat library!

I've been playing around and testing the JS port with the Audio Worklet API https://developer.mozilla.org/en-US/docs/Web/API/AudioWorklet. Due to the fact that the Script Processor API used in this project, and many other Audio projects, has been deprecated for some years now.

The good news is that the library works with the Worklet API! As to be expected, there were some audio regressions. I've put this down to the buffer size on each getAudio call is considerably smaller at 128 samples. The Script Processsor fetches a buffer size of usually 4096 samples.

I've put together an example project comparing the two:

@martincameron https://github.com/martincameron I'm sure it's been a long time since you've been familiar with the code, but does anything come to mind where I should be investigating further with the JS code? Or what method is the likely culprit?

— Reply to this email directly, view it on GitHub https://github.com/martincameron/micromod/issues/19, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACYU5UUI2QHNPZGUGIXKQM3VVF3F3ANCNFSM54IIJVUA . You are receiving this because you were mentioned.Message ID: @.***>