|
7 | 7 | #include <SDL.h> |
8 | 8 | #define MIX_INIT_FLUIDSYNTH MIX_INIT_MID // renamed with SDL2_mixer >= 2.0.2 |
9 | 9 | #include <SDL_mixer.h> |
| 10 | +#include <map> |
10 | 11 | #include "aifcplayer.h" |
11 | 12 | #include "mixer.h" |
12 | 13 | #include "sfxplayer.h" |
@@ -126,6 +127,7 @@ struct Mixer_impl { |
126 | 127 | Mix_Music *_music; |
127 | 128 | MixerChannel _channels[kMixChannels]; // 0,3:left 1,2:right |
128 | 129 | SfxPlayer *_sfx; |
| 130 | + std::map<int, Mix_Chunk *> _preloads; // AIFF preloads (3DO) |
129 | 131 |
|
130 | 132 | void init(bool softwareMixer) { |
131 | 133 | memset(_sounds, 0, sizeof(_sounds)); |
@@ -168,12 +170,6 @@ struct Mixer_impl { |
168 | 170 | Mix_Chunk *chunk = loadAndConvertWav(rw, 1, freq, kMixFormat, kMixSoundChannels, kMixFreq); |
169 | 171 | playSound(channel, volume, chunk, loops); |
170 | 172 | } |
171 | | - void playSoundAiff(uint8_t channel, const uint8_t *data, uint8_t volume) { |
172 | | - const uint32_t size = READ_BE_UINT32(data + 4) + 8; |
173 | | - SDL_RWops *rw = SDL_RWFromConstMem(data, size); |
174 | | - Mix_Chunk *chunk = Mix_LoadWAV_RW(rw, 1); |
175 | | - playSound(channel, volume, chunk); |
176 | | - } |
177 | 173 | void playSound(uint8_t channel, int volume, Mix_Chunk *chunk, int loops = 0) { |
178 | 174 | stopSound(channel); |
179 | 175 | if (chunk) { |
@@ -267,6 +263,32 @@ struct Mixer_impl { |
267 | 263 | } |
268 | 264 | stopMusic(); |
269 | 265 | stopSfxMusic(); |
| 266 | + for (std::map<int, Mix_Chunk *>::iterator it = _preloads.begin(); it != _preloads.end(); ++it) { |
| 267 | + debug(DBG_SND, "Flush preload %d", it->first); |
| 268 | + Mix_FreeChunk(it->second); |
| 269 | + } |
| 270 | + _preloads.clear(); |
| 271 | + } |
| 272 | + |
| 273 | + void preloadSoundAiff(int num, const uint8_t *data) { |
| 274 | + if (_preloads.find(num) != _preloads.end()) { |
| 275 | + warning("AIFF sound %d is already preloaded", num); |
| 276 | + } else { |
| 277 | + const uint32_t size = READ_BE_UINT32(data + 4) + 8; |
| 278 | + SDL_RWops *rw = SDL_RWFromConstMem(data, size); |
| 279 | + Mix_Chunk *chunk = Mix_LoadWAV_RW(rw, 1); |
| 280 | + _preloads[num] = chunk; |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | + void playSoundAiff(int channel, int num, int volume) { |
| 285 | + if (_preloads.find(num) == _preloads.end()) { |
| 286 | + warning("AIFF sound %d is not preloaded", num); |
| 287 | + } else { |
| 288 | + Mix_Chunk *chunk = _preloads[num]; |
| 289 | + Mix_PlayChannel(channel, chunk, 0); |
| 290 | + Mix_Volume(channel, volume * MIX_MAX_VOLUME / 63); |
| 291 | + } |
270 | 292 | } |
271 | 293 | }; |
272 | 294 |
|
@@ -308,13 +330,6 @@ void Mixer::playSoundWav(uint8_t channel, const uint8_t *data, uint16_t freq, ui |
308 | 330 | } |
309 | 331 | } |
310 | 332 |
|
311 | | -void Mixer::playSoundAiff(uint8_t channel, const uint8_t *data, uint8_t volume) { |
312 | | - debug(DBG_SND, "Mixer::playSoundAiff(%d, %d)", channel, volume); |
313 | | - if (_impl) { |
314 | | - return _impl->playSoundAiff(channel, data, volume); |
315 | | - } |
316 | | -} |
317 | | - |
318 | 333 | void Mixer::stopSound(uint8_t channel) { |
319 | 334 | debug(DBG_SND, "Mixer::stopChannel(%d)", channel); |
320 | 335 | if (_impl) { |
@@ -384,3 +399,17 @@ void Mixer::stopAll() { |
384 | 399 | return _impl->stopAll(); |
385 | 400 | } |
386 | 401 | } |
| 402 | + |
| 403 | +void Mixer::preloadSoundAiff(uint8_t num, const uint8_t *data) { |
| 404 | + debug(DBG_SND, "Mixer::preloadSoundAiff(num:%d, data:%p)", num, data); |
| 405 | + if (_impl) { |
| 406 | + return _impl->preloadSoundAiff(num, data); |
| 407 | + } |
| 408 | +} |
| 409 | + |
| 410 | +void Mixer::playSoundAiff(uint8_t channel, uint8_t num, uint8_t volume) { |
| 411 | + debug(DBG_SND, "Mixer::playSoundAiff()"); |
| 412 | + if (_impl) { |
| 413 | + return _impl->playSoundAiff(channel, num, volume); |
| 414 | + } |
| 415 | +} |
0 commit comments