From 6bfe0eb8a078eef7c899fd867d44c914cc23a269 Mon Sep 17 00:00:00 2001 From: David Wursteisen Date: Tue, 6 Feb 2024 21:08:26 +0100 Subject: [PATCH] Restore the context when changing tab. --- tiny-cli/src/jvmMain/resources/sfx/game.lua | 97 +++++++++---------- .../src/jvmMain/resources/sfx/widgets.lua | 5 + .../com/github/minigdx/tiny/lua/SfxLib.kt | 11 ++- .../minigdx/tiny/sound/WaveGenerator.kt | 16 +++ 4 files changed, 73 insertions(+), 56 deletions(-) diff --git a/tiny-cli/src/jvmMain/resources/sfx/game.lua b/tiny-cli/src/jvmMain/resources/sfx/game.lua index 2971d559..0faeffc0 100644 --- a/tiny-cli/src/jvmMain/resources/sfx/game.lua +++ b/tiny-cli/src/jvmMain/resources/sfx/game.lua @@ -12,28 +12,34 @@ local labels = {"C0", "Db0", "D0", "Eb0", "E0", "F0", "Gb0", "G0", "Ab0", "A0", local waves = {{ type = "sine", color = 9, - index = 1 + index = 1, + overlay = 16 +}, { + type = "square", + color = 15, + index = 2, + overlay = 21, +}, { + type = "triangle", + color = 13, + index = 3, + overlay = 19 }, { type = "noise", color = 4, - index = 2 + index = 4, + overlay = 17 }, { type = "pulse", color = 10, - index = 3 -}, { - type = "triangle", - color = 13, - index = 4 + index = 5, + overlay = 18 }, { type = "saw", color = 11, - index = 5 -}, { - type = "square", - color = 15, + overlay = 20, index = 6 -}} +},} local bpm = nil local patterns = nil @@ -51,40 +57,39 @@ end local active_tab = nil function on_active_tab(current, prec) - local data = {} - -- save the current score - for f in all(faders) do - table.insert(data, { - wave = f.data.wave, - note = f.data.note, - value = f.value, - color = f.tip_color - }) - end if prec ~= nil then - prec.data = data + local score = generate_score() + prec.content = sfx.to_table(score) end - -- restore the previous score - if current.data ~= nil then - local data = current.data + -- restore the previous score of the current tab + if current.content ~= nil then + local data = current.content + bpm.value = data["bpm"] + -- always get the first pattern + local beats = data["patterns"][1] for k, f in ipairs(faders) do - f.data = data[k] - f.value = data[k].value - f.label = labels[f.value] - f.tip_color = data[k].color + widgets.resetFaderValue(f) + if beats[k] ~= nil then + for b in all(beats[k]) do + if b.index > 0 then + local w = waves[b.index] + widgets.setFaderValue(f, b.index, b.note, w.color) + else + -- set silence value + widgets.resetFaderValue(f) + end + end + else + -- set silence value + widgets.resetFaderValue(f) + end end else + bpm.value = 120 -- no data, reset to 0 for k, f in ipairs(faders) do - f.value = 0 - f.label = "" - f.data = { - wave = "", - note = 0, - value = 0, - color = 0 - } + widgets.resetFaderValue(f) end end @@ -103,7 +108,6 @@ end function on_play_button() local score = generate_score() - debug.console(score) sfx.sfx(score) end @@ -189,12 +193,13 @@ function _init(w, h) table.insert(faders, f) end + -- buttons for i = #waves - 1, 0, -1 do local w = widgets.createButton({ x = 10, y = 250 - i * 16, - overlay = 16 + i, + overlay = waves[i + 1].overlay, data = { wave = waves[i + 1] }, @@ -265,14 +270,7 @@ function init_faders(tabs) colors[v.type] = v.color end - for t in all(tabs) do - local song = t.content - if song then - t.data = song - end - end on_active_tab(tabs[1]) - end function to_hex(number) @@ -295,17 +293,14 @@ function generate_score() for f in all(faders) do local beat = "" if f.values ~= nil and next(f.values) then - debug.console(f.values) for k, v in pairs(f.values) do - debug.console("key "..k) - debug.console(v) if #beat > 0 then beat = beat .. ":" end beat = beat .. to_hex(k) .. to_hex(v.value) .. to_hex(255) end else - beat = "0000FF" + beat = "0000FF" end strip = strip .. beat .. " " diff --git a/tiny-cli/src/jvmMain/resources/sfx/widgets.lua b/tiny-cli/src/jvmMain/resources/sfx/widgets.lua index 50999406..62ee13dc 100644 --- a/tiny-cli/src/jvmMain/resources/sfx/widgets.lua +++ b/tiny-cli/src/jvmMain/resources/sfx/widgets.lua @@ -39,6 +39,7 @@ local Tab = { width = 0, height = 8, label = "+", + content = nil, status = 0, -- 0 : inactive ; 1 : active new_tab = false } @@ -102,6 +103,10 @@ factory.setFaderValue = function(fader, index, value, color) end end +factory.resetFaderValue = function(fader) + fader.values = {} +end + factory.createFader = function(value) local result = new(Fader, value) table.insert(widgets, result) diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/SfxLib.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/SfxLib.kt index 6dfd4aa0..c35a48f8 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/SfxLib.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/SfxLib.kt @@ -172,6 +172,7 @@ class SfxLib( notes.forEach { wave -> val note = LuaTable() note.set("type", wave.name) + note.set("index", wave.index) note.set("note", wave.note.index) beat.insert(0, note) } @@ -225,11 +226,11 @@ class SfxLib( return when (wave) { 1 -> SineWave(Note.fromIndex(noteIndex), duration, volume) - 2 -> NoiseWave(Note.fromIndex(noteIndex), duration, volume) - 3 -> PulseWave(Note.fromIndex(noteIndex), duration, volume) - 4 -> TriangleWave(Note.fromIndex(noteIndex), duration, volume) - 5 -> SawToothWave(Note.fromIndex(noteIndex), duration, volume) - 6 -> SquareWave(Note.fromIndex(noteIndex), duration, volume) + 2 -> SquareWave(Note.fromIndex(noteIndex), duration, volume) + 3 -> TriangleWave(Note.fromIndex(noteIndex), duration, volume) + 4 -> NoiseWave(Note.fromIndex(noteIndex), duration, volume) + 5 -> PulseWave(Note.fromIndex(noteIndex), duration, volume) + 6 -> SawToothWave(Note.fromIndex(noteIndex), duration, volume) else -> SilenceWave(duration) } } diff --git a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/sound/WaveGenerator.kt b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/sound/WaveGenerator.kt index c66ba70e..630893a0 100644 --- a/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/sound/WaveGenerator.kt +++ b/tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/sound/WaveGenerator.kt @@ -44,6 +44,8 @@ sealed class WaveGenerator( abstract val name: String + abstract val index: Int + companion object { internal const val PI = kotlin.math.PI.toFloat() internal const val TWO_PI = 2.0f * PI @@ -58,6 +60,8 @@ class SineWave(note: Note, duration: Seconds, volume: Percent = 1.0f) : WaveGene override fun copy(duration: Seconds, volume: Percent): WaveGenerator = SineWave(note, duration, volume) override val name: String = "sine" + + override val index: Int = 1 } class SquareWave(note: Note, duration: Seconds, volume: Percent = 1.0f) : WaveGenerator(note, duration, volume) { @@ -73,6 +77,8 @@ class SquareWave(note: Note, duration: Seconds, volume: Percent = 1.0f) : WaveGe override fun copy(duration: Seconds, volume: Percent): WaveGenerator = SquareWave(note, duration, volume) override val name: String = "square" + + override val index: Int = 2 } class TriangleWave(note: Note, duration: Seconds, volume: Percent = 1.0f) : WaveGenerator(note, duration, volume) { @@ -85,6 +91,8 @@ class TriangleWave(note: Note, duration: Seconds, volume: Percent = 1.0f) : Wave override fun copy(duration: Seconds, volume: Percent): WaveGenerator = TriangleWave(note, duration, volume) override val name: String = "triangle" + + override val index: Int = 3 } class NoiseWave(note: Note, duration: Seconds, volume: Percent = 1.0f) : WaveGenerator(note, duration, volume) { @@ -100,6 +108,8 @@ class NoiseWave(note: Note, duration: Seconds, volume: Percent = 1.0f) : WaveGen override fun copy(duration: Seconds, volume: Percent): WaveGenerator = NoiseWave(note, duration, volume) override val name: String = "noise" + + override val index: Int = 4 } class PulseWave(note: Note, duration: Seconds, volume: Percent = 1.0f) : WaveGenerator(note, duration, volume) { @@ -116,6 +126,8 @@ class PulseWave(note: Note, duration: Seconds, volume: Percent = 1.0f) : WaveGen override fun copy(duration: Seconds, volume: Percent): WaveGenerator = PulseWave(note, duration, volume) override val name: String = "pulse" + + override val index: Int = 5 } class SawToothWave(note: Note, duration: Seconds, volume: Percent = 1.0f) : WaveGenerator(note, duration, volume) { @@ -128,6 +140,8 @@ class SawToothWave(note: Note, duration: Seconds, volume: Percent = 1.0f) : Wave override fun copy(duration: Seconds, volume: Percent): WaveGenerator = SawToothWave(note, duration, volume) override val name: String = "sawtooth" + + override val index: Int = 6 } class SilenceWave(duration: Seconds) : WaveGenerator(Note.C0, duration, 1.0f) { @@ -140,4 +154,6 @@ class SilenceWave(duration: Seconds) : WaveGenerator(Note.C0, duration, 1.0f) { override fun copy(duration: Seconds, volume: Percent): WaveGenerator = SilenceWave(duration) override val name: String = " " + + override val index: Int = 0 }