Fix DC-offset audio glitches in browser hosted emulator#6
Merged
Merged
Conversation
Owner
|
Thanks for finding this one and proposing a fix. I will try running it hopefully later today and will merge if I confirm it also works for me. A very thorough description of the issue. I really appreciate it. |
Owner
|
I have just tried this out and I'm happy that it works and is a big improvement. I can't hear those clicks anymore. I will merge it in a few minutes. It should be live maybe 10 minutes after that. |
Contributor
Author
|
Great, thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix for audio glitches in the browser-hosted emulator
1. DC offset on the AudioWorklet output
The emulated AY-3-8912 produces a unipolar signal: each of the three channels contributes a non-negative value, with a DC offset that varies over time based on overall volume. So silence is 0, and a full volume signal will average over time around the 16384 mid-point. To convert to the -1 to +1 range the AudioWorkletProcessor the original conversion subtracted a fixed value
16384.0fto centre the signal to 0, which would correct the DC offset when the a signal is being played at max volume but meant that silence was effectively being converted to -1. This leads to loud clicks when the audio signal is switched on/off (e.g. emulator paused or restarted, or sound on/off icon clicked on) and could possibly in some cases lead to excess speaker coil load depending on the physical output chain.This proposed fix replaces the static subtraction with a one-pole DC blocker (
y[n] = x[n] - x[n-1] + R·y[n-1],R = 0.995). At the 22050 Hz sample rate used this gives a -3 dB corner around 17.5 Hz which should be below any audible content the Oric would normally produce, but high enough to effectively remove the DC offset drift this bug is about.2. Wrap-on-overflow in the channel sum
I'm not totally sure if this condition would ever be hit (I don't know the emulator well enough), but the three output channels were summed and then masked with
& 0x7FFF. If the sum ever exceeded 15 bits the mask would effectively wrap the value, producing a sharp discontinuity in the sample stream and an audible click. Replaced withMath.min(..., 0x7FFF)so loud sums clamp at the rail instead of wrapping.Implementation notes
GwtAYPSG; desktop / Android paths are unaffected.Testing
Tested locally in Safari and Chrome and the previously very audible clicks/thumps on transitions are gone. No audible regressions on content that I could hear in the programs I tested with, but it would be great to get a quick double check by whoever reviews this PR with programs they are familiar with.