Wavetable editor fits above the keyboard: one oscillator per tab, table drawn in its card (0.36.1)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-14 05:34:41 +01:00
co-authored by Claude Opus 5.5
parent 3170e8dd7a
commit bc257203e3
8 changed files with 120 additions and 73 deletions
@@ -19,20 +19,27 @@ object DeviceLayouts {
fun pagesFor(type: ToolboxType): List<Page> = when (type) {
ToolboxType.WAVETABLE -> listOf(
Page("Osc", listOf(
Panel("Oscillator 1", Tone.SOURCE, osc(1)),
Panel("Oscillator 2", Tone.SOURCE, osc(2)),
// One oscillator per tab, each drawing its own table, so every page fits
// above the keyboard without scrolling. A loaded WAV lands on oscillator 1,
// so LOAD TABLE and its frame size sit on that card.
Page("Osc 1", listOf(
Panel("Oscillator 1", Tone.SOURCE, listOf("o1table", "wtframe") + osc(1).drop(1), Viz.Wavetable(1),
labels = mapOf("wtframe" to "Frame"), action = { vm, slot -> LoadTableAction(vm, slot) }),
)),
Page("Osc 2", listOf(
Panel("Oscillator 2", Tone.SOURCE, osc(2), Viz.Wavetable(2)),
)),
Page("Voice", listOf(
Panel("Voice", Tone.SOURCE, listOf("unison", "spread", "sub", "noise", "velsens")),
Panel("Table import", Tone.OUT, listOf("wtframe")),
)),
Page("Filter", listOf(
Panel("Filter", Tone.FILTER, listOf("ftype", "cutoff", "reso", "keytrack", "fenv", "drive"),
Viz.Filter("ftype", "cutoff", "reso"), labels = mapOf("ftype" to "Type", "fenv" to "Env Amt")),
)),
Page("Env", listOf(
Page("Amp", listOf(
Panel("Amp envelope", Tone.ENV, listOf("attack", "decay", "sustain", "release"), ADSR),
)),
Page("Mod", listOf(
Panel("Mod envelope", Tone.MOD, listOf("mattack", "mdecay", "msustain", "mrelease", "menvpos"),
Viz.Envelope("mattack", "mdecay", "msustain", "mrelease")),
)),
@@ -79,6 +79,8 @@ sealed interface Viz {
data class Filter(val type: String?, val cutoff: String, val reso: String) : Viz
/** Two cycles of the wave named by [wave]'s choice. */
data class Lfo(val wave: String) : Viz
/** The Wavetable oscillator [osc]'s table, frames stacked, the one at its position lit. */
data class Wavetable(val osc: Int) : Viz
}
/**
@@ -95,6 +97,8 @@ class Panel(
val labels: Map<String, String> = emptyMap(),
val faders: List<String>? = null,
val faderScale: Float = 1f,
/** A small action drawn at the right of the card's title (e.g. the Wavetable's LOAD TABLE). */
val action: (@Composable (AppViewModel, ToolboxSlot) -> Unit)? = null,
)
/** A tab of cards. */
@@ -131,22 +135,22 @@ fun DevicePanels(vm: AppViewModel, slot: ToolboxSlot, pages: List<Page>) {
@Composable
private fun PageTabs(pages: List<Page>, selected: Int, onSelect: (Int) -> Unit) {
val c = LocalRetro.current
Row(
Modifier.fillMaxWidth().horizontalScroll(rememberScrollState()),
horizontalArrangement = Arrangement.spacedBy(4.dp),
) {
// Tabs share the width (up to seven fit a phone), so none hides off-screen.
Row(Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.spacedBy(4.dp)) {
pages.forEachIndexed { i, p ->
val on = i == selected
val tone = toneColor(p.panels.first().tone)
Box(
Modifier
.weight(1f)
.border(1.dp, if (on) tone else c.grid, RectangleShape)
.background(if (on) tone.copy(alpha = 0.22f) else c.surface)
.clickable { onSelect(i) }
.padding(horizontal = 12.dp, vertical = 7.dp),
.padding(horizontal = 2.dp, vertical = 7.dp),
contentAlignment = Alignment.Center,
) {
Text(p.title.uppercase(), color = if (on) tone else c.textDim,
fontFamily = FontFamily.Monospace, fontSize = 12.sp)
fontFamily = FontFamily.Monospace, fontSize = 12.sp, maxLines = 1, softWrap = false)
}
}
}
@@ -174,9 +178,13 @@ fun PanelCard(vm: AppViewModel, slot: ToolboxSlot, panel: Panel, rev: Int = vm.r
Box(Modifier.size(8.dp).background(tone))
Spacer(Modifier.width(6.dp))
Text(panel.title.uppercase(), color = tone, fontFamily = FontFamily.Monospace, fontSize = 11.sp,
letterSpacing = 1.sp)
letterSpacing = 1.sp, modifier = Modifier.weight(1f))
panel.action?.invoke(vm, slot)
}
panel.viz?.let {
if (it is Viz.Wavetable) WavetablePreview(slot, it.osc, tone, rev)
else VizCanvas(slot, it, byKey, tone, rev)
}
panel.viz?.let { VizCanvas(slot, it, byKey, tone, rev) }
if (panel.faders != null) {
FaderBank(vm, slot, specs, panel, tone, rev)
@@ -368,6 +376,7 @@ private fun VizCanvas(slot: ToolboxSlot, viz: Viz, byKey: Map<String, ParamSpec>
drawLine(c.grid, Offset(0f, zeroY), Offset(w, zeroY), strokeWidth = 1f)
drawLine(c.grid, Offset(fc * w, top), Offset(fc * w, bottom), strokeWidth = 1f)
}
is Viz.Wavetable -> Unit // drawn by WavetablePreview
is Viz.Lfo -> {
val name = choice(viz.wave).lowercase()
val n = 160
@@ -350,7 +350,7 @@ private fun ParamEditor(vm: AppViewModel, slot: ToolboxSlot, onClose: () -> Unit
// The Synth / Sampler / SoundFont / Delay / tape and glitch effects have
// bespoke editors; everything else is grouped cards ([DevicePanels]), with
// a source strip on top for the Wavetable and Grain Cloud and the LFO's
// a source strip on top for the Grain Cloud and the LFO's
// target picker below.
when (type) {
ToolboxType.NES_SYNTH -> NesSynthEditor(vm, slot)
@@ -360,10 +360,6 @@ private fun ParamEditor(vm: AppViewModel, slot: ToolboxSlot, onClose: () -> Unit
ToolboxType.SAMPLER -> SamplerEditor(vm, slot, samplerPad, onSelectPad = { samplerPad = it })
ToolboxType.SOUNDFONT -> SoundFontEditor(vm, slot)
ToolboxType.DELAY -> DelayEditor(vm, slot)
ToolboxType.WAVETABLE -> {
WavetableEditor(vm, slot)
DevicePanels(vm, slot, DeviceLayouts.pagesFor(type))
}
ToolboxType.GRAIN_CLOUD -> {
GrainCloudEditor(vm, slot)
DevicePanels(vm, slot, DeviceLayouts.pagesFor(type))
@@ -7,10 +7,12 @@ import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
@@ -21,28 +23,76 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import cc.tiredsysadmin.android.sizzle.model.ToolboxSlot
import cc.tiredsysadmin.android.sizzle.ui.AppViewModel
import cc.tiredsysadmin.android.sizzle.ui.components.RetroButton
import cc.tiredsysadmin.android.sizzle.ui.theme.LocalRetro
import cc.tiredsysadmin.android.sizzle.wavetable.WaveDefs
import cc.tiredsysadmin.android.sizzle.wavetable.WaveTables
/**
* The Wavetable's table strip: LOAD TABLE (a Serum / WaveEdit / power-of-two-frame
* WAV becomes the "Custom" table) and a view of oscillator 1's table, every frame
* stacked back to front with the one at its POSITION lit.
/*
* The Wavetable's own pieces inside its generic cards (see DeviceLayouts): each
* oscillator card draws that oscillator's table ([WavetablePreview], via
* [Viz.Wavetable]), and Oscillator 1's card carries LOAD TABLE ([LoadTableAction]),
* because a loaded WAV becomes the "Custom" table and is selected on oscillator 1.
*/
/** Every frame of oscillator [osc]'s table stacked back to front, the one at its POSITION lit. */
@Composable
fun WavetableEditor(vm: AppViewModel, slot: ToolboxSlot) {
fun WavetablePreview(slot: ToolboxSlot, osc: Int, tone: Color, rev: Int) {
val c = LocalRetro.current
val name = slot.string("o${osc}table", WaveDefs.TABLES[0])
val frame = slot.string("wtframe", WaveDefs.FRAME_SIZES[0]).toIntOrNull() ?: 0
val table = if (name == WaveTables.CUSTOM) WaveTables.forSample(slot.string(WaveDefs.SAMPLE_KEY), frame) else WaveTables.factory(name)
val pos = slot.float("o${osc}pos", 0f)
val caption = if (table == null) "$name (preparing…)"
else "${if (name == WaveTables.CUSTOM) "your table" else name} · ${table.frames} frames"
Box(Modifier.fillMaxWidth().height(72.dp).background(c.background).border(1.dp, c.grid, RectangleShape)) {
if (table != null) {
// 128 points of each frame's richest level, stacked; recomputed per table only
val curves = remember(table) {
val n = table.size(0); val step = maxOf(1, table.frames / 16)
(0 until table.frames step step).map { f -> FloatArray(128) { i -> table.data[0][f * n + i * n / 128] } to f }
}
val lit = (pos * (table.frames - 1) + 0.5f).toInt()
Canvas(Modifier.fillMaxWidth().height(72.dp)) {
if (rev < 0) return@Canvas // capture the revision so the drawing follows edits
val k = curves.size
val rowH = size.height * 0.45f
for ((idx, cf) in curves.withIndex().reversed()) {
val (pts, f) = cf
val depth = if (k > 1) idx.toFloat() / (k - 1) else 0f
val x0 = size.width * 0.04f + depth * size.width * 0.18f
val w = size.width * 0.76f
val yMid = size.height * 0.72f - depth * size.height * 0.45f
val near = kotlin.math.abs(f - lit) * 2 <= maxOf(1, table.frames / curves.size)
val col = if (near) tone else c.text.copy(alpha = 0.25f)
for (i in 1 until pts.size) {
drawLine(col,
Offset(x0 + w * (i - 1) / pts.size, yMid - pts[i - 1] * rowH * 0.5f),
Offset(x0 + w * i / pts.size, yMid - pts[i] * rowH * 0.5f),
strokeWidth = if (near) 2f else 1f)
}
}
}
}
Text(caption, color = c.textDim, fontFamily = FontFamily.Monospace, fontSize = 9.sp,
modifier = Modifier.padding(horizontal = 4.dp, vertical = 2.dp))
}
}
/** LOAD TABLE in Oscillator 1's title: a Serum / WaveEdit / power-of-two-frame WAV
* becomes the "Custom" table and is selected on oscillator 1. */
@Composable
fun LoadTableAction(vm: AppViewModel, slot: ToolboxSlot) {
val c = LocalRetro.current
val context = LocalContext.current
@Suppress("UNUSED_VARIABLE") val rev = vm.revision
var importError by remember(slot.index) { mutableStateOf<String?>(null) }
val picker = rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) { uri ->
if (uri == null) return@rememberLauncherForActivityResult
@@ -54,47 +104,13 @@ fun WavetableEditor(vm: AppViewModel, slot: ToolboxSlot) {
"Couldn't load this wavetable. Use an uncompressed .wav (Serum, WaveEdit, or frames of 256–2048 samples)."
}
}
val name = slot.string("o1table", WaveDefs.TABLES[0])
val frame = slot.string("wtframe", WaveDefs.FRAME_SIZES[0]).toIntOrNull() ?: 0
val table = if (name == WaveTables.CUSTOM) WaveTables.forSample(slot.string(WaveDefs.SAMPLE_KEY), frame) else WaveTables.factory(name)
val pos = slot.float("o1pos", 0f)
Column(Modifier.fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(8.dp)) {
RetroButton("LOAD TABLE") { picker.launch(arrayOf("*/*")) }
Text(
if (table == null) "Osc 1: $name (preparing…)" else "Osc 1: ${if (name == WaveTables.CUSTOM) "your table" else name} · ${table.frames} frames",
color = c.textDim, fontFamily = FontFamily.Monospace, fontSize = 10.sp,
)
if (table != null) {
// 128 points of each frame's richest level, stacked; recomputed per table only
val curves = remember(table) {
val n = table.size(0); val step = maxOf(1, table.frames / 16)
(0 until table.frames step step).map { f -> FloatArray(128) { i -> table.data[0][f * n + i * n / 128] } to f }
}
val lit = (pos * (table.frames - 1) + 0.5f).toInt()
Canvas(Modifier.fillMaxWidth().height(110.dp).background(c.surface)) {
val k = curves.size
val rowH = size.height * 0.45f
for ((idx, cf) in curves.withIndex().reversed()) {
val (pts, f) = cf
val depth = if (k > 1) idx.toFloat() / (k - 1) else 0f
val x0 = size.width * 0.04f + depth * size.width * 0.18f
val w = size.width * 0.76f
val yMid = size.height * 0.72f - depth * size.height * 0.45f
val near = kotlin.math.abs(f - lit) * 2 <= maxOf(1, table.frames / curves.size)
val col = if (near) c.accent else c.text.copy(alpha = 0.25f)
for (i in 1 until pts.size) {
drawLine(col,
Offset(x0 + w * (i - 1) / pts.size, yMid - pts[i - 1] * rowH * 0.5f),
Offset(x0 + w * i / pts.size, yMid - pts[i] * rowH * 0.5f),
strokeWidth = if (near) 2f else 1f)
}
}
}
}
}
Text(
"+ LOAD TABLE", color = c.accent, fontFamily = FontFamily.Monospace, fontSize = 11.sp,
modifier = Modifier
.border(1.dp, c.accent.copy(alpha = 0.6f), RectangleShape)
.clickable { picker.launch(arrayOf("*/*")) }
.padding(horizontal = 8.dp, vertical = 3.dp),
)
importError?.let { msg ->
AlertDialog(
onDismissRequest = { importError = null },