2020-08-23 15:28:03 +03:00
|
|
|
import time
|
|
|
|
import math
|
|
|
|
import sokol.audio
|
|
|
|
|
|
|
|
const (
|
2021-07-20 11:17:08 +03:00
|
|
|
sw = time.new_stopwatch()
|
2020-08-23 15:28:03 +03:00
|
|
|
sw_start_ms = sw.elapsed().milliseconds()
|
|
|
|
)
|
|
|
|
|
|
|
|
[inline]
|
2020-10-26 14:14:21 +03:00
|
|
|
fn sintone(periods int, frame int, num_frames int) f32 {
|
2020-08-23 15:28:03 +03:00
|
|
|
return math.sinf(f32(periods) * (2 * math.pi) * f32(frame) / f32(num_frames))
|
|
|
|
}
|
|
|
|
|
2020-10-26 14:14:21 +03:00
|
|
|
fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int) {
|
2020-08-23 15:28:03 +03:00
|
|
|
ms := sw.elapsed().milliseconds() - sw_start_ms
|
|
|
|
unsafe {
|
|
|
|
mut soundbuffer := buffer
|
|
|
|
for frame := 0; frame < num_frames; frame++ {
|
|
|
|
for ch := 0; ch < num_channels; ch++ {
|
|
|
|
idx := frame * num_channels + ch
|
2021-02-21 18:05:03 +03:00
|
|
|
if ms < 250 {
|
|
|
|
soundbuffer[idx] = 0.5 * sintone(20, frame, num_frames)
|
|
|
|
} else if ms < 300 {
|
|
|
|
soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames)
|
2020-08-23 15:28:03 +03:00
|
|
|
} else if ms < 1500 {
|
|
|
|
soundbuffer[idx] *= sintone(22, frame, num_frames)
|
|
|
|
} else {
|
2021-02-21 18:05:03 +03:00
|
|
|
soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames)
|
2020-08-23 15:28:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-02-21 18:05:03 +03:00
|
|
|
audio.setup(
|
2020-08-23 15:28:03 +03:00
|
|
|
stream_cb: my_audio_stream_callback
|
2021-02-21 18:05:03 +03:00
|
|
|
)
|
2021-02-27 20:41:06 +03:00
|
|
|
time.sleep(2000 * time.millisecond)
|
2020-08-23 15:28:03 +03:00
|
|
|
audio.shutdown()
|
|
|
|
}
|