summaryrefslogtreecommitdiff
path: root/src/audio/audio.cpp
blob: 9e778f17dee3e5ba33c001f3f8aeb34796b4bd9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
#include <math.h>

static ma_device device;
static float phase = 0.0f;

static void audio_callback(ma_device*, void* output, const void*, ma_uint32 frames) {
    int16_t* out = (int16_t*)output;
    const float freq = 440.0f;
    const float sr = 32000.0f;

    for (ma_uint32 i = 0; i < frames; i++) {
        float s = sinf(phase) * 0.2f;
        phase += 2.0f * 3.14159265f * freq / sr;
        if (phase > 2.0f * 3.14159265f) phase -= 2.0f * 3.14159265f;
        out[i] = (int16_t)(s * 32767.0f);
    }
}

void audio_init() {
    ma_device_config cfg = ma_device_config_init(ma_device_type_playback);
    cfg.playback.format = ma_format_s16;
    cfg.playback.channels = 1;
    cfg.sampleRate = 32000;
    cfg.dataCallback = audio_callback;

    ma_device_init(nullptr, &cfg, &device);
    ma_device_start(&device);
}

void audio_update() {}
void audio_shutdown() { ma_device_uninit(&device); }