Skip to content

Commit

Permalink
Restore the context when changing tab.
Browse files Browse the repository at this point in the history
  • Loading branch information
dwursteisen committed Feb 6, 2024
1 parent cb60d35 commit 6bfe0eb
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 56 deletions.
97 changes: 46 additions & 51 deletions tiny-cli/src/jvmMain/resources/sfx/game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -103,7 +108,6 @@ end

function on_play_button()
local score = generate_score()
debug.console(score)
sfx.sfx(score)
end

Expand Down Expand Up @@ -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]
},
Expand Down Expand Up @@ -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)
Expand All @@ -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 .. " "
Expand Down
5 changes: 5 additions & 0 deletions tiny-cli/src/jvmMain/resources/sfx/widgets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ local Tab = {
width = 0,
height = 8,
label = "+",
content = nil,
status = 0, -- 0 : inactive ; 1 : active
new_tab = false
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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
}

0 comments on commit 6bfe0eb

Please sign in to comment.