This repository has been archived by the owner on Mar 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
mixer.cpp
524 lines (467 loc) · 13 KB
/
mixer.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
* Another World engine rewrite
* Copyright (C) 2004-2005 Gregory Montoir ([email protected])
*/
#include <SDL.h>
#define MIX_INIT_FLUIDSYNTH MIX_INIT_MID // renamed with SDL2_mixer >= 2.0.2
#include <SDL_mixer.h>
#include <map>
#include "aifcplayer.h"
#include "mixer.h"
#include "sfxplayer.h"
#include "util.h"
enum {
TAG_RIFF = 0x46464952,
TAG_WAVE = 0x45564157,
TAG_fmt = 0x20746D66,
TAG_data = 0x61746164
};
static const bool kAmigaStereoChannels = false; // 0,3:left 1,2:right
static int16_t toS16(int a) {
return ((a << 8) | a) - 32768;
}
static int16_t mixS16(int sample1, int sample2) {
const int sample = sample1 + sample2;
return sample < -32768 ? -32768 : ((sample > 32767 ? 32767 : sample));
}
struct MixerChannel {
const uint8_t *_data;
Frac _pos;
uint32_t _len;
uint32_t _loopLen, _loopPos;
int _volume;
void (MixerChannel::*_mixWav)(int16_t *sample, int count);
void initRaw(const uint8_t *data, int freq, int volume, int mixingFreq) {
_data = data + 8;
_pos.reset(freq, mixingFreq);
const int len = READ_BE_UINT16(data) * 2;
_loopLen = READ_BE_UINT16(data + 2) * 2;
_loopPos = _loopLen ? len : 0;
_len = len;
_volume = volume;
}
void initWav(const uint8_t *data, int freq, int volume, int mixingFreq, int len, bool bits16, bool stereo, bool loop) {
_data = data;
_pos.reset(freq, mixingFreq);
_len = len;
_loopLen = loop ? len : 0;
_loopPos = 0;
_volume = volume;
_mixWav = bits16 ? (stereo ? &MixerChannel::mixWav<16, true> : &MixerChannel::mixWav<16, false>) : (stereo ? &MixerChannel::mixWav<8, true> : &MixerChannel::mixWav<8, false>);
}
void mixRaw(int16_t &sample) {
if (_data) {
uint32_t pos = _pos.getInt();
_pos.offset += _pos.inc;
if (_loopLen != 0) {
if (pos >= _loopPos + _loopLen) {
pos = _loopPos;
_pos.offset = (_loopPos << Frac::BITS) + _pos.inc;
}
} else {
if (pos >= _len) {
_data = 0;
return;
}
}
sample = mixS16(sample, toS16(_data[pos] ^ 0x80) * _volume / 64);
}
}
template<int bits, bool stereo>
void mixWav(int16_t *samples, int count) {
for (int i = 0; i < count; i += 2) {
uint32_t pos = _pos.getInt();
_pos.offset += _pos.inc;
if (pos >= _len) {
if (_loopLen != 0) {
pos = 0;
_pos.offset = _pos.inc;
} else {
_data = 0;
break;
}
}
if (stereo) {
pos *= 2;
}
int valueL;
if (bits == 8) { // U8
valueL = toS16(_data[pos]) * _volume / 64;
} else { // S16
valueL = ((int16_t)READ_LE_UINT16(&_data[pos * sizeof(int16_t)])) * _volume / 64;
}
*samples = mixS16(*samples, valueL);
++samples;
int valueR;
if (!stereo) {
valueR = valueL;
} else {
if (bits == 8) { // U8
valueR = toS16(_data[pos + 1]) * _volume / 64;
} else { // S16
valueR = ((int16_t)READ_LE_UINT16(&_data[(pos + 1) * sizeof(int16_t)])) * _volume / 64;
}
}
*samples = mixS16(*samples, valueR);
++samples;
}
}
};
static const uint8_t *loadWav(const uint8_t *data, int &freq, int &len, bool &bits16, bool &stereo) {
uint32_t riffMagic = READ_LE_UINT32(data);
if (riffMagic != TAG_RIFF) return 0;
uint32_t riffLength = READ_LE_UINT32(data + 4);
uint32_t waveMagic = READ_LE_UINT32(data + 8);
if (waveMagic != TAG_WAVE) return 0;
uint32_t offset = 12;
uint32_t chunkMagic, chunkLength = 0;
// find fmt chunk
do {
offset += chunkLength + (chunkLength & 1);
if (offset >= riffLength) return 0;
chunkMagic = READ_LE_UINT32(data + offset);
chunkLength = READ_LE_UINT32(data + offset + 4);
offset += 8;
} while (chunkMagic != TAG_fmt);
if (chunkLength < 14) return 0;
if (offset + chunkLength >= riffLength) return 0;
// read format
int formatTag = READ_LE_UINT16(data + offset);
int channels = READ_LE_UINT16(data + offset + 2);
int samplesPerSec = READ_LE_UINT32(data + offset + 4);
int bitsPerSample = 0;
if (chunkLength >= 16) {
bitsPerSample = READ_LE_UINT16(data + offset + 14);
} else if (formatTag == 1 && channels != 0) {
int blockAlign = READ_LE_UINT16(data + offset + 12);
bitsPerSample = (blockAlign * 8) / channels;
}
// check supported format
if ((formatTag != 1) || // PCM
(channels != 1 && channels != 2) || // mono or stereo
(bitsPerSample != 8 && bitsPerSample != 16)) { // 8bit or 16bit
warning("Unsupported wave file");
return 0;
}
// find data chunk
do {
offset += chunkLength + (chunkLength & 1);
if (offset >= riffLength) return 0;
chunkMagic = READ_LE_UINT32(data + offset);
chunkLength = READ_LE_UINT32(data + offset + 4);
offset += 8;
} while (chunkMagic != TAG_data);
uint32_t lengthSamples = chunkLength;
if (offset + lengthSamples - 4 > riffLength) {
lengthSamples = riffLength + 4 - offset;
}
if (channels == 2) lengthSamples >>= 1;
if (bitsPerSample == 16) lengthSamples >>= 1;
freq = samplesPerSec;
len = lengthSamples;
bits16 = (bitsPerSample == 16);
stereo = (channels == 2);
return data + offset;
}
struct Mixer_impl {
static const int kMixFreq = 44100;
static const SDL_AudioFormat kMixFormat = AUDIO_S16SYS;
static const int kMixSoundChannels = 2;
static const int kMixBufSize = 4096;
static const int kMixChannels = 4;
Mix_Chunk *_sounds[kMixChannels];
Mix_Music *_music;
MixerChannel _channels[kMixChannels];
SfxPlayer *_sfx;
std::map<int, Mix_Chunk *> _preloads; // AIFF preloads (3DO)
void init(MixerType mixerType) {
memset(_sounds, 0, sizeof(_sounds));
_music = 0;
memset(_channels, 0, sizeof(_channels));
for (int i = 0; i < kMixChannels; ++i) {
_channels[i]._mixWav = &MixerChannel::mixWav<8, false>;
}
_sfx = 0;
Mix_Init(MIX_INIT_OGG | MIX_INIT_FLUIDSYNTH);
if (Mix_OpenAudio(kMixFreq, kMixFormat, kMixSoundChannels, kMixBufSize) < 0) {
warning("Mix_OpenAudio failed: %s", Mix_GetError());
}
switch (mixerType) {
case kMixerTypeRaw:
Mix_HookMusic(mixAudio, this);
break;
case kMixerTypeWav:
Mix_SetPostMix(mixAudioWav, this);
break;
case kMixerTypeAiff:
Mix_AllocateChannels(kMixChannels);
break;
}
}
void quit() {
stopAll();
Mix_CloseAudio();
Mix_Quit();
}
void update() {
for (int i = 0; i < kMixChannels; ++i) {
if (_sounds[i] && !Mix_Playing(i)) {
freeSound(i);
}
}
}
void playSoundRaw(uint8_t channel, const uint8_t *data, int freq, uint8_t volume) {
SDL_LockAudio();
_channels[channel].initRaw(data, freq, volume, kMixFreq);
SDL_UnlockAudio();
}
void playSoundWav(uint8_t channel, const uint8_t *data, int freq, uint8_t volume, bool loop) {
int wavFreq, len;
bool bits16, stereo;
const uint8_t *wavData = loadWav(data, wavFreq, len, bits16, stereo);
if (!wavData) return;
if (wavFreq == 22050 || wavFreq == 44100 || wavFreq == 48000) {
freq = (int)(freq * (wavFreq / 9943.0f));
}
SDL_LockAudio();
_channels[channel].initWav(wavData, freq, volume, kMixFreq, len, bits16, stereo, loop);
SDL_UnlockAudio();
}
void playSound(uint8_t channel, int volume, Mix_Chunk *chunk, int loops = 0) {
stopSound(channel);
if (chunk) {
Mix_PlayChannel(channel, chunk, loops);
}
setChannelVolume(channel, volume);
_sounds[channel] = chunk;
}
void stopSound(uint8_t channel) {
SDL_LockAudio();
_channels[channel]._data = 0;
SDL_UnlockAudio();
Mix_HaltChannel(channel);
freeSound(channel);
}
void freeSound(int channel) {
Mix_FreeChunk(_sounds[channel]);
_sounds[channel] = 0;
}
void setChannelVolume(uint8_t channel, uint8_t volume) {
SDL_LockAudio();
_channels[channel]._volume = volume;
SDL_UnlockAudio();
Mix_Volume(channel, volume * MIX_MAX_VOLUME / 63);
}
void playMusic(const char *path, int loops = 0) {
stopMusic();
_music = Mix_LoadMUS(path);
if (_music) {
Mix_VolumeMusic(MIX_MAX_VOLUME / 2);
Mix_PlayMusic(_music, loops);
} else {
warning("Failed to load music '%s', %s", path, Mix_GetError());
}
}
void stopMusic() {
Mix_HaltMusic();
Mix_FreeMusic(_music);
_music = 0;
}
static void mixAifcPlayer(void *data, uint8_t *s16buf, int len) {
((AifcPlayer *)data)->readSamples((int16_t *)s16buf, len / 2);
}
void playAifcMusic(AifcPlayer *aifc) {
Mix_HookMusic(mixAifcPlayer, aifc);
}
void stopAifcMusic() {
Mix_HookMusic(0, 0);
}
void playSfxMusic(SfxPlayer *sfx) {
SDL_LockAudio();
_sfx = sfx;
_sfx->play(kMixFreq);
SDL_UnlockAudio();
}
void stopSfxMusic() {
SDL_LockAudio();
if (_sfx) {
_sfx->stop();
_sfx = 0;
}
SDL_UnlockAudio();
}
void mixChannels(int16_t *samples, int count) {
if (kAmigaStereoChannels) {
for (int i = 0; i < count; i += 2) {
_channels[0].mixRaw(*samples);
_channels[3].mixRaw(*samples);
++samples;
_channels[1].mixRaw(*samples);
_channels[2].mixRaw(*samples);
++samples;
}
} else {
for (int i = 0; i < count; i += 2) {
for (int j = 0; j < kMixChannels; ++j) {
_channels[j].mixRaw(samples[i]);
}
samples[i + 1] = samples[i];
}
}
}
static void mixAudio(void *data, uint8_t *s16buf, int len) {
memset(s16buf, 0, len);
Mixer_impl *mixer = (Mixer_impl *)data;
mixer->mixChannels((int16_t *)s16buf, len / sizeof(int16_t));
if (mixer->_sfx) {
mixer->_sfx->readSamples((int16_t *)s16buf, len / sizeof(int16_t));
}
}
void mixChannelsWav(int16_t *samples, int count) {
for (int i = 0; i < kMixChannels; ++i) {
if (_channels[i]._data) {
(_channels[i].*_channels[i]._mixWav)(samples, count);
}
}
}
static void mixAudioWav(void *data, uint8_t *s16buf, int len) {
Mixer_impl *mixer = (Mixer_impl *)data;
mixer->mixChannelsWav((int16_t *)s16buf, len / sizeof(int16_t));
}
void stopAll() {
for (int i = 0; i < kMixChannels; ++i) {
stopSound(i);
}
stopMusic();
stopSfxMusic();
for (std::map<int, Mix_Chunk *>::iterator it = _preloads.begin(); it != _preloads.end(); ++it) {
debug(DBG_SND, "Flush preload %d", it->first);
Mix_FreeChunk(it->second);
}
_preloads.clear();
}
void preloadSoundAiff(int num, const uint8_t *data) {
if (_preloads.find(num) != _preloads.end()) {
warning("AIFF sound %d is already preloaded", num);
} else {
const uint32_t size = READ_BE_UINT32(data + 4) + 8;
SDL_RWops *rw = SDL_RWFromConstMem(data, size);
Mix_Chunk *chunk = Mix_LoadWAV_RW(rw, 1);
_preloads[num] = chunk;
}
}
void playSoundAiff(int channel, int num, int volume) {
if (_preloads.find(num) == _preloads.end()) {
warning("AIFF sound %d is not preloaded", num);
} else {
Mix_Chunk *chunk = _preloads[num];
Mix_PlayChannel(channel, chunk, 0);
Mix_Volume(channel, volume * MIX_MAX_VOLUME / 63);
}
}
};
Mixer::Mixer(SfxPlayer *sfx)
: _aifc(0), _sfx(sfx) {
}
void Mixer::init(MixerType mixerType) {
_impl = new Mixer_impl();
_impl->init(mixerType);
}
void Mixer::quit() {
stopAll();
if (_impl) {
_impl->quit();
delete _impl;
}
delete _aifc;
}
void Mixer::update() {
if (_impl) {
_impl->update();
}
}
void Mixer::playSoundRaw(uint8_t channel, const uint8_t *data, uint16_t freq, uint8_t volume) {
debug(DBG_SND, "Mixer::playChannel(%d, %d, %d)", channel, freq, volume);
if (_impl) {
return _impl->playSoundRaw(channel, data, freq, volume);
}
}
void Mixer::playSoundWav(uint8_t channel, const uint8_t *data, uint16_t freq, uint8_t volume, uint8_t loop) {
debug(DBG_SND, "Mixer::playSoundWav(%d, %d, %d)", channel, volume, loop);
if (_impl) {
return _impl->playSoundWav(channel, data, freq, volume, loop);
}
}
void Mixer::stopSound(uint8_t channel) {
debug(DBG_SND, "Mixer::stopChannel(%d)", channel);
if (_impl) {
return _impl->stopSound(channel);
}
}
void Mixer::setChannelVolume(uint8_t channel, uint8_t volume) {
debug(DBG_SND, "Mixer::setChannelVolume(%d, %d)", channel, volume);
if (_impl) {
return _impl->setChannelVolume(channel, volume);
}
}
void Mixer::playMusic(const char *path, uint8_t loop) {
debug(DBG_SND, "Mixer::playMusic(%s, %d)", path, loop);
if (_impl) {
return _impl->playMusic(path, (loop != 0) ? -1 : 0);
}
}
void Mixer::stopMusic() {
debug(DBG_SND, "Mixer::stopMusic()");
if (_impl) {
return _impl->stopMusic();
}
}
void Mixer::playAifcMusic(const char *path, uint32_t offset) {
debug(DBG_SND, "Mixer::playAifcMusic(%s)", path);
if (!_aifc) {
_aifc = new AifcPlayer();
}
if (_impl) {
_impl->stopAifcMusic();
if (_aifc->play(Mixer_impl::kMixFreq, path, offset)) {
_impl->playAifcMusic(_aifc);
}
}
}
void Mixer::stopAifcMusic() {
debug(DBG_SND, "Mixer::stopAifcMusic()");
if (_impl && _aifc) {
_aifc->stop();
_impl->stopAifcMusic();
}
}
void Mixer::playSfxMusic(int num) {
debug(DBG_SND, "Mixer::playSfxMusic(%d)", num);
if (_impl && _sfx) {
return _impl->playSfxMusic(_sfx);
}
}
void Mixer::stopSfxMusic() {
debug(DBG_SND, "Mixer::stopSfxMusic()");
if (_impl && _sfx) {
return _impl->stopSfxMusic();
}
}
void Mixer::stopAll() {
debug(DBG_SND, "Mixer::stopAll()");
if (_impl) {
return _impl->stopAll();
}
}
void Mixer::preloadSoundAiff(uint8_t num, const uint8_t *data) {
debug(DBG_SND, "Mixer::preloadSoundAiff(num:%d, data:%p)", num, data);
if (_impl) {
return _impl->preloadSoundAiff(num, data);
}
}
void Mixer::playSoundAiff(uint8_t channel, uint8_t num, uint8_t volume) {
debug(DBG_SND, "Mixer::playSoundAiff()");
if (_impl) {
return _impl->playSoundAiff(channel, num, volume);
}
}