Morph Pad instrument: FRASIL's drone voice ported to Kotlin (0.30.0)
A new instrument for evolving pads and drones, ported from FRASIL 3.3 (Styves Goupil, an M-VAVE FM-1 firmware, MPL-2.0): a detuned saw choir, eight drifting additive partials and noise through resonators, an Ornstein-Uhlenbeck MOTION walk and a shared TIDE. Morph toward any of FRASIL's 36 drone sounds by hand, by MIDI LFO or over a set time while a note is held. Played by SynthVoice (so the tracker, live MIDI and audition pools all get it); the frasil/ package stays MPL-2.0, LICENSES/MPL-2.0.txt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
dae8ac46ac
commit
2d59b0429d
@@ -6,6 +6,8 @@ package cc.tiredsysadmin.android.sizzle.audio
|
||||
import android.media.AudioAttributes
|
||||
import android.media.AudioFormat
|
||||
import android.media.AudioTrack
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilClock
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilPatch
|
||||
import cc.tiredsysadmin.android.sizzle.model.Arrangement
|
||||
import cc.tiredsysadmin.android.sizzle.model.Pattern
|
||||
import cc.tiredsysadmin.android.sizzle.model.Project
|
||||
@@ -610,6 +612,7 @@ class AudioEngine(
|
||||
*/
|
||||
fun fillBlock(out: FloatArray, frames: Int) {
|
||||
val proj = project
|
||||
FrasilClock.advance(frames, sampleRate) // the Morph Pad's shared TIDE runs on it
|
||||
// Per-block param housekeeping: LFOs modulate targets first, then effects
|
||||
// read their (possibly modulated) parameters.
|
||||
if (proj != null) {
|
||||
@@ -1264,7 +1267,7 @@ class AudioEngine(
|
||||
if (slot?.type == ToolboxType.SAMPLER) return
|
||||
// Remember which slot is being auditioned so refreshSynthPatches pushes live
|
||||
// knob turns into the held preview note.
|
||||
if (slot?.type == ToolboxType.NES_SYNTH) auditionSlotIdx = slotIndex
|
||||
if (hasSynthPatch(slot?.type)) auditionSlotIdx = slotIndex
|
||||
val v = liveVoices.firstOrNull { !it.isActive } ?: liveVoices[0].also { it.kill() }
|
||||
v.noteOn(midi, 110, patchForSlot(slot, project?.tempoBpm ?: 120f))
|
||||
}
|
||||
@@ -1451,8 +1454,14 @@ class AudioEngine(
|
||||
/** Parse a slot's synth patch (default init patch if it isn't an NES synth).
|
||||
* Allocates one [SynthPatch] — used only for the first audition note; the
|
||||
* sequencer/bus paths use the per-block cache in [refreshSynthPatches]. */
|
||||
private fun patchForSlot(slot: ToolboxSlot?, tempoBpm: Float): SynthPatch =
|
||||
if (slot?.type == ToolboxType.NES_SYNTH) SynthPatch.from(slot, tempoBpm) else SynthPatch.DEFAULT
|
||||
private fun patchForSlot(slot: ToolboxSlot?, tempoBpm: Float): SynthPatch = when (slot?.type) {
|
||||
ToolboxType.NES_SYNTH -> SynthPatch.from(slot, tempoBpm)
|
||||
ToolboxType.MORPH_PAD -> SynthPatch.forPad(FrasilPatch.from(slot))
|
||||
else -> SynthPatch.DEFAULT
|
||||
}
|
||||
|
||||
/** The slot types whose notes a [SynthVoice] plays with a parsed [SynthPatch]. */
|
||||
private fun hasSynthPatch(type: ToolboxType?) = type == ToolboxType.NES_SYNTH || type == ToolboxType.MORPH_PAD
|
||||
|
||||
/**
|
||||
* Once per block: refresh each bus's cached synth patch (reparsing only when the
|
||||
@@ -1466,9 +1475,9 @@ class AudioEngine(
|
||||
val tempo = proj.tempoBpm
|
||||
for (ch in 0 until Pattern.TRACK_COUNT) {
|
||||
val slot = proj.toolbox.getOrNull(proj.mixer.channels[ch].instrumentSlot)
|
||||
if (slot?.type != ToolboxType.NES_SYNTH) { synthPatch[ch] = null; synthPatchVer[ch] = -1; continue }
|
||||
if (slot == null || !hasSynthPatch(slot.type)) { synthPatch[ch] = null; synthPatchVer[ch] = -1; continue }
|
||||
if (synthPatch[ch] == null || slot.version != synthPatchVer[ch] || tempo != synthPatchTempo[ch]) {
|
||||
synthPatch[ch] = SynthPatch.from(slot, tempo)
|
||||
synthPatch[ch] = patchForSlot(slot, tempo)
|
||||
synthPatchVer[ch] = slot.version
|
||||
synthPatchTempo[ch] = tempo
|
||||
}
|
||||
@@ -1480,9 +1489,9 @@ class AudioEngine(
|
||||
}
|
||||
// Editor audition voices follow the slot currently being previewed.
|
||||
val aslot = proj.toolbox.getOrNull(auditionSlotIdx)
|
||||
if (aslot?.type == ToolboxType.NES_SYNTH) {
|
||||
if (aslot != null && hasSynthPatch(aslot.type)) {
|
||||
if (auditionPatch == null || aslot.version != auditionPatchVer || tempo != auditionPatchTempo) {
|
||||
auditionPatch = SynthPatch.from(aslot, tempo)
|
||||
auditionPatch = patchForSlot(aslot, tempo)
|
||||
auditionPatchVer = aslot.version
|
||||
auditionPatchTempo = tempo
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
package cc.tiredsysadmin.android.sizzle.audio
|
||||
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilPatch
|
||||
import cc.tiredsysadmin.android.sizzle.model.SynthDefs
|
||||
import cc.tiredsysadmin.android.sizzle.model.TimeDivision
|
||||
import cc.tiredsysadmin.android.sizzle.model.ToolboxSlot
|
||||
@@ -41,6 +42,9 @@ class SynthPatch(
|
||||
val modSrc: IntArray, // index into MOD_SRC (0 == Off)
|
||||
val modDst: IntArray, // index into MOD_DST
|
||||
val modAmt: FloatArray, // -1..1
|
||||
/** Set for a Morph Pad slot: the voice then plays FRASIL's drone voice from this
|
||||
* instead of the subtractive path (see [SynthVoice]); everything above is unused. */
|
||||
val pad: FrasilPatch? = null,
|
||||
) {
|
||||
companion object {
|
||||
const val OSC_COUNT = SynthDefs.OSC_COUNT
|
||||
@@ -96,5 +100,11 @@ class SynthPatch(
|
||||
|
||||
/** A plain default patch (for generic live audition not tied to a synth slot). */
|
||||
val DEFAULT: SynthPatch = from(null, 120f)
|
||||
|
||||
/** A Morph Pad patch: the default synth fields, carrying [pad]. */
|
||||
fun forPad(pad: FrasilPatch): SynthPatch = with(DEFAULT) {
|
||||
SynthPatch(oscWave, oscOct, oscSemi, oscFine, oscLevel, noiseLevel, cutoff, resonance, keytrack,
|
||||
filterEnvAmt, fA, fD, fS, fR, aA, aD, aS, aR, lfoWave, lfoHz, lfoPhase, modSrc, modDst, modAmt, pad)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
package cc.tiredsysadmin.android.sizzle.audio
|
||||
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilVoice
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.exp
|
||||
import kotlin.math.sin
|
||||
@@ -71,15 +72,30 @@ class SynthVoice(private val sampleRate: Int) {
|
||||
private val ampEnv = Adsr()
|
||||
private val filtEnv = Adsr()
|
||||
|
||||
val isActive: Boolean get() = ampEnv.active
|
||||
// A Morph Pad note plays FRASIL's drone voice instead of the subtractive path. It
|
||||
// lives inside this class so every synth pool (sequencer, mix-bus, audition) can
|
||||
// play a pad with no change to how voices are allocated, stolen or released.
|
||||
private val pad = FrasilVoice(sampleRate)
|
||||
private var padMode = false
|
||||
|
||||
val isActive: Boolean get() = if (padMode) pad.isActive else ampEnv.active
|
||||
/** Gate is still open (attack/decay/sustain) — the note is *held*. A voice in its
|
||||
* release tail is still [isActive] but no longer gated; distinguishing the two
|
||||
* lets a note-off target the held voice instead of one that's already ringing
|
||||
* out (which otherwise leaves the held note stuck under rapid re-tapping). */
|
||||
val isGated: Boolean get() = ampEnv.gateOpen
|
||||
val isGated: Boolean get() = if (padMode) pad.isGated else ampEnv.gateOpen
|
||||
|
||||
/** Start (or steal-and-restart) this voice with the given [patch]. */
|
||||
fun noteOn(midi: Int, velocity: Int, patch: SynthPatch) {
|
||||
val padPatch = patch.pad
|
||||
if (padPatch != null) {
|
||||
if (!padMode) { ampEnv.kill(); filtEnv.kill(); padMode = true }
|
||||
this.patch = patch
|
||||
pitch = midi
|
||||
pad.noteOn(midi, velocity / 127f, padPatch)
|
||||
return
|
||||
}
|
||||
if (padMode) { pad.kill(); padMode = false }
|
||||
this.patch = patch
|
||||
pitch = midi
|
||||
this.velocity = (velocity / 127f).coerceIn(0f, 1f)
|
||||
@@ -98,11 +114,12 @@ class SynthVoice(private val sampleRate: Int) {
|
||||
computeTuning()
|
||||
}
|
||||
|
||||
fun noteOff() { ampEnv.gateOff(); filtEnv.gateOff() }
|
||||
fun noteOff() { if (padMode) pad.noteOff() else { ampEnv.gateOff(); filtEnv.gateOff() } }
|
||||
|
||||
/** Hard stop with no release tail (transport stop / voice steal). */
|
||||
fun kill() {
|
||||
ampEnv.kill(); filtEnv.kill()
|
||||
pad.kill()
|
||||
pitch = -1
|
||||
ownerTrack = -1
|
||||
icB = 0f; icL = 0f
|
||||
@@ -114,6 +131,13 @@ class SynthVoice(private val sampleRate: Int) {
|
||||
* turns (cutoff, levels, mod…) without clicking or retriggering. */
|
||||
fun setPatch(p: SynthPatch) {
|
||||
if (p === patch) return
|
||||
val padPatch = p.pad
|
||||
if (padMode || padPatch != null) {
|
||||
// Only within the same kind: a pad note follows pad edits, a synth note synth
|
||||
// edits; a slot switched between the two takes effect on the next note.
|
||||
if (padMode && padPatch != null) { patch = p; pad.setPatch(padPatch) }
|
||||
return
|
||||
}
|
||||
patch = p
|
||||
lfoInc = p.lfoHz.toDouble() / sampleRate
|
||||
computeTuning()
|
||||
@@ -130,6 +154,7 @@ class SynthVoice(private val sampleRate: Int) {
|
||||
}
|
||||
|
||||
fun render(): Float {
|
||||
if (padMode) return pad.render()
|
||||
if (!ampEnv.active) return 0f
|
||||
val dt = 1f / sampleRate
|
||||
val ampE = ampEnv.advance(dt)
|
||||
|
||||
@@ -0,0 +1,230 @@
|
||||
// SPDX-FileCopyrightText: 2026 Styves Goupil (FRASIL)
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown (Kotlin port for Sizzle)
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
||||
// If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
// https://mozilla.org/MPL/2.0/ (also in LICENSES/MPL-2.0.txt of this repository).
|
||||
//
|
||||
// Ported from FRASIL 3.3 (https://github.com/styvesgoupil/frasil, commit e7a8fb2):
|
||||
// engine/params.c (the drone's settings, their ranges, the 36 drone presets).
|
||||
|
||||
package cc.tiredsysadmin.android.sizzle.frasil
|
||||
|
||||
import cc.tiredsysadmin.android.sizzle.model.ParamSpec
|
||||
|
||||
/**
|
||||
* The Morph Pad's settings and factory sounds, taken from FRASIL's drone (its
|
||||
* CHOEUR / PARTIELS / SOUFFLE layers, MOTION and TIDE) and its 36 drone presets.
|
||||
*
|
||||
* Every slider is stored 0..1 exactly as FRASIL's 0..127 settings divided by 127;
|
||||
* [engineFrom] turns them into the engine's units (FRASIL params.c
|
||||
* `params_to_engine`). Three settings are choices (INTERVAL, TYPE, PITCH).
|
||||
*
|
||||
* Left out, because Sizzle's channels are mono or Sizzle has its own: SPREAD (the
|
||||
* voices across the stereo field), SPACE (FRASIL's own reverb: put an Ambience on
|
||||
* the channel instead), PRESSURE, TUNE, GLINT / BASS / LEAD and the master effects.
|
||||
*
|
||||
* Added for Sizzle: MORPH. FRASIL glides a whole scene to another over 1..120 s;
|
||||
* here a pad morphs toward one of the factory sounds ([MORPH_TO]), by hand
|
||||
* ([MORPH], an LFO can move it) and, with [MORPH_TIME], on its own while a note is
|
||||
* held: each note starts at MORPH and arrives at the target after MORPH TIME.
|
||||
*/
|
||||
object FrasilDefs {
|
||||
|
||||
const val MORPH_TO = "morphTo"
|
||||
const val MORPH = "morph"
|
||||
const val MORPH_TIME = "mtime"
|
||||
|
||||
val INTERVALS = listOf("Unison", "-12", "+7", "+12")
|
||||
private val INTERVAL_RATIO = floatArrayOf(1f, 0.5f, 1.4983071f, 2f)
|
||||
val FILTER_TYPES = listOf("Low", "Band", "High")
|
||||
val BREATH_PITCHES = listOf("-12", "Unison", "+12", "+19", "+24")
|
||||
private val BREATH_RATIO = floatArrayOf(0.5f, 1f, 2f, 2.9966142f, 4f)
|
||||
|
||||
// ---- engine parameter indices (FrasilPatch arrays; all glide, as FRASIL's floats do) ----
|
||||
const val CHOIR = 0
|
||||
const val PARTIALS = 1
|
||||
const val BREATH = 2
|
||||
const val WARMTH = 3
|
||||
const val DETUNE = 4
|
||||
const val CUTOFF = 5
|
||||
const val TILT = 6
|
||||
const val ODD = 7
|
||||
const val STRETCH = 8
|
||||
const val RESONANCE = 9
|
||||
const val COLOR = 10
|
||||
const val ATTACK = 11
|
||||
const val RELEASE = 12
|
||||
const val MOTION = 13
|
||||
const val MOTION_RATE = 14
|
||||
const val TIDE = 15
|
||||
const val TIDE_RATE = 16
|
||||
const val TIMBRE = 17
|
||||
const val SHAPE = 18
|
||||
const val CH_RESO = 19
|
||||
const val EVOLVE = 20
|
||||
const val BEAT = 21
|
||||
const val FORMANT = 22 // log2 of the harmonic, 0..3
|
||||
const val PEAK = 23
|
||||
const val GRAIN = 24
|
||||
const val TONE = 25 // -1 dark .. 1 bright
|
||||
const val AIR = 26
|
||||
const val SOFT = 27
|
||||
const val VELOCITY = 28
|
||||
const val BODY = 29 // FRASIL's FULLNESS
|
||||
const val INTERVAL = 30 // the centre saw's ratio
|
||||
const val BR_PITCH = 31 // the resonators' ratio
|
||||
const val N = 32
|
||||
|
||||
/** The slider/choice parameters, in editor order. Defaults: FRASIL's MIST. */
|
||||
val PARAMS: List<ParamSpec> by lazy { listOf(
|
||||
ParamSpec(MORPH_TO, "Morph To", choices = listOf("Off") + PRESET_NAMES()),
|
||||
ParamSpec(MORPH, "Morph", 0f, 0f, 1f),
|
||||
ParamSpec(MORPH_TIME, "Morph Time s", 0f, 0f, 120f),
|
||||
ParamSpec("choir", "Choir", r(76)), ParamSpec("partials", "Partials", r(89)),
|
||||
ParamSpec("breath", "Breath", r(32)), ParamSpec("timbre", "Timbre", r(64)),
|
||||
ParamSpec("warmth", "Warmth", r(19)), ParamSpec("body", "Body", 1f),
|
||||
ParamSpec("attack", "Attack", r(89)), ParamSpec("release", "Release", r(83)),
|
||||
ParamSpec("velsens", "Velocity", r(38)),
|
||||
ParamSpec("motion", "Motion", r(44)), ParamSpec("mrate", "Motion Rate", r(51)),
|
||||
ParamSpec("tide", "Tide", r(25)), ParamSpec("tperiod", "Tide Speed", r(38)),
|
||||
ParamSpec("detune", "Detune", r(51)), ParamSpec("cfilter", "Filter", r(44)),
|
||||
ParamSpec("shape", "Shape", 0f), ParamSpec("interval", "Interval", choices = INTERVALS),
|
||||
ParamSpec("creso", "Resonance", 0f), ParamSpec("ctype", "Filter Type", choices = FILTER_TYPES),
|
||||
ParamSpec("soft", "Soft", 0f),
|
||||
ParamSpec("tilt", "Tilt", r(57)), ParamSpec("odd", "Odd", r(25)),
|
||||
ParamSpec("stretch", "Stretch", 0f), ParamSpec("evolve", "Evolve", 0f),
|
||||
ParamSpec("beat", "Beat", 0f), ParamSpec("formant", "Formant", r(85)),
|
||||
ParamSpec("peak", "Peak", 0f),
|
||||
ParamSpec("bres", "Glass", r(70)), ParamSpec("color", "Color", 0f),
|
||||
ParamSpec("grain", "Grain", 0f), ParamSpec("bpitch", "Pitch", choices = BREATH_PITCHES),
|
||||
ParamSpec("btone", "Tone", r(64)), ParamSpec("air", "Air", 0f),
|
||||
) }
|
||||
|
||||
/** The editor's pages: (title, keys). */
|
||||
val SECTIONS: List<Pair<String, List<String>>> = listOf(
|
||||
"Morph" to listOf(MORPH_TO, MORPH, MORPH_TIME),
|
||||
"Layers" to listOf("choir", "partials", "breath", "timbre", "warmth", "body"),
|
||||
"Shape" to listOf("attack", "release", "velsens"),
|
||||
"Motion" to listOf("motion", "mrate", "tide", "tperiod"),
|
||||
"Choir" to listOf("detune", "cfilter", "shape", "interval", "creso", "ctype", "soft"),
|
||||
"Partials" to listOf("tilt", "odd", "stretch", "evolve", "beat", "formant", "peak"),
|
||||
"Breath" to listOf("bres", "color", "grain", "bpitch", "btone", "air"),
|
||||
)
|
||||
|
||||
private fun r(v: Int) = v / 127f
|
||||
|
||||
/**
|
||||
* A sound's engine values from its stored settings: [num] reads a slider (0..1,
|
||||
* its default if absent), [choice] a choice's label. Returns the float array
|
||||
* and the choir filter's TYPE (0 low, 1 band, 2 high).
|
||||
*/
|
||||
fun engineFrom(num: (String, Float) -> Float, choice: (String, String) -> String): Pair<FloatArray, Int> {
|
||||
val e = FloatArray(N)
|
||||
fun u(key: String, def: Float) = num(key, def).coerceIn(0f, 1f)
|
||||
e[CHOIR] = u("choir", r(76)); e[PARTIALS] = u("partials", r(89)); e[BREATH] = u("breath", r(32))
|
||||
e[WARMTH] = u("warmth", r(19)); e[DETUNE] = u("detune", r(51)); e[CUTOFF] = u("cfilter", r(44))
|
||||
e[TILT] = u("tilt", r(57)); e[ODD] = u("odd", r(25)); e[STRETCH] = u("stretch", 0f)
|
||||
e[RESONANCE] = u("bres", r(70)); e[COLOR] = u("color", 0f)
|
||||
e[ATTACK] = u("attack", r(89)); e[RELEASE] = u("release", r(83))
|
||||
e[MOTION] = u("motion", r(44)); e[MOTION_RATE] = u("mrate", r(51))
|
||||
e[TIDE] = u("tide", r(25)); e[TIDE_RATE] = u("tperiod", r(38)); e[TIMBRE] = u("timbre", r(64))
|
||||
e[SHAPE] = u("shape", 0f); e[CH_RESO] = u("creso", 0f)
|
||||
e[EVOLVE] = u("evolve", 0f); e[BEAT] = u("beat", 0f)
|
||||
e[FORMANT] = 3f * u("formant", r(85)); e[PEAK] = u("peak", 0f)
|
||||
e[GRAIN] = u("grain", 0f)
|
||||
val tn = u("btone", r(64)) * 127f
|
||||
e[TONE] = if (tn >= 126.5f) 1f else (tn - 64f) / 63f
|
||||
e[AIR] = u("air", 0f); e[SOFT] = u("soft", 0f)
|
||||
e[VELOCITY] = u("velsens", r(38)); e[BODY] = u("body", 1f)
|
||||
e[INTERVAL] = INTERVAL_RATIO[INTERVALS.indexOf(choice("interval", INTERVALS[0])).coerceAtLeast(0)]
|
||||
e[BR_PITCH] = BREATH_RATIO[BREATH_PITCHES.indexOf(choice("bpitch", BREATH_PITCHES[1])).let { if (it < 0) 1 else it }]
|
||||
val type = FILTER_TYPES.indexOf(choice("ctype", FILTER_TYPES[0])).coerceAtLeast(0)
|
||||
return e to type
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- presets ---
|
||||
|
||||
class Preset(val name: String, val values: Map<String, String>)
|
||||
|
||||
/** FRASIL's 36 drone presets, each as the slot values it sets (params.c DRONE_PRESET). */
|
||||
val FACTORY: List<Preset> by lazy {
|
||||
RAW.map { (name, row) ->
|
||||
val m = LinkedHashMap<String, String>()
|
||||
for (c in COLS.indices) {
|
||||
val key = COLS[c] ?: continue
|
||||
m[key] = when (key) {
|
||||
"interval" -> INTERVALS[row[c] % 4]
|
||||
"ctype" -> FILTER_TYPES[row[c] % 3]
|
||||
"bpitch" -> BREATH_PITCHES[row[c] % 5]
|
||||
else -> (row[c] / 127f).toString()
|
||||
}
|
||||
}
|
||||
// Sizzle's own, not in FRASIL's preset: as FRASIL's defaults, no morph.
|
||||
m["velsens"] = r(38).toString(); m["body"] = "1.0"
|
||||
m[MORPH_TO] = "Off"; m[MORPH] = "0.0"; m[MORPH_TIME] = "0.0"
|
||||
Preset(name, m)
|
||||
}
|
||||
}
|
||||
|
||||
/** A factory sound's engine values by its name (MORPH TO's target), or null. */
|
||||
fun factoryEngine(name: String): Pair<FloatArray, Int>? = engineByName[name]
|
||||
|
||||
private val engineByName: Map<String, Pair<FloatArray, Int>> by lazy {
|
||||
FACTORY.associate { p ->
|
||||
p.name to engineFrom({ k, d -> p.values[k]?.toFloatOrNull() ?: d }, { k, d -> p.values[k] ?: d })
|
||||
}
|
||||
}
|
||||
|
||||
// CHO PAR SOU WAR DET FIL TIL ODD STR BRES COL SPR ATT REL MOT RAT TID PER TIM SPA SHP INT CRES TYP EVO BEAT
|
||||
// FORM PEAK GRN PIT TONE AIR SOFT. SPREAD and SPACE (null) are FRASIL's stereo field and reverb.
|
||||
private val COLS: Array<String?> = arrayOf(
|
||||
"choir", "partials", "breath", "warmth", "detune", "cfilter", "tilt", "odd", "stretch", "bres", "color",
|
||||
null, "attack", "release", "motion", "mrate", "tide", "tperiod", "timbre", null, "shape", "interval",
|
||||
"creso", "ctype", "evolve", "beat", "formant", "peak", "grain", "bpitch", "btone", "air", "soft",
|
||||
)
|
||||
|
||||
private fun PRESET_NAMES(): List<String> = RAW.map { it.first }
|
||||
|
||||
private val RAW: List<Pair<String, IntArray>> by lazy {
|
||||
listOf(
|
||||
"Mist" to intArrayOf(76, 89, 32, 19, 51, 44, 57, 25, 0, 70, 0, 89, 89, 83, 44, 51, 25, 38, 64, 57, 0, 0, 0, 0, 0, 0, 85, 0, 0, 1, 64, 0, 0),
|
||||
"Cathedral" to intArrayOf(110, 50, 20, 25, 85, 55, 60, 30, 0, 60, 0, 110, 100, 100, 60, 40, 20, 50, 60, 95, 0, 1, 20, 0, 10, 0, 85, 0, 0, 1, 64, 0, 0),
|
||||
"Glacier" to intArrayOf(20, 100, 70, 5, 30, 60, 35, 50, 60, 115, 127, 100, 80, 110, 40, 30, 30, 40, 80, 90, 0, 0, 0, 0, 40, 15, 100, 30, 20, 2, 80, 0, 0),
|
||||
"Abyss" to intArrayOf(90, 70, 25, 50, 70, 20, 90, 40, 0, 50, 0, 70, 100, 105, 50, 25, 50, 25, 40, 70, 30, 1, 40, 0, 20, 0, 60, 0, 0, 0, 30, 0, 0),
|
||||
"Aurora" to intArrayOf(60, 105, 40, 15, 45, 65, 40, 15, 10, 85, 60, 115, 90, 105, 55, 45, 45, 50, 75, 95, 0, 0, 0, 0, 70, 25, 90, 35, 0, 2, 75, 10, 0),
|
||||
"Ember" to intArrayOf(100, 55, 20, 80, 40, 40, 70, 60, 0, 50, 30, 80, 70, 80, 35, 40, 20, 40, 55, 50, 64, 0, 50, 0, 10, 0, 70, 0, 0, 1, 50, 0, 0),
|
||||
"Tide" to intArrayOf(80, 80, 45, 25, 60, 45, 55, 30, 10, 75, 40, 100, 95, 100, 70, 30, 110, 60, 60, 70, 0, 0, 30, 0, 30, 0, 85, 0, 0, 1, 64, 0, 0),
|
||||
"Dust" to intArrayOf(25, 40, 85, 20, 40, 50, 60, 30, 20, 100, 60, 120, 60, 90, 50, 50, 30, 40, 60, 75, 0, 0, 0, 0, 30, 0, 85, 0, 90, 2, 85, 40, 0),
|
||||
"Wind" to intArrayOf(0, 20, 85, 0, 40, 50, 60, 30, 0, 20, 30, 127, 80, 95, 90, 80, 50, 70, 64, 80, 0, 0, 0, 0, 0, 0, 85, 0, 0, 1, 75, 50, 0),
|
||||
"Organ" to intArrayOf(40, 115, 10, 30, 10, 50, 45, 0, 0, 60, 0, 60, 40, 60, 15, 40, 10, 40, 64, 50, 0, 2, 0, 0, 0, 0, 85, 0, 0, 1, 64, 0, 0),
|
||||
"Hollow" to intArrayOf(105, 80, 25, 20, 35, 50, 50, 110, 0, 70, 64, 90, 80, 90, 40, 45, 25, 40, 60, 65, 64, 0, 40, 1, 15, 0, 85, 0, 0, 1, 60, 0, 0),
|
||||
"Vowel" to intArrayOf(65, 125, 15, 10, 30, 55, 30, 20, 0, 70, 0, 85, 90, 95, 45, 35, 60, 45, 64, 70, 0, 0, 60, 1, 20, 0, 95, 105, 0, 1, 64, 0, 0),
|
||||
"Shimmer" to intArrayOf(45, 100, 50, 10, 55, 75, 25, 20, 15, 100, 90, 120, 85, 110, 50, 55, 30, 40, 85, 100, 0, 3, 0, 0, 40, 50, 100, 20, 10, 3, 90, 20, 0),
|
||||
"Chanter" to intArrayOf(105, 45, 15, 40, 30, 55, 60, 70, 0, 60, 30, 70, 50, 70, 30, 45, 15, 40, 60, 45, 110, 0, 35, 1, 0, 0, 85, 0, 0, 1, 64, 0, 0),
|
||||
"Ensemble" to intArrayOf(115, 40, 20, 30, 75, 60, 55, 25, 0, 60, 0, 115, 95, 90, 55, 55, 20, 40, 65, 75, 90, 0, 15, 0, 0, 0, 85, 0, 0, 1, 64, 0, 0),
|
||||
"Bells" to intArrayOf(15, 115, 45, 5, 25, 50, 30, 20, 95, 110, 127, 95, 40, 110, 30, 40, 20, 40, 75, 85, 0, 0, 0, 0, 30, 30, 85, 0, 0, 2, 80, 0, 0),
|
||||
"Nocturne" to intArrayOf(90, 60, 30, 35, 55, 25, 85, 30, 0, 55, 0, 90, 110, 115, 45, 20, 40, 20, 45, 85, 20, 1, 70, 0, 25, 0, 70, 0, 0, 0, 35, 0, 0),
|
||||
"Haze" to intArrayOf(85, 50, 60, 10, 60, 70, 50, 25, 0, 40, 30, 120, 100, 105, 60, 50, 30, 50, 70, 90, 0, 3, 20, 2, 20, 0, 85, 0, 0, 2, 80, 30, 0),
|
||||
"Pulse" to intArrayOf(50, 95, 25, 20, 30, 50, 45, 30, 0, 70, 20, 100, 60, 80, 30, 50, 20, 40, 64, 65, 0, 0, 0, 0, 15, 100, 85, 0, 0, 1, 64, 0, 0),
|
||||
"Rime" to intArrayOf(30, 110, 70, 0, 20, 80, 20, 40, 30, 120, 100, 110, 70, 100, 35, 60, 20, 40, 90, 90, 0, 3, 0, 2, 30, 20, 110, 40, 40, 4, 100, 15, 0),
|
||||
"Deep" to intArrayOf(100, 100, 0, 40, 25, 25, 100, 20, 0, 50, 0, 60, 90, 95, 25, 30, 20, 30, 40, 45, 0, 1, 10, 0, 0, 0, 40, 0, 0, 0, 30, 0, 0),
|
||||
"Spectral" to intArrayOf(20, 120, 15, 5, 20, 50, 20, 10, 0, 70, 0, 90, 90, 100, 50, 40, 80, 50, 64, 80, 0, 0, 0, 0, 50, 10, 85, 127, 0, 1, 64, 0, 0),
|
||||
"Rust" to intArrayOf(80, 50, 40, 100, 50, 45, 65, 60, 20, 80, 40, 80, 60, 80, 40, 55, 25, 40, 55, 55, 70, 0, 80, 1, 20, 15, 75, 0, 60, 1, 45, 20, 0),
|
||||
"Still" to intArrayOf(0, 125, 0, 0, 0, 50, 127, 0, 0, 50, 0, 60, 100, 100, 5, 20, 0, 40, 50, 70, 0, 0, 0, 0, 0, 0, 85, 0, 0, 1, 64, 0, 0),
|
||||
"Velvet" to intArrayOf(95, 45, 10, 15, 35, 40, 85, 20, 0, 50, 0, 100, 105, 110, 35, 30, 20, 45, 52, 70, 0, 0, 10, 0, 0, 0, 85, 0, 0, 1, 50, 0, 120),
|
||||
"Chrome" to intArrayOf(35, 115, 15, 35, 15, 75, 15, 120, 45, 90, 80, 85, 60, 90, 35, 55, 15, 40, 85, 60, 64, 3, 85, 2, 20, 35, 110, 70, 0, 3, 95, 0, 0),
|
||||
"Swarm" to intArrayOf(120, 75, 30, 30, 120, 60, 40, 30, 0, 60, 20, 127, 70, 85, 110, 100, 40, 15, 70, 60, 120, 0, 40, 1, 40, 70, 85, 0, 30, 2, 70, 20, 0),
|
||||
"Foghorn" to intArrayOf(127, 60, 15, 70, 20, 22, 95, 90, 0, 40, 0, 50, 120, 120, 20, 20, 60, 70, 35, 65, 64, 1, 60, 0, 0, 0, 40, 30, 0, 0, 30, 0, 30),
|
||||
"Solar" to intArrayOf(70, 105, 45, 20, 55, 90, 15, 15, 5, 80, 30, 120, 100, 110, 60, 50, 70, 35, 100, 90, 10, 3, 15, 2, 60, 25, 120, 40, 0, 3, 110, 30, 0),
|
||||
"Cave" to intArrayOf(55, 40, 95, 30, 30, 18, 110, 30, 0, 100, 60, 70, 110, 120, 30, 20, 45, 60, 30, 100, 0, 1, 50, 0, 10, 0, 40, 0, 50, 0, 18, 5, 60),
|
||||
"Glass Harp" to intArrayOf(10, 120, 20, 0, 12, 60, 30, 10, 25, 100, 90, 110, 50, 120, 25, 30, 15, 55, 90, 95, 0, 3, 0, 0, 45, 40, 120, 110, 0, 4, 100, 0, 127),
|
||||
"Monk" to intArrayOf(110, 85, 10, 25, 30, 38, 70, 40, 0, 60, 0, 70, 100, 100, 30, 25, 30, 65, 50, 80, 0, 1, 55, 1, 15, 0, 62, 90, 0, 1, 50, 0, 90),
|
||||
"Static" to intArrayOf(35, 40, 127, 15, 40, 70, 40, 50, 0, 110, 90, 127, 40, 70, 80, 110, 20, 20, 80, 55, 30, 0, 30, 2, 0, 0, 85, 0, 85, 3, 100, 40, 0),
|
||||
"Lullaby" to intArrayOf(85, 70, 8, 10, 22, 45, 100, 10, 0, 60, 0, 80, 110, 120, 20, 15, 35, 80, 55, 85, 0, 2, 0, 0, 10, 8, 85, 20, 0, 1, 64, 0, 110),
|
||||
"Storm" to intArrayOf(80, 40, 110, 90, 70, 28, 90, 40, 0, 70, 40, 127, 90, 110, 100, 85, 120, 25, 40, 75, 30, 1, 70, 0, 50, 20, 60, 0, 70, 0, 35, 60, 20),
|
||||
"Prism" to intArrayOf(50, 120, 25, 10, 40, 65, 25, 60, 15, 90, 50, 120, 85, 105, 60, 45, 50, 40, 80, 90, 0, 3, 20, 1, 100, 30, 105, 80, 0, 2, 85, 10, 40),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// SPDX-FileCopyrightText: 2026 Styves Goupil (FRASIL)
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown (Kotlin port for Sizzle)
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
||||
// If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
// https://mozilla.org/MPL/2.0/ (also in LICENSES/MPL-2.0.txt of this repository).
|
||||
|
||||
package cc.tiredsysadmin.android.sizzle.frasil
|
||||
|
||||
import cc.tiredsysadmin.android.sizzle.model.ToolboxSlot
|
||||
|
||||
/**
|
||||
* An immutable snapshot of a Morph Pad slot, parsed off the audio thread's hot path
|
||||
* (once per slot change, like the synth's patch): the sound's engine values [a] and
|
||||
* its filter type, and when MORPH TO names a factory sound, that sound's [b] to
|
||||
* morph toward.
|
||||
*/
|
||||
class FrasilPatch(
|
||||
val a: FloatArray,
|
||||
val aType: Int,
|
||||
val b: FloatArray?,
|
||||
val bType: Int,
|
||||
/** Where a note starts between [a] (0) and [b] (1). */
|
||||
val morph: Float,
|
||||
/** Seconds a held note takes from [morph] to [b]; 0 stays at [morph]. */
|
||||
val morphTime: Float,
|
||||
) {
|
||||
companion object {
|
||||
fun from(slot: ToolboxSlot?): FrasilPatch {
|
||||
val (a, at) = FrasilDefs.engineFrom(
|
||||
{ k, d -> slot?.float(k, d) ?: d },
|
||||
{ k, d -> slot?.string(k, d) ?: d },
|
||||
)
|
||||
val target = slot?.string(FrasilDefs.MORPH_TO, "Off") ?: "Off"
|
||||
val b = if (target == "Off") null else FrasilDefs.factoryEngine(target)
|
||||
return FrasilPatch(
|
||||
a, at, b?.first, b?.second ?: at,
|
||||
morph = (slot?.float(FrasilDefs.MORPH, 0f) ?: 0f).coerceIn(0f, 1f),
|
||||
morphTime = (slot?.float(FrasilDefs.MORPH_TIME, 0f) ?: 0f).coerceIn(0f, 120f),
|
||||
)
|
||||
}
|
||||
|
||||
val DEFAULT: FrasilPatch by lazy { from(null) }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* FRASIL's TIDE is one slow sine shared by every voice (a chord brightens and darkens
|
||||
* together). Sizzle renders voice by voice, so the shared part is the time it runs
|
||||
* on: the audio engine advances this clock once a block, and each voice reads its
|
||||
* tide from it at its own TIDE SPEED. Audio thread only.
|
||||
*/
|
||||
object FrasilClock {
|
||||
var seconds = 0.0
|
||||
private set
|
||||
|
||||
fun advance(frames: Int, sampleRate: Int) { seconds += frames.toDouble() / sampleRate }
|
||||
}
|
||||
@@ -0,0 +1,667 @@
|
||||
// SPDX-FileCopyrightText: 2026 Styves Goupil (FRASIL)
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown (Kotlin port for Sizzle)
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
||||
// If a copy of the MPL was not distributed with this file, You can obtain one at
|
||||
// https://mozilla.org/MPL/2.0/ (also in LICENSES/MPL-2.0.txt of this repository).
|
||||
//
|
||||
// Ported from FRASIL 3.3 (https://github.com/styvesgoupil/frasil, commit e7a8fb2):
|
||||
// engine/dsp/frasil.c, its drone voice (voice_start, drift, voice_block, choir_run,
|
||||
// ch_svf, ch_lp, blepi, partial_ramp, partials_chunk, souffle_chunk, breath_noise,
|
||||
// render_part) and the mix's DC blocker, warmth and FULLNESS high-pass.
|
||||
|
||||
package cc.tiredsysadmin.android.sizzle.frasil
|
||||
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.AIR
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.ATTACK
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.BEAT
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.BODY
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.BREATH
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.BR_PITCH
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.CHOIR
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.CH_RESO
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.COLOR
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.CUTOFF
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.DETUNE
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.EVOLVE
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.FORMANT
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.GRAIN
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.INTERVAL
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.MOTION
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.MOTION_RATE
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.ODD
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.PARTIALS
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.PEAK
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.RELEASE
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.RESONANCE
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.SHAPE
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.SOFT
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.STRETCH
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.TIDE
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.TIDE_RATE
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.TILT
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.TIMBRE
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.TONE
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.VELOCITY
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs.WARMTH
|
||||
import kotlin.math.cos
|
||||
import kotlin.math.exp
|
||||
import kotlin.math.floor
|
||||
import kotlin.math.ln
|
||||
import kotlin.math.pow
|
||||
import kotlin.math.sin
|
||||
import kotlin.math.sqrt
|
||||
import kotlin.math.tan
|
||||
import kotlin.math.tanh
|
||||
|
||||
/**
|
||||
* One note of the Morph Pad: FRASIL's drone voice, three layers summed and slowly
|
||||
* drifting while the note is held.
|
||||
*
|
||||
* - CHOIR: three PolyBLEP saws (centre at INTERVAL, the sides detuned ±), SHAPE
|
||||
* turns each toward a swaying pulse, SOFT integrates it toward a triangle or a
|
||||
* sine; two one-pole low-passes, or with RESONANCE / TYPE a TPT state-variable
|
||||
* filter, the cutoff in harmonics of the note.
|
||||
* - PARTIALS: eight additive partials as "magic circle" oscillators, tilt, odd,
|
||||
* stretch, a formant, each one's level and tuning drifting (EVOLVE), BEAT pulsing.
|
||||
* - BREATH (FRASIL's SOUFFLE): noise or sparse grains through three resonators at
|
||||
* the note times harmonic / odd / bell ratios, Q from air to glass.
|
||||
*
|
||||
* MOTION is an Ornstein-Uhlenbeck random walk per drifting thing (a step a block),
|
||||
* TIDE one slow sine on the timbre ([FrasilClock]). Then, per voice, FRASIL's mix
|
||||
* stages: a DC blocker, WARMTH (tanh) and the BODY high-pass.
|
||||
*
|
||||
* Differences from FRASIL, all because Sizzle's voices are mono and per note: the
|
||||
* stereo buses are summed (centre + sides at -3 dB), there is no pan, no pressure,
|
||||
* no voice shedding, the noise is the voice's own, warmth and the DC blocker run on
|
||||
* each voice instead of the mix, every parameter glides toward its patch value per
|
||||
* block (FRASIL glides them globally), and the partials' levels are always ramped
|
||||
* over the block. Rendering is FRASIL's: a block of [BLOCK] frames sets the voice
|
||||
* up, chunks of [CH] frames are computed at once and handed out a sample at a time
|
||||
* by [render]. Allocation-free after construction.
|
||||
*/
|
||||
class FrasilVoice(private val fs: Int) {
|
||||
|
||||
private val invFs = 1f / fs
|
||||
private val r44 = 44100f / fs // FRASIL's per-step constants were made at 44.1 kHz
|
||||
private val nyq = 0.45f * fs
|
||||
|
||||
var note = -1; private set
|
||||
private var gate = false
|
||||
private var vel = 0f
|
||||
private var env = 0f
|
||||
private var lvl = 0f
|
||||
private var patch: FrasilPatch = FrasilPatch.DEFAULT
|
||||
private var prime = true
|
||||
private val cur = FloatArray(FrasilDefs.N)
|
||||
private var type = 0
|
||||
private var ageFrames = 0L
|
||||
private var tideV = 0f
|
||||
private var rng: Int = nextSeed()
|
||||
|
||||
// drift, each about -1..1
|
||||
private val ouC = FloatArray(3)
|
||||
private var ouCut = 0f
|
||||
private val ouPa = FloatArray(NP)
|
||||
private val ouPf = FloatArray(NP)
|
||||
private val ouR = FloatArray(NR)
|
||||
|
||||
// CHOIR
|
||||
private val cph = FloatArray(3)
|
||||
private val cdt = FloatArray(3)
|
||||
private val cid = FloatArray(3)
|
||||
private val cw = FloatArray(3)
|
||||
private val cpw = FloatArray(3)
|
||||
private var csh = 0f
|
||||
private var sc0 = 0f; private var sc1 = 0f; private var sc2 = 1f
|
||||
private var sp0 = 0f; private var sp1 = 0f; private var sp2 = 1f
|
||||
private val clp = FloatArray(4)
|
||||
private var ca = 0f
|
||||
private var sa1 = 0f; private var sa2 = 0f; private var sa3 = 0f
|
||||
private var cgain = 1f
|
||||
private val cint = FloatArray(6)
|
||||
private var csvf = false
|
||||
|
||||
// PARTIALS
|
||||
private val px = FloatArray(NP); private val py = FloatArray(NP); private val pa = FloatArray(NP)
|
||||
private val ps = FloatArray(NP); private val pc = FloatArray(NP)
|
||||
private val pac = FloatArray(NP); private val pda = FloatArray(NP); private val pbt = FloatArray(NP)
|
||||
private var np = 0
|
||||
|
||||
// BREATH
|
||||
private val r1 = FloatArray(NR); private val r2 = FloatArray(NR)
|
||||
private val rb1 = FloatArray(NR); private val rb2 = FloatArray(NR); private val rg = FloatArray(NR)
|
||||
private var nlp = 0f
|
||||
|
||||
// the voice's output stages
|
||||
private var dcState = 0f
|
||||
private var thinState = 0f
|
||||
private var ka = 0f; private var kr = 0f
|
||||
private var dk = 1f; private var dmake = 1f; private var driveOn = false
|
||||
private var thinA = 0f; private var thin = false
|
||||
private val kdc = brK(0.0014f)
|
||||
private val glideK = brK(0.2f)
|
||||
private val tideK = 1f - exp(-BLOCK.toFloat() / (fs * TIDE_SMOOTH_S))
|
||||
|
||||
// chunk buffers
|
||||
private val bc = FloatArray(CH); private val ba = FloatArray(CH); private val bb = FloatArray(CH)
|
||||
private val x0 = FloatArray(CH); private val x1 = FloatArray(CH); private val xc = FloatArray(CH)
|
||||
private val nz = FloatArray(CH)
|
||||
private val out = FloatArray(CH)
|
||||
private var pos = CH
|
||||
private var chunk = 0
|
||||
private var ending = false
|
||||
|
||||
val isActive: Boolean get() = note >= 0
|
||||
val isGated: Boolean get() = note >= 0 && gate
|
||||
|
||||
/** Start [midi] at [velocity] (0..1). A voice still sounding goes on from where it
|
||||
* is (FRASIL's same-note / steal rule: its phases continue, no click). */
|
||||
fun noteOn(midi: Int, velocity: Float, p: FrasilPatch) {
|
||||
val fresh = note < 0
|
||||
note = midi
|
||||
gate = true
|
||||
vel = velocity.coerceIn(0f, 1f)
|
||||
patch = p
|
||||
ageFrames = 0
|
||||
ending = false
|
||||
chunk = 0 // the new pitch from the next chunk on
|
||||
if (fresh) start()
|
||||
}
|
||||
|
||||
fun noteOff() { gate = false }
|
||||
|
||||
/** Silence now, no tail. */
|
||||
fun kill() {
|
||||
note = -1; gate = false; env = 0f; lvl = 0f; pos = CH; ending = false
|
||||
}
|
||||
|
||||
/** A gliding engine value now (a [FrasilDefs] index), for tests. */
|
||||
internal fun valueForTest(i: Int): Float = cur[i]
|
||||
|
||||
/** Follow edits while sounding: the values glide there over ~25 ms. */
|
||||
fun setPatch(p: FrasilPatch) { patch = p }
|
||||
|
||||
fun render(): Float {
|
||||
if (pos >= CH) {
|
||||
if (note < 0) return 0f
|
||||
if (ending) { note = -1; ending = false; return 0f }
|
||||
renderChunk()
|
||||
pos = 0
|
||||
}
|
||||
return out[pos++]
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------- start ---
|
||||
private fun start() {
|
||||
env = 0f; lvl = 0f; pos = CH; prime = true
|
||||
dcState = 0f; thinState = 0f; nlp = 0f
|
||||
for (k in 0 until 3) {
|
||||
cph[k] = urand()
|
||||
cpw[k] = k * 0.33f
|
||||
ouC[k] = randf() * 0.5f
|
||||
}
|
||||
clp.fill(0f); cint.fill(0f); csvf = false
|
||||
for (k in 0 until NP) { // random phases: no peak where they all start together
|
||||
val ph = urand() * TWO_PI
|
||||
px[k] = cos(ph); py[k] = sin(ph)
|
||||
ps[k] = 0f; pc[k] = 1f
|
||||
ouPa[k] = randf() * 0.5f; ouPf[k] = randf() * 0.5f
|
||||
pac[k] = 0f; pda[k] = 0f
|
||||
pbt[k] = urand()
|
||||
}
|
||||
for (k in 0 until NR) { r1[k] = 0f; r2[k] = 0f; ouR[k] = randf() * 0.5f }
|
||||
ouCut = randf() * 0.5f
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------- block ---
|
||||
private fun block() {
|
||||
val p = patch
|
||||
val b = p.b
|
||||
val m = if (b == null) 0f else morphPosition(p)
|
||||
val gk = if (prime) 1f else glideK
|
||||
for (i in 0 until FrasilDefs.N) {
|
||||
val t = if (b == null) p.a[i] else p.a[i] + (b[i] - p.a[i]) * m
|
||||
cur[i] += (t - cur[i]) * gk
|
||||
}
|
||||
type = if (b != null && m >= 0.5f) p.bType else p.aType
|
||||
val period = 300f * (1f / 30f).pow(cur[TIDE_RATE])
|
||||
val ph = FrasilClock.seconds / period
|
||||
val tideT = sin(TWO_PI * (ph - floor(ph)).toFloat())
|
||||
tideV = if (prime) tideT else tideV + (tideT - tideV) * tideK
|
||||
val wasPrime = prime
|
||||
prime = false
|
||||
ageFrames += BLOCK
|
||||
|
||||
drift()
|
||||
voiceBlock(wasPrime)
|
||||
|
||||
// envelope: a one-pole a chunk to the target, a third of the time a time constant
|
||||
ka = 1f - exp(-CH * invFs * 3f / (0.005f * 6000f.pow(cur[ATTACK])))
|
||||
kr = 1f - exp(-CH * invFs * 3f / (0.05f * 1200f.pow(cur[RELEASE])))
|
||||
val full = cur[BODY]
|
||||
dk = 1f + 5f * cur[WARMTH] * full
|
||||
dmake = 1f / tanh(dk * 0.35f) * 0.35f
|
||||
driveOn = cur[WARMTH] * full > 0.001f
|
||||
thin = full < 0.995f
|
||||
thinA = 1f - exp(-TWO_PI * 20f * 20f.pow(1f - full) * invFs)
|
||||
}
|
||||
|
||||
/** Where this note is between the sound (0) and MORPH TO's (1): MORPH, then with
|
||||
* MORPH TIME a smoothstep from there to 1 while the note is held. */
|
||||
private fun morphPosition(p: FrasilPatch): Float {
|
||||
if (p.morphTime <= 0f) return p.morph
|
||||
val x = ((ageFrames.toFloat() * invFs) / p.morphTime).coerceIn(0f, 1f)
|
||||
return p.morph + (1f - p.morph) * x * x * (3f - 2f * x)
|
||||
}
|
||||
|
||||
/** One Ornstein-Uhlenbeck step: back toward 0 by th, a kick of s (unit variance:
|
||||
* three uniforms). */
|
||||
private fun ou(x: Float, th: Float, s: Float): Float =
|
||||
(x - th * x + s * (randf() + randf() + randf())).coerceIn(-1.5f, 1.5f)
|
||||
|
||||
private fun drift() {
|
||||
val tau = 20f * 0.025f.pow(cur[MOTION_RATE]) // 20 s .. 0.5 s
|
||||
val th = BLOCK * invFs / tau
|
||||
val s = 0.6f * sqrt(2f * th)
|
||||
val te = th * (1f + 3f * cur[EVOLVE])
|
||||
val se = 0.6f * sqrt(2f * te)
|
||||
for (k in 0 until 3) ouC[k] = ou(ouC[k], th, s)
|
||||
for (k in 0 until NP) { ouPa[k] = ou(ouPa[k], te, se); ouPf[k] = ou(ouPf[k], th, s) }
|
||||
for (k in 0 until NR) ouR[k] = ou(ouR[k], th, s)
|
||||
ouCut = ou(ouCut, th, s)
|
||||
}
|
||||
|
||||
private fun voiceBlock(fresh: Boolean) {
|
||||
val c = cur
|
||||
val f = 440f * 2f.pow((note - 69) / 12f)
|
||||
val m = c[MOTION]
|
||||
val mw = m * (1f + 2f * m) // MOTION's wander: twice as wide half-way, three times at the top
|
||||
val bright = (c[TIMBRE] - 0.5f) + tideV * c[TIDE] * 0.5f
|
||||
val full = c[BODY]
|
||||
|
||||
// CHOIR: the saws' tunings, SHAPE's widths, the low-pass, RESONANCE's state-variable
|
||||
if (c[CHOIR] > 0.001f) {
|
||||
val dc = c[DETUNE] * c[DETUNE] * 40f * (0.2f + 0.8f * full)
|
||||
val sh = c[SHAPE]
|
||||
val a = if (sh < 0.5f) 2f * sh else 1f
|
||||
val w = if (sh < 0.5f) 0.5f else 0.5f - 0.8f * (sh - 0.5f)
|
||||
val sw = if (sh > 0.5f) 0.03f + 0.18f * (sh - 0.5f) else 0.03f * a
|
||||
for (j in 0 until 3) {
|
||||
val cents = SIDE[j] * dc + mw * 7f * ouC[j]
|
||||
cdt[j] = (f * (if (j == 0) c[INTERVAL] else 1f) * 2f.pow(cents / 1200f) * invFs).coerceIn(1e-6f, 0.45f)
|
||||
cid[j] = 1f / cdt[j]
|
||||
cpw[j] += PWR[j] * BLOCK * invFs
|
||||
cpw[j] -= floor(cpw[j])
|
||||
cw[j] = (w + sw * sin(TWO_PI * cpw[j]) + 0.1f * mw * ouC[j] * a).coerceIn(0.04f, 0.96f)
|
||||
}
|
||||
csh = a
|
||||
val ce = (c[CUTOFF] + bright * 0.6f + mw * 0.2f * ouCut).coerceIn(0f, 1f)
|
||||
val hc = 1f + 39f * ce * ce
|
||||
val fc = (f * hc).coerceIn(20f, minOf(16000f, nyq))
|
||||
ca = 1f - exp(-TWO_PI * fc * invFs)
|
||||
val pv = (1f + a * a) / 3f - 2f * a * (1f / 3f - 2f * w * (1f - w)) // a saw minus a of it w on: its power
|
||||
cgain = sqrt((1f / 3f) / (if (pv > 0.05f) pv else 0.05f))
|
||||
if (c[CH_RESO] > 0.002f || type != 0) {
|
||||
val g = tan(PI * fc * invFs)
|
||||
val q = 0.5f * 40f.pow(c[CH_RESO])
|
||||
val k = 1f / q
|
||||
sa1 = 1f / (1f + g * (g + k)); sa2 = g * sa1; sa3 = g * sa2
|
||||
when (type) {
|
||||
1 -> { sc0 = 0f; sc1 = k * (1.4f + 0.12f * sqrt(hc)) * sqrt(q); sc2 = 0f } // band, peak at 1
|
||||
2 -> { // high, made up
|
||||
val mk = sqrt(0.75f * hc).coerceIn(1f, 4f)
|
||||
sc0 = mk; sc1 = -k * mk; sc2 = -mk
|
||||
}
|
||||
else -> { sc0 = 0f; sc1 = 0f; sc2 = 1f }
|
||||
}
|
||||
cgain *= 1f / sqrt(1f + 0.22f * (q - 0.5f))
|
||||
}
|
||||
if (fresh) { sp0 = sc0; sp1 = sc1; sp2 = sc2 }
|
||||
}
|
||||
|
||||
// PARTIALS: frequencies (stretched, drifting), amplitudes (tilt, odd, drift), levelled
|
||||
if (c[PARTIALS] > 0.001f) {
|
||||
val pe = (c[TILT] - bright * 0.7f).coerceIn(0f, 1f)
|
||||
val pw = 0.3f + 2.7f * pe
|
||||
val sx = 1f + 0.12f * c[STRETCH]
|
||||
val fnp = 2f + 6f * full
|
||||
var nmax = fnp.toInt()
|
||||
var top = fnp - nmax
|
||||
if (top > 0.001f) nmax++ else top = 1f
|
||||
if (nmax > NP) nmax = NP
|
||||
var sum2 = 0f
|
||||
var n = 0
|
||||
for (j in 0 until nmax) {
|
||||
val fk = f * 2f.pow(sx * LOG2K[j] + mw * 5f * ouPf[j] / 1200f)
|
||||
if (fk > minOf(15000f, nyq)) break
|
||||
val w = TWO_PI * fk * invFs
|
||||
// the oscillator: back to its phasor, renormalised, on with this block's step (a half-angle rotation)
|
||||
val cw0 = cos(w); val sn = sin(w)
|
||||
val q = sqrt(2f * (1f + cw0))
|
||||
var x = px[j]
|
||||
var y = (py[j] - x * ps[j]) * pc[j]
|
||||
val gn = 1.5f - 0.5f * (x * x + y * y)
|
||||
val shh = sn / q
|
||||
val chh = (1f + cw0) / q
|
||||
x *= gn; y *= gn
|
||||
ps[j] = shh
|
||||
pc[j] = q / (1f + cw0)
|
||||
px[j] = x
|
||||
py[j] = y * chh + x * shh
|
||||
var a = 2f.pow(-pw * LOG2K[j] + 1.6f * (m + 2.5f * c[EVOLVE]) * ouPa[j])
|
||||
if (j and 1 == 1) a *= 1f - c[ODD] // the even harmonics (2, 4 ...)
|
||||
val pk = c[PEAK]
|
||||
if (pk > 0.001f) { // the formant
|
||||
val d = (sx * LOG2K[j] - c[FORMANT]) * (1.5f + 4f * pk)
|
||||
val fl = 1f - 0.97f * pk
|
||||
a *= fl + (1f + 7f * pk - fl) / (1f + d * d)
|
||||
}
|
||||
if (j + 1 == nmax) a *= top
|
||||
pa[j] = a
|
||||
sum2 += a * a
|
||||
n++
|
||||
}
|
||||
np = n
|
||||
val norm = 0.6f / sqrt(sum2 + 1e-9f)
|
||||
for (j in 0 until n) pa[j] *= norm
|
||||
if (c[BEAT] > 0.001f) { // BEAT: each one pulsing at its own pace
|
||||
val bt = c[BEAT]
|
||||
val dep = minOf(1f, bt * 1.4f)
|
||||
val hz = 0.15f + 5f * bt * bt
|
||||
for (j in 0 until n) {
|
||||
pbt[j] += hz * BEAT_SPREAD[j] * BLOCK * invFs
|
||||
pbt[j] -= floor(pbt[j])
|
||||
pa[j] *= (1f - dep * (0.5f + 0.5f * sin(TWO_PI * pbt[j]))) / (1f - 0.5f * dep)
|
||||
}
|
||||
}
|
||||
for (j in 0 until n) pda[j] = (pa[j] - pac[j]) / BLOCK
|
||||
for (j in n until NP) { pac[j] = 0f; pda[j] = 0f }
|
||||
}
|
||||
|
||||
// BREATH: resonators at the note x ratios (COLOR between the sets), levelled so
|
||||
// the loudness follows neither Q nor pitch: y = g x + b1 y1 - b2 y2
|
||||
if (c[BREATH] > 0.001f) {
|
||||
var col = c[COLOR].coerceIn(0f, 1f) * 2f
|
||||
val lo = col < 1f
|
||||
val ra = RATIO_L2[if (lo) 0 else 1]
|
||||
val rb = RATIO_L2[if (lo) 1 else 2]
|
||||
if (!lo) col -= 1f
|
||||
val q = 2f * 300f.pow((c[RESONANCE] + mw * 0.1f * ouR[0]).coerceIn(0f, 1f))
|
||||
val sb = (0.55f + bright * 0.8f).coerceIn(0.05f, 1f)
|
||||
for (j in 0 until NR) {
|
||||
val l2 = ra[j] + (rb[j] - ra[j]) * col + mw * 12f * ouR[j] / 1200f
|
||||
val fr = (f * c[BR_PITCH] * 2f.pow(l2)).coerceIn(20f, minOf(16000f, nyq))
|
||||
val r = exp(-PI * fr / q * invFs).toDouble()
|
||||
val b1 = 2.0 * r * cos(TWO_PI * fr * invFs).toDouble()
|
||||
val b2 = r * r
|
||||
val ng = (1.0 + b2) / ((1.0 - b2) * ((1.0 + b2) * (1.0 + b2) - b1 * b1)) // the sum of h^2
|
||||
rb1[j] = b1.toFloat(); rb2[j] = b2.toFloat()
|
||||
rg[j] = (0.4 * sqrt(3.0 / ng)).toFloat() * (if (j == 0) 1f else sb * (if (j == 2) sb else 1f))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------- chunk ---
|
||||
private fun renderChunk() {
|
||||
if (chunk == 0) block()
|
||||
chunk = (chunk + 1) % (BLOCK / CH)
|
||||
val c = cur
|
||||
env += ((if (gate) 1f else 0f) - env) * (if (gate) ka else kr)
|
||||
val level = (1f - c[VELOCITY] + c[VELOCITY] * vel) * VOICE_G
|
||||
val g1 = if (!gate && env < TAIL_END) 0f else level * env * env
|
||||
var g = lvl
|
||||
lvl = g1
|
||||
val dg = (g1 - g) / CH
|
||||
bc.fill(0f); ba.fill(0f); bb.fill(0f)
|
||||
val full = c[BODY]
|
||||
if (c[CHOIR] > 0.001f) choirChunk(0.35f * c[CHOIR] * (0.3f + 0.7f * full))
|
||||
if (c[PARTIALS] > 0.001f) partialsChunk(c[PARTIALS] * (0.7f + 0.3f * full))
|
||||
if (c[BREATH] > 0.001f) { breathNoise(); souffleChunk(c[BREATH] * full * full) }
|
||||
|
||||
// a guard: a voice gone wild or not a number never reaches the mix; it starts again
|
||||
val z = bc[CH - 1] + ba[CH - 1] + bb[CH - 1] + bc[CH / 2] + ba[CH / 2] + bb[CH / 2]
|
||||
if (!(z > -1e4f && z < 1e4f)) {
|
||||
bc.fill(0f); ba.fill(0f); bb.fill(0f)
|
||||
start()
|
||||
prime = false
|
||||
}
|
||||
|
||||
var dcs = dcState
|
||||
var th = thinState
|
||||
for (i in 0 until CH) {
|
||||
g += dg
|
||||
var x = (bc[i] + SIDE_GAIN * (ba[i] + bb[i])) * g
|
||||
dcs += kdc * (x - dcs)
|
||||
x -= dcs
|
||||
if (driveOn) x = tanh(x * dk) * dmake
|
||||
if (thin) { th += thinA * (x - th); x -= th }
|
||||
out[i] = x * OUT_GAIN
|
||||
}
|
||||
dcState = flush(dcs)
|
||||
thinState = if (thin) flush(th) else 0f
|
||||
if (g1 == 0f && !gate && env < TAIL_END) ending = true
|
||||
}
|
||||
|
||||
/** CHOIR's saws into the two sides (left: centre + -detune, right: centre + +detune),
|
||||
* then their filters into [ba] / [bb] at [g0]. */
|
||||
private fun choirChunk(g0: Float) {
|
||||
val svf = cur[CH_RESO] > 0.002f || type != 0
|
||||
val sq = cur[SHAPE] > 0.002f
|
||||
val soft = cur[SOFT] > 0.002f
|
||||
if (svf != csvf) { // the other filter: the low-pass's state carried over
|
||||
clp[0] = if (svf) 0f else clp[1]
|
||||
clp[2] = if (svf) 0f else clp[3]
|
||||
csvf = svf
|
||||
}
|
||||
val g = g0 * cgain
|
||||
var p0 = cph[0]; var p1 = cph[1]; var p2 = cph[2]
|
||||
val d0 = cdt[0]; val d1 = cdt[1]; val d2 = cdt[2]
|
||||
val i0 = cid[0]; val i1 = cid[1]; val i2 = cid[2]
|
||||
val sh = csh; val w0 = cw[0]; val w1 = cw[1]; val w2 = cw[2]
|
||||
for (i in 0 until CH) {
|
||||
p0 += d0; if (p0 >= 1f) p0 -= 1f
|
||||
p1 += d1; if (p1 >= 1f) p1 -= 1f
|
||||
p2 += d2; if (p2 >= 1f) p2 -= 1f
|
||||
var s0 = 2f * p0 - 1f - blep(p0, d0, i0)
|
||||
var s1 = 2f * p1 - 1f - blep(p1, d1, i1)
|
||||
var s2 = 2f * p2 - 1f - blep(p2, d2, i2)
|
||||
if (sq) { // each minus its share of itself a width on: a pulse
|
||||
var q0 = p0 + w0; if (q0 >= 1f) q0 -= 1f
|
||||
var q1 = p1 + w1; if (q1 >= 1f) q1 -= 1f
|
||||
var q2 = p2 + w2; if (q2 >= 1f) q2 -= 1f
|
||||
s0 -= sh * (2f * q0 - 1f - blep(q0, d0, i0))
|
||||
s1 -= sh * (2f * q1 - 1f - blep(q1, d1, i1))
|
||||
s2 -= sh * (2f * q2 - 1f - blep(q2, d2, i2))
|
||||
}
|
||||
if (soft) { xc[i] = s0; x0[i] = s1; x1[i] = s2 } else { x0[i] = s0 + s1; x1[i] = s0 + s2 }
|
||||
}
|
||||
cph[0] = p0; cph[1] = p1; cph[2] = p2
|
||||
if (soft) {
|
||||
// SOFT: integrated once (the square a triangle), then twice (near a sine), each saw at
|
||||
// its own gain (its fundamental kept), a leak at a sixth of it; crossfaded by quarter turns
|
||||
val kc = TWO_PI * d0; val ks = TWO_PI * d1
|
||||
val lc = 1f - 0.15f * kc; val ls = 1f - 0.15f * ks
|
||||
val u = cur[SOFT]
|
||||
val a0: Float; val a1: Float; val a2: Float
|
||||
if (u < 0.5f) { a0 = cos(PI * u); a1 = sin(PI * u); a2 = 0f }
|
||||
else { a0 = 0f; a1 = cos(PI * (u - 0.5f)); a2 = sin(PI * (u - 0.5f)) }
|
||||
val c1 = a1 * kc; val c2 = a2 * kc * kc; val e1 = a1 * ks; val e2 = a2 * ks * ks
|
||||
var cA = cint[0]; var cB = cint[1]; var l1 = cint[2]; var l2 = cint[3]; var q1 = cint[4]; var q2 = cint[5]
|
||||
for (j in 0 until CH) {
|
||||
val vc = xc[j]; val vl = x0[j]; val vr = x1[j]
|
||||
cA = lc * cA + vc; cB = lc * cB + cA
|
||||
l1 = ls * l1 + vl; l2 = ls * l2 + l1
|
||||
q1 = ls * q1 + vr; q2 = ls * q2 + q1
|
||||
val cc = a0 * vc + c1 * cA + c2 * cB
|
||||
x0[j] = cc + a0 * vl + e1 * l1 + e2 * l2
|
||||
x1[j] = cc + a0 * vr + e1 * q1 + e2 * q2
|
||||
}
|
||||
if (!(cB > -1e9f && cB < 1e9f && l2 > -1e9f && l2 < 1e9f && q2 > -1e9f && q2 < 1e9f)) {
|
||||
cA = 0f; cB = 0f; l1 = 0f; l2 = 0f; q1 = 0f; q2 = 0f
|
||||
}
|
||||
cint[0] = cA; cint[1] = cB; cint[2] = l1; cint[3] = l2; cint[4] = q1; cint[5] = q2
|
||||
} else {
|
||||
cint.fill(0f)
|
||||
}
|
||||
if (svf) { // the type's mix, gliding to sc over the chunk
|
||||
val o0 = sp0 * g; val o1 = sp1 * g; val o2 = sp2 * g
|
||||
val e0 = (sc0 - sp0) * g / CH; val e1 = (sc1 - sp1) * g / CH; val e2 = (sc2 - sp2) * g / CH
|
||||
svfRun(ba, x0, 0, o0, o1, o2, e0, e1, e2)
|
||||
svfRun(bb, x1, 2, o0, o1, o2, e0, e1, e2)
|
||||
} else {
|
||||
val a = ca
|
||||
var l1 = clp[0]; var l2 = clp[1]; var q1 = clp[2]; var q2 = clp[3]
|
||||
for (i in 0 until CH) {
|
||||
l1 += a * (x0[i] - l1); l2 += a * (l1 - l2)
|
||||
q1 += a * (x1[i] - q1); q2 += a * (q1 - q2)
|
||||
ba[i] += l2 * g; bb[i] += q2 * g
|
||||
}
|
||||
clp[0] = l1; clp[1] = l2; clp[2] = q1; clp[3] = q2
|
||||
}
|
||||
sp0 = sc0; sp1 = sc1; sp2 = sc2
|
||||
}
|
||||
|
||||
private fun svfRun(b: FloatArray, x: FloatArray, s: Int, o0i: Float, o1i: Float, o2i: Float,
|
||||
e0: Float, e1: Float, e2: Float) {
|
||||
var s1 = clp[s]; var s2 = clp[s + 1]
|
||||
var o0 = o0i; var o1 = o1i; var o2 = o2i
|
||||
val a1 = sa1; val a2 = sa2; val a3 = sa3
|
||||
for (i in 0 until CH) {
|
||||
val v = x[i] - s2
|
||||
val t1 = a1 * s1 + a2 * v
|
||||
val t2 = s2 + a2 * s1 + a3 * v
|
||||
s1 = 2f * t1 - s1
|
||||
s2 = 2f * t2 - s2
|
||||
b[i] += o0 * x[i] + o1 * t1 + o2 * t2
|
||||
o0 += e0; o1 += e1; o2 += e2
|
||||
}
|
||||
clp[s] = s1; clp[s + 1] = s2
|
||||
}
|
||||
|
||||
/** The fundamental in the centre; harmonics 2, 4, 6, 8 to one side, 3, 5, 7 to the other. */
|
||||
private fun partialsChunk(g: Float) {
|
||||
for (j in 0 until np) {
|
||||
val bus = when (j) { 0 -> bc; 1, 3, 5, 7 -> ba; else -> bb }
|
||||
var x = px[j]; var y = py[j]
|
||||
val e = 2f * ps[j]
|
||||
var a = pac[j] * g
|
||||
val d = pda[j] * g
|
||||
for (i in 0 until CH) {
|
||||
x -= e * y
|
||||
y += e * x
|
||||
bus[i] += a * x
|
||||
a += d
|
||||
}
|
||||
px[j] = x; py[j] = y
|
||||
pac[j] += pda[j] * CH
|
||||
}
|
||||
}
|
||||
|
||||
/** BREATH's source for the chunk: a hiss (white, variance 1/3) or GRAIN's sparse
|
||||
* grains (as loud on average); TONE darker (a low-pass) or brighter (a high-pass). */
|
||||
private fun breathNoise() {
|
||||
val grain = cur[GRAIN]
|
||||
if (grain < 0.002f) {
|
||||
for (i in 0 until CH) nz[i] = randf()
|
||||
} else {
|
||||
val p = 2f.pow(-13f * grain) * r44
|
||||
val amp = minOf(16f, sqrt(1f / (3f * 0.5833f * p)))
|
||||
val th = (p * 16777216f).toInt()
|
||||
for (i in 0 until CH) {
|
||||
val r = nextRand()
|
||||
val a = amp * (0.5f + (r and 255) * (0.5f / 255f))
|
||||
nz[i] = if ((r ushr 8) < th) (if (r and 256 != 0) a else -a) else 0f
|
||||
}
|
||||
}
|
||||
val t = cur[TONE]
|
||||
if (t < -0.01f) {
|
||||
val fc = 16000f * (300f / 16000f).pow(-t)
|
||||
val a = 1f - exp(-TWO_PI * minOf(fc, nyq) * invFs)
|
||||
val gn = sqrt((2f - a) / a)
|
||||
var l = nlp
|
||||
for (i in 0 until CH) { l += a * (nz[i] - l); nz[i] = l * gn }
|
||||
nlp = flush(l)
|
||||
} else if (t > 0.01f) {
|
||||
val fc = 20f * 150f.pow(t)
|
||||
val a = 1f - exp(-TWO_PI * fc * invFs)
|
||||
val gn = 1f / sqrt(1f + a / (2f - a) - 2f * a)
|
||||
var l = nlp
|
||||
for (i in 0 until CH) { l += a * (nz[i] - l); nz[i] = (nz[i] - l) * gn }
|
||||
nlp = flush(l)
|
||||
}
|
||||
}
|
||||
|
||||
private fun souffleChunk(g: Float) {
|
||||
val air = cur[AIR]
|
||||
val ga = air * 0.7f * g
|
||||
for (j in 0 until NR) {
|
||||
val bus = when (j) { 0 -> bc; 1 -> ba; else -> bb }
|
||||
var y1 = r1[j]; var y2 = r2[j]
|
||||
val b1 = rb1[j]; val b2 = rb2[j]; val gg = rg[j] * g
|
||||
val withAir = j == 0 && air > 0.001f
|
||||
for (i in 0 until CH) {
|
||||
val y = gg * nz[i] + b1 * y1 - b2 * y2
|
||||
y2 = y1; y1 = y
|
||||
bus[i] += if (withAir) y + ga * nz[i] else y
|
||||
}
|
||||
if (y1 > -1e-20f && y1 < 1e-20f && y2 > -1e-20f && y2 < 1e-20f) { y1 = 0f; y2 = 0f }
|
||||
r1[j] = y1; r2[j] = y2
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------- helpers ---
|
||||
private fun brK(k: Float): Float = 1f - (1f - k).pow(r44)
|
||||
|
||||
/** 32-bit xorshift: the noise and the drift. */
|
||||
private fun nextRand(): Int {
|
||||
var x = rng
|
||||
x = x xor (x shl 13)
|
||||
x = x xor (x ushr 17)
|
||||
x = x xor (x shl 5)
|
||||
rng = x
|
||||
return x
|
||||
}
|
||||
|
||||
private fun randf(): Float = nextRand() * (1f / 2147483648f) // -1..1
|
||||
private fun urand(): Float = (nextRand() ushr 8) * (1f / 16777216f) // 0..1
|
||||
|
||||
companion object {
|
||||
const val NP = 8
|
||||
const val NR = 3
|
||||
const val CH = 64
|
||||
const val BLOCK = 256
|
||||
private const val VOICE_G = 0.2f
|
||||
private const val TAIL_END = 0.01f // a released voice ends here: its gain (env^2) 80 dB down
|
||||
private const val SIDE_GAIN = 0.70710678f // the stereo sides summed into mono at -3 dB
|
||||
/** Brings a held FRASIL voice to about the level of a Sizzle synth note. */
|
||||
private const val OUT_GAIN = 2.2f
|
||||
private const val TIDE_SMOOTH_S = 0.5f
|
||||
private const val PI = 3.14159265f
|
||||
private const val TWO_PI = 6.2831853f
|
||||
private val SIDE = floatArrayOf(0f, -1f, 1f)
|
||||
private val PWR = floatArrayOf(0.21f, 0.29f, 0.37f) // the widths' sways, Hz
|
||||
private val LOG2K = floatArrayOf(0f, 1f, 1.5849625f, 2f, 2.3219281f, 2.5849625f, 2.8073549f, 3f)
|
||||
private val BEAT_SPREAD = floatArrayOf(0.55f, 1.25f, 0.8f, 1.45f, 0.65f, 1.1f, 0.95f, 1.35f)
|
||||
/** BREATH's ratios (log2), COLOR across them: harmonic, odd, bell (a free bar's modes). */
|
||||
private val RATIO_L2: Array<FloatArray> = arrayOf(
|
||||
floatArrayOf(1f, 2f, 3f), floatArrayOf(1f, 3f, 5f), floatArrayOf(1f, 2.756f, 5.404f),
|
||||
).map { r -> FloatArray(NR) { (ln(r[it].toDouble()) / ln(2.0)).toFloat() } }.toTypedArray()
|
||||
|
||||
private var seeds = 0x2545F491
|
||||
private fun nextSeed(): Int {
|
||||
seeds += 0x6D2B79F5
|
||||
return if (seeds == 0) 0x2545F491 else seeds
|
||||
}
|
||||
|
||||
/** A blep with the step's inverse. */
|
||||
private fun blep(t0: Float, dt: Float, idt: Float): Float {
|
||||
if (t0 < dt) { val t = t0 * idt; return t + t - t * t - 1f }
|
||||
if (t0 > 1f - dt) { val t = (t0 - 1f) * idt; return t * t + t + t + 1f }
|
||||
return 0f
|
||||
}
|
||||
|
||||
private fun flush(x: Float): Float = if (x > -1e-20f && x < 1e-20f) 0f else x
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
package cc.tiredsysadmin.android.sizzle.io
|
||||
|
||||
import cc.tiredsysadmin.android.sizzle.audio.SampleStore
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs
|
||||
import cc.tiredsysadmin.android.sizzle.model.AmbiencePresets
|
||||
import cc.tiredsysadmin.android.sizzle.model.MixTightenerPresets
|
||||
import cc.tiredsysadmin.android.sizzle.model.NesSynthPresets
|
||||
@@ -52,6 +53,9 @@ class PresetLibrary(private val baseDir: File) {
|
||||
seedType(ToolboxType.NES_SYNTH, NesSynthPresets.FACTORY.map { it.name }) { slot, name ->
|
||||
NesSynthPresets.apply(slot, NesSynthPresets.FACTORY.first { it.name == name })
|
||||
}
|
||||
seedType(ToolboxType.MORPH_PAD, FrasilDefs.FACTORY.map { it.name }) { slot, name ->
|
||||
FrasilDefs.FACTORY.first { it.name == name }.values.forEach { (k, v) -> slot.set(k, v) }
|
||||
}
|
||||
seedType(ToolboxType.MIX_TIGHTENER, MixTightenerPresets.FACTORY.map { it.name }) { slot, name ->
|
||||
MixTightenerPresets.apply(slot, MixTightenerPresets.FACTORY.first { it.name == name })
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
package cc.tiredsysadmin.android.sizzle.model
|
||||
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs
|
||||
|
||||
/**
|
||||
* The third tab is a toolbox of 16 slots. Every slot can hold one instrument OR
|
||||
* one effect. Rather than a separate class per device (which would be a lot of
|
||||
@@ -113,6 +115,12 @@ enum class ToolboxType(
|
||||
),
|
||||
),
|
||||
|
||||
// FRASIL's drone voice (MPL-2.0, see the frasil package): three layers (a detuned
|
||||
// saw choir, drifting additive partials, noise through resonators) that evolve
|
||||
// while held, morphing toward one of FRASIL's 36 drone sounds. Rendered by
|
||||
// FrasilVoice inside SynthVoice; edited by the paged editor in ToolboxScreen.
|
||||
MORPH_PAD(ToolboxKind.INSTRUMENT, "Morph Pad", FrasilDefs.PARAMS),
|
||||
|
||||
// ---------- Effects ----------
|
||||
ARPEGGIATOR(
|
||||
ToolboxKind.EFFECT, "MIDI Arpeggiator",
|
||||
|
||||
@@ -53,9 +53,11 @@ import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.font.FontFamily
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.Dp
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.core.content.FileProvider
|
||||
import cc.tiredsysadmin.android.sizzle.frasil.FrasilDefs
|
||||
import cc.tiredsysadmin.android.sizzle.model.ParamSpec
|
||||
import cc.tiredsysadmin.android.sizzle.model.ToolboxSlot
|
||||
import cc.tiredsysadmin.android.sizzle.model.ToolboxType
|
||||
@@ -294,7 +296,8 @@ private fun ParamEditor(vm: AppViewModel, slot: ToolboxSlot, onClose: () -> Unit
|
||||
// The SoundFont and synth editors pin an audition keyboard at the bottom (same as
|
||||
// the toolbox / tracker keyboards), so their controls scroll in a weighted region
|
||||
// above the keyboard rather than the whole editor scrolling as one.
|
||||
val pinnedKeyboard = type == ToolboxType.SOUNDFONT || type == ToolboxType.NES_SYNTH
|
||||
val pinnedKeyboard = type == ToolboxType.SOUNDFONT || type == ToolboxType.NES_SYNTH ||
|
||||
type == ToolboxType.MORPH_PAD
|
||||
// The Sampler pins its 16-pad grid at the bottom in that same region, so the
|
||||
// playable pads are the same height as the keyboards across every instrument editor.
|
||||
val pinnedBottom = pinnedKeyboard || type == ToolboxType.SAMPLER
|
||||
@@ -340,6 +343,7 @@ private fun ParamEditor(vm: AppViewModel, slot: ToolboxSlot, onClose: () -> Unit
|
||||
ToolboxType.SOUNDFONT -> SoundFontEditor(vm, slot)
|
||||
ToolboxType.DELAY -> DelayEditor(vm, slot)
|
||||
ToolboxType.AMBIENCE -> AmbiencePager(vm, slot)
|
||||
ToolboxType.MORPH_PAD -> SectionPager(vm, slot, FrasilDefs.SECTIONS, 300.dp)
|
||||
ToolboxType.LFO -> {
|
||||
ParamGrid(vm, slot, type.params)
|
||||
LfoTargetPicker(vm, slot)
|
||||
@@ -465,25 +469,30 @@ internal fun ParamGrid(vm: AppViewModel, slot: ToolboxSlot, params: List<ParamSp
|
||||
* dot pagination below. Each page uses the same two-column [ParamGrid]. Works the
|
||||
* same in portrait and landscape (the editor overlay is full-width in both). */
|
||||
@Composable
|
||||
private fun AmbiencePager(vm: AppViewModel, slot: ToolboxSlot) {
|
||||
private fun AmbiencePager(vm: AppViewModel, slot: ToolboxSlot) =
|
||||
SectionPager(vm, slot, AMBIENCE_SECTIONS.map { it.title to it.keys }, 210.dp)
|
||||
|
||||
/** A device's parameters as swipeable pages of (title, keys), with dot pagination. */
|
||||
@Composable
|
||||
private fun SectionPager(vm: AppViewModel, slot: ToolboxSlot, sections: List<Pair<String, List<String>>>, height: Dp) {
|
||||
val c = LocalRetro.current
|
||||
val byKey = (slot.type ?: return).params.associateBy { it.key }
|
||||
val pagerState = rememberPagerState(pageCount = { AMBIENCE_SECTIONS.size })
|
||||
val pagerState = rememberPagerState(pageCount = { sections.size })
|
||||
val scope = rememberCoroutineScope()
|
||||
|
||||
Column(Modifier.fillMaxWidth()) {
|
||||
HorizontalPager(
|
||||
state = pagerState,
|
||||
modifier = Modifier.fillMaxWidth().height(210.dp),
|
||||
modifier = Modifier.fillMaxWidth().height(height),
|
||||
pageSpacing = 12.dp,
|
||||
) { page ->
|
||||
val section = AMBIENCE_SECTIONS[page]
|
||||
val (title, keys) = sections[page]
|
||||
Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.spacedBy(6.dp)) {
|
||||
Text(
|
||||
section.title, color = c.accent,
|
||||
title, color = c.accent,
|
||||
fontFamily = FontFamily.Monospace, fontSize = 12.sp,
|
||||
)
|
||||
ParamGrid(vm, slot, section.keys.mapNotNull { byKey[it] })
|
||||
ParamGrid(vm, slot, keys.mapNotNull { byKey[it] })
|
||||
}
|
||||
}
|
||||
// Dot pagination: tap a dot (or swipe the page) to jump to that section.
|
||||
@@ -491,7 +500,7 @@ private fun AmbiencePager(vm: AppViewModel, slot: ToolboxSlot) {
|
||||
Modifier.fillMaxWidth().padding(top = 8.dp),
|
||||
horizontalArrangement = Arrangement.Center,
|
||||
) {
|
||||
for (i in AMBIENCE_SECTIONS.indices) {
|
||||
for (i in sections.indices) {
|
||||
val active = i == pagerState.currentPage
|
||||
Box(
|
||||
Modifier
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
package cc.tiredsysadmin.android.sizzle.frasil
|
||||
|
||||
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.assertTrue
|
||||
import org.junit.Test
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.sqrt
|
||||
|
||||
class FrasilVoiceTest {
|
||||
|
||||
private val fs = 48_000
|
||||
|
||||
private fun slot(preset: String? = null, vararg extra: Pair<String, String>): ToolboxSlot =
|
||||
ToolboxSlot(0).apply {
|
||||
fill(ToolboxType.MORPH_PAD)
|
||||
preset?.let { n -> FrasilDefs.FACTORY.first { it.name == n }.values.forEach { (k, v) -> set(k, v) } }
|
||||
extra.forEach { (k, v) -> set(k, v) }
|
||||
}
|
||||
|
||||
private class Stats(val rms: Float, val peak: Float, val finite: Boolean)
|
||||
|
||||
private fun render(v: FrasilVoice, frames: Int): Stats {
|
||||
var sum = 0.0; var peak = 0f; var finite = true
|
||||
repeat(frames) {
|
||||
val x = v.render()
|
||||
if (!x.isFinite()) finite = false
|
||||
sum += x * x; peak = maxOf(peak, abs(x))
|
||||
}
|
||||
return Stats(sqrt(sum / frames).toFloat(), peak, finite)
|
||||
}
|
||||
|
||||
@Test fun everyFactorySoundPlaysFiniteBoundedAndFreesAfterRelease() {
|
||||
assertEquals(36, FrasilDefs.FACTORY.size)
|
||||
for (p in FrasilDefs.FACTORY) {
|
||||
val v = FrasilVoice(fs)
|
||||
v.noteOn(48, 0.8f, FrasilPatch.from(slot(p.name)))
|
||||
val held = render(v, fs * 3)
|
||||
assertTrue("${p.name} finite", held.finite)
|
||||
assertTrue("${p.name} sounds (rms ${held.rms})", held.rms > 0.003f)
|
||||
// Sparse GRAIN clicks ring the resonators past the steady level (FRASIL's crackle);
|
||||
// the master limiter takes those. The body of every sound stays well under.
|
||||
assertTrue("${p.name} peak ${held.peak}", held.peak < 1.5f)
|
||||
assertTrue("${p.name} rms ${held.rms}", held.rms < 0.5f)
|
||||
println("${p.name}: rms ${"%.3f".format(held.rms)} peak ${"%.3f".format(held.peak)}")
|
||||
v.noteOff()
|
||||
// RELEASE tops out at 60 s (a time constant a third of it): the tail ends well inside 70 s.
|
||||
var frames = 0
|
||||
while (v.isActive && frames < fs * 70) { v.render(); frames++ }
|
||||
assertFalse("${p.name} frees after its release", v.isActive)
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun pitchFollowsTheNote() {
|
||||
// STILL: partials alone, darkest tilt: nearly a sine at the note.
|
||||
val v = FrasilVoice(fs)
|
||||
v.noteOn(69, 1f, FrasilPatch.from(slot("Still", "motion" to "0.0")))
|
||||
render(v, fs) // past the attack
|
||||
var prev = v.render(); var crossings = 0; val n = fs * 2
|
||||
repeat(n) { val x = v.render(); if (prev < 0f && x >= 0f) crossings++; prev = x }
|
||||
assertEquals(440.0, crossings / 2.0, 3.0)
|
||||
}
|
||||
|
||||
@Test fun morphTimeCarriesAHeldNoteToTheTarget() {
|
||||
val glacier = FrasilDefs.factoryEngine("Glacier")!!.first
|
||||
val v = FrasilVoice(fs)
|
||||
v.noteOn(60, 1f, FrasilPatch.from(slot("Mist", FrasilDefs.MORPH_TO to "Glacier", FrasilDefs.MORPH_TIME to "1.0")))
|
||||
render(v, fs / 100)
|
||||
assertEquals(76 / 127f, v.valueForTest(FrasilDefs.CHOIR), 0.02f) // starts at Mist
|
||||
render(v, fs + fs / 5)
|
||||
assertEquals(glacier[FrasilDefs.CHOIR], v.valueForTest(FrasilDefs.CHOIR), 0.01f)
|
||||
assertEquals(glacier[FrasilDefs.STRETCH], v.valueForTest(FrasilDefs.STRETCH), 0.01f)
|
||||
}
|
||||
|
||||
@Test fun manualMorphSitsBetweenTheTwo() {
|
||||
val glacier = FrasilDefs.factoryEngine("Glacier")!!.first
|
||||
val v = FrasilVoice(fs)
|
||||
v.noteOn(60, 1f, FrasilPatch.from(slot("Mist", FrasilDefs.MORPH_TO to "Glacier", FrasilDefs.MORPH to "0.5")))
|
||||
render(v, fs / 2)
|
||||
assertEquals((76 / 127f + glacier[FrasilDefs.CHOIR]) / 2f, v.valueForTest(FrasilDefs.CHOIR), 0.01f)
|
||||
}
|
||||
|
||||
@Test fun aHeldPadIsAboutAsLoudAsASynthNote() {
|
||||
val pad = SynthVoice(fs).apply { noteOn(60, 100, SynthPatch.forPad(FrasilPatch.from(slot("Mist")))) }
|
||||
val syn = SynthVoice(fs).apply { noteOn(60, 100, SynthPatch.DEFAULT) }
|
||||
fun rms(v: SynthVoice): Float {
|
||||
repeat(fs * 2) { v.render() }
|
||||
var s = 0.0; repeat(fs) { val x = v.render(); s += x * x }
|
||||
return sqrt(s / fs).toFloat()
|
||||
}
|
||||
val ratio = rms(pad) / rms(syn)
|
||||
println("pad/synth rms ratio $ratio")
|
||||
assertTrue("pad/synth level ratio $ratio within -6..+6 dB", ratio in 0.5f..2f)
|
||||
}
|
||||
|
||||
@Test fun synthVoicePlaysPadThenSynthAgain() {
|
||||
val v = SynthVoice(fs)
|
||||
v.noteOn(57, 100, SynthPatch.forPad(FrasilPatch.from(slot("Organ"))))
|
||||
assertTrue(v.isActive && v.isGated)
|
||||
var e = 0.0; repeat(fs) { val x = v.render(); e += x * x }
|
||||
assertTrue(e > 0.0)
|
||||
v.noteOff()
|
||||
assertFalse(v.isGated)
|
||||
v.noteOn(57, 100, SynthPatch.DEFAULT) // the slot became a synth
|
||||
var e2 = 0.0; repeat(fs / 4) { val x = v.render(); e2 += x * x }
|
||||
assertTrue(e2 > 0.0)
|
||||
v.kill()
|
||||
assertFalse(v.isActive)
|
||||
}
|
||||
|
||||
@Test fun cpuBudgetForFullPolyphony() {
|
||||
// 4 channels x 8 voices of the heaviest kind of sound (all three layers, SOFT, BEAT, a resonant filter).
|
||||
val p = FrasilPatch.from(slot("Prism", "choir" to "1.0", "creso" to "0.5", "soft" to "0.5", "breath" to "0.8"))
|
||||
val voices = Array(32) { FrasilVoice(fs).also { v -> v.noteOn(36 + it, 0.9f, p) } }
|
||||
repeat(fs / 2) { for (v in voices) v.render() } // warm up the JIT
|
||||
val seconds = 2
|
||||
val t0 = System.nanoTime()
|
||||
repeat(fs * seconds) { for (v in voices) v.render() }
|
||||
val load = (System.nanoTime() - t0) / 1e9 / seconds
|
||||
println("32 Morph Pad voices: ${"%.1f".format(load * 100)} % of real time on this host")
|
||||
assertTrue("32 voices must render faster than real time on the host ($load)", load < 1.0)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user