-
Notifications
You must be signed in to change notification settings - Fork 77
/
linux-audio.c
313 lines (246 loc) · 9.5 KB
/
linux-audio.c
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
////////////////////////////////////////////////////////////////////////////////////
// The MIT License (MIT)
//
// Copyright (c) 2021 Tarek Sherif
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////////
#include <alloca.h>
#include <alsa/asoundlib.h>
#include <pthread.h>
#include "../../shared/constants.h"
#include "../../shared/utils.h"
#include "../../shared/debug.h"
#include "../../shared/data.h"
#include "../../shared/platform-interface.h"
#include "linux-audio.h"
//////////////////////////////////////////////////////////////
// Uses ALSA and pthread:
// - https://www.alsa-project.org/alsa-doc/alsa-lib/pcm.html
// - https://en.wikipedia.org/wiki/Pthreads
//////////////////////////////////////////////////////////////
#define MIX_BUFFER_FRAMES 2048
typedef struct {
int16_t* data;
int32_t count;
int32_t cursor;
bool loop;
} AudioStream;
static struct {
Data_Buffer data[SPACE_SHOOTER_AUDIO_MAX_SOUNDS];
int32_t count;
} sounds;
static struct {
pthread_t handle;
struct {
AudioStream sounds[SPACE_SHOOTER_AUDIO_MIXER_CHANNELS];
int32_t count;
pthread_mutex_t lock;
} queue;
struct {
pthread_mutex_t lock;
bool signalled;
} shutdown;
bool initialized;
} threadInterface;
static void *audioThread(void* args) {
snd_pcm_t* device = NULL;
struct {
AudioStream channels[SPACE_SHOOTER_AUDIO_MIXER_CHANNELS];
int32_t count;
int16_t buffer[MIX_BUFFER_FRAMES * 2];
} mixer = { 0 };
/////////////////////////////////////
// Open audio device and set to:
// - 16-bit
// - 44.1k
// - stereo
// - 2k sample (~50ms) buffer size
/////////////////////////////////////
if (snd_pcm_open(&device, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0) {
goto EXIT_NO_RESOURCES;
}
snd_pcm_hw_params_t *deviceParams = NULL;
snd_pcm_hw_params_alloca(&deviceParams);
snd_pcm_hw_params_any(device, deviceParams);
if (snd_pcm_hw_params_set_access(device, deviceParams, SND_PCM_ACCESS_RW_INTERLEAVED) < 0) {
goto EXIT_DEVICE;
}
if (snd_pcm_hw_params_set_format(device, deviceParams, SND_PCM_FORMAT_S16_LE) < 0) {
goto EXIT_DEVICE;
}
if (snd_pcm_hw_params_set_rate(device, deviceParams, 44100, 0) < 0) {
goto EXIT_DEVICE;
}
if (snd_pcm_hw_params_set_channels(device, deviceParams, 2) < 0) {
goto EXIT_DEVICE;
}
if (snd_pcm_hw_params_set_buffer_size(device, deviceParams, MIX_BUFFER_FRAMES) < 0) {
goto EXIT_DEVICE;
}
if (snd_pcm_hw_params(device, deviceParams) < 0) {
goto EXIT_DEVICE;
}
bool running = true;
while (running) {
//////////////////////////////////////
// Copy queued audio into mixer
//////////////////////////////////////
pthread_mutex_lock(&threadInterface.queue.lock);
if (threadInterface.queue.count > 0) {
int32_t channelsAvailable = SPACE_SHOOTER_AUDIO_MIXER_CHANNELS - mixer.count;
int32_t copyCount = channelsAvailable < threadInterface.queue.count ? channelsAvailable : threadInterface.queue.count;
for (int32_t i = 0; i < copyCount; ++i) {
mixer.channels[mixer.count] = threadInterface.queue.sounds[i];
++mixer.count;
}
threadInterface.queue.count = 0;
}
pthread_mutex_unlock(&threadInterface.queue.lock);
//////////////////////////////////////
// Simple additive mix with clipping
//////////////////////////////////////
int32_t numSamples = MIX_BUFFER_FRAMES * 2;
for (int32_t i = 0; i < numSamples; ++i) {
mixer.buffer[i] = 0;
}
for (int32_t i = 0; i < mixer.count; ++i) {
AudioStream* channel = mixer.channels + i;
DEBUG_ASSERT(channel->count > 0, "linux-audio.c: Mixer should not be playing empty sounds.")
for (int32_t i = 0; i < numSamples; ++i) {
if (channel->cursor == channel->count) {
if (channel->loop) {
channel->cursor = 0;
} else {
break;
}
}
int32_t sample = mixer.buffer[i] + channel->data[channel->cursor];
if (sample < INT16_MIN) {
sample = INT16_MIN;
}
if (sample > INT16_MAX) {
sample = INT16_MAX;
}
mixer.buffer[i] = sample;
++channel->cursor;
}
}
//////////////////////////////////////
// Handle streams that have finished.
//////////////////////////////////////
for (int32_t i = mixer.count - 1; i >= 0; --i) {
AudioStream* channel = mixer.channels + i;
if (channel->cursor == channel->count && !channel->loop) {
//////////////////////////////////////////////////////////////
// "Delete" stream by swapping to past the end of the array.
//////////////////////////////////////////////////////////////
int32_t last = mixer.count - 1;
mixer.channels[i].data = mixer.channels[last].data;
mixer.channels[i].count = mixer.channels[last].count;
mixer.channels[i].cursor = mixer.channels[last].cursor;
mixer.channels[i].loop = mixer.channels[last].loop;
--mixer.count;
}
}
// This blocks until the device needs more data
if (snd_pcm_writei(device, mixer.buffer, MIX_BUFFER_FRAMES) < 0) {
snd_pcm_prepare(device);
}
pthread_mutex_lock(&threadInterface.shutdown.lock);
if (threadInterface.shutdown.signalled) {
running = false;
}
pthread_mutex_unlock(&threadInterface.shutdown.lock);
}
EXIT_DEVICE:
snd_pcm_drop(device);
snd_pcm_close(device);
EXIT_NO_RESOURCES:
return NULL;
}
bool linux_initAudio(void) {
////////////////////////
// Create audio thread
////////////////////////
if (pthread_mutex_init(&threadInterface.queue.lock, NULL)) {
goto ERROR_NO_RESOURCES;
}
if (pthread_mutex_init(&threadInterface.shutdown.lock, NULL)) {
goto ERROR_QUEUE_LOCK;
}
if (pthread_create(&threadInterface.handle, NULL, audioThread, NULL)) {
goto ERROR_SHUTDOWN_LOCK;
}
threadInterface.initialized = true;
/////////////
// Success!
/////////////
return true;
///////////////////
// Error handling
///////////////////
ERROR_SHUTDOWN_LOCK:
pthread_mutex_destroy(&threadInterface.shutdown.lock);
ERROR_QUEUE_LOCK:
pthread_mutex_destroy(&threadInterface.queue.lock);
ERROR_NO_RESOURCES:
return false;
}
int32_t platform_loadSound(const char* fileName) {
DEBUG_ASSERT(sounds.count < SPACE_SHOOTER_AUDIO_MAX_SOUNDS, "Attempting to load too many sounds.");
int32_t id = sounds.count;
if (!utils_loadWavData(fileName, sounds.data + id)) {
return -1;
}
++sounds.count;
return id;
}
void platform_playSound(int32_t id, bool loop) {
if (!threadInterface.initialized) {
return;
}
if (!sounds.data[id].data) {
return;
}
////////////////////////
// Add sound to queue
////////////////////////
pthread_mutex_lock(&threadInterface.queue.lock);
if (threadInterface.queue.count < SPACE_SHOOTER_AUDIO_MIXER_CHANNELS) {
int32_t soundIndex = threadInterface.queue.count;
threadInterface.queue.sounds[soundIndex].data = (int16_t *) sounds.data[id].data;
threadInterface.queue.sounds[soundIndex].count = sounds.data[id].size / 2;
threadInterface.queue.sounds[soundIndex].cursor = 0;
threadInterface.queue.sounds[soundIndex].loop = loop;
++threadInterface.queue.count;
}
pthread_mutex_unlock(&threadInterface.queue.lock);
}
void linux_closeAudio(void) {
if (!threadInterface.initialized) {
return;
}
pthread_mutex_lock(&threadInterface.shutdown.lock);
threadInterface.shutdown.signalled = true;
pthread_mutex_unlock(&threadInterface.shutdown.lock);
pthread_join(threadInterface.handle, NULL);
pthread_mutex_destroy(&threadInterface.queue.lock);
pthread_mutex_destroy(&threadInterface.shutdown.lock);
threadInterface.initialized = false;
}