iOS port plan and instrument reference harness

PORTING.md: native SwiftUI app on a C++ engine verified against Kotlin.
IosGoldenInstruments renders note scripts through the Kotlin voices; the C++
runner plays the same scripts through TestVoice makers (weak placeholders
until each port lands).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-15 21:49:02 +01:00
co-authored by Claude Opus 5.5
parent 6b7e2108e7
commit bf5f2b53af
7 changed files with 327 additions and 10 deletions
@@ -0,0 +1,147 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
package cc.tiredsysadmin.android.sizzle.audio
import cc.tiredsysadmin.android.sizzle.echofield.EchofieldDefs
import cc.tiredsysadmin.android.sizzle.echofield.GrainCloud
import cc.tiredsysadmin.android.sizzle.echofield.GrainPatch
import cc.tiredsysadmin.android.sizzle.frasil.FrasilClock
import cc.tiredsysadmin.android.sizzle.frasil.FrasilPatch
import cc.tiredsysadmin.android.sizzle.frasil.FrasilVoice
import cc.tiredsysadmin.android.sizzle.model.ToolboxSlot
import cc.tiredsysadmin.android.sizzle.model.ToolboxType
import cc.tiredsysadmin.android.sizzle.wavetable.WavePatch
import cc.tiredsysadmin.android.sizzle.wavetable.WaveTables
import cc.tiredsysadmin.android.sizzle.wavetable.WaveVoice
import org.junit.Assume.assumeTrue
import org.junit.Test
import java.io.File
import java.nio.ByteBuffer
import java.nio.ByteOrder
import kotlin.math.roundToInt
/**
* Reference renders of the instrument voices for the iOS port (see ios/PORTING.md),
* next to IosGoldenExport's effects. Runs only when SIZZLE_GOLDEN_DIR is set.
*
* Each scenario plays a fixed note script through one voice, the way AudioEngine
* drives it: the patch is built from the slot exactly as the engine does
* (patchForSlot / GrainPatch.from), and the Morph Pad's shared FrasilClock advances
* once per 256-frame block before the block renders. Files, in SIZZLE_GOLDEN_DIR:
*
* <KIND>_<TYPE>__<variant>.scn kind, type, sr, tempo, block, the FrasilClock start
* ("clock"), the slot's values ("p"), the note
* script ("on <frame> <midi> <vel>", "off <frame> <midi>")
* and the length ("frames")
* <KIND>_<TYPE>__<variant>.f32 the output, raw little-endian float32
*
* Kinds: SYNTH = SynthVoice with the engine's patch (Synth, Morph Pad, Wavetable);
* FRASIL = FrasilVoice alone; WAVE = WaveVoice alone; GRAIN = GrainCloud.
*/
class IosGoldenInstruments {
private data class Ev(val frame: Int, val on: Boolean, val midi: Int, val vel: Int)
/** Two notes: the second starts after the first's release has begun, so both
* attack, release and retrigger paths run. GRAIN adds an overlapping third. */
private fun script(kind: String): List<Ev> {
val s = mutableListOf(
Ev(0, true, 60, 100), Ev(at(0.35), false, 60, 0),
Ev(at(0.50), true, 67, 64), Ev(at(0.90), false, 67, 0),
)
if (kind == "GRAIN") { s += Ev(at(0.10), true, 64, 90); s += Ev(at(0.60), false, 64, 0) }
return s.sortedBy { it.frame }
}
private fun at(seconds: Double) = (seconds * SR).roundToInt()
@Test fun export() {
val dir = System.getenv("SIZZLE_GOLDEN_DIR")
assumeTrue("set SIZZLE_GOLDEN_DIR to export", dir != null)
val out = File(dir!!).apply { mkdirs() }
WaveTables.prewarmBlocking()
GrainSources.prewarmBlocking()
val kinds = listOf(
"SYNTH" to ToolboxType.NES_SYNTH, "SYNTH" to ToolboxType.MORPH_PAD, "SYNTH" to ToolboxType.WAVETABLE,
"FRASIL" to ToolboxType.MORPH_PAD, "WAVE" to ToolboxType.WAVETABLE, "GRAIN" to ToolboxType.GRAIN_CLOUD,
)
for ((kind, type) in kinds) for ((variant, values) in variants(type)) {
val slot = ToolboxSlot(0).apply { fill(type) }
values.forEach { (k, v) -> slot.set(k, v) }
val events = script(kind)
val clock0 = FrasilClock.seconds
val y = render(kind, type, slot, events)
val base = "${kind}_${type.name}__$variant"
File(out, "$base.scn").writeText(buildString {
append("kind $kind\ntype ${type.name}\nsr $SR\ntempo $TEMPO\nblock $BLOCK\n")
append("clock ${java.lang.Double.toString(clock0)}\nframes $FRAMES\n")
slot.values.toSortedMap().forEach { (k, v) -> append("p $k $v\n") }
events.forEach { e -> append(if (e.on) "on ${e.frame} ${e.midi} ${e.vel}\n" else "off ${e.frame} ${e.midi}\n") }
})
val bb = ByteBuffer.allocate(y.size * 4).order(ByteOrder.LITTLE_ENDIAN)
y.forEach { bb.putFloat(it) }
File(out, "$base.f32").writeBytes(bb.array())
}
}
private fun render(kind: String, type: ToolboxType, slot: ToolboxSlot, events: List<Ev>): FloatArray {
val synth = SynthVoice(SR)
val frasil = FrasilVoice(SR)
val wave = WaveVoice(SR)
val cloud = GrainCloud(SR)
val synthPatch = when (type) {
ToolboxType.NES_SYNTH -> SynthPatch.from(slot, TEMPO)
ToolboxType.MORPH_PAD -> SynthPatch.forPad(FrasilPatch.from(slot))
ToolboxType.WAVETABLE -> SynthPatch.forWave(WavePatch.from(slot, TEMPO))
else -> SynthPatch.DEFAULT
}
val frasilPatch = if (type == ToolboxType.MORPH_PAD) FrasilPatch.from(slot) else null
val wavePatch = if (type == ToolboxType.WAVETABLE) WavePatch.from(slot, TEMPO) else null
if (kind == "GRAIN") cloud.setPatch(GrainPatch.from(slot, TEMPO))
val y = FloatArray(FRAMES)
var next = 0
for (i in 0 until FRAMES) {
if (i % BLOCK == 0) FrasilClock.advance(BLOCK, SR)
while (next < events.size && events[next].frame == i) {
val e = events[next++]
when (kind) {
"SYNTH" -> if (e.on) synth.noteOn(e.midi, e.vel, synthPatch) else synth.noteOff()
"FRASIL" -> if (e.on) frasil.noteOn(e.midi, e.vel / 127f, frasilPatch!!) else frasil.noteOff()
"WAVE" -> if (e.on) wave.noteOn(e.midi, e.vel / 127f, wavePatch!!) else wave.noteOff()
"GRAIN" -> if (e.on) cloud.noteOn(e.midi, e.vel, GrainCloud.OWNER_LIVE) else cloud.noteOff(e.midi)
}
}
y[i] = when (kind) {
"SYNTH" -> synth.render()
"FRASIL" -> frasil.render()
"WAVE" -> wave.render()
else -> cloud.render()
}
}
return y
}
/** Defaults, then every listed parameter at 75 % / 25 % of its range (enums at
* other choices, never "Custom", which needs a user sample). */
private fun variants(type: ToolboxType): List<Pair<String, Map<String, String>>> {
fun variant(frac: Float, choice: (List<String>) -> String) = type.params.associate { s ->
s.key to if (s.isEnum) choice(s.choices.filter { it != EchofieldDefs.CUSTOM && it != WaveTables.CUSTOM })
else (s.min + frac * (s.max - s.min)).let { if (s.isInt) it.roundToInt().toFloat() else it }.toString()
}
return listOf(
"default" to emptyMap(),
"high" to variant(0.75f) { it.last() },
"low" to variant(0.25f) { it[minOf(1, it.lastIndex)] },
)
}
private companion object {
const val SR = 48_000
const val TEMPO = 120f
const val BLOCK = 256
const val FRAMES = 72_000 // 1.5 s
}
}