sokol.audio #
fn buffer_frames #
fn buffer_frames() int
buffer_frames - return the actual backend buffer size in number of frames
fn channels #
fn channels() int
channels - return the actual number of channels
fn expect #
fn expect() int
expect - get current number of frames to fill packet queue; use in combination with audio.push
fn fclamp #
fn fclamp(x f32, flo f32, fhi f32) f32
fclamp - helper function to 'clamp' a number to a certain range
Example
realsample := audio.fclamp(sample, -1.0, 1.0)
fn is_valid #
fn is_valid() bool
is_valid - true after setup if audio backend was successfully initialized
fn max #
fn max(x int, y int) int
max - helper function to return the larger of two numbers
Note: math.max returns f32
values, this returns int
values
Example
larger := audio.max(1, 5) // larger == 5
fn min #
fn min(x int, y int) int
min - helper function to return the smaller of two numbers
Note: math.min returns f32
values, this returns int
values
Example
smaller := audio.min(1, 5) // smaller == 1
fn push #
fn push(frames &f32, num_frames int) int
push - push sample frames from main thread, returns number of frames actually pushed
fn query #
fn query() C.saudio_desc
query - return a copy of the original saudio_desc struct
fn sample_rate #
fn sample_rate() int
sample_rate - return the actual sample rate
fn setup #
fn setup(desc &C.saudio_desc)
setup - setup sokol-audio
fn shutdown #
fn shutdown()
shutdown - shutdown sokol-audio
fn suspended #
fn suspended() bool
suspended returns true if audio context is currently suspended (only in WebAudio backend, all other backends return false)
fn user_data #
fn user_data() voidptr
userdata - return the saudio_desc.user_data pointer
type FNStreamingCB #
type FNStreamingCB = fn (buffer &f32, num_frames int, num_channels int)
callback function for stream_cb
in [C.saudio_desc] when calling audio.setup()
sokol callback functions run in a separate thread
This function will be called with a reference to the C buffer and the maximum number of frames and channels the audio backend is expecting in its buffer.
Terms:- sample - a 32-bit floating point number from -1.0
to +1.0
representing the waveform amplitude at that instant
- frame - one sample for each channel at that instant
To determine the number of samples expected, do num_frames * num_channels
. Then, write up to that many f32
samples into buffer
using unsafe operations.
Do not write more data to the buffer than it is requesting, but you may write less. The buffer is initialized with zeroes, so unwritten data will result in audio silence.
Examples
unsafe { C.memcpy(buffer, &samples, samples.len * int(sizeof(f32))) }
unsafe { mut b := buffer; for i, sample in samples { b[i] = sample } }
fn (FNStreamingCB) str #
fn (x FNStreamingCB) str() string
type FnStreamingCBWithUserData #
type FnStreamingCBWithUserData = fn (buffer &f32, num_frames int, num_channels int, user_data voidptr)
callback function for stream_userdata_cb
to use in C.saudio_desc
when calling audio.setup()
sokol callback functions run in a separate thread
This function operates the same way as [FNStreamingCB] but it passes customizable user_data
to the callback. This is the method to use if your audio data is stored in a struct or array. Identify the user_data
when you call audio.setup()
and that object will be passed to the callback as the last arg.
Examples
mut soundbuffer := []f32
soundbuffer << previously_parsed_wavfile_bytes
audio.setup(stream_userdata_cb: mycallback, user_data: soundbuffer)
fn mycallback(buffer &f32, num_frames int, num_channels int, mut sb []f32) { ... }
fn (FnStreamingCBWithUserData) str #
fn (x FnStreamingCBWithUserData) str() string
struct C.saudio_allocator #
struct C.saudio_allocator {
pub mut:
alloc_fn memory.FnAllocatorAlloc
free_fn memory.FnAllocatorFree
user_data voidptr
}
struct C.saudio_desc #
struct C.saudio_desc {
pub:
sample_rate int
num_channels int
buffer_frames int
packet_frames int
num_packets int
stream_cb FNStreamingCB
stream_userdata_cb FnStreamingCBWithUserData
pub mut:
user_data voidptr
allocator C.saudio_allocator
logger C.saudio_logger
}
only one of stream_cb
or stream_userdata_cb
should be used
default values (internal to sokol C library):
variable | default | note |
---|---|---|
sample_rate | 44100 | higher sample rates take more memory but are higher quality |
num_channels | 1 | for stereo sound, this should be 2 |
buffer_frames | 2048 | buffer size in frames, larger is more latency, smaller means higher CPU |
packet_frames | 128 | push model only, number of frames that will be pushed in each packet |
num_packets | 64 | for push model only, number of packets in the backend ringbuffer |
struct C.saudio_logger #
struct C.saudio_logger {
pub mut:
func memory.FnLogCb
user_data voidptr
}