Last active
November 16, 2019 01:47
-
-
Save timusus/cf3ec3642ee432f0d9aa946215f866d1 to your computer and use it in GitHub Desktop.
EqualizerAudioProcessor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EqualizerAudioProcessor : BaseAudioProcessor() { | |
lateinit var prevXArray: Array<ShortArray> | |
lateinit var prevYArray: Array<ShortArray> | |
override fun configure(sampleRateHz: Int, channelCount: Int, encoding: Int): Boolean { | |
val flush = setInputFormat(sampleRateHz, channelCount, encoding) | |
prevXArray = Array(channelCount) { ShortArray(2) { 0 } } | |
prevYArray = Array(channelCount) { ShortArray(2) { 0 } } | |
return flush | |
} | |
override fun isActive(): Boolean { | |
return true | |
} | |
override fun queueInput(inputBuffer: ByteBuffer) { | |
var position = inputBuffer.position() | |
val limit = inputBuffer.limit() | |
val frameCount = (limit - position) / (2 * channelCount) | |
val outputSize = frameCount * outputChannelCount * 2 | |
val buffer = replaceOutputBuffer(outputSize) | |
val fs = sampleRateHz | |
val Bf = 5 | |
val GB = 9 | |
val G0 = 0 | |
val G = 12 | |
val f0 = 500 | |
val beta = tan(Bf / (2.0 * PI) / (fs / 2.0)) * | |
sqrt(abs((10.0.pow(GB / 20.0)).pow(2) - ((10.0.pow(G0 / 20.0)).pow(2)))) / | |
sqrt(abs((10.0.pow(G / 20.0)).pow(2) - ((10.0.pow(GB / 20.0)).pow(2)))) | |
val b0 = (10.0.pow(G0 / 20.0) + 10.0.pow(G / 20.0) * beta) / (1 + beta) | |
val b1 = (-2 * 10.0.pow(G0 / 20.0) * cos(f0 * PI / (fs / 2.0))) / (1 + beta) | |
val b2 = (10.0.pow(G0 / 20.0) - 10.0.pow(G / 20.0) * beta) / (1 + beta) | |
val a0 = 1 | |
val a1 = -2.0 * cos(f0 * PI / (fs / 2.0)) / (1 + beta) | |
val a2 = (1 - beta) / (1 + beta) | |
while (position < (limit - channelCount * 2)) { | |
for (channelIndex in 0 until outputChannelCount) { | |
val samplePosition = position + 2 * channelIndex | |
val sample = inputBuffer.getShort(samplePosition) | |
val x1 = prevXArray[channelIndex][0] | |
val x2 = prevXArray[channelIndex][1] | |
val y1 = prevYArray[channelIndex][0] | |
val y2 = prevYArray[channelIndex][1] | |
val newSample = ( | |
b0 * sample | |
+ b1 * x1 | |
+ b2 * x2 | |
- a1 * y1 | |
- a2 * y2 | |
).toShort() | |
prevXArray[channelIndex][1] = prevXArray[channelIndex][0] | |
prevXArray[channelIndex][0] = sample | |
prevYArray[channelIndex][1] = prevYArray[channelIndex][0] | |
prevYArray[channelIndex][0] = newSample | |
buffer.putShort(samplePosition, newSample) | |
buffer.position(samplePosition + 2) | |
} | |
position += channelCount * 2 | |
} | |
inputBuffer.position(limit) | |
buffer.flip() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment