Skip to content

Commit

Permalink
WIP: Refactor processing channels
Browse files Browse the repository at this point in the history
  • Loading branch information
Danand committed Mar 18, 2024
1 parent f0a6f15 commit 769c56c
Showing 1 changed file with 62 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,56 +56,30 @@ class AudioOutput(
val bufferMonoLeft = FloatArray(bufferSizeMono)
val bufferMonoRight = FloatArray(bufferSizeMono)

val sampleTimeStep = 1.0f / samplingRate

var timeElapsedSeconds = 0.0f

while (isActive) {
for (sampleIndex in bufferMonoLeft.indices) {
val sampleTime = timeElapsedSeconds + (sampleIndex * sampleTimeStep)

var sampleValueMax = 0.0f

for (signalProcessor in signalProcessors) {
if (signalProcessor.getChannel() == 0) {
val sampleValue = signalProcessor.process(sampleTime)
sampleValueMax = max(sampleValueMax, sampleValue)
}
}

bufferMonoLeft[sampleIndex] = sampleValueMax
}

for (sampleIndex in bufferMonoRight.indices) {
val sampleTime = timeElapsedSeconds + (sampleIndex * sampleTimeStep)

var sampleValueMax = 0.0f

for (signalProcessor in signalProcessors) {
if (signalProcessor.getChannel() == 1) {
val sampleValue = signalProcessor.process(sampleTime)
sampleValueMax = max(sampleValueMax, sampleValue)
}
}

bufferMonoRight[sampleIndex] = sampleValueMax
}

timeElapsedSeconds += bufferSizeMono * sampleTimeStep

var sampleIndexStereo = 0

val channelMergeFactor = 0.125f
processChannel(
0,
samplingRate,
timeElapsedSeconds,
bufferMonoLeft,
)

for (sampleIndexMono in bufferMonoLeft.indices) {
val sampleLeft = bufferMonoLeft[sampleIndexMono]
val sampleRight = bufferMonoRight[sampleIndexMono]
processChannel(
1,
samplingRate,
timeElapsedSeconds,
bufferMonoRight,
)

bufferStereo[sampleIndexStereo] = (sampleLeft * (1 - channelMergeFactor)) + (sampleRight * channelMergeFactor)
bufferStereo[sampleIndexStereo + 1] = (sampleRight * (1 - channelMergeFactor)) + (sampleLeft * channelMergeFactor)
timeElapsedSeconds += bufferSizeMono * (1.0f / samplingRate)

sampleIndexStereo += 2
}
processStereo(
bufferMonoLeft,
bufferMonoRight,
bufferStereo,
)

for (effect in effects) {
effect.process(bufferStereo)
Expand All @@ -126,4 +100,48 @@ class AudioOutput(
fun stop() {
scope.cancel()
}

private fun processChannel(
channelIndex: Int,
samplingRate: Int,
time: Float,
buffer: FloatArray,
) {
val sampleTimeStep = 1.0f / samplingRate

for (sampleIndex in buffer.indices) {
val sampleTime = time + (sampleIndex * sampleTimeStep)

var sampleValueMax = 0.0f

for (signalProcessor in this.signalProcessors) {
if (signalProcessor.getChannel() == channelIndex) {
val sampleValue = signalProcessor.process(sampleTime)
sampleValueMax = max(sampleValueMax, sampleValue)
}
}

buffer[sampleIndex] = sampleValueMax
}
}

private fun processStereo(
bufferMonoLeft: FloatArray,
bufferMonoRight: FloatArray,
bufferStereo: FloatArray,
) {
var sampleIndexStereo = 0

val channelMergeFactor = 0.125f

for (sampleIndexMono in bufferMonoLeft.indices) {
val sampleLeft = bufferMonoLeft[sampleIndexMono]
val sampleRight = bufferMonoRight[sampleIndexMono]

bufferStereo[sampleIndexStereo] = (sampleLeft * (1 - channelMergeFactor)) + (sampleRight * channelMergeFactor)
bufferStereo[sampleIndexStereo + 1] = (sampleRight * (1 - channelMergeFactor)) + (sampleLeft * channelMergeFactor)

sampleIndexStereo += 2
}
}
}

0 comments on commit 769c56c

Please sign in to comment.