From 86816b1aad972efaecd74581d249b7ed2b5bc472 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Sat, 10 Oct 2020 19:36:48 +0300 Subject: [PATCH] examples: add bytebeat melody using sokol.audio --- examples/sokol/sounds/melody.v | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 examples/sokol/sounds/melody.v diff --git a/examples/sokol/sounds/melody.v b/examples/sokol/sounds/melody.v new file mode 100644 index 0000000000..992d4bf264 --- /dev/null +++ b/examples/sokol/sounds/melody.v @@ -0,0 +1,35 @@ +import os +import sokol.audio + +struct AContext { +mut: + frame_0 int +} + +fn my_audio_stream_callback(buffer &f32, num_frames, num_channels int, mut acontext AContext) { + mut soundbuffer := buffer + for frame := 0; frame < num_frames; frame++ { + t := int(f32(acontext.frame_0 + frame) * 0.245) + // Credits for the formula below: https://www.youtube.com/watch?v=V4GfkFbDojc + // "Techno" by Gabriel Miceli + y := (t * (((t / 10 | 0) ^ ((t / 10 | 0) - + 1280)) % 11) / 2 & 127) + + (t * (((t / 640 | 0) ^ ((t / 640 | 0) - 2)) % 13) / 2 & 127) + for ch := 0; ch < num_channels; ch++ { + idx := frame * num_channels + ch + unsafe { + soundbuffer[idx] = f32(byte(y) - 127) / 255.0 + } + } + } + acontext.frame_0 += num_frames +} + +fn main() { + audio.setup({ + stream_userdata_cb: my_audio_stream_callback + user_data: &AContext{} + }) + os.input('Press Enter to exit') + audio.shutdown() +}