Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local aukit = require("aukit")
- local speaker = peripheral.find("speaker")
- local file = fs.open("robots.wav", "rb")
- local data = file.readAll()
- file.close()
- -- Create the filters
- local function makeBiquad(a0, a1, a2, b0, b1, b2)
- local x1, x2, y1, y2 = 0, 0, 0, 0
- return function(x0)
- local y0 = (b0 / a0) * x0 + (b1 / a0) * x1 + (b2 / a0) * x2 - (a1 / a0) * y1 - (a2 / a0) * y2
- x2, x1 = x1, x0
- y2, y1 = y1, y0
- return y0
- end
- end
- local function make10(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, b0, b1, b2, b3, b4 ,b5 ,b6, b7, b8, b9, b10)
- local x1, x2, x3, x4, x5, x6, x7, x8, x9, x10 = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
- local y1, y2, y3, y4, y5, y6, y7, y8, y9, y10 = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
- return function (x0)
- local y0 = (b0 / a0) * x0 + (b1 / a0) * x1 + (b2 / a0) * x2 + (b3 / a0) * x3 + (b4 / a0) * x4 + (b5 / a0) * x5 + (b6 / a0) * x6 + (b7 / a0) * x7 + (b8 / a0) * x8 + (b9 / a0) * x9 + (b10 / a0) * x10 - (a1 / a0) * y1 - (a2 / a0) * y2 - (a3 / a0) * y3 - (a4 / a0) * y4 - (a5 / a0) * y5 - (a6 / a0) * y6 - (a7 / a0) * y7 - (a8 / a0) * y8 - (a9 / a0) * y9 - (a10 / a0) * y10
- x10, x9, x8, x7, x6, x5, x4, x3, x2, x1 = x9, x8, x7, x6, x5, x4, x3, x2, x1, x0
- y10, y9, y8, y7, y6, y5, y4, y3, y2, y1 = y9, y8, y7, y6, y5, y4, y3, y2, y1, y0
- return y0
- end
- end
- -- Parameters; highpass is a 2nd-order butterworth filter, yule is a yulewalk-designed filter based on the equal loudness contours by Robinson and Dadson
- local highpass = makeBiquad(
- 1,
- -1.9722334,
- 0.9726137,
- 0.9862118,
- -1.9724236,
- 0.9862118
- )
- local yule = make10(
- 1,
- -3.846646,
- 7.8150167,
- -11.341703,
- 13.055042,
- -12.287599,
- 9.482938,
- -5.8725786,
- 2.7546587,
- -0.8698438,
- 0.13919315,
- 0.038575996,
- -0.021603672,
- -0.0012339532,
- -9.291678E-5,
- -0.016552603,
- 0.021615269,
- -0.020740451,
- 0.0059429808,
- 0.0030642801,
- 1.2025322E-4,
- 0.0028846369
- )
- -- whether to apply the effect or not
- local biquadEnabled = false
- for buffer, time in aukit.stream.wav(data, true) do
- local mono = buffer[1]
- if biquadEnabled then
- -- on each sample:
- -- preAmp by approx 10db
- -- apply yule
- -- apply highpass
- -- clamp to [-128, 127]
- for i = 1, #buffer[1] do
- mono[i] = math.max(math.min(127, highpass(yule(mono[i] * 3))), -128)
- end
- end
- -- play the modified buffer
- while not speaker.playAudio(mono) do
- os.pullEvent("speaker_audio_empty")
- end
- -- process inputs if needed
- local event = { os.pullEvent() }
- if event[1] == "key" then
- biquadEnabled = not biquadEnabled
- print("Equal loudness effect enabled: " .. tostring(biquadEnabled))
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement