-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.v
44 lines (40 loc) · 966 Bytes
/
player.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module vmidi
import audio
import sync
import time
import math
fn (track Track)play(mut ctx audio.Context, mut wg sync.WaitGroup, micros_per_tick int) {
for event in track.data {
if event.is_event() {
match event {
NoteOn {
note := f32(440 * math.pow(2, (f32(event.note) - 69) / 12))
sleep := event.delta_time * u64(micros_per_tick)
time.sleep(int(sleep) * time.millisecond)
if event.velocity != 0 {
ctx.play(note, 0.2)
} else {
ctx.pause(note)
}
}
NoteOff {
note := f32(440 * math.pow(2, (f32(event.note) - 69) / 12))
sleep := event.delta_time * u64(micros_per_tick)
time.sleep(int(sleep) * time.millisecond)
ctx.pause(note)
}
else {}
}
}
}
wg.done()
}
pub fn (midi Midi)play() {
mut ctx := audio.new_context()
mut wg := sync.new_waitgroup()
wg.add(midi.tracks.len)
for track in midi.tracks {
go track.play(mut ctx, mut wg, midi.micros_per_tick)
}
wg.wait()
}