diff --git a/src/net/torvald/terrarum/audio/FFT.kt b/src/net/torvald/terrarum/audio/FFT.kt index b62ee26af..c0fe64e8d 100644 --- a/src/net/torvald/terrarum/audio/FFT.kt +++ b/src/net/torvald/terrarum/audio/FFT.kt @@ -5,10 +5,10 @@ import org.apache.commons.math3.transform.DftNormalization import org.apache.commons.math3.transform.TransformType import org.apache.commons.math3.util.FastMath -data class FComplex(var real: Float = 0f, var imaginary: Float = 0f) { +data class FComplex(var re: Float = 0f, var im: Float = 0f) { operator fun times(other: FComplex) = FComplex( - this.real * other.real - this.imaginary * other.imaginary, - this.real * other.imaginary + this.imaginary * other.real + this.re * other.re - this.im * other.im, + this.re * other.im + this.im * other.re ) } @@ -34,8 +34,8 @@ object FFT { fun ifftAndGetReal(y: Array): FloatArray { val dataRI = Array(2) { FloatArray(y.size) } for (i in y.indices) { - dataRI[0][i] = y[i].real - dataRI[1][i] = y[i].imaginary + dataRI[0][i] = y[i].re + dataRI[1][i] = y[i].im } transformInPlace(dataRI, DftNormalization.STANDARD, TransformType.INVERSE) @@ -112,8 +112,6 @@ object FFT { bitReversalShuffle2(dataR, dataI) - // Do 4-term DFT. - // Do 4-term DFT. if (type == TransformType.INVERSE) { var i0 = 0 diff --git a/src/net/torvald/terrarum/audio/TerrarumAudioFilter.kt b/src/net/torvald/terrarum/audio/TerrarumAudioFilter.kt index bc80b720e..81d600744 100644 --- a/src/net/torvald/terrarum/audio/TerrarumAudioFilter.kt +++ b/src/net/torvald/terrarum/audio/TerrarumAudioFilter.kt @@ -315,12 +315,16 @@ class Convolv(ir: File, val gain: Float = 1f / 256f): TerrarumAudioFilter() { private val fftLen: Int private val convFFT: Array> + private val convFFTpartd: Array>> // index: Channel, partition, frequencies private val inbuf: Array private val BLOCKSIZE = BUFFER_SIZE / 4 var processingSpeed = 1f; private set + private val partSizes: IntArray + private val partOffsets: IntArray + init { if (!ir.exists()) { throw IllegalArgumentException("Impulse Response file '${ir.path}' does not exist.") @@ -358,6 +362,30 @@ class Convolv(ir: File, val gain: Float = 1f / 256f): TerrarumAudioFilter() { } // println("convFFT Length = ${convFFT[0].size}") + + if (fftLen < BUFFER_SIZE) // buffer size is always 4x the samples in the buffer + throw Error("FIR size is too small (minimum: $BUFFER_SIZE samples)") + + + val partitions = ArrayList() + var cnt = fftLen + while (cnt > BUFFER_SIZE / 4) { + cnt /= 2 + partitions.add(cnt) + } + partitions.add(cnt) + + partSizes = partitions.reversed().toIntArray() + partOffsets = partSizes.clone().also { it[0] = 0 } + + // allocate arrays + convFFTpartd = Array(2) { ch -> + Array(partSizes.size) { partNo -> + Array(partSizes[partNo]) { + convFFT[ch][partOffsets[partNo] + it] + } + } + } } /**