sokol.audio: add simple_sin_tones.v example
parent
542b149290
commit
1c9e02094c
|
@ -0,0 +1,42 @@
|
|||
import time
|
||||
import math
|
||||
import sokol.audio
|
||||
|
||||
const (
|
||||
sw = time.new_stopwatch({})
|
||||
sw_start_ms = sw.elapsed().milliseconds()
|
||||
)
|
||||
|
||||
[inline]
|
||||
fn sintone(periods, frame, 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, 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 < 500 {
|
||||
soundbuffer[idx] = sintone(20, frame, num_frames)
|
||||
} else if ms < 1000 {
|
||||
soundbuffer[idx] = sintone(25, frame, num_frames)
|
||||
} else if ms < 1500 {
|
||||
soundbuffer[idx] *= sintone(22, frame, num_frames)
|
||||
} else {
|
||||
soundbuffer[idx] = sintone(25, frame, num_frames)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
audio.setup({
|
||||
stream_cb: my_audio_stream_callback
|
||||
})
|
||||
time.sleep_ms(2500)
|
||||
audio.shutdown()
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,105 @@
|
|||
module audio
|
||||
|
||||
#flag -I @VROOT/thirdparty/sokol
|
||||
#define SOKOL_IMPL
|
||||
#include "sokol_audio.h"
|
||||
#flag linux -lasound
|
||||
//
|
||||
pub type FNStreamingCB = fn (buffer &f32, num_frames, num_channels int)
|
||||
|
||||
pub type FnStreamingCBWithUserData = fn (buffer &f32, num_frames, num_channels int, user_data voidptr)
|
||||
|
||||
//
|
||||
[typedef]
|
||||
pub struct C.saudio_desc {
|
||||
sample_rate int
|
||||
num_channels int
|
||||
buffer_frames int
|
||||
packet_frames int
|
||||
num_packets int
|
||||
stream_cb FNStreamingCB
|
||||
stream_userdata_cb FnStreamingCBWithUserData
|
||||
user_data voidptr
|
||||
}
|
||||
|
||||
fn C.saudio_setup(desc &C.saudio_desc)
|
||||
|
||||
fn C.saudio_shutdown()
|
||||
|
||||
fn C.saudio_isvalid() bool
|
||||
|
||||
fn C.saudio_userdata() voidptr
|
||||
|
||||
fn C.saudio_query_desc() C.saudio_desc
|
||||
|
||||
fn C.saudio_sample_rate() int
|
||||
|
||||
fn C.saudio_buffer_frames() int
|
||||
|
||||
fn C.saudio_channels() int
|
||||
|
||||
fn C.saudio_expect() int
|
||||
|
||||
fn C.saudio_push(frames &f32, num_frames int) int
|
||||
|
||||
// audio.setup - setup sokol-audio
|
||||
pub fn setup(desc C.saudio_desc) {
|
||||
C.saudio_setup(&desc)
|
||||
}
|
||||
|
||||
// audio.shutdown - shutdown sokol-audio
|
||||
pub fn shutdown() {
|
||||
C.saudio_shutdown()
|
||||
}
|
||||
|
||||
// audio.is_valid - true after setup if audio backend was successfully initialized
|
||||
pub fn is_valid() bool {
|
||||
return C.saudio_isvalid()
|
||||
}
|
||||
|
||||
// audio.userdata - return the saudio_desc.user_data pointer
|
||||
pub fn user_data() voidptr {
|
||||
return C.saudio_userdata()
|
||||
}
|
||||
|
||||
// audio.query - return a copy of the original saudio_desc struct
|
||||
pub fn query() C.saudio_desc {
|
||||
return C.saudio_query_desc()
|
||||
}
|
||||
|
||||
// audio.sample_rate - actual sample rate
|
||||
pub fn sample_rate() int {
|
||||
return C.saudio_sample_rate()
|
||||
}
|
||||
|
||||
// audio.buffer_frames - return actual backend buffer size in number of frames
|
||||
pub fn buffer_frames() int {
|
||||
return C.saudio_buffer_frames()
|
||||
}
|
||||
|
||||
// audio.channels - actual number of channels
|
||||
pub fn channels() int {
|
||||
return C.saudio_channels()
|
||||
}
|
||||
|
||||
// audio.expect - get current number of frames to fill packet queue
|
||||
pub fn expect() int {
|
||||
return C.saudio_expect()
|
||||
}
|
||||
|
||||
// audio.push - push sample frames from main thread, returns number of frames actually pushed
|
||||
pub fn push(frames &f32, num_frames int) int {
|
||||
return C.saudio_push(frames, num_frames)
|
||||
}
|
||||
|
||||
//
|
||||
[inline]
|
||||
pub fn fclamp(x, flo, fhi f32) f32 {
|
||||
if x > fhi {
|
||||
return fhi
|
||||
}
|
||||
if x < flo {
|
||||
return flo
|
||||
}
|
||||
return x
|
||||
}
|
Loading…
Reference in New Issue