Skip to content

Commit

Permalink
Add output in stereo
Browse files Browse the repository at this point in the history
  • Loading branch information
Danand committed Mar 17, 2024
1 parent 784b1ed commit fdbe0fb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,42 @@ class AudioOutput(

val bufferSizeMono = bufferSize / 2

val bufferMono = FloatArray(bufferSizeMono)
val bufferMonoLeft = FloatArray(bufferSizeMono)
val bufferMonoRight = FloatArray(bufferSizeMono)

val sampleTimeStep = 1.0f / samplingRate

var timeElapsedSeconds = 0.0f

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

var sampleValueMax = 0.0f

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

bufferMono[sampleIndex] = sampleValueMax
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
}

// TODO: Uncomment when delay effect will be fixed.
Expand All @@ -82,9 +100,9 @@ class AudioOutput(

var sampleIndexStereo = 0

for (sampleIndexMono in bufferMono.indices) {
bufferStereo[sampleIndexStereo] = bufferMono[sampleIndexMono]
bufferStereo[sampleIndexStereo + 1] = bufferMono[sampleIndexMono]
for (sampleIndexMono in bufferMonoLeft.indices) {
bufferStereo[sampleIndexStereo] = bufferMonoLeft[sampleIndexMono]
bufferStereo[sampleIndexStereo + 1] = bufferMonoRight[sampleIndexMono]

sampleIndexStereo += 2
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,15 @@ class MainActivity : ComponentActivity(), SensorEventListener {
pressure = sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE)
proximity = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY)

val signalProcessorSensors = SignalProcessorSensors(sensorsState)
val signalProcessorSensorsLeft = SignalProcessorSensors(sensorsState, 0)
val signalProcessorSensorsRight = SignalProcessorSensors(sensorsState, 1)

val effectDelay = EffectDelay(sensorsState)

audioOutput = AudioOutput(
arrayOf(
signalProcessorSensors,
signalProcessorSensorsLeft,
signalProcessorSensorsRight,
),
arrayOf(
effectDelay,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.danand.juicynoise.interfaces

interface SignalProcessor {
fun getChannel(): Int

fun process(time: Float): Float
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import kotlin.math.min
import kotlin.math.tanh
import kotlin.random.Random

class SignalProcessorSensors(private val sensorsState: MutableState<Sensors>) : SignalProcessor {
class SignalProcessorSensors(
private val sensorsState: MutableState<Sensors>,
private val channel: Int,
) : SignalProcessor {
private val sensorGetters: Array<() -> Float> = arrayOf(
{ sensorsState.value.longitude },
{ sensorsState.value.latitude },
Expand Down Expand Up @@ -93,6 +96,10 @@ class SignalProcessorSensors(private val sensorsState: MutableState<Sensors>) :

private var weirdEffectDivider = 9

override fun getChannel(): Int {
return this.channel
}

override fun process(time: Float): Float {
val angularSpeedMagnitude = magnitude(
this.sensorsState.value.angularSpeedX,
Expand Down

0 comments on commit fdbe0fb

Please sign in to comment.