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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
#include <SDL3/SDL_log.h>
#include "audio.h"
struct pw_audio *pw_audio_init(void) {
struct pw_audio *audio = SDL_calloc(1, sizeof(*audio));
if (!audio) {
return NULL;
}
// PICO-8 exposes a `_mix(len0, len1)` function for copying audio data
// from an internal buffer to the host's audio context. From the
// Emscripten bundle, we see a few things:
//
// - It takes two lengths, one is the number of samples at the internal
// sample rate, and the other at the external sample rate
// - The internal sample rate is always 22050 Hz and uses 16-bit signed
// integer samples
//
// For us, this means we can let SDL do all the resampling for us and
// just choose a format and sample rate that matches PICO-8, meaning no
// resampling is done by the engine, and we can just call `_mix(l, l)`.
SDL_AudioSpec spec = {
.format = SDL_AUDIO_S16,
.channels = 1,
.freq = 22050,
};
audio->stream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK,
&spec, NULL, NULL);
if (!audio->stream) {
SDL_LogWarn(0, "starting without audio");
}
return audio;
}
void pw_audio_free(struct pw_audio *audio) {
if (!audio) {
SDL_InvalidParamError("error");
return;
}
if (audio->stream) {
SDL_DestroyAudioStream(audio->stream);
}
SDL_free(audio);
}
bool pw_audio_start(struct pw_audio *audio, uint32_t size) {
if (!audio || !audio->stream || audio->size) {
return SDL_InvalidParamError("audio");
}
bool ok = SDL_ResumeAudioStreamDevice(audio->stream);
if (ok) {
audio->size = size;
}
return ok;
}
static void destroy_stream(struct pw_audio *audio) {
SDL_DestroyAudioStream(audio->stream);
audio->stream = NULL;
audio->size = 0;
}
bool pw_audio_stop(struct pw_audio *audio) {
if (!audio || !audio->stream) {
return SDL_InvalidParamError("audio");
}
if (!audio->size) {
return true;
}
// If we fail to pause and clear the stream, then there is clearly
// something wrong with it, so mark the stream as destroyed meaning
// our audio device is dead.
if (!SDL_PauseAudioStreamDevice(audio->stream) ||
!SDL_ClearAudioStream(audio->stream)) {
SDL_LogCritical(0, "audio stream cannot be stopped");
destroy_stream(audio);
return false;
}
audio->size = 0;
return true;
}
bool pw_audio_ready(struct pw_audio *audio) {
if (!audio) {
return SDL_InvalidParamError("error");
}
return audio->stream && audio->size;
}
static bool unlock_stream(struct pw_audio *audio) {
bool ok = SDL_UnlockAudioStream(audio->stream);
// If we get in this state, something is seriously weird, and means
// we will likely not recover audio. Log and close the audio device
// to prevent further use.
if (!ok) {
SDL_LogCritical(0, "audio stream mutex poisoned: %s", SDL_GetError());
destroy_stream(audio);
}
return ok;
}
static int16_t no_samples[] = {0};
int16_t *pw_audio_lock(struct pw_audio *audio, uint32_t *len) {
if (!audio || !audio->stream || !audio->size || audio->lock) {
SDL_InvalidParamError("audio");
return NULL;
}
if (!len) {
SDL_InvalidParamError("len");
return NULL;
}
// Pick a buffer to use for collecting samples, since the `inuse` flag
// is reset by an audio callback, we need to make sure to hold a lock
// to the audio device to prevent threading issues.
if (!SDL_LockAudioStream(audio->stream)) {
return false;
}
int16_t *data = NULL;
uint32_t request = 0;
int queued = SDL_GetAudioStreamQueued(audio->stream);
if (queued < 0) {
goto cleanup;
}
uint32_t q =
(uint32_t)SDL_min(queued, INT32_MAX) / sizeof(*audio->buffers->data);
if (q >= audio->size) {
data = no_samples;
goto cleanup;
}
size_t i = 0;
struct pw_audio_buffer *buffer = NULL;
for (i = 0; i < SDL_arraysize(audio->buffers); ++i) {
if (!audio->buffers[i].inuse) {
buffer = &audio->buffers[i];
break;
}
}
if (!buffer) {
SDL_SetError("no remaining audio buffers");
goto cleanup;
}
data = buffer->data;
request = SDL_min(audio->size - q, PW_AUDIO_BUFFER_MAX);
audio->lock = i * PW_AUDIO_BUFFER_MAX + request;
buffer->inuse = true;
cleanup:
if (!unlock_stream(audio)) {
return NULL;
}
if (data) {
*len = request;
}
return data;
}
static void data_complete(void *userdata, const void *buf, int buflen) {
(void)buflen;
struct pw_audio *audio = userdata;
for (size_t i = 0; i < SDL_arraysize(audio->buffers); ++i) {
struct pw_audio_buffer *b = audio->buffers + i;
if (b->data == buf && b->inuse) {
b->inuse = false;
return;
}
}
SDL_LogWarn(0, "could not find completed in-use buffer");
}
bool pw_audio_unlock(struct pw_audio *audio) {
if (!audio || !audio->stream || !audio->size || !audio->lock) {
return SDL_InvalidParamError("audio");
}
size_t i = (audio->lock - 1) / PW_AUDIO_BUFFER_MAX;
size_t len = (audio->lock - 1) % PW_AUDIO_BUFFER_MAX + 1;
if (!SDL_LockAudioStream(audio->stream)) {
return false;
}
bool ok = false;
struct pw_audio_buffer *buffer = audio->buffers + i;
if (!buffer->inuse) {
goto cleanup;
}
size_t sz = len * sizeof(*buffer->data);
ok = SDL_PutAudioStreamDataNoCopy(audio->stream, buffer->data, sz,
data_complete, audio);
if (!ok) {
buffer->inuse = false;
}
cleanup:
audio->lock = 0;
return unlock_stream(audio) && ok;
}
|