Skip to content

Commit 7fdbdb0

Browse files
committed
- Let the SFX editor edit one file
- Allow to switch octave in the editor
1 parent 505102d commit 7fdbdb0

File tree

9 files changed

+82
-29
lines changed

9 files changed

+82
-29
lines changed

tiny-cli/src/main/kotlin/com/github/minigdx/tiny/cli/command/SfxCommand.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import com.github.minigdx.tiny.cli.config.GameParameters
99
import com.github.minigdx.tiny.engine.GameEngine
1010
import com.github.minigdx.tiny.engine.TinyException
1111
import com.github.minigdx.tiny.file.CommonVirtualFileSystem
12-
import com.github.minigdx.tiny.file.JvmLocalFile
1312
import com.github.minigdx.tiny.log.StdOutLogger
14-
import com.github.minigdx.tiny.lua.WorkspaceLib
1513
import com.github.minigdx.tiny.lua.errorLine
1614
import com.github.minigdx.tiny.platform.glfw.GlfwPlatform
1715
import com.github.minigdx.tiny.render.LwjglGLRender
@@ -58,15 +56,16 @@ class SfxCommand : CliktCommand(name = "sfx") {
5856
}
5957
val commandParameters = GameParameters.JSON.decodeFromStream<GameParameters>(configFile)
6058

61-
if (filename.exists()) {
59+
if (!filename.exists()) {
6260
val json = Json.encodeToString(Music())
6361
filename.writeBytes(json.encodeToByteArray())
6462
}
65-
WorkspaceLib.DEFAULT = listOf(JvmLocalFile(filename.name, workingDirectory = filename.absoluteFile.parentFile))
6663

6764
val logger = StdOutLogger("tiny-cli")
6865
val vfs = CommonVirtualFileSystem()
6966
val commandOptions = commandParameters.toGameOptions()
67+
.copy(sounds = listOf(filename.name))
68+
7069
val gameEngine = GameEngine(
7170
gameOptions = commandOptions,
7271
platform = GlfwPlatform(

tiny-cli/src/main/resources/sfx/_tiny.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,8 @@
4343
"levels": [
4444
"editor.ldtk"
4545
],
46+
"sounds": [
47+
"test.sfx"
48+
],
4649
"hideMouseCursor": true
4750
}

tiny-cli/src/main/resources/sfx/bar-editor.lua

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ CursorEditor._update = function(self)
5252
self.beat = 0
5353
self.play = not self.play
5454
self.time = 0
55+
56+
if self.play then
57+
state.current_bar.play()
58+
end
5559
end
5660

5761
if self.play then
@@ -105,8 +109,12 @@ local BarEditor = {
105109
{ y = 185, h = 8 },
106110
},
107111

108-
octave = 0,
112+
octave = 4,
109113
note = 0,
114+
hitbox_octaves = {
115+
{ x = -16, y = -8, width = 8, height = 8 },
116+
{ x = -16, y = 0, width = 8, height = 8 },
117+
}
110118
}
111119

112120
BarEditor._init = function(self)
@@ -115,6 +123,21 @@ end
115123

116124
BarEditor._update = function(self)
117125
local p = ctrl.touch()
126+
127+
-- octave management
128+
-- (as the octave buttons is using the default mouse, the mouse position needs to be checked without the offset)
129+
local up = self.hitbox_octaves[1]
130+
local down = self.hitbox_octaves[2]
131+
if ctrl.touched(0) then
132+
if inside_widget({ x = self.x + up.x, y = self.y + up.y, width = up.width, height = up.height }, p.x, p.y) then
133+
self.octave = self.octave + 1
134+
elseif inside_widget({ x = self.x + down.x, y = self.y + self.height + down.y, width = down.width, height = down.height }, p.x, p.y) then
135+
self.octave = self.octave - 1
136+
end
137+
138+
self.octave = math.clamp(0, self.octave, 8)
139+
end
140+
118141
p = { x = p.x, y = p.y + 8 }
119142
if inside_widget(self, p.x, p.y) then
120143
state.edit = true
@@ -171,7 +194,6 @@ BarEditor._update = function(self)
171194
self.current_edit = nil
172195
elseif inside_widget(self, p.x, p.y) and ctrl.touching(1) ~= nil then
173196
local local_x = p.x - self.x
174-
local local_y = p.y - self.y
175197

176198
local value = {
177199
beat = roundToHalf((local_x) / 16.0),
@@ -215,24 +237,30 @@ BarEditor._draw = function(self)
215237
end
216238

217239
for note in all(state.current_bar.notes()) do
218-
local i = (note.notei) % 25
219240

220-
local keys = self.keys_y[1 + #self.keys_y - i]
221-
local y = keys.y
222-
local h = keys.h
241+
local i = note.notei - self.octave * 12
223242

224-
shape.rectf(
225-
self.x + note.beat * 16, self.y + y,
226-
note.duration * 16, h,
227-
9
228-
)
243+
if self.octave <= note.octave and note.octave < self.octave + 2 then
244+
local keys = self.keys_y[1 + #self.keys_y - i]
245+
if not keys then
246+
debug.console("i -> ", i, " index -> ", 1 + #self.keys_y - i, " notei ", note.notei)
247+
end
248+
local y = keys.y
249+
local h = keys.h
250+
251+
shape.rectf(
252+
self.x + note.beat * 16, self.y + y,
253+
note.duration * 16, h,
254+
9
255+
)
256+
end
229257
end
230258

231259
if self.current_edit then
232260
local t = self.current_edit
233-
debug.console(t.note)
261+
234262
local note = state.current_bar.note_data(t.note)
235-
local i = (note.notei) % 25
263+
local i = note.notei - self.octave * 12
236264
local keys = self.keys_y[1 + #self.keys_y - i]
237265

238266
local y = keys.y
@@ -248,15 +276,22 @@ BarEditor._draw = function(self)
248276
-- border
249277
shape.rect(self.x, self.y, self.width, self.height, 4)
250278

279+
-- keyboard
251280
spr.sdraw(self.x - 3 * 8, self.y, 136, 64, 3 * 8, self.height)
281+
-- octave up
282+
local up = self.hitbox_octaves[1]
283+
spr.sdraw(self.x + up.x, self.y + up.y, 240, 40, up.width, up.height)
284+
-- octave down
285+
local down = self.hitbox_octaves[2]
286+
spr.sdraw(self.x + down.x, self.y + self.height + down.y, 240, 40, down.width, down.height, false, true)
252287

253288
gfx.pal(2, 8)
254289
local p = ctrl.touch()
255290
local x = math.clamp(self.x, p.x, self.x + self.width)
256291
spr.sdraw(x - 8, self.y, 232, 64, 3 * 8, self.height)
257292
gfx.pal()
258293

259-
print(self.note, self.x - 10, self.y - 8)
294+
print(self.note, x, self.y - 8)
260295
end
261296

262297
local w = {}
@@ -265,7 +300,10 @@ function _init()
265300
w = {}
266301
test = {}
267302
state = new(State)
303+
sfx.load(0) -- Load the sound file before init everything
304+
268305
state.current_bar = sfx.bar(0)
306+
debug.console(state.current_bar)
269307
state.current_instrument = sfx.instrument(state.current_bar.instrument())
270308

271309
map.level("BarEditor")
@@ -355,10 +393,6 @@ function _update()
355393
for widget in all(w) do
356394
widget:_update()
357395
end
358-
359-
if ctrl.pressed(keys.space) then
360-
state.current_bar.play()
361-
end
362396
end
363397

364398
function _draw()

tiny-cli/src/main/resources/sfx/sfx-0.sfx

Lines changed: 0 additions & 4 deletions
This file was deleted.

tiny-cli/src/main/resources/sfx/sfx-1.sfx

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"instruments":[{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]},{"index":1,"name":"violon","attack":0.1,"decay":0.1,"sustain":0.9,"release":0.05,"harmonics":[1.0,0.65,0.7,0.55,0.45,0.35,0.3]},{"index":2,"name":"obos","wave":"SAW_TOOTH","attack":0.1,"decay":0.1,"sustain":0.9,"release":0.05,"harmonics":[1.0,0.05,0.01]},{"index":3,"name":"drum","wave":"NOISE","attack":0.1,"decay":0.1,"sustain":0.9,"release":0.05,"harmonics":[1.0],"lastOutput":0.06498258,"lastFrequencyUsed":146.83,"cachedAlpha":0.020702422}],"musicalBars":[{"instrument":{"index":1,"name":"violon","attack":0.1,"decay":0.1,"sustain":0.9,"release":0.05,"harmonics":[1.0,0.65,0.7,0.55,0.45,0.35,0.3]},"tempo":380,"beats":[{"note":"A3","beat":0.5,"duration":0.5,"volume":1.0},{"note":"D3","beat":0.0,"duration":0.5,"volume":1.0}]},{"index":2,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":3,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":4,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":5,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":6,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":7,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":8,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":9,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":10,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":11,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":12,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":13,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":14,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":15,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":16,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":17,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":18,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":19,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":20,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":21,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":22,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":23,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":24,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":25,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":26,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":27,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":28,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":29,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":30,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":31,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}},{"index":32,"instrument":{"index":0,"name":"clarinet","wave":"TRIANGLE","attack":0.01,"decay":0.1,"sustain":0.8,"release":0.5,"harmonics":[1.1,0.3,0.031,0.039,0.345,0.29,0.0119]}}]}

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/NotesLib.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ enum class Note(val frequency: Float, val index: Int) {
184184
B8(7902.13f, OCTAVE_8 + 12),
185185
;
186186

187+
val octave: Int
188+
get() {
189+
val note = (index - 1) % OCTAVE_1
190+
val octave = (index - 1 - note) / OCTAVE_1
191+
return octave
192+
}
193+
187194
companion object {
188195
private val notesPerIndex = entries.toTypedArray().distinctBy { it.index }.sortedBy { it.index }.toTypedArray()
189196

tiny-engine/src/commonMain/kotlin/com/github/minigdx/tiny/lua/SfxLib.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ class SfxLib(
269269
LuaTable().apply {
270270
this.set("note", valueOf(note.name))
271271
this.set("notei", valueOf(note.index))
272+
this.set("octave", valueOf(note.octave))
272273
}
273274
}
274275

@@ -278,6 +279,7 @@ class SfxLib(
278279
LuaTable().apply {
279280
this.set("note", it.note?.name?.let { valueOf(it) } ?: NIL)
280281
this.set("notei", it.note?.index?.let { valueOf(it) } ?: NIL)
282+
this.set("octave", it.note?.octave?.let { valueOf(it) } ?: NIL)
281283
this.set("beat", valueOf(it.beat.toDouble()))
282284
this.set("duration", valueOf(it.duration.toDouble()))
283285
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.github.minigdx.tiny.lua
2+
3+
import kotlin.test.Test
4+
import kotlin.test.assertEquals
5+
6+
class NotesLibTest {
7+
@Test
8+
fun octave() {
9+
assertEquals(0, Note.C0.octave)
10+
assertEquals(1, Note.C1.octave)
11+
assertEquals(1, Note.B1.octave)
12+
assertEquals(5, Note.C5.octave)
13+
}
14+
}

0 commit comments

Comments
 (0)