-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminiaudio.cpp
377 lines (284 loc) · 8.28 KB
/
miniaudio.cpp
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "Audio.h"
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#define MINIAUDIO_IMPLEMENTATION
#define MA_NO_DECODING
#define MA_API static
#include "miniaudio.h"
#include "Organya.h"
#include "WindowsWrapper.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define CLAMP(x, y, z) MIN(MAX((x), (y)), (z))
#ifdef __GNUC__
#define ATTR_HOT __attribute__((hot))
#else
#define ATTR_HOT
#endif
struct AudioBackend_Sound
{
unsigned char *samples;
size_t frames;
double position;
double advance_delta;
BOOL playing;
BOOL looping;
unsigned int frequency;
float volume;
float pan_l;
float pan_r;
float volume_l;
float volume_r;
struct AudioBackend_Sound *next;
};
static AudioBackend_Sound *sound_list_head;
static ma_device device;
static ma_mutex mutex;
static ma_mutex organya_mutex;
static unsigned long output_frequency;
static unsigned short organya_timer;
static double MillibelToScale(long volume)
{
// Volume is in hundredths of decibels, from 0 to -10000
volume = CLAMP(volume, -10000, 0);
return pow(10.0, volume / 2000.0);
}
static void SetSoundFrequency(AudioBackend_Sound *sound, unsigned int frequency)
{
sound->frequency = frequency;
sound->advance_delta = (double)frequency / (double)output_frequency;
}
static void SetSoundVolume(AudioBackend_Sound *sound, long volume)
{
sound->volume = (float)MillibelToScale(volume);
sound->volume_l = sound->pan_l * sound->volume;
sound->volume_r = sound->pan_r * sound->volume;
}
static void SetSoundPan(AudioBackend_Sound *sound, long pan)
{
sound->pan_l = (float)MillibelToScale(-pan);
sound->pan_r = (float)MillibelToScale(pan);
sound->volume_l = sound->pan_l * sound->volume;
sound->volume_r = sound->pan_r * sound->volume;
}
// Most CPU-intensive function in the game (2/3rd CPU time consumption in my experience), so marked with attrHot so the compiler considers it a hot spot (as it is) when optimizing
ATTR_HOT static void MixSounds(float *stream, unsigned int frames_total)
{
ma_mutex_lock(&mutex);
for (AudioBackend_Sound *sound = sound_list_head; sound != NULL; sound = sound->next)
{
if (sound->playing)
{
float *steam_pointer = stream;
for (unsigned int frames_done = 0; frames_done < frames_total; ++frames_done)
{
// Get two samples, and normalise them to 0-1
const float sample1 = (sound->samples[(size_t)sound->position] - 128.0f) / 128.0f;
const float sample2 = (sound->samples[(size_t)sound->position + 1] - 128.0f) / 128.0f;
// Perform linear interpolation
const float interpolated_sample = sample1 + ((sample2 - sample1) * fmod((float)sound->position, 1.0f));
*steam_pointer++ += interpolated_sample * sound->volume_l;
*steam_pointer++ += interpolated_sample * sound->volume_r;
sound->position += sound->advance_delta;
if (sound->position >= sound->frames)
{
if (sound->looping)
{
sound->position = fmod(sound->position, (double)sound->frames);
}
else
{
sound->playing = FALSE;
sound->position = 0.0;
break;
}
}
}
}
}
ma_mutex_unlock(&mutex);
}
static void Callback(ma_device *device, void *output_stream, const void *input_stream, ma_uint32 frames_total)
{
(void)device;
(void)input_stream;
float *stream = (float*)output_stream;
ma_mutex_lock(&organya_mutex);
if (organya_timer == 0)
{
MixSounds(stream, frames_total);
}
else
{
// Synchronise audio generation with Organya.
// In the original game, Organya ran asynchronously in a separate thread,
// firing off commands to DirectSound in realtime. To match that, we'd
// need a very low-latency buffer, otherwise we'd get mistimed instruments.
// Instead, we can just do this.
unsigned int frames_done = 0;
while (frames_done != frames_total)
{
static unsigned long organya_countdown;
if (organya_countdown == 0)
{
organya_countdown = (organya_timer * output_frequency) / 1000; // organya_timer is in milliseconds, so convert it to audio frames
UpdateOrganya();
}
const unsigned int frames_to_do = MIN(organya_countdown, frames_total - frames_done);
MixSounds(stream + frames_done * 2, frames_to_do);
frames_done += frames_to_do;
organya_countdown -= frames_to_do;
}
}
ma_mutex_unlock(&organya_mutex);
}
BOOL AudioBackend_Init(void)
{
ma_device_config config = ma_device_config_init(ma_device_type_playback);
config.playback.pDeviceID = NULL;
config.playback.format = ma_format_f32;
config.playback.channels = 2;
config.sampleRate = 0;
config.dataCallback = Callback;
config.pUserData = NULL;
config.performanceProfile = ma_performance_profile_conservative;
if (ma_device_init(NULL, &config, &device) == MA_SUCCESS)
{
output_frequency = device.sampleRate;
if (ma_mutex_init(device.pContext, &mutex) == MA_SUCCESS)
{
if (ma_mutex_init(device.pContext, &organya_mutex) == MA_SUCCESS)
{
if (ma_device_start(&device) == MA_SUCCESS)
return TRUE;
ma_mutex_uninit(&organya_mutex);
}
ma_mutex_uninit(&mutex);
}
ma_device_uninit(&device);
}
return FALSE;
}
void AudioBackend_Deinit(void)
{
ma_device_stop(&device);
ma_mutex_uninit(&organya_mutex);
ma_mutex_uninit(&mutex);
ma_device_uninit(&device);
}
AudioBackend_Sound* AudioBackend_CreateSound(unsigned int frequency, size_t frames)
{
AudioBackend_Sound *sound = (AudioBackend_Sound*)malloc(sizeof(AudioBackend_Sound));
if (sound == NULL)
return NULL;
sound->samples = (unsigned char*)malloc(frames + 1);
if (sound->samples == NULL)
{
free(sound);
return NULL;
}
sound->frames = frames;
sound->playing = FALSE;
sound->position = 0.0;
SetSoundFrequency(sound, frequency);
SetSoundVolume(sound, 0);
SetSoundPan(sound, 0);
ma_mutex_lock(&mutex);
sound->next = sound_list_head;
sound_list_head = sound;
ma_mutex_unlock(&mutex);
return sound;
}
void AudioBackend_DestroySound(AudioBackend_Sound *sound)
{
if (sound == NULL)
return;
ma_mutex_lock(&mutex);
for (AudioBackend_Sound **sound_pointer = &sound_list_head; *sound_pointer != NULL; sound_pointer = &(*sound_pointer)->next)
{
if (*sound_pointer == sound)
{
*sound_pointer = sound->next;
free(sound->samples);
free(sound);
break;
}
}
ma_mutex_unlock(&mutex);
}
unsigned char* AudioBackend_LockSound(AudioBackend_Sound *sound, size_t *size)
{
if (sound == NULL)
return NULL;
ma_mutex_lock(&mutex);
if (size != NULL)
*size = sound->frames;
return sound->samples;
}
void AudioBackend_UnlockSound(AudioBackend_Sound *sound)
{
if (sound == NULL)
return;
ma_mutex_unlock(&mutex);
}
void AudioBackend_PlaySound(AudioBackend_Sound *sound, BOOL looping)
{
if (sound == NULL)
return;
ma_mutex_lock(&mutex);
sound->playing = TRUE;
sound->looping = looping;
sound->samples[sound->frames] = looping ? sound->samples[0] : 0x80; // For the linear interpolator
ma_mutex_unlock(&mutex);
}
void AudioBackend_StopSound(AudioBackend_Sound *sound)
{
if (sound == NULL)
return;
ma_mutex_lock(&mutex);
sound->playing = FALSE;
sound->position = 0.0;
ma_mutex_unlock(&mutex);
}
void AudioBackend_RewindSound(AudioBackend_Sound *sound)
{
if (sound == NULL)
return;
ma_mutex_lock(&mutex);
sound->position = 0.0;
ma_mutex_unlock(&mutex);
}
void AudioBackend_SetSoundFrequency(AudioBackend_Sound *sound, unsigned int frequency)
{
if (sound == NULL)
return;
ma_mutex_lock(&mutex);
SetSoundFrequency(sound, frequency);
ma_mutex_unlock(&mutex);
}
void AudioBackend_SetSoundVolume(AudioBackend_Sound *sound, long volume)
{
if (sound == NULL)
return;
ma_mutex_lock(&mutex);
SetSoundVolume(sound, volume);
ma_mutex_unlock(&mutex);
}
void AudioBackend_SetSoundPan(AudioBackend_Sound *sound, long pan)
{
if (sound == NULL)
return;
ma_mutex_lock(&mutex);
SetSoundPan(sound, pan);
ma_mutex_unlock(&mutex);
}
void AudioBackend_SetOrganyaTimer(unsigned short timer)
{
ma_mutex_lock(&organya_mutex);
organya_timer = timer;
ma_mutex_unlock(&organya_mutex);
}