From 51d1501267f43a520716e9761720e78ed0b29d5b Mon Sep 17 00:00:00 2001 From: minjaesong Date: Sat, 25 Nov 2023 20:42:08 +0900 Subject: [PATCH] trying to fix the crackling sound issue --- src/net/torvald/terrarum/audio/AudioMixer.kt | 4 ++-- .../terrarum/audio/TerrarumAudioFilter.kt | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/net/torvald/terrarum/audio/AudioMixer.kt b/src/net/torvald/terrarum/audio/AudioMixer.kt index f329d6b87..0f7023b95 100644 --- a/src/net/torvald/terrarum/audio/AudioMixer.kt +++ b/src/net/torvald/terrarum/audio/AudioMixer.kt @@ -105,7 +105,7 @@ object AudioMixer: Disposable { // musicTrack.filters[0] = BinoPan((Math.random() * 2.0 - 1.0).toFloat()) // musicTrack.filters[1] = Reverb(36f, 0.92f, 1200f) - masterTrack.filters[0] = SoftClp +// masterTrack.filters[0] = SoftClp masterTrack.filters[1] = Buffer masterTrack.filters[2] = Scope() @@ -113,7 +113,7 @@ object AudioMixer: Disposable { fadeBus.addSidechainInput(ambientTrack, 1.0) fadeBus.addSidechainInput(sfxMixTrack, 1.0) fadeBus.filters[0] = Lowpass(SAMPLING_RATE / 2f) - fadeBus.filters[1] = Convolv(ModMgr.getFile("basegame", "audio/convolution/EchoThief - TransitCenter.bin")) + fadeBus.filters[1] = Convolv(ModMgr.getFile("basegame", "audio/convolution/WoodruffLane.bin")) masterTrack.addSidechainInput(fadeBus, 1.0) masterTrack.addSidechainInput(guiTrack, 1.0) diff --git a/src/net/torvald/terrarum/audio/TerrarumAudioFilter.kt b/src/net/torvald/terrarum/audio/TerrarumAudioFilter.kt index 0fc6f4192..a247a2f20 100644 --- a/src/net/torvald/terrarum/audio/TerrarumAudioFilter.kt +++ b/src/net/torvald/terrarum/audio/TerrarumAudioFilter.kt @@ -61,8 +61,8 @@ object SoftClp : TerrarumAudioFilter() { } class Scope : TerrarumAudioFilter() { - val backbufL = Array(BUFFER_SIZE / 16) { FloatArray(BUFFER_SIZE / 4) } - val backbufR = Array(BUFFER_SIZE / 16) { FloatArray(BUFFER_SIZE / 4) } + val backbufL = Array((4096f / BUFFER_SIZE * 4).roundToInt()) { FloatArray(BUFFER_SIZE / 4) } + val backbufR = Array((4096f / BUFFER_SIZE * 4).roundToInt()) { FloatArray(BUFFER_SIZE / 4) } private val sqrt2p = 0.7071067811865475 @@ -355,7 +355,7 @@ class Convolv(ir: File, val gain: Float = decibelsToFullscale(-12.0).toFloat()): val t1 = System.nanoTime() for (ch in outbuf1.indices) { - push(inbuf1[ch].toDoubleArray(), inbuf[ch]) + push(inbuf1[ch].toDoubleArray(gain), inbuf[ch]) val inputFFT = FastFourier(inbuf[ch]).let { it.transform(); it.getComplex(false) } @@ -366,7 +366,10 @@ class Convolv(ir: File, val gain: Float = decibelsToFullscale(-12.0).toFloat()): val Y = multiply(inputFFT, convFFT[ch]) val y = real(ifft(Y)) - val u = y.sliceArray(Ny - BLOCKSIZE until Ny).toFloatArray(gain) +// val u = y.sliceArray(Ny - BLOCKSIZE until Ny).toFloatArray(gain) // y size == Ny + val u = y.takeLast(BLOCKSIZE).map { it.toFloat() }.toFloatArray() + +// println("y size: ${y.size}; Ny=$Ny") System.arraycopy(u, 0, outbuf1[ch], 0, BLOCKSIZE) } @@ -374,10 +377,10 @@ class Convolv(ir: File, val gain: Float = decibelsToFullscale(-12.0).toFloat()): val ptime = (t2 - t1).toDouble() val realtime = BLOCKSIZE / SAMPLING_RATED * 1000000000L if (realtime >= ptime) { - println("Processing speed: ${realtime / ptime}x FASTER than realtime") +// println("Processing speed: ${realtime / ptime}x FASTER than realtime") } else { - println("Processing speed: ${ptime / realtime}x SLOWER than realtime") +// println("Processing speed: ${ptime / realtime}x SLOWER than realtime") } } @@ -412,10 +415,10 @@ class Convolv(ir: File, val gain: Float = decibelsToFullscale(-12.0).toFloat()): private fun push(samples: DoubleArray, buf: DoubleArray) { System.arraycopy(buf, samples.size, buf, 0, buf.size - samples.size) - System.arraycopy(samples, 0, buf, buf.size - samples.size - 1, samples.size) + System.arraycopy(samples, 0, buf, buf.size - samples.size, samples.size) } - private fun FloatArray.toDoubleArray() = this.map { it.toDouble() }.toDoubleArray() + private fun FloatArray.toDoubleArray(gain: Float = 1f) = this.map { it.toDouble() * gain }.toDoubleArray() private fun DoubleArray.toFloatArray(gain: Float = 1f) = this.map { it.toFloat() * gain }.toFloatArray() }