-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
143 additions
and
9 deletions.
There are no files selected for viewing
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
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
23 changes: 23 additions & 0 deletions
23
juicy-noise-android/app/src/main/java/com/danand/juicynoise/WeightedRandomChoice.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.danand.juicynoise | ||
|
||
import kotlin.random.Random | ||
|
||
class WeightedRandomChoice<T>(private val items: List<Pair<T, Double>>) { | ||
|
||
private val cumulativeProbabilities: List<Double> = calculateCumulativeProbabilities() | ||
|
||
private fun calculateCumulativeProbabilities(): List<Double> { | ||
var cumulativeProbability = 0.0 | ||
return items.map { (_, weight) -> | ||
cumulativeProbability += weight | ||
cumulativeProbability | ||
} | ||
} | ||
|
||
fun next(): T { | ||
val randomValue = Random.nextDouble(0.0, cumulativeProbabilities.last()) | ||
val index = cumulativeProbabilities.indexOfFirst { it >= randomValue } | ||
|
||
return items[index].first | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
juicy-noise-android/app/src/main/java/com/danand/juicynoise/effects/EffectDelay.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.danand.juicynoise.effects | ||
|
||
import com.danand.juicynoise.Sensors | ||
import com.danand.juicynoise.interfaces.Effect | ||
import com.danand.juicynoise.magnitude | ||
|
||
import kotlin.math.max | ||
import kotlin.math.ceil | ||
|
||
import androidx.compose.runtime.MutableState | ||
import kotlin.math.abs | ||
|
||
class EffectDelay( | ||
private val sensorsState: MutableState<Sensors>, | ||
) : Effect { | ||
private val buffers: ArrayList<FloatArray> = ArrayList() | ||
|
||
private var magneticMagnitudeMax: Float = 0.0f | ||
|
||
override fun process(buffer: FloatArray, bufferSize: Int) { | ||
val delayMilliseconds = max(0.0f, 5000.0f - this.sensorsState.value.light) | ||
|
||
val magneticMagnitude = magnitude( | ||
this.sensorsState.value.magneticX, | ||
this.sensorsState.value.magneticY, | ||
this.sensorsState.value.magneticZ, | ||
) | ||
|
||
magneticMagnitudeMax = max(magneticMagnitudeMax, magneticMagnitude) | ||
|
||
val dry = magneticMagnitude / magneticMagnitudeMax | ||
val wet = 1 - dry | ||
|
||
val bufferAmount = ceil(delayMilliseconds / bufferSize).toInt() | ||
|
||
val bufferCount = this.buffers.count() | ||
|
||
val skipBuffersCount = bufferCount - bufferAmount | ||
|
||
if (skipBuffersCount < 0) { | ||
for (i in 0..abs(skipBuffersCount)) { | ||
this.buffers.add(FloatArray(bufferSize)) | ||
} | ||
} | ||
|
||
val lastBuffer = this.buffers[0] | ||
|
||
for (sampleIndex in buffer.indices) { | ||
lastBuffer[sampleIndex] = buffer[sampleIndex] | ||
} | ||
|
||
for (bufferIndex in this.buffers.indices) { | ||
if (bufferIndex < skipBuffersCount) { | ||
continue | ||
} | ||
|
||
if (bufferIndex < 1) { | ||
continue | ||
} | ||
|
||
val bufferSaved = this.buffers[bufferIndex] | ||
|
||
val bufferMix = bufferIndex / bufferAmount.toFloat() | ||
|
||
for (sampleIndex in buffer.indices) { | ||
val delayedSample = bufferSaved[sampleIndex] * bufferMix | ||
buffer[sampleIndex] = (buffer[sampleIndex] * dry) + (delayedSample * wet) | ||
} | ||
} | ||
|
||
for (bufferIndex in 0 until this.buffers.lastIndex) { | ||
this.buffers[bufferIndex] = this.buffers[bufferIndex + 1] | ||
} | ||
|
||
this.buffers[this.buffers.lastIndex] = lastBuffer | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
juicy-noise-android/app/src/main/java/com/danand/juicynoise/interfaces/Effect.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.danand.juicynoise.interfaces | ||
|
||
interface Effect { | ||
fun process(buffer: FloatArray, bufferSize: Int) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ package com.danand.juicynoise.interfaces | |
|
||
interface SignalProcessor { | ||
fun process(time: Float): Float | ||
} | ||
} |
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