aboutsummaryrefslogtreecommitdiff
path: root/music.nim
diff options
context:
space:
mode:
Diffstat (limited to 'music.nim')
-rw-r--r--music.nim59
1 files changed, 38 insertions, 21 deletions
diff --git a/music.nim b/music.nim
index 15a2fc3..24c01ca 100644
--- a/music.nim
+++ b/music.nim
@@ -1,7 +1,7 @@
-import musiclib, std/math
+import musiclib, std/[math, sugar]
# Number of times to sample each second
-let bitrate = 44100
+const bitrate = 44100
func osc_piano*(f, t: float): float =
## Returns the intensity of a tone of frequency f sampled at time t
@@ -20,15 +20,28 @@ func osc_piano*(f, t: float): float =
Y *= 1 + 16 * t * math.exp(-6 * t)
Y
+func osc_pulse*(f,t:float):float =
+ let width = (1.0 / f) / 2
+ let phase: float = t mod (width * 2)
+ if phase < width: 1.0 else: -1.0
+
func freq*(octave, step: float): float =
## Returns the frequency of a note
55 * pow(2, (octave + step / 12 - 1))
-func p*(len, octave, step, vol: float = 1): Note =
+var osc: OscFn = (f, t: float) => 0.0
+
+proc p*(len, octave, step, vol: float = 1): Note =
## Note helper constructor
- let osc: OscFn = osc_piano
(len, freq(octave, step), vol, osc)
+
+#------- song region -------
+
+const GAIN_NORMAL = 0.24
+
+osc = (f, t: float) => osc_piano(f, t) * GAIN_NORMAL
+
let intro = [
p(1,3,3),
p(1,3,7),
@@ -51,6 +64,8 @@ let intro = [
p(8,3,7),
]
+# osc = osc_pulse
+
let melody = [
p(1,3,3),
p(1,3,7),
@@ -119,17 +134,6 @@ let melody = [
p(2,4,10),
]
-let bass = [
- p(1,1,3),
- p(1,1,10),
- p(1,1,1),
- p(1,1,8),
- p(1,1,3),
- p(1,2,3),
- p(1,1,1),
- p(1,1,10),
-]
-
let melody2 = [
p(1,0,0),
p(1,5,10),
@@ -273,6 +277,19 @@ let outro = [
p(16,3,7,2),
]
+osc = (f, t: float) => osc_piano(f, t) * GAIN_NORMAL * 1.5
+
+let bass = [
+ p(1,1,3),
+ p(1,1,10),
+ p(1,1,1),
+ p(1,1,8),
+ p(1,1,3),
+ p(1,2,3),
+ p(1,1,1),
+ p(1,1,10),
+]
+
from std/algorithm import sort
# Process all lists of notes
@@ -280,16 +297,16 @@ var music: seq[ProcessedNote] = @[]
music.process(intro, 0, 4)
music.process(melody, 8, 4)
music.process(melody, 24, 4)
-music.process(bass, 24, gain=1.5)
-music.process(bass, 32, gain=1.5)
+music.process(bass, 24)
+music.process(bass, 32)
music.process(melody, 40, 4)
music.process(melody2, 40, 4)
-music.process(bass, 40, gain=1.5)
-music.process(bass, 48, gain=1.5)
+music.process(bass, 40)
+music.process(bass, 48)
music.process(melody, 56, 4)
music.process(melody3, 56, 4)
-music.process(bass, 56, gain=1.5)
-music.process(bass, 64, gain=1.5)
+music.process(bass, 56)
+music.process(bass, 64)
music.process(outro, 72, 4)
music.sortByStart()