mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-10 18:44:05 +09:00
optimised convolve op
This commit is contained in:
@@ -247,6 +247,8 @@ object AudioMixer: Disposable {
|
|||||||
|
|
||||||
private var testAudioMixRatio = 0.0
|
private var testAudioMixRatio = 0.0
|
||||||
|
|
||||||
|
private var muteLatched = false
|
||||||
|
|
||||||
fun update(delta: Float) {
|
fun update(delta: Float) {
|
||||||
// test the panning
|
// test the panning
|
||||||
/*musicTrack.getFilter<BinoPan>().let {
|
/*musicTrack.getFilter<BinoPan>().let {
|
||||||
@@ -262,14 +264,20 @@ object AudioMixer: Disposable {
|
|||||||
|
|
||||||
if (Gdx.input.isKeyPressed(Input.Keys.UP))
|
if (Gdx.input.isKeyPressed(Input.Keys.UP))
|
||||||
testAudioMixRatio += mixDelta
|
testAudioMixRatio += mixDelta
|
||||||
else if (Gdx.input.isKeyPressed(Input.Keys.DOWN))
|
if (Gdx.input.isKeyPressed(Input.Keys.DOWN))
|
||||||
testAudioMixRatio -= mixDelta
|
testAudioMixRatio -= mixDelta
|
||||||
else if (Gdx.input.isKeyPressed(Input.Keys.NUM_1))
|
if (Gdx.input.isKeyPressed(Input.Keys.NUM_1))
|
||||||
testAudioMixRatio = -1.0
|
testAudioMixRatio = -1.0
|
||||||
else if (Gdx.input.isKeyPressed(Input.Keys.NUM_2))
|
if (Gdx.input.isKeyPressed(Input.Keys.NUM_2))
|
||||||
testAudioMixRatio = 0.0
|
testAudioMixRatio = 0.0
|
||||||
else if (Gdx.input.isKeyPressed(Input.Keys.NUM_3))
|
if (Gdx.input.isKeyPressed(Input.Keys.NUM_3))
|
||||||
testAudioMixRatio = 1.0
|
testAudioMixRatio = 1.0
|
||||||
|
if (!muteLatched && Gdx.input.isKeyPressed(Input.Keys.NUM_4)) {
|
||||||
|
AudioMixer.sumBus.volume = 1.0 - AudioMixer.sumBus.volume
|
||||||
|
muteLatched = true
|
||||||
|
}
|
||||||
|
else if (!Gdx.input.isKeyPressed(Input.Keys.NUM_4))
|
||||||
|
muteLatched = false
|
||||||
|
|
||||||
testAudioMixRatio = testAudioMixRatio.coerceIn(MaterialCodex["AIIR"].sondrefl.absoluteValue * -1.0, 1.0)
|
testAudioMixRatio = testAudioMixRatio.coerceIn(MaterialCodex["AIIR"].sondrefl.absoluteValue * -1.0, 1.0)
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ class Convolv(ir: File, val gain: Float = 1f / 512f): TerrarumAudioFilter() {
|
|||||||
|
|
||||||
val fftLen: Int
|
val fftLen: Int
|
||||||
private val convFFT: Array<ComplexArray>
|
private val convFFT: Array<ComplexArray>
|
||||||
private val inbuf: Array<ComplexArray>
|
// private val inbuf: Array<ComplexArray>
|
||||||
|
private val sumbuf: ComplexArray
|
||||||
|
|
||||||
private val BLOCKSIZE = TerrarumAudioMixerTrack.AUDIO_BUFFER_SIZE
|
private val BLOCKSIZE = TerrarumAudioMixerTrack.AUDIO_BUFFER_SIZE
|
||||||
|
|
||||||
@@ -30,7 +31,8 @@ class Convolv(ir: File, val gain: Float = 1f / 512f): TerrarumAudioFilter() {
|
|||||||
println("IR '${ir.path}' Sample Count = $sampleCount; FFT Length = $fftLen")
|
println("IR '${ir.path}' Sample Count = $sampleCount; FFT Length = $fftLen")
|
||||||
|
|
||||||
val conv = Array(2) { FloatArray(fftLen) }
|
val conv = Array(2) { FloatArray(fftLen) }
|
||||||
inbuf = Array(2) { ComplexArray(FloatArray(fftLen * 2)) }
|
// inbuf = Array(2) { ComplexArray(FloatArray(fftLen * 2)) }
|
||||||
|
sumbuf = ComplexArray(FloatArray(fftLen * 2))
|
||||||
|
|
||||||
ir.inputStream().let {
|
ir.inputStream().let {
|
||||||
for (i in 0 until sampleCount) {
|
for (i in 0 until sampleCount) {
|
||||||
@@ -72,10 +74,8 @@ class Convolv(ir: File, val gain: Float = 1f / 512f): TerrarumAudioFilter() {
|
|||||||
private val realtime = (BLOCKSIZE / TerrarumAudioMixerTrack.SAMPLING_RATEF * 1000000000L)
|
private val realtime = (BLOCKSIZE / TerrarumAudioMixerTrack.SAMPLING_RATEF * 1000000000L)
|
||||||
private val fftIn = ComplexArray(FloatArray(fftLen * 2))
|
private val fftIn = ComplexArray(FloatArray(fftLen * 2))
|
||||||
private val fftMult = ComplexArray(FloatArray(fftLen * 2))
|
private val fftMult = ComplexArray(FloatArray(fftLen * 2))
|
||||||
private val fftOut_lL = FloatArray(fftLen) // small l/r: input audio
|
private val fftOutL = FloatArray(fftLen)
|
||||||
private val fftOut_rL = FloatArray(fftLen) // large L/R: impulse response
|
private val fftOutR = FloatArray(fftLen)
|
||||||
private val fftOut_lR = FloatArray(fftLen)
|
|
||||||
private val fftOut_rR = FloatArray(fftLen)
|
|
||||||
|
|
||||||
private fun convolve(x: ComplexArray, h: ComplexArray, output: FloatArray) {
|
private fun convolve(x: ComplexArray, h: ComplexArray, output: FloatArray) {
|
||||||
FFT.fftInto(x, fftIn)
|
FFT.fftInto(x, fftIn)
|
||||||
@@ -91,17 +91,14 @@ class Convolv(ir: File, val gain: Float = 1f / 512f): TerrarumAudioFilter() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
push(gain, inbuf[0], this.inbuf[0])
|
pushSum(gain, inbuf[0], inbuf[1], sumbuf)
|
||||||
push(gain, inbuf[1], this.inbuf[1])
|
|
||||||
|
|
||||||
convolve(this.inbuf[0], convFFT[0], fftOut_lL)
|
convolve(sumbuf, convFFT[0], fftOutL)
|
||||||
convolve(this.inbuf[1], convFFT[0], fftOut_rL)
|
convolve(sumbuf, convFFT[1], fftOutR)
|
||||||
convolve(this.inbuf[0], convFFT[1], fftOut_lR)
|
|
||||||
convolve(this.inbuf[1], convFFT[1], fftOut_rR)
|
|
||||||
|
|
||||||
for (i in 0 until BLOCKSIZE) {
|
for (i in 0 until BLOCKSIZE) {
|
||||||
outbuf[0][i] = fftOut_lL[fftLen - BLOCKSIZE + i] + fftOut_rL[fftLen - BLOCKSIZE + i]
|
outbuf[0][i] = fftOutL[fftLen - BLOCKSIZE + i]
|
||||||
outbuf[1][i] = fftOut_rR[fftLen - BLOCKSIZE + i] + fftOut_lR[fftLen - BLOCKSIZE + i]
|
outbuf[1][i] = fftOutR[fftLen - BLOCKSIZE + i]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -113,7 +110,9 @@ class Convolv(ir: File, val gain: Float = 1f / 512f): TerrarumAudioFilter() {
|
|||||||
|
|
||||||
|
|
||||||
fun push(gain: Float, samples: FloatArray, buf: ComplexArray) {
|
fun push(gain: Float, samples: FloatArray, buf: ComplexArray) {
|
||||||
|
// shift numbers
|
||||||
System.arraycopy(buf.reim, samples.size * 2, buf.reim, 0, buf.reim.size - samples.size * 2)
|
System.arraycopy(buf.reim, samples.size * 2, buf.reim, 0, buf.reim.size - samples.size * 2)
|
||||||
|
// fill in the shifted area
|
||||||
val baseI = buf.reim.size - samples.size * 2
|
val baseI = buf.reim.size - samples.size * 2
|
||||||
samples.forEachIndexed { index, fl ->
|
samples.forEachIndexed { index, fl ->
|
||||||
buf.reim[baseI + index * 2 + 0] = fl * gain
|
buf.reim[baseI + index * 2 + 0] = fl * gain
|
||||||
@@ -121,4 +120,15 @@ class Convolv(ir: File, val gain: Float = 1f / 512f): TerrarumAudioFilter() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun pushSum(gain: Float, sampleL: FloatArray, sampleR: FloatArray, buf: ComplexArray) {
|
||||||
|
// shift numbers
|
||||||
|
System.arraycopy(buf.reim, sampleL.size * 2, buf.reim, 0, buf.reim.size - sampleL.size * 2)
|
||||||
|
// fill in the shifted area
|
||||||
|
val baseI = buf.reim.size - sampleL.size * 2
|
||||||
|
for (index in sampleL.indices) {
|
||||||
|
buf.reim[baseI + index * 2 + 0] = (sampleL[index] + sampleR[index]) * gain
|
||||||
|
buf.reim[baseI + index * 2 + 1] = 0f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user