diff semiconginev2/audio/generators.nim @ 1225:27cd1c21290e compiletime-tests

did: refactor resources
author sam <sam@basx.dev>
date Wed, 17 Jul 2024 22:20:59 +0700
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/semiconginev2/audio/generators.nim	Wed Jul 17 22:20:59 2024 +0700
@@ -0,0 +1,12 @@
+proc sinewave(f: float): proc(x: float): float =
+  proc ret(x: float): float =
+    sin(x * 2 * Pi * f)
+  result = ret
+
+proc SineSoundData*(f: float, len: float, rate: int, amplitude = 0.5'f32): SoundData =
+  let dt = 1'f / float(rate)
+  var sine = sinewave(f)
+  for i in 0 ..< int(float(rate) * len):
+    let t = dt * float(i)
+    let value = int16(sine(t) * float(high(int16)) * amplitude)
+    result.add [value, value]