Skip to content

Commit 769c56c

Browse files
committed
WIP: Refactor processing channels
1 parent f0a6f15 commit 769c56c

File tree

1 file changed

+62
-44
lines changed
  • juicy-noise-android/app/src/main/java/com/danand/juicynoise

1 file changed

+62
-44
lines changed

juicy-noise-android/app/src/main/java/com/danand/juicynoise/AudioOutput.kt

Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -56,56 +56,30 @@ class AudioOutput(
5656
val bufferMonoLeft = FloatArray(bufferSizeMono)
5757
val bufferMonoRight = FloatArray(bufferSizeMono)
5858

59-
val sampleTimeStep = 1.0f / samplingRate
60-
6159
var timeElapsedSeconds = 0.0f
6260

6361
while (isActive) {
64-
for (sampleIndex in bufferMonoLeft.indices) {
65-
val sampleTime = timeElapsedSeconds + (sampleIndex * sampleTimeStep)
66-
67-
var sampleValueMax = 0.0f
68-
69-
for (signalProcessor in signalProcessors) {
70-
if (signalProcessor.getChannel() == 0) {
71-
val sampleValue = signalProcessor.process(sampleTime)
72-
sampleValueMax = max(sampleValueMax, sampleValue)
73-
}
74-
}
75-
76-
bufferMonoLeft[sampleIndex] = sampleValueMax
77-
}
78-
79-
for (sampleIndex in bufferMonoRight.indices) {
80-
val sampleTime = timeElapsedSeconds + (sampleIndex * sampleTimeStep)
81-
82-
var sampleValueMax = 0.0f
83-
84-
for (signalProcessor in signalProcessors) {
85-
if (signalProcessor.getChannel() == 1) {
86-
val sampleValue = signalProcessor.process(sampleTime)
87-
sampleValueMax = max(sampleValueMax, sampleValue)
88-
}
89-
}
90-
91-
bufferMonoRight[sampleIndex] = sampleValueMax
92-
}
93-
94-
timeElapsedSeconds += bufferSizeMono * sampleTimeStep
95-
96-
var sampleIndexStereo = 0
97-
98-
val channelMergeFactor = 0.125f
62+
processChannel(
63+
0,
64+
samplingRate,
65+
timeElapsedSeconds,
66+
bufferMonoLeft,
67+
)
9968

100-
for (sampleIndexMono in bufferMonoLeft.indices) {
101-
val sampleLeft = bufferMonoLeft[sampleIndexMono]
102-
val sampleRight = bufferMonoRight[sampleIndexMono]
69+
processChannel(
70+
1,
71+
samplingRate,
72+
timeElapsedSeconds,
73+
bufferMonoRight,
74+
)
10375

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

107-
sampleIndexStereo += 2
108-
}
78+
processStereo(
79+
bufferMonoLeft,
80+
bufferMonoRight,
81+
bufferStereo,
82+
)
10983

11084
for (effect in effects) {
11185
effect.process(bufferStereo)
@@ -126,4 +100,48 @@ class AudioOutput(
126100
fun stop() {
127101
scope.cancel()
128102
}
103+
104+
private fun processChannel(
105+
channelIndex: Int,
106+
samplingRate: Int,
107+
time: Float,
108+
buffer: FloatArray,
109+
) {
110+
val sampleTimeStep = 1.0f / samplingRate
111+
112+
for (sampleIndex in buffer.indices) {
113+
val sampleTime = time + (sampleIndex * sampleTimeStep)
114+
115+
var sampleValueMax = 0.0f
116+
117+
for (signalProcessor in this.signalProcessors) {
118+
if (signalProcessor.getChannel() == channelIndex) {
119+
val sampleValue = signalProcessor.process(sampleTime)
120+
sampleValueMax = max(sampleValueMax, sampleValue)
121+
}
122+
}
123+
124+
buffer[sampleIndex] = sampleValueMax
125+
}
126+
}
127+
128+
private fun processStereo(
129+
bufferMonoLeft: FloatArray,
130+
bufferMonoRight: FloatArray,
131+
bufferStereo: FloatArray,
132+
) {
133+
var sampleIndexStereo = 0
134+
135+
val channelMergeFactor = 0.125f
136+
137+
for (sampleIndexMono in bufferMonoLeft.indices) {
138+
val sampleLeft = bufferMonoLeft[sampleIndexMono]
139+
val sampleRight = bufferMonoRight[sampleIndexMono]
140+
141+
bufferStereo[sampleIndexStereo] = (sampleLeft * (1 - channelMergeFactor)) + (sampleRight * channelMergeFactor)
142+
bufferStereo[sampleIndexStereo + 1] = (sampleRight * (1 - channelMergeFactor)) + (sampleLeft * channelMergeFactor)
143+
144+
sampleIndexStereo += 2
145+
}
146+
}
129147
}

0 commit comments

Comments
 (0)