| 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` |
| 2 | import time |
| 3 | import math |
| 4 | import sokol.audio |
| 5 | |
| 6 | const sw = time.new_stopwatch() |
| 7 | const sw_start_ms = sw.elapsed().milliseconds() |
| 8 | |
| 9 | @[inline] |
| 10 | fn 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 | |
| 14 | fn 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 | |
| 32 | fn main() { |
| 33 | audio.setup( |
| 34 | stream_cb: my_audio_stream_callback |
| 35 | ) |
| 36 | time.sleep(2000 * time.millisecond) |
| 37 | audio.shutdown() |
| 38 | } |
| 39 | |