Skip to content

Commit

Permalink
Merge pull request #6 from minigdx/custom-sound
Browse files Browse the repository at this point in the history
Add volume management in the SFX editor
dwursteisen authored Feb 10, 2024
2 parents 14555c9 + 4d893e6 commit 9c7f5e8
Showing 7 changed files with 79 additions and 25 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -18,6 +18,15 @@ Tiny is a lightweight, multiplatform game engine that allows developers to creat

The Tiny game engine comes with extensive documentation that covers everything from installing the engine to creating games. The documentation is available on the [GitHub website](https://minigdx.github.io/tiny/).

## Games

Want to build a game like those? Read the documentation above ⬆️ and start to build your own!

[![Camping](./tiny-doc/src/docs/asciidoc/sample/camping.gif)](https://dwursteisen.itch.io/trijam-camping)
[![Type It](./tiny-doc/src/docs/asciidoc/sample/level-up.gif)](https://dwursteisen.itch.io/trijam-220-type-it)
[![Memory Pong](./tiny-doc/src/docs/asciidoc/sample/memory.gif)](https://dwursteisen.itch.io/memory-pong-trijam-251)
[![Connect Me](./tiny-doc/src/docs/asciidoc/sample/connect_me.gif)](https://dwursteisen.itch.io/connect-me)

## Community

Tiny has a growing community of developers who use the engine to create games. You can join the community on social media platforms like Twitter, itch.io and Reddit.
83 changes: 60 additions & 23 deletions tiny-cli/src/jvmMain/resources/sfx/game.lua
Original file line number Diff line number Diff line change
@@ -43,6 +43,8 @@ local waves = {{

local bpm = nil
local patterns = nil
local volume = nil

local faders = {}
local current_wave = waves[1]

@@ -117,28 +119,36 @@ function active_pattern(index, data)
end

function on_active_tab(current, prec)

fader_mode = false
on_switch_mode()

if prec ~= nil then
local score = generate_score()
prec.content = sfx.to_table(score)
debug.console(generate_score())
end

-- restore the previous score of the current tab
if current.content ~= nil then

local data = current.content
bpm.value = data["bpm"]
volume.value = (data["volume"] / 255) * 10
debug.console("stuff")
debug.console(data["volume"])
-- always get the first pattern
active_pattern(1, data)

debug.console(generate_score())
else
bpm.value = 120
patterns.value = 1
volume.value = 10
-- no data, reset to 0
for k, f in ipairs(faders) do
for k, f in pairs(faders) do
widgets.resetFaderValue(f)
end
active_tab = { }
current.content = sfx.to_table(generate_score())
end

@@ -150,15 +160,13 @@ local window = {
height = 0
}



function on_new_tab(tab)
local filename = ws.create("sfx", "sfx")
tab.label = filename
end

function on_play_button()
local score = nil
local score = nil
if fader_mode then
score = generate_score(patterns.value)
else
@@ -198,7 +206,13 @@ function on_increase_pattern(counter)
counter.value = math.min(counter.value + 1, #active_tab.content["patterns"])
end

function on_decrease_volume(counter)
counter.value = math.max(counter.value - 1, 1)
end

function on_increase_volume(counter)
counter.value = math.min(counter.value + 1, 10)
end

function on_switch_mode()
fader_mode = not fader_mode
@@ -237,7 +251,6 @@ function _init(w, h)
on_active_button = on_play_button
})


widgets.createButton({
x = 10,
y = 16 + 2 + 16,
@@ -259,26 +272,37 @@ function _init(w, h)

patterns = widgets.createCounter({
x = 10,
y = 112,
y = 90,
value = 1,
label = "pattern",
on_left = on_previous_patterns,
on_right = on_next_patterns
})

table.insert(fader_widgets, patterns)

bpm = widgets.createCounter({
x = 10,
y = 112 + 24,
y = 90 + 24,
value = 120,
label = "bpm",
on_left = on_decrease_bpm,
on_right = on_increase_bpm
})

table.insert(fader_widgets, bpm)


volume = widgets.createCounter({
x = 10,
y = 90 + 24 + 24,
value = 10,
label = "volume",
on_left = on_decrease_volume,
on_right = on_increase_volume
})

table.insert(fader_widgets, bpm)

-- faders
for i = 1, 32 do
local f = widgets.createFader({
@@ -296,7 +320,7 @@ function _init(w, h)
table.insert(faders, f)
table.insert(fader_widgets, f)
end

-- buttons
for i = #waves - 1, 0, -1 do
local w = widgets.createButton({
@@ -308,7 +332,7 @@ function _init(w, h)
},
on_active_button = on_active_button
})

table.insert(fader_widgets, w)

if i == 0 then
@@ -317,14 +341,14 @@ function _init(w, h)
end

-- music buttons
for x=1,8 do
for y=1,8 do
for x = 1, 8 do
for y = 1, 8 do
local w = widgets.createCounter({
x = 28 + x * 48,
y = y * 32,
value = nil,
enabled = false,
index = x + (y -1) * 8,
index = x + (y - 1) * 8,
label = "pattern",
on_left = on_decrease_pattern,
on_right = on_increase_pattern
@@ -333,7 +357,7 @@ function _init(w, h)
if x == 1 and y == 1 then
w.value = 1
end
music_widgets[x + (y-1) * 8] = w
music_widgets[x + (y - 1) * 8] = w
end
end
-- tabs
@@ -431,7 +455,17 @@ function generate_score(played_pattern)
active_tab.content.patterns[1] = new_pattern
end
local p = active_tab.content["patterns"]
local score = "tiny-sfx " .. #p .. " " .. bpm.value .. " 255\n"
local v = math.floor((volume.value * 25.5))

debug.console("volume value")
debug.console(volume.value)
debug.console("computed volume")
debug.console(v)
local score = "tiny-sfx " .. #p .. " " .. bpm.value .. " "..v.."\n"

debug.console("SCORE ???")
debug.console(score)
debug.console(volume.value)
-- write patterns
for patterns in all(active_tab.content["patterns"]) do
local strip = ""
@@ -442,7 +476,11 @@ function generate_score(played_pattern)
beatStr = beatStr .. "0000FF"
else
for note in all(beat) do
beatStr = beatStr .. to_hex(note.index) .. to_hex(note.note) .. to_hex(255) .. ":"
if note.note == 0 then
beatStr = beatStr .. "0000FF:"
else
beatStr = beatStr .. to_hex(note.index) .. to_hex(note.note) .. to_hex(255) .. ":"
end
end
beatStr = beatStr:sub(1, -2)
end
@@ -460,8 +498,8 @@ function generate_score(played_pattern)
if w.value == 0 then
stop = true
end
if(not stop) then
played_pattern = played_pattern..w.value.." "
if (not stop) then
played_pattern = played_pattern .. w.value .. " "
end
end
end
@@ -475,8 +513,7 @@ function _update()
widgets._update()

if ctrl.pressed(keys.space) then
local score = generate_score()
sfx.sfx(score)
on_play_button()
end

local new_wave = current_wave
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions tiny-doc/src/docs/asciidoc/tiny-cli.adoc
Original file line number Diff line number Diff line change
@@ -232,6 +232,13 @@ NOTE: The tiny serve command is intended for local testing only and should not b

The tiny-cli sfx command is used to start a SFX edtor to generate sound to use in your `🧸 Tiny` game.

https://dwursteisen.itch.io/tiny-sfx-editor[This SFX editor can be tried online] on itch.io.

image:sample/tiny-cli-sfx1.png[]

image:sample/tiny-cli-sfx0.png[]


==== Usage

To use the `tiny-cli sfx` command, follow the syntax:
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ import org.luaj.vm2.LuaValue
import org.luaj.vm2.lib.OneArgFunction
import org.luaj.vm2.lib.ThreeArgFunction
import org.luaj.vm2.lib.TwoArgFunction
import kotlin.math.floor
import kotlin.math.max
import kotlin.math.min

@@ -215,7 +216,7 @@ class SfxLib(
}
val result = LuaTable()
result["bpm"] = valueOf(song.bpm)
result["volume"] = valueOf(song.volume.toDouble())
result["volume"] = valueOf(floor(song.volume.toDouble() * 255))
result["patterns"] = patterns
return result
}
Original file line number Diff line number Diff line change
@@ -96,7 +96,7 @@ abstract class SoundManager {
buffers.forEach { (_, line) ->
result += line[sample]
}
mix[sample] = result / buffers.size.toFloat()
mix[sample] = (result / buffers.size.toFloat()) * song.volume
}
return mix to lastBeat * numberOfSamplesPerBeat
}

0 comments on commit 9c7f5e8

Please sign in to comment.