Skip to content

Commit

Permalink
Add direction on the sweep effect.
Browse files Browse the repository at this point in the history
Fix general volume
  • Loading branch information
dwursteisen committed Feb 29, 2024
1 parent 58c379a commit c95152d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 76 deletions.
22 changes: 20 additions & 2 deletions tiny-cli/src/jvmMain/resources/sfx/game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ editor.generate_score = function(content, pattern_selector)
to_hex(t["env"]["sustain"]) .. " " .. to_hex(t["env"]["release"])

if t["mod"]["type"] == 1 then
track = track .. " 01 " .. to_hex(t["mod"]["a"]) .. " 00 00 00\n"
track = track .. " 01 " .. to_hex(t["mod"]["a"]) .. " ".. to_hex(t["mod"]["b"] * 255) .. " 00 00\n"
elseif t["mod"]["type"] == 2 then
track = track .. " 02 " .. to_hex(t["mod"]["a"]) .. " " .. to_hex(t["mod"]["b"]) .. " 00 00\n"
else
Expand Down Expand Up @@ -235,12 +235,14 @@ editor.on_active_tab = function(current, prev)
editor.vibrato_knob.value = 0
editor.depth_knob.value = 0
editor.sweep_knob.value = 0
editor.acceleration_knob.value = 0
editor.c_sweep.value = false
editor.c_vibrato.value = false
local type = data["tracks"][editor.env.index]["mod"].type
if type == 1 then
editor.c_sweep.value = true
editor.sweep_knob.value = data["tracks"][editor.env.index]["mod"].a / 255
editor.acceleration_knob.value = data["tracks"][editor.env.index]["mod"].b

elseif type == 2 then
editor.c_vibrato.value = true
Expand Down Expand Up @@ -555,11 +557,26 @@ editor.create_widgets = function()
y = env.y + env.height + 4 + 32,
label = "sweep",
on_update = on_update_sweep,
value = env.release
})
table.insert(editor.patterns_fx_widgets, sweep)
editor.sweep_knob = sweep

local on_update_acceleration = function(knob)
if editor.active_tab.content["tracks"][env.index]["mod"]["type"] ~= 1 then
return
end
editor.active_tab.content["tracks"][env.index]["mod"]["b"] = knob.value
end

local acceleration = widgets.createKnob({
x = env.x + 32,
y = env.y + env.height + 4 + 32,
label = "acceleration",
on_update = on_update_acceleration,
})
table.insert(editor.patterns_fx_widgets, acceleration)
editor.acceleration_knob = acceleration

local c_sweep = widgets.createCheckbox({
x = 40,
y = env.y + env.height + 4 + 32,
Expand Down Expand Up @@ -621,6 +638,7 @@ editor.create_widgets = function()
c_vibrato.value = false
editor.active_tab.content["tracks"][env.index]["mod"]["type"] = 1
editor.active_tab.content["tracks"][env.index]["mod"]["a"] = editor.sweep_knob.value * 255
editor.active_tab.content["tracks"][env.index]["mod"]["b"] = editor.acceleration_knob.value
end

-- tabs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import com.github.minigdx.tiny.sound.Envelope
import com.github.minigdx.tiny.sound.Modulation
import com.github.minigdx.tiny.sound.Noise2
import com.github.minigdx.tiny.sound.NoiseWave
import com.github.minigdx.tiny.sound.Pattern
import com.github.minigdx.tiny.sound.Pattern2
import com.github.minigdx.tiny.sound.Pulse2
import com.github.minigdx.tiny.sound.PulseWave
Expand All @@ -22,7 +21,6 @@ import com.github.minigdx.tiny.sound.Silence2
import com.github.minigdx.tiny.sound.SilenceWave
import com.github.minigdx.tiny.sound.Sine2
import com.github.minigdx.tiny.sound.SineWave
import com.github.minigdx.tiny.sound.Song
import com.github.minigdx.tiny.sound.Song2
import com.github.minigdx.tiny.sound.SoundGenerator
import com.github.minigdx.tiny.sound.Square2
Expand Down Expand Up @@ -248,7 +246,7 @@ class SfxLib(
is Sweep -> {
mod["type"] = 1
mod["a"] = Note.fromFrequency(modulation.sweep).index
mod["b"] = 0
mod["b"] = if (modulation.acceleration) 1 else 0
mod["c"] = 0
mod["d"] = 0
}
Expand Down Expand Up @@ -415,7 +413,7 @@ class SfxLib(

val modulation = if (mod > 0) {
if (mod == 1) {
Sweep(Note.fromIndex(1 + modA * Note.B8.index / 255).frequency.toInt())
Sweep(Note.fromIndex(1 + modA * Note.B8.index / 255).frequency.toInt(), modB / 255f > 0.5f)
} else {
Vibrato(Note.fromIndex(1 + modA * Note.B8.index / 255).frequency, modB / 255f)
}
Expand Down Expand Up @@ -462,54 +460,6 @@ class SfxLib(
return Song2(bpm.toInt(), volume.toInt() / 255f, tracks.toTypedArray())
}

fun convertScoreToSong(score: String): Song {
val lines = score.lines()
if (lines.isEmpty()) {
throw IllegalArgumentException(
"The content of the score is empty. Can't convert it into a song. " +
"Check if the score is not empty or correctly loaded!",
)
}

val header = lines.first()
if (!header.startsWith(TINY_SFX_HEADER)) {
throw IllegalArgumentException(
"The '$TINY_SFX_HEADER' is missing from the fist line of the score. " +
"Is the score a valid score?",
)
}

val (_, nbPattern, bpm, volume) = header.split(" ")

val duration = 60f / bpm.toFloat() / 8f

// Map<Index, Pattern>
val patterns = lines.drop(1).take(nbPattern.toInt()).mapIndexed { indexPattern, pattern ->
val beatsStr = pattern.trim().split(" ")
val beats = convertToWaves(beatsStr, duration)
Pattern(indexPattern + 1, beats)
}.associateBy { it.index }

val patternOrder = lines.drop(nbPattern.toInt() + 1).firstOrNull()
val orders = if (patternOrder.isNullOrBlank()) {
listOf(1)
} else {
patternOrder.trim().split(" ").map { it.toInt() }
}

val patternsOrdered = orders.map { patterns[it]!! }

return Song(bpm.toInt(), volume.toInt() / 255f, patterns, patternsOrdered)
}

private fun convertToWaves(beatsStr: List<String>, duration: Seconds): List<WaveGenerator> {
val beats = beatsStr
.asSequence()
.filter { it.isNotBlank() }
.map { beat -> convertToWave(beat, duration) }
return beats.toList()
}

private fun convertToSound(beatsStr: List<String>, mod: Modulation?, env: Envelope?): List<SoundGenerator> {
val beats = beatsStr
.asSequence()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ interface Modulation {
*/
class Sweep(
val sweep: Frequency,
val acceleration: Boolean,
) : Modulation {

private val way = if (acceleration) {
1
} else {
-1
}

override fun apply(index: Int, frequency: Float): Float {
return frequency + index * sweep / SAMPLE_RATE.toFloat()
return frequency + index * (sweep * way) / SAMPLE_RATE.toFloat()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ abstract class SoundManager {
result[index] += sample
}

result[index] = result[index] / divider
result[index] = (result[index] / divider) * song.volume
}
return SoundBuffer(result, numberOfSample)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,6 @@ class SfxLibTest {
override fun sfx(song: Song2) = Unit
}

@Test
fun scoreToSong() {
val score = """tiny-sfx 2 120 255
|0101FF 0101FF
|0101FF 0101FF
|1 2 1
""".trimMargin()

val song = SfxLib.convertScoreToSong(score)

assertEquals(120, song.bpm)
assertEquals(1f, song.volume)
// patterns by index
assertEquals(2, song.patterns.size)
// patterns ordered by usage
assertEquals(3, song.music.size)

assertEquals(song.patterns[1]!!.notes.size, 2)
}

@Test
fun scoreToSong2() {
val score = """tiny-sfx 120 255
Expand Down

0 comments on commit c95152d

Please sign in to comment.