summaryrefslogtreecommitdiff
path: root/src/audio/audio.cc
blob: 434171264748c637546355dadf45cd2c2ed4192c (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// This file is part of the 64k demo project.
// It manages the low-level audio device and high-level audio state.
// Implementation uses miniaudio for cross-platform support.

#include "audio.h"

#ifndef DEMO_BUILD_TOOLS
#define MA_NO_FLAC
#define MA_NO_ENCODING
#define MA_NO_MP3
#define MA_NO_WAV
#endif
#define MINIAUDIO_IMPLEMENTATION
#include "miniaudio.h"
#include "synth.h"

#include <stdio.h>

static ma_device g_device;

void audio_data_callback(ma_device *pDevice, void *pOutput, const void *pInput,
                         ma_uint32 frameCount) {
  (void)pInput;
  float *fOutput = (float *)pOutput;
  synth_render(fOutput, (int)frameCount);
}

void audio_init() {
  synth_init();

  ma_device_config config = ma_device_config_init(ma_device_type_playback);
  config.playback.format = ma_format_f32;
  config.playback.channels = 2;
  config.sampleRate = 32000;
  config.dataCallback = audio_data_callback;

  if (ma_device_init(NULL, &config, &g_device) != MA_SUCCESS) {
    printf("Failed to open playback device.\n");
    return;
  }
}

void audio_start() {
  if (ma_device_start(&g_device) != MA_SUCCESS) {
    printf("Failed to start playback device.\n");
    ma_device_uninit(&g_device);
    return;
  }
}

#ifndef STRIP_ALL
void audio_render_silent(float duration_sec) {
  const int sample_rate = 32000;
  const int chunk_size = 512;
  int total_frames = (int)(duration_sec * sample_rate);
  float buffer[chunk_size * 2]; // Stereo

  while (total_frames > 0) {
    int frames_to_render =
        (total_frames > chunk_size) ? chunk_size : total_frames;
    synth_render(buffer, frames_to_render);
    total_frames -= frames_to_render;
  }
}
#endif

void audio_update() {
}

void audio_shutdown() {
  ma_device_stop(&g_device);
  ma_device_uninit(&g_device);
  synth_shutdown();
}