aboutsummaryrefslogtreecommitdiff
path: root/musiclib.nim
diff options
context:
space:
mode:
Diffstat (limited to 'musiclib.nim')
-rw-r--r--musiclib.nim20
1 files changed, 10 insertions, 10 deletions
diff --git a/musiclib.nim b/musiclib.nim
index 4984596..91e63c4 100644
--- a/musiclib.nim
+++ b/musiclib.nim
@@ -16,12 +16,9 @@ type
vol: float
osc: OscFn
-## give some seconds for the note to fade out
-const HACK_DROPOFF_DELAY = 2.0
-
const HACK_LONGEST_NOTE = 16.0
-func process*(music: var seq[ProcessedNote], notes: openArray[Note]; start_init: float, speed: float=1, gain:float = 1) =
+func process*(music: var seq[ProcessedNote], notes: openArray[Note]; start_init: float, speed: float=1) =
## Adds a list of notes to the music list
##
## `notes` sequence of notes with no rests in between
@@ -32,7 +29,7 @@ func process*(music: var seq[ProcessedNote], notes: openArray[Note]; start_init:
assert note.len <= HACK_LONGEST_NOTE, &"note too long: {note.len}"
start = t
let stop = t + note.len / speed
- music &= (start, stop, note.freq, note.vol * gain, note.osc)
+ music &= (start, stop, note.freq, note.vol, note.osc)
t = stop
func sortByStart*(music: var seq[ProcessedNote]) =
@@ -45,7 +42,7 @@ func bisect(music: openArray[ProcessedNote], x: float): int =
music.lowerBound(x, (m, key) => cmp(m.start, key))
-const GAIN_BIAS: float = pow(2.0, 28.0)
+const GAIN_BIAS: float = pow(2.0, 31.0)
proc at*(music: openArray[ProcessedNote], t: float): int32 =
## Returns the total intensity of music sampled at time t
@@ -59,10 +56,13 @@ proc at*(music: openArray[ProcessedNote], t: float): int32 =
while i >= 0:
let m = music[i]
assert m.start <= t
- if m.stop + HACK_DROPOFF_DELAY >= t:
- ret += m.vol * m.osc(m.freq, t - m.start)
- if m.start + HACK_LONGEST_NOTE + HACK_DROPOFF_DELAY < t:
+ if m.start + HACK_LONGEST_NOTE < t:
break
+ else:
+ ret += m.vol * m.osc(m.freq, t - m.start)
i -= 1
- int32(ret * GAIN_BIAS)
+ ret *= GAIN_BIAS
+
+ # clip sample
+ clamp(ret, int32.low.float..int32.high.float).int32