diff options
-rw-r--r-- | source/saw/main.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/source/saw/main.c b/source/saw/main.c index 691307a..d32188f 100644 --- a/source/saw/main.c +++ b/source/saw/main.c @@ -26,7 +26,34 @@ #define MINIAUDIO_IMPLEMENTATION #include "../thirdparty/miniaudio.h" +#include "../kit/math.h" + +enum { + SAW_CHANNEL_COUNT = 2, + SAW_SAMPLE_RATE = 44100, +}; + static struct NVGcontext *saw_nvg; +static ma_device saw_ma; + +static void saw_audio(ma_device *device, void *void_out_, + void const *void_in_, ma_uint32 frame_count) { + static i64 t = 0; + + f64 period = M_PI * 2.; + f64 amplitude = .2; + f64 frequency = 240.; + + f32 *out = (f32 *) void_out_; + + for (i64 i = 0; i < frame_count; i++) { + f64 k = (period * frequency) / SAW_SAMPLE_RATE; + out[i * 2] = (f32) (sin(k * t) * amplitude); + out[i * 2 + 1] = (f32) (sin(k * t) * amplitude); + + t++; + } +} static void saw_init(void) { #ifdef SOKOL_GLCORE33 @@ -34,6 +61,22 @@ static void saw_init(void) { #else saw_nvg = nvgCreateGLES3(NVG_ANTIALIAS | NVG_STENCIL_STROKES); #endif + + ma_device_config config = ma_device_config_init( + ma_device_type_playback); + + config.playback.format = ma_format_f32; + config.playback.channels = SAW_CHANNEL_COUNT; + config.sampleRate = SAW_SAMPLE_RATE; + config.dataCallback = saw_audio; + config.pUserData = NULL; + + if (ma_device_init(NULL, &config, &saw_ma) != MA_SUCCESS) { + printf("ma_device_init failed.\n"); + return; + } + + ma_device_start(&saw_ma); } static void saw_frame(void) { @@ -57,6 +100,8 @@ static void saw_frame(void) { } static void saw_cleanup(void) { + ma_device_uninit(&saw_ma); + #ifdef SOKOL_GLCORE33 nvgDeleteGL3(saw_nvg); #else |