v2 / examples / sokol / sounds / simple_sin_tones.v
38 lines · 34 sloc · 1.13 KB · e1b7cb16f36095dbe0ed62c9c7341850d78ae507
Raw
1// vtest build: !openbsd && !sanitize-memory-clang // Fails compilation with: `ld: /lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line`
2import time
3import math
4import sokol.audio
5
6const sw = time.new_stopwatch()
7const sw_start_ms = sw.elapsed().milliseconds()
8
9@[inline]
10fn sintone(periods int, frame int, num_frames int) f32 {
11 return math.sinf(f32(periods) * (2 * math.pi) * f32(frame) / f32(num_frames))
12}
13
14fn my_audio_stream_callback(mut soundbuffer &f32, num_frames int, num_channels int) {
15 ms := sw.elapsed().milliseconds() - sw_start_ms
16 for frame := 0; frame < num_frames; frame++ {
17 for ch := 0; ch < num_channels; ch++ {
18 idx := frame * num_channels + ch
19 if ms < 250 {
20 soundbuffer[idx] = 0.5 * sintone(20, frame, num_frames)
21 } else if ms < 300 {
22 soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames)
23 } else if ms < 1500 {
24 soundbuffer[idx] *= sintone(22, frame, num_frames)
25 } else {
26 soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames)
27 }
28 }
29 }
30}
31
32fn main() {
33 audio.setup(
34 stream_cb: my_audio_stream_callback
35 )
36 time.sleep(2000 * time.millisecond)
37 audio.shutdown()
38}
39