From 769c56c42a7ab23a1ebfcdce414d0b0357e86976 Mon Sep 17 00:00:00 2001 From: Andrey Danilov Date: Tue, 19 Mar 2024 00:10:41 +0300 Subject: [PATCH] WIP: Refactor processing channels --- .../java/com/danand/juicynoise/AudioOutput.kt | 106 ++++++++++-------- 1 file changed, 62 insertions(+), 44 deletions(-) diff --git a/juicy-noise-android/app/src/main/java/com/danand/juicynoise/AudioOutput.kt b/juicy-noise-android/app/src/main/java/com/danand/juicynoise/AudioOutput.kt index 277748c..7058656 100644 --- a/juicy-noise-android/app/src/main/java/com/danand/juicynoise/AudioOutput.kt +++ b/juicy-noise-android/app/src/main/java/com/danand/juicynoise/AudioOutput.kt @@ -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) @@ -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 + } + } }