aboutsummaryrefslogtreecommitdiff
path: root/src/audio.h
blob: 2f70e8fcc3b1e9582ed93eb999028a85f4110319 (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
#ifndef PW_AUDIO_H
#define PW_AUDIO_H

#include <SDL3/SDL_audio.h>

#define PW_AUDIO_BUFFER_COUNT 8
#define PW_AUDIO_BUFFER_MAX 4096

/**
 * An audio buffer that we pass to the SDL audio stream.
 *
 * This allows buffer reuse and avoid double copying from Web Assembly
 * linear memory (where a copy is required in order to be portable on
 * big-endian platforms where data is written backwards).
 */
struct pw_audio_buffer {
  bool inuse;
  int16_t data[PW_AUDIO_BUFFER_MAX];
};

/**
 * PICO-8 audio stream.
 */
struct pw_audio {
  SDL_AudioStream *stream;
  uint32_t size;
  uint32_t lock;
  struct pw_audio_buffer buffers[PW_AUDIO_BUFFER_COUNT];
};

/**
 * Initializes the PICO-8 audio stream.
 */
struct pw_audio *pw_audio_init(void);

/**
 * Frees the PICO-8 audio stream and all of its resources.
 */
void pw_audio_free(struct pw_audio *audio);

/**
 * Starts playing audio from the stream with the specified buffer size.
 */
bool pw_audio_start(struct pw_audio *audio, uint32_t size);

/**
 * Stops playing audio from the stream.
 */
bool pw_audio_stop(struct pw_audio *audio);

/**
 * Whether or not the audio system is ready.
 */
bool pw_audio_ready(struct pw_audio *audio);

/**
 * Locks the audio device returning a buffer to write samples to.
 *
 * The audio stream expects `len` 16-bit samples to be written.
 */
int16_t *pw_audio_lock(struct pw_audio *audio, uint32_t *len);

/**
 * Unlocks the audio device.
 */
bool pw_audio_unlock(struct pw_audio *audio);

#endif