iOS core: Morph Pad, Wavetable and Grain Cloud ported, bit-exact with Kotlin
Ported by parallel agents against reference renders. The export now records Kotlin's global voice-seed counters and every factory wavetable's data, and adds Sampler/SF2 scenarios (not ported yet). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
bf5f2b53af
commit
8d85b14a67
@@ -9,6 +9,7 @@ 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.SamplerPads
|
||||
import cc.tiredsysadmin.android.sizzle.model.ToolboxSlot
|
||||
import cc.tiredsysadmin.android.sizzle.model.ToolboxType
|
||||
import cc.tiredsysadmin.android.sizzle.wavetable.WavePatch
|
||||
@@ -35,9 +36,19 @@ import kotlin.math.roundToInt
|
||||
* script ("on <frame> <midi> <vel>", "off <frame> <midi>")
|
||||
* and the length ("frames")
|
||||
* <KIND>_<TYPE>__<variant>.f32 the output, raw little-endian float32
|
||||
* TABLE__<name>.scn / .f32 each factory wavetable's data, all mip levels
|
||||
* concatenated (checks the table generators)
|
||||
*
|
||||
* The Morph Pad and Grain Cloud seed each new voice from a private global counter;
|
||||
* "seeds <frasil> <grain>" records both counters right before the scenario builds
|
||||
* its voices (render() builds a SynthVoice, whose FrasilVoice takes the next
|
||||
* Frasil seed, then a FrasilVoice, then a GrainCloud).
|
||||
*
|
||||
* Kinds: SYNTH = SynthVoice with the engine's patch (Synth, Morph Pad, Wavetable);
|
||||
* FRASIL = FrasilVoice alone; WAVE = WaveVoice alone; GRAIN = GrainCloud.
|
||||
* FRASIL = FrasilVoice alone; WAVE = WaveVoice alone; GRAIN = GrainCloud;
|
||||
* SAMPLE = SampleVoice with the engine's voiceSample() settings (Sampler pads 0 and
|
||||
* 7, SF2/XI loader). SAMPLE scenarios also write the test sample as
|
||||
* <base>.smp.f32 and a line "sample <rate> <loopStart> <loopEnd>".
|
||||
*/
|
||||
class IosGoldenInstruments {
|
||||
|
||||
@@ -46,6 +57,10 @@ class IosGoldenInstruments {
|
||||
/** 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> {
|
||||
if (kind == "SAMPLE") return listOf( // Sampler pads 0 and 7 (C2, G2); SF2 plays any note
|
||||
Ev(0, true, 36, 100), Ev(at(0.35), false, 36, 0),
|
||||
Ev(at(0.50), true, 43, 64), Ev(at(0.90), false, 43, 0),
|
||||
)
|
||||
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),
|
||||
@@ -62,27 +77,100 @@ class IosGoldenInstruments {
|
||||
val out = File(dir!!).apply { mkdirs() }
|
||||
WaveTables.prewarmBlocking()
|
||||
GrainSources.prewarmBlocking()
|
||||
exportTables(out)
|
||||
|
||||
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,
|
||||
"SAMPLE" to ToolboxType.SAMPLER, "SAMPLE" to ToolboxType.SOUNDFONT,
|
||||
)
|
||||
// 0.6 s at 44.1 kHz (so playback resamples): a decaying saw plus a little
|
||||
// noise; loop points for the SF2 path.
|
||||
var seed = 0x2468ACE
|
||||
val smp = SampleStore.Sample(FloatArray(26_460) { i ->
|
||||
seed = seed * 1103515245 + 12345
|
||||
val t = i / 44_100.0
|
||||
(0.7 * (2 * ((t * 220) % 1.0) - 1) * kotlin.math.exp(-t * 3)).toFloat() +
|
||||
((seed ushr 8) and 0xFFFF) / 65535f * 0.05f - 0.025f
|
||||
}, 44_100, loopStart = 8_000, loopEnd = 20_000)
|
||||
SampleStore.put(SAMPLE_ID, smp)
|
||||
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) }
|
||||
if (kind == "SAMPLE") sampleSlot(type, variant, slot)
|
||||
val events = script(kind)
|
||||
val clock0 = FrasilClock.seconds
|
||||
val seeds = "${counter(FrasilVoice::class.java)} ${counter(GrainCloud::class.java)}"
|
||||
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")
|
||||
append("clock ${java.lang.Double.toString(clock0)}\nframes $FRAMES\nseeds $seeds\n")
|
||||
slot.values.toSortedMap().forEach { (k, v) -> append("p $k $v\n") }
|
||||
if (kind == "SAMPLE") append("sample ${smp.sampleRate} ${smp.loopStart} ${smp.loopEnd}\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())
|
||||
if (kind == "SAMPLE") {
|
||||
val sb = ByteBuffer.allocate(smp.data.size * 4).order(ByteOrder.LITTLE_ENDIAN)
|
||||
smp.data.forEach { sb.putFloat(it) }
|
||||
File(out, "$base.smp.f32").writeBytes(sb.array())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The private `seeds` counter a voice class takes its next seed from. */
|
||||
private fun counter(c: Class<*>): Int {
|
||||
val f = runCatching { c.getDeclaredField("seeds") }.getOrElse {
|
||||
Class.forName(c.name + "\$Companion").getDeclaredField("seeds")
|
||||
}
|
||||
f.isAccessible = true
|
||||
return f.getInt(null)
|
||||
}
|
||||
|
||||
private fun exportTables(out: File) {
|
||||
for (name in WaveTables.FACTORY_NAMES) {
|
||||
val t = WaveTables.factory(name)!!
|
||||
val all = (0 until t.data.size).flatMap { t.data[it].asList() }
|
||||
val bb = ByteBuffer.allocate(all.size * 4).order(ByteOrder.LITTLE_ENDIAN)
|
||||
all.forEach { bb.putFloat(it) }
|
||||
val base = "TABLE__" + name.replace(Regex("[^A-Za-z0-9]"), "_")
|
||||
File(out, "$base.scn").writeText("kind TABLE\ntype WAVETABLE\ntable $name\n")
|
||||
File(out, "$base.f32").writeBytes(bb.array())
|
||||
}
|
||||
}
|
||||
|
||||
/** Point the slot at the test sample: Sampler pads 0 and 7 with per-variant
|
||||
* slices and volumes, or the SF2/XI loader's sample with root note 60. */
|
||||
private fun sampleSlot(type: ToolboxType, variant: String, slot: ToolboxSlot) {
|
||||
if (type == ToolboxType.SOUNDFONT) {
|
||||
slot.set("sfSample", SAMPLE_ID); slot.set("sfRoot", 60f)
|
||||
return
|
||||
}
|
||||
for (pad in listOf(0, 7)) {
|
||||
slot.set(SamplerPads.key(pad), SAMPLE_ID)
|
||||
when (variant) {
|
||||
"high" -> { slot.set(SamplerPads.sliceStartKey(pad), 0.1f); slot.set(SamplerPads.sliceEndKey(pad), 0.8f); slot.set(SamplerPads.volumeKey(pad), 1f) }
|
||||
"low" -> { slot.set(SamplerPads.sliceStartKey(pad), 0.7f); slot.set(SamplerPads.sliceEndKey(pad), 0.2f); slot.set(SamplerPads.volumeKey(pad), 0.3f) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The engine's voiceSample(): which sample a note plays and how. */
|
||||
private fun sampleNoteOn(v: SampleVoice, slot: ToolboxSlot, type: ToolboxType, note: Int, velocity: Int) {
|
||||
if (type == ToolboxType.SAMPLER) {
|
||||
val pad = SamplerPads.padFor(note)
|
||||
if (pad !in 0 until SamplerPads.COUNT) return
|
||||
val s = SampleStore.get(slot.string(SamplerPads.key(pad))) ?: return
|
||||
v.noteOn(s, note, velocity, note, slot.float(SamplerPads.sliceStartKey(pad), 0f),
|
||||
slot.float(SamplerPads.sliceEndKey(pad), 1f), slot.float(SamplerPads.volumeKey(pad), SamplerPads.DEFAULT_VOLUME))
|
||||
} else {
|
||||
val s = SampleStore.get(slot.string("sfSample")) ?: return
|
||||
v.noteOn(s, note, velocity, slot.float("sfRoot", 60f).toInt(), 0f, 1f, slot.float("volume", 0.8f),
|
||||
slot.float("attack", 0.005f), slot.float("decay", 0.10f), slot.float("sustain", 1f),
|
||||
slot.float("release", 0.20f), slot.string("loop", "Off") == "On", slot.float("loopXfade", 0f))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,6 +179,7 @@ class IosGoldenInstruments {
|
||||
val frasil = FrasilVoice(SR)
|
||||
val wave = WaveVoice(SR)
|
||||
val cloud = GrainCloud(SR)
|
||||
val sampler = SampleVoice(SR)
|
||||
val synthPatch = when (type) {
|
||||
ToolboxType.NES_SYNTH -> SynthPatch.from(slot, TEMPO)
|
||||
ToolboxType.MORPH_PAD -> SynthPatch.forPad(FrasilPatch.from(slot))
|
||||
@@ -112,12 +201,14 @@ class IosGoldenInstruments {
|
||||
"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)
|
||||
"SAMPLE" -> if (e.on) sampleNoteOn(sampler, slot, type, e.midi, e.vel) else sampler.noteOff()
|
||||
}
|
||||
}
|
||||
y[i] = when (kind) {
|
||||
"SYNTH" -> synth.render()
|
||||
"FRASIL" -> frasil.render()
|
||||
"WAVE" -> wave.render()
|
||||
"SAMPLE" -> sampler.render()
|
||||
else -> cloud.render()
|
||||
}
|
||||
}
|
||||
@@ -143,5 +234,6 @@ class IosGoldenInstruments {
|
||||
const val TEMPO = 120f
|
||||
const val BLOCK = 256
|
||||
const val FRAMES = 72_000 // 1.5 s
|
||||
const val SAMPLE_ID = "ios-golden-sample"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user