Grain Cloud instrument: ECHOFIELD's granular engine ported to Kotlin (0.31.0)
A granular drone machine from ECHOFIELD FM-1 (Philip Bos, MIT): six modes (Cloud, Dust, Tape, Bloom, Pulse, Drift), up to 32 grains, held notes pitch the grains, freeze, auto-level. One shared cloud per mixer channel (plus one for editor audition) with note ownership, so sequencer releases never cut a held MIDI note. Ten built-in sources synthesised at startup (GrainSources, GPL), or a loaded/recorded sample kept across restarts. 14 factory presets. The echofield/ package stays MIT, LICENSES/MIT.txt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
2d59b0429d
commit
aa023d1f01
@@ -0,0 +1,134 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package cc.tiredsysadmin.android.sizzle.echofield
|
||||
|
||||
import cc.tiredsysadmin.android.sizzle.audio.GrainSources
|
||||
import cc.tiredsysadmin.android.sizzle.audio.SampleStore
|
||||
import cc.tiredsysadmin.android.sizzle.audio.SynthPatch
|
||||
import cc.tiredsysadmin.android.sizzle.audio.SynthVoice
|
||||
import cc.tiredsysadmin.android.sizzle.model.ToolboxSlot
|
||||
import cc.tiredsysadmin.android.sizzle.model.ToolboxType
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.BeforeClass
|
||||
import org.junit.Test
|
||||
import kotlin.math.PI
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.sin
|
||||
import kotlin.math.sqrt
|
||||
|
||||
class GrainCloudTest {
|
||||
|
||||
companion object {
|
||||
@JvmStatic @BeforeClass fun sources() = GrainSources.prewarmBlocking()
|
||||
}
|
||||
|
||||
private val fs = 48_000
|
||||
|
||||
private fun slot(preset: String? = null, vararg extra: Pair<String, String>): ToolboxSlot =
|
||||
ToolboxSlot(0).apply {
|
||||
fill(ToolboxType.GRAIN_CLOUD)
|
||||
preset?.let { n -> EchofieldDefs.FACTORY.first { it.name == n }.values.forEach { (k, v) -> set(k, v) } }
|
||||
extra.forEach { (k, v) -> set(k, v) }
|
||||
}
|
||||
|
||||
private fun cloud(s: ToolboxSlot, tempo: Float = 120f) = GrainCloud(fs).apply { setPatch(GrainPatch.from(s, tempo)) }
|
||||
|
||||
private class Stats(val rms: Float, val peak: Float, val finite: Boolean)
|
||||
|
||||
private fun render(c: GrainCloud, frames: Int): Stats {
|
||||
var sum = 0.0; var peak = 0f; var finite = true
|
||||
repeat(frames) { val x = c.render(); if (!x.isFinite()) finite = false; sum += x * x; peak = maxOf(peak, abs(x)) }
|
||||
return Stats(sqrt(sum / frames).toFloat(), peak, finite)
|
||||
}
|
||||
|
||||
@Test fun builtInSourcesAreReady() {
|
||||
for (name in EchofieldDefs.SOURCES.filter { it != EchofieldDefs.CUSTOM }) {
|
||||
val s = assertNotNull(name, GrainSources.get(name)).let { GrainSources.get(name)!! }
|
||||
assertEquals(name, GrainSources.RATE, s.sampleRate)
|
||||
assertTrue(name, s.data.size >= GrainSources.RATE * 5)
|
||||
assertTrue(name, s.data.all { it.isFinite() && abs(it) <= 0.9f })
|
||||
assertTrue("$name not silent", s.data.maxOf { abs(it) } > 0.5f)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun everyFactoryPresetPlaysAndFreesAfterRelease() {
|
||||
assertEquals(14, EchofieldDefs.FACTORY.size)
|
||||
for (p in EchofieldDefs.FACTORY) {
|
||||
val c = cloud(slot(p.name))
|
||||
c.noteOn(60, 100)
|
||||
val held = render(c, fs * 4)
|
||||
println("${p.name}: rms ${"%.3f".format(held.rms)} peak ${"%.3f".format(held.peak)}")
|
||||
assertTrue("${p.name} finite", held.finite)
|
||||
assertTrue("${p.name} sounds (rms ${held.rms})", held.rms > 0.01f)
|
||||
assertTrue("${p.name} rms ${held.rms}", held.rms < 0.6f)
|
||||
c.noteOff(60)
|
||||
var frames = 0
|
||||
while (c.isActive && frames < fs * 15) { c.render(); frames++ }
|
||||
assertFalse("${p.name} frees after its release", c.isActive)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun grainsFollowTheNote() {
|
||||
// a pure C4 sine as the source: A4 must come out at 440 Hz
|
||||
val n = 48_000 * 4
|
||||
SampleStore.put("test:sine", SampleStore.Sample(FloatArray(n) { (0.8 * sin(2 * PI * 261.6256 * it / 48_000)).toFloat() }, 48_000))
|
||||
val c = cloud(slot(null, "source" to EchofieldDefs.CUSTOM, EchofieldDefs.SAMPLE_KEY to "test:sine",
|
||||
"drift" to "0.0", "harmony" to "0.0", "reverse" to "0.0", "scatter" to "0.0", "grain_tone" to "0.0", "motion" to "0.0"))
|
||||
c.noteOn(69, 127)
|
||||
render(c, fs)
|
||||
var prev = c.render(); var crossings = 0
|
||||
repeat(fs * 2) { val x = c.render(); if (prev < 0f && x >= 0f) crossings++; prev = x }
|
||||
assertEquals(440.0, crossings / 2.0, 6.0)
|
||||
}
|
||||
|
||||
@Test fun aCloudIsAboutAsLoudAsASynthNote() {
|
||||
val c = cloud(slot("Felt Clouds")).apply { noteOn(60, 100) }
|
||||
render(c, fs * 3)
|
||||
val cloud = render(c, fs).rms
|
||||
val syn = SynthVoice(fs).apply { noteOn(60, 100, SynthPatch.DEFAULT) }
|
||||
repeat(fs) { syn.render() }
|
||||
var s = 0.0; repeat(fs) { val x = syn.render(); s += x * x }
|
||||
val ratio = cloud / sqrt(s / fs).toFloat()
|
||||
println("cloud/synth rms ratio $ratio")
|
||||
assertTrue("cloud/synth level $ratio within -6..+6 dB", ratio in 0.5f..2f)
|
||||
}
|
||||
|
||||
@Test fun ownersReleaseOnlyTheirNotes() {
|
||||
val c = cloud(slot("Felt Clouds"))
|
||||
c.noteOn(60, 100, owner = 3)
|
||||
c.noteOn(64, 100, owner = GrainCloud.OWNER_LIVE)
|
||||
c.noteOn(67, 100, owner = GrainCloud.OWNER_SEQ)
|
||||
c.releaseOwner(3)
|
||||
render(c, fs)
|
||||
assertTrue(c.isActive)
|
||||
c.releaseSequenced() // the arpeggio's note goes, the live one stays
|
||||
render(c, fs * 2)
|
||||
assertTrue("live note still held", c.isActive)
|
||||
c.noteOff(64)
|
||||
var frames = 0
|
||||
while (c.isActive && frames < fs * 15) { c.render(); frames++ }
|
||||
assertFalse(c.isActive)
|
||||
}
|
||||
|
||||
@Test fun pulseModeFollowsTheTempo() {
|
||||
val c = cloud(slot("Kalimba Steps"), tempo = 120f)
|
||||
c.noteOn(60, 100)
|
||||
val s = render(c, fs * 3)
|
||||
assertTrue(s.finite && s.rms > 0.005f)
|
||||
}
|
||||
|
||||
@Test fun cpuBudgetForFourDenseClouds() {
|
||||
val clouds = Array(4) { cloud(slot("Glass Choir", "dens_mul" to "2.0", "size_mul" to "1.0")).apply { noteOn(48 + it * 5, 100) } }
|
||||
repeat(fs) { for (c in clouds) c.render() }
|
||||
val seconds = 2
|
||||
val t0 = System.nanoTime()
|
||||
repeat(fs * seconds) { for (c in clouds) c.render() }
|
||||
val load = (System.nanoTime() - t0) / 1e9 / seconds
|
||||
println("4 dense Grain Clouds: ${"%.1f".format(load * 100)} % of real time on this host")
|
||||
assertTrue(load < 1.0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user