v/vlib/sokol
2024-01-01 23:19:55 +02:00
..
audio all: update attributes to use new syntax 2023-11-15 16:16:01 +11:00
c sokol: fix compiling gg and other graphical examples on OpenBSD (#20333) 2024-01-01 23:19:55 +02:00
f thirdparty: update all sokol and fontstash headers with their upstream versions (#16940) 2023-01-11 11:29:38 +02:00
gfx all: update attributes to use new syntax 2023-11-15 16:16:01 +11:00
memory os,picohttpparser,sokol,strconv: prepare for making -W impure-v the default (#19846) 2023-11-12 09:52:14 +02:00
sapp all: unwrap const() blocks 2023-11-25 10:02:51 +03:00
sfons all: update attributes to use new syntax 2023-11-15 16:16:01 +11:00
sgl all: update attributes to use new syntax 2023-11-15 16:16:01 +11:00
README.md ci: run VAUTOFIX=1 ./v check-md -hide-warnings . to fix doc failures 2023-11-25 09:34:46 +02:00
sokol.v all: unwrap const() blocks 2023-11-25 10:02:51 +03:00

Description:

sokol is a thin wrapper around sokol, which in turn is a library of "Simple STB-style cross-platform libraries for C and C++, written in C.", that provide access to graphics/audio/input processing.

Each .h file in the sokol source code is well-documented as can be seen here:

sokol_audio.h

Example from @VROOTDIR/examples/sokol/sounds/simple_sin_tones.v:

import time
import math
import sokol.audio

const sw = time.new_stopwatch()
const sw_start_ms = sw.elapsed().milliseconds()

@[inline]
fn sintone(periods int, frame int, num_frames int) f32 {
	return math.sinf(f32(periods) * (2 * math.pi) * f32(frame) / f32(num_frames))
}

fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int) {
	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
				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)
				} else if ms < 1500 {
					soundbuffer[idx] *= sintone(22, frame, num_frames)
				} else {
					soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames)
				}
			}
		}
	}
}

fn main() {
	audio.setup(
		stream_cb: my_audio_stream_callback
	)
	time.sleep(2000 * time.millisecond)
	audio.shutdown()
}