| 1 |
// This is copyrighted software. More information is at the end of this file. |
| 2 |
#include "Aulib/DecoderFluidsynth.h" |
| 3 |
#include "Aulib/DecoderModplug.h" |
| 4 |
#include "Aulib/DecoderMpg123.h" |
| 5 |
#include "Aulib/DecoderSndfile.h" |
| 6 |
#include "Aulib/DecoderVorbis.h" |
| 7 |
#include "Aulib/ResamplerSpeex.h" |
| 8 |
#include "Aulib/Stream.h" |
| 9 |
#include "aulib_config.h" |
| 10 |
#include "aulib_global.h" |
| 11 |
#include "aulib_log.h" |
| 12 |
#include "sdl_mixer_emu.h" |
| 13 |
|
| 14 |
// Currently active global music stream (SDL_mixer only supports one.) |
| 15 |
static Aulib::Stream* gMusic = nullptr; |
| 16 |
static float gMusicVolume = 1.0f; |
| 17 |
|
| 18 |
extern "C" { |
| 19 |
static void (*gMusicFinishHook)() = nullptr; |
| 20 |
} |
| 21 |
|
| 22 |
static void musicFinishHookWrapper(Aulib::Stream& /*unused*/) |
| 23 |
{ |
| 24 |
if (gMusicFinishHook) { |
| 25 |
gMusicFinishHook(); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
auto Mix_LoadMUS(const char* file) -> Mix_Music* |
| 30 |
{ |
| 31 |
aulib::log::debugLn("{}", __func__); |
| 32 |
|
| 33 |
auto strm = new Aulib::Stream(file, Aulib::Decoder::decoderFor(file), |
| 34 |
std::make_unique<Aulib::ResamplerSpeex>()); |
| 35 |
strm->open(); |
| 36 |
return (Mix_Music*)strm; |
| 37 |
} |
| 38 |
|
| 39 |
auto Mix_LoadMUS_RW(SDL_RWops* rw) -> Mix_Music* |
| 40 |
{ |
| 41 |
aulib::log::debugLn("{}", __func__); |
| 42 |
|
| 43 |
auto strm = new Aulib::Stream(rw, Aulib::Decoder::decoderFor(rw), |
| 44 |
std::make_unique<Aulib::ResamplerSpeex>(), false); |
| 45 |
strm->open(); |
| 46 |
return (Mix_Music*)strm; |
| 47 |
} |
| 48 |
|
| 49 |
auto Mix_LoadMUSType_RW(SDL_RWops* rw, Mix_MusicType type, int freesrc) -> Mix_Music* |
| 50 |
{ |
| 51 |
aulib::log::debugLn("{} type: {}", __func__, type); |
| 52 |
|
| 53 |
auto decoder = std::unique_ptr<Aulib::Decoder>(nullptr); |
| 54 |
switch (type) { |
| 55 |
case MUS_WAV: |
| 56 |
case MUS_FLAC: |
| 57 |
decoder = std::make_unique<Aulib::DecoderSndfile>(); |
| 58 |
break; |
| 59 |
|
| 60 |
case MUS_MOD: |
| 61 |
case MUS_MODPLUG: |
| 62 |
decoder = std::make_unique<Aulib::DecoderModplug>(); |
| 63 |
break; |
| 64 |
|
| 65 |
case MUS_MID: |
| 66 |
decoder = std::make_unique<Aulib::DecoderFluidsynth>(); |
| 67 |
break; |
| 68 |
|
| 69 |
case MUS_OGG: |
| 70 |
decoder = std::make_unique<Aulib::DecoderVorbis>(); |
| 71 |
break; |
| 72 |
|
| 73 |
case MUS_MP3: |
| 74 |
case MUS_MP3_MAD: |
| 75 |
decoder = std::make_unique<Aulib::DecoderMpg123>(); |
| 76 |
break; |
| 77 |
|
| 78 |
default: |
| 79 |
aulib::log::debugLn("NO DECODER FOUND"); |
| 80 |
return nullptr; |
| 81 |
} |
| 82 |
|
| 83 |
auto strm = new Aulib::Stream(rw, std::move(decoder), std::make_unique<Aulib::ResamplerSpeex>(), |
| 84 |
freesrc != 0); |
| 85 |
strm->open(); |
| 86 |
return (Mix_Music*)strm; |
| 87 |
} |
| 88 |
|
| 89 |
void Mix_FreeMusic(Mix_Music* music) |
| 90 |
{ |
| 91 |
aulib::log::debugLn("{}", __func__); |
| 92 |
|
| 93 |
auto* strm = (Aulib::Stream*)music; |
| 94 |
if (gMusic == strm) { |
| 95 |
gMusic = nullptr; |
| 96 |
} |
| 97 |
delete strm; |
| 98 |
} |
| 99 |
|
| 100 |
auto Mix_GetNumMusicDecoders() -> int |
| 101 |
{ |
| 102 |
aulib::log::debugLn("{}", __func__); |
| 103 |
|
| 104 |
return 0; |
| 105 |
} |
| 106 |
|
| 107 |
auto Mix_GetMusicDecoder(int /*index*/) -> const char* |
| 108 |
{ |
| 109 |
aulib::log::debugLn("{}", __func__); |
| 110 |
|
| 111 |
return nullptr; |
| 112 |
} |
| 113 |
|
| 114 |
auto Mix_GetMusicType(const Mix_Music* /*music*/) -> Mix_MusicType |
| 115 |
{ |
| 116 |
aulib::log::debugLn("{}", __func__); |
| 117 |
|
| 118 |
return MUS_NONE; |
| 119 |
} |
| 120 |
|
| 121 |
void Mix_HookMusic(void (*/*mix_func*/)(void*, Uint8*, int), void* /*arg*/) |
| 122 |
{ |
| 123 |
aulib::log::debugLn("{}", __func__); |
| 124 |
} |
| 125 |
|
| 126 |
void Mix_HookMusicFinished(void (*music_finished)()) |
| 127 |
{ |
| 128 |
aulib::log::debugLn("{}", __func__); |
| 129 |
|
| 130 |
if (not music_finished) { |
| 131 |
if (gMusic) { |
| 132 |
gMusic->unsetFinishCallback(); |
| 133 |
} |
| 134 |
gMusicFinishHook = nullptr; |
| 135 |
} else { |
| 136 |
gMusicFinishHook = music_finished; |
| 137 |
if (gMusic) { |
| 138 |
gMusic->setFinishCallback(musicFinishHookWrapper); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
auto Mix_GetMusicHookData() -> void* |
| 144 |
{ |
| 145 |
aulib::log::debugLn("{}", __func__); |
| 146 |
|
| 147 |
return nullptr; |
| 148 |
} |
| 149 |
|
| 150 |
auto Mix_PlayMusic(Mix_Music* music, int loops) -> int |
| 151 |
{ |
| 152 |
aulib::log::debugLn("{}", __func__); |
| 153 |
|
| 154 |
if (gMusic) { |
| 155 |
gMusic->stop(); |
| 156 |
gMusic = nullptr; |
| 157 |
} |
| 158 |
if (loops == 0) { |
| 159 |
return 0; |
| 160 |
} |
| 161 |
gMusic = (Aulib::Stream*)music; |
| 162 |
gMusic->setVolume(gMusicVolume); |
| 163 |
return static_cast<int>(gMusic->play(loops == -1 ? 0 : loops)); |
| 164 |
} |
| 165 |
|
| 166 |
auto Mix_FadeInMusic(Mix_Music* music, int loops, int /*ms*/) -> int |
| 167 |
{ |
| 168 |
aulib::log::debugLn("{}", __func__); |
| 169 |
|
| 170 |
return Mix_PlayMusic(music, loops); |
| 171 |
} |
| 172 |
|
| 173 |
auto Mix_FadeInMusicPos(Mix_Music* /*music*/, int /*loops*/, int /*ms*/, double /*position*/) -> int |
| 174 |
{ |
| 175 |
aulib::log::debugLn("{}", __func__); |
| 176 |
|
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
auto Mix_VolumeMusic(int volume) -> int |
| 181 |
{ |
| 182 |
aulib::log::debugLn("{}", __func__); |
| 183 |
|
| 184 |
int prevVol = gMusicVolume * MIX_MAX_VOLUME; |
| 185 |
volume = std::min(std::max(-1, volume), MIX_MAX_VOLUME); |
| 186 |
|
| 187 |
if (volume >= 0) { |
| 188 |
gMusicVolume = (float)volume / (float)MIX_MAX_VOLUME; |
| 189 |
if (gMusic) { |
| 190 |
gMusic->setVolume(gMusicVolume); |
| 191 |
} |
| 192 |
} |
| 193 |
return prevVol; |
| 194 |
} |
| 195 |
|
| 196 |
auto Mix_HaltMusic() -> int |
| 197 |
{ |
| 198 |
aulib::log::debugLn("{}", __func__); |
| 199 |
|
| 200 |
if (gMusic) { |
| 201 |
gMusic->stop(); |
| 202 |
gMusic = nullptr; |
| 203 |
} |
| 204 |
return 0; |
| 205 |
} |
| 206 |
|
| 207 |
auto Mix_FadeOutMusic(int /*ms*/) -> int |
| 208 |
{ |
| 209 |
aulib::log::debugLn("{}", __func__); |
| 210 |
|
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
auto Mix_FadingMusic() -> Mix_Fading |
| 215 |
{ |
| 216 |
aulib::log::debugLn("{}", __func__); |
| 217 |
|
| 218 |
return MIX_NO_FADING; |
| 219 |
} |
| 220 |
|
| 221 |
void Mix_PauseMusic() |
| 222 |
{ |
| 223 |
aulib::log::debugLn("{}", __func__); |
| 224 |
|
| 225 |
if (gMusic) { |
| 226 |
gMusic->pause(); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
void Mix_ResumeMusic() |
| 231 |
{ |
| 232 |
aulib::log::debugLn("{}", __func__); |
| 233 |
|
| 234 |
if (gMusic and gMusic->isPaused()) { |
| 235 |
gMusic->resume(); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
void Mix_RewindMusic() |
| 240 |
{ |
| 241 |
aulib::log::debugLn("{}", __func__); |
| 242 |
|
| 243 |
if (gMusic) { |
| 244 |
gMusic->rewind(); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
auto Mix_PausedMusic() -> int |
| 249 |
{ |
| 250 |
aulib::log::debugLn("{}", __func__); |
| 251 |
|
| 252 |
if (gMusic) { |
| 253 |
return static_cast<int>(gMusic->isPaused()); |
| 254 |
} |
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
auto Mix_SetMusicPosition(double /*position*/) -> int |
| 259 |
{ |
| 260 |
aulib::log::debugLn("{}", __func__); |
| 261 |
|
| 262 |
return -1; |
| 263 |
} |
| 264 |
|
| 265 |
auto Mix_PlayingMusic() -> int |
| 266 |
{ |
| 267 |
aulib::log::debugLn("{}", __func__); |
| 268 |
|
| 269 |
if (gMusic) { |
| 270 |
return static_cast<int>(gMusic->isPlaying()); |
| 271 |
} |
| 272 |
return 0; |
| 273 |
} |
| 274 |
|
| 275 |
auto Mix_SetMusicCMD(const char* /*command*/) -> int |
| 276 |
{ |
| 277 |
aulib::log::debugLn("{}", __func__); |
| 278 |
|
| 279 |
return -1; |
| 280 |
} |
| 281 |
|
| 282 |
auto Mix_SetSynchroValue(int /*value*/) -> int |
| 283 |
{ |
| 284 |
aulib::log::debugLn("{}", __func__); |
| 285 |
|
| 286 |
return -1; |
| 287 |
} |
| 288 |
|
| 289 |
auto Mix_GetSynchroValue() -> int |
| 290 |
{ |
| 291 |
aulib::log::debugLn("{}", __func__); |
| 292 |
|
| 293 |
return -1; |
| 294 |
} |
| 295 |
|
| 296 |
auto Mix_SetSoundFonts(const char* /*paths*/) -> int |
| 297 |
{ |
| 298 |
aulib::log::debugLn("{}", __func__); |
| 299 |
|
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
auto Mix_GetSoundFonts() -> const char* |
| 304 |
{ |
| 305 |
aulib::log::debugLn("{}", __func__); |
| 306 |
|
| 307 |
return nullptr; |
| 308 |
} |
| 309 |
|
| 310 |
auto Mix_EachSoundFont(int (*/*function*/)(const char*, void*), void* /*data*/) -> int |
| 311 |
{ |
| 312 |
aulib::log::debugLn("{}", __func__); |
| 313 |
|
| 314 |
return 0; |
| 315 |
} |
| 316 |
|
| 317 |
/* |
| 318 |
|
| 319 |
Copyright (C) 2014, 2015, 2016, 2017, 2018, 2019 Nikos Chantziaras. |
| 320 |
|
| 321 |
This file is part of SDL_audiolib. |
| 322 |
|
| 323 |
SDL_audiolib is free software: you can redistribute it and/or modify it under |
| 324 |
the terms of the GNU Lesser General Public License as published by the Free |
| 325 |
Software Foundation, either version 3 of the License, or (at your option) any |
| 326 |
later version. |
| 327 |
|
| 328 |
SDL_audiolib is distributed in the hope that it will be useful, but WITHOUT |
| 329 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 330 |
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 331 |
details. |
| 332 |
|
| 333 |
You should have received a copy of the GNU Lesser General Public License |
| 334 |
along with SDL_audiolib. If not, see <http://www.gnu.org/licenses/>. |
| 335 |
|
| 336 |
*/ |