1
0
Fork 0
forked from a/yue

format nim

slight performance improvement
This commit is contained in:
Locria Cyber 2023-03-12 20:05:28 +00:00
parent 5fe21ce19c
commit 7db4553fad
Signed by: iacore
GPG key ID: ED0D424AE4406330
2 changed files with 63 additions and 59 deletions

View file

@ -26,9 +26,11 @@ func osc_piano*(f, t: float): float =
# Y *= 1 + 16 * t * math.exp(-6 * t)
let w = 2 * PI * f
let ewt = math.exp(-0.001 * w * t)
var Y = 0.6 * math.sin(w * t) * ewt +
0.2 * math.sin(2 * w * t) * ewt +
0.05 * math.sin(3 * w * t) * ewt
var Y = (
0.6 * math.sin(w * t) +
0.2 * math.sin(2 * w * t) +
0.05 * math.sin(3 * w * t)
) * ewt
let Y2 = Y * (Y * Y + 1)
Y2 * (1 + 16 * t * math.exp(-6 * t))

View file

@ -1,8 +1,7 @@
import std/[algorithm, math, sugar, strformat, logging]
type
OscFn* = proc (f: float, t: float): float
OscFn* = proc (f: float, t: float): float {.gcsafe.}
Note* = tuple
len: float ## seconds
freq: float
@ -51,18 +50,21 @@ proc at*(music: openArray[ProcessedNote], t: float): int32 =
var i: int = music.bisect(t) - 1
var ret: float = 0
var notes: seq[ProcessedNote] = @[]
while i >= 0:
let m = music[i]
assert m.start <= t
if m.start + HACK_LONGEST_NOTE < t:
break
else:
ret += m.vol * m.osc(m.freq, t - m.start)
notes &= m
i -= 1
ret *= GAIN_BIAS
var sample_sum: float = 0
for m in notes:
sample_sum += (m.vol * m.osc(m.freq, t - m.start))
let ret = sample_sum * GAIN_BIAS
# clip sample
if ret >= int32.high.float: