Open b0mbie opened 4 months ago
CC:T re-encodes audio to DFPWM when sending it from the client to the server. DFPWM uses a single bit per sample, and due to its design, isn't very good at holding a constant value at higher amplitudes.
We can use the cc.audio.dfpwm
module to see what this gets resampled as.
local dfpwm = require "cc.audio.dfpwm"
local audio = {}
for i = 1, 1024 do audio[i] = -127 end
local resampled = dfpwm.decode(dfpwm.encode(audio))
print(table.concat(resampled, " ", #resampled - 8))
-- -122 -125 -81 -45 -79 -102 -115 -122 -125
This should correspond to your plots in audacity. As mentioned, DFPWM isn't very good at sustaining high amplitudes. That said, I'm a little surprised it fluctuates as much as it does — I'd need to plot some of the encoder internals to see what's going on.
Oh, I see! Thank you for the explanation.
I initially thought that speakers supported "true" PCM audio because of this quote on the tweaked.cc
page "Playing audio with speakers":
CC: Tweaked's speakers also work with PCM audio. It plays back 48,000 samples a second, where each sample is an integer between -128 and 127. This is more commonly referred to as 48kHz and an 8-bit resolution.
This is partially true, since speaker.playAudio
does accept PCM samples, but is misleading because the speaker does not play those same PCM samples. I think this would be something important to point out.
Plotting the various bits of encoder/decoder state, we can see what's going on here:
Inside the DFPWM encoder/decoder, "charge" effectively models the output of the decoder (before the low-pass filter and antijerk is applied), while "strength" is used to control the rate at which the charge changes.
When we see a sample that differs to the current charge, the strength increases until it reaches the sample, at which point it decreases. Well, that's what's meant to happen. However, what actually happens is that we hit the desired value exactly, and so the strength increases more and more, causing the charge to flail more and more around the target value.
I'm not actually sure there's a good fix in the encoder/decoder here. Have documented this for now (7285c32d58aadf5037597471c28cacf1733cdae0), and have a bit more of a think.
Minecraft Version
1.20.1
Version
1.111.0
Details
When given samples above or below
0
, the speaker starts playing a really obnoxious, extremely high-pitched noise.I initially spotted this when trying to synthesize sawtooth waves, but I reduced the code to just this:
What I discovered:
0
seem to reduce this noise's volume.0
, it does not appear to be playing at all.-128
or127
, the speaker also seems to be completely silent.Video demonstration of this code (volume warning): https://github.com/user-attachments/assets/9803764d-65d7-4848-99f0-67138e17eca6
Plot of the squeal, put into Audacity for inspection: Ditto, but with the script changed to output
126
constantly:The reason why I even spotted this is because the code below, which produces a sawtooth wave, seemed to have very prominent resonance, even though that was never programmed in:
Plot of audio produced in-game: Below is plot of a sawtooth signal produced at the same frequency (50Hz):
The expected behavior is for speakers to: