| 1 | // Copyright (C) 2006-2009 by Tor Andersson, Lorenzo Marcantonio. |
| 2 | // Copyright (C) 2010 by Ben Cressey, Chris Spiegel. |
| 3 | // |
| 4 | // This file is part of Gargoyle. |
| 5 | // |
| 6 | // Gargoyle is free software; you can redistribute it and/or modify |
| 7 | // it under the terms of the GNU General Public License as published by |
| 8 | // the Free Software Foundation; either version 2 of the License, or |
| 9 | // (at your option) any later version. |
| 10 | // |
| 11 | // Gargoyle is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU General Public License |
| 17 | // along with Gargoyle; if not, write to the Free Software |
| 18 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | |
| 20 | // SDL support donated by Lorenzo Marcantonio |
| 21 | |
| 22 | #ifdef _WIN32 |
| 23 | #define SDL_MAIN_HANDLED |
| 24 | #endif |
| 25 | |
| 26 | #include <algorithm> |
| 27 | #include <array> |
| 28 | #include <cmath> |
| 29 | #include <cstring> |
| 30 | #include <functional> |
| 31 | #include <stdexcept> |
| 32 | #include <string> |
| 33 | #include <utility> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include <SDL.h> |
| 37 | #include <SDL_mixer.h> |
| 38 | |
| 39 | #include "format.h" |
| 40 | |
| 41 | #include "glk.h" |
| 42 | #include "garglk.h" |
| 43 | |
| 44 | #include "gi_blorb.h" |
| 45 | |
| 46 | #define giblorb_ID_MOD (giblorb_make_id('M', 'O', 'D', ' ')) |
| 47 | #define giblorb_ID_OGG (giblorb_make_id('O', 'G', 'G', 'V')) |
| 48 | #define giblorb_ID_FORM (giblorb_make_id('F', 'O', 'R', 'M')) |
| 49 | #define giblorb_ID_AIFF (giblorb_make_id('A', 'I', 'F', 'F')) |
| 50 | |
| 51 | // non-standard types |
| 52 | #define giblorb_ID_MP3 (giblorb_make_id('M', 'P', '3', ' ')) |
| 53 | #define giblorb_ID_WAVE (giblorb_make_id('W', 'A', 'V', 'E')) |
| 54 | #define giblorb_ID_MIDI (giblorb_make_id('M', 'I', 'D', 'I')) |
| 55 | |
| 56 | #define SDL_CHANNELS 64 |
| 57 | #define GLK_MAXVOLUME 0x10000 |
| 58 | #define FADE_GRANULARITY 100 |
| 59 | |
| 60 | #define GLK_VOLUME_TO_SDL_VOLUME(x) ((x) < GLK_MAXVOLUME ? (std::round(std::pow(((double)x) / GLK_MAXVOLUME, std::log(4)) * MIX_MAX_VOLUME)) : (MIX_MAX_VOLUME)) |
| 61 | |
| 62 | enum { CHANNEL_IDLE, CHANNEL_SOUND, CHANNEL_MUSIC }; |
| 63 | |
| 64 | struct glk_schannel_struct { |
| 65 | glui32 rock; |
| 66 | |
| 67 | Mix_Chunk *sample; |
| 68 | Mix_Music *music; |
| 69 | |
| 70 | SDL_RWops *sdl_rwops; |
| 71 | std::vector<unsigned char> sdl_memory; |
| 72 | int sdl_channel; |
| 73 | |
| 74 | int resid; // for notifies |
| 75 | int status; |
| 76 | int channel; |
| 77 | int volume; |
| 78 | glui32 loop; |
| 79 | int notify; |
| 80 | |
| 81 | bool paused; |
| 82 | |
| 83 | // for volume fades |
| 84 | int volume_notify; |
| 85 | int volume_timeout; |
| 86 | int target_volume; |
| 87 | double float_volume; |
| 88 | double volume_delta; |
| 89 | SDL_TimerID timer; |
| 90 | |
| 91 | gidispatch_rock_t disprock; |
| 92 | channel_t *chain_next, *chain_prev; |
| 93 | }; |
| 94 | |
| 95 | static channel_t *gli_channellist = nullptr; |
| 96 | static std::array<channel_t *, SDL_CHANNELS> sound_channels; |
| 97 | static channel_t *music_channel; |
| 98 | |
| 99 | static schanid_t gli_bleep_channel; |
| 100 | |
| 101 | static const int FREE = 1; |
| 102 | static const int BUSY = 2; |
| 103 | |
| 104 | gidispatch_rock_t gli_sound_get_channel_disprock(const channel_t *chan) |
| 105 | { |
| 106 | return chan->disprock; |
| 107 | } |
| 108 | |
| 109 | void gli_initialize_sound() |
| 110 | { |
| 111 | if (gli_conf_sound) { |
| 112 | SDL_SetMainReady(); |
| 113 | if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_TIMER) == -1) { |
| 114 | gli_strict_warning("SDL init failed\n"); |
| 115 | gli_strict_warning(SDL_GetError()); |
| 116 | gli_conf_sound = false; |
| 117 | return; |
| 118 | } |
| 119 | // MixInit? |
| 120 | |
| 121 | if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) == -1) { |
| 122 | gli_strict_warning("SDL Mixer init failed\n"); |
| 123 | gli_strict_warning(Mix_GetError()); |
| 124 | gli_conf_sound = false; |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | std::string soundfonts; |
| 129 | for (const auto &soundfont : gli_conf_soundfonts) { |
| 130 | if (soundfont.find(';') == std::string::npos) { |
| 131 | soundfonts += soundfont + ';'; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (!soundfonts.empty()) { |
| 136 | Mix_SetSoundFonts(soundfonts.c_str()); |
| 137 | } |
| 138 | |
| 139 | int channels = Mix_AllocateChannels(SDL_CHANNELS); |
| 140 | Mix_GroupChannels(0, channels - 1, FREE); |
| 141 | Mix_ChannelFinished(nullptr); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | schanid_t glk_schannel_create(glui32 rock) |
| 146 | { |
| 147 | return glk_schannel_create_ext(rock, GLK_MAXVOLUME); |
| 148 | } |
| 149 | |
| 150 | schanid_t glk_schannel_create_ext(glui32 rock, glui32 volume) |
| 151 | { |
| 152 | channel_t *chan; |
| 153 | |
| 154 | if (!gli_conf_sound) { |
| 155 | return nullptr; |
| 156 | } |
| 157 | chan = new channel_t; |
| 158 | |
| 159 | chan->rock = rock; |
| 160 | chan->status = CHANNEL_IDLE; |
| 161 | chan->volume = GLK_VOLUME_TO_SDL_VOLUME(volume); |
| 162 | chan->resid = 0; |
| 163 | chan->loop = 0; |
| 164 | chan->notify = 0; |
| 165 | chan->sdl_rwops = nullptr; |
| 166 | chan->sample = nullptr; |
| 167 | chan->paused = false; |
| 168 | chan->sdl_channel = -1; |
| 169 | chan->music = nullptr; |
| 170 | |
| 171 | chan->volume_notify = 0; |
| 172 | chan->volume_timeout = 0; |
| 173 | chan->target_volume = 0; |
| 174 | chan->float_volume = 0; |
| 175 | chan->volume_delta = 0; |
| 176 | chan->timer = 0; |
| 177 | |
| 178 | chan->chain_prev = nullptr; |
| 179 | chan->chain_next = gli_channellist; |
| 180 | gli_channellist = chan; |
| 181 | if (chan->chain_next != nullptr) { |
| 182 | chan->chain_next->chain_prev = chan; |
| 183 | } |
| 184 | |
| 185 | if (gli_register_obj != nullptr) { |
| 186 | chan->disprock = (*gli_register_obj)(chan, gidisp_Class_Schannel); |
| 187 | } else { |
| 188 | chan->disprock.ptr = nullptr; |
| 189 | } |
| 190 | |
| 191 | return chan; |
| 192 | } |
| 193 | |
| 194 | static void cleanup_channel(schanid_t chan) |
| 195 | { |
| 196 | if (chan->sdl_rwops != nullptr) { |
| 197 | SDL_RWclose(chan->sdl_rwops); |
| 198 | chan->sdl_rwops = nullptr; |
| 199 | } |
| 200 | |
| 201 | chan->sdl_memory.clear(); |
| 202 | |
| 203 | switch (chan->status) { |
| 204 | case CHANNEL_SOUND: |
| 205 | if (chan->sample != nullptr) { |
| 206 | Mix_FreeChunk(chan->sample); |
| 207 | } |
| 208 | if (chan->sdl_channel >= 0) { |
| 209 | Mix_GroupChannel(chan->sdl_channel, FREE); |
| 210 | sound_channels[chan->sdl_channel] = nullptr; |
| 211 | } |
| 212 | break; |
| 213 | case CHANNEL_MUSIC: |
| 214 | if (chan->music != nullptr) { |
| 215 | Mix_FreeMusic(chan->music); |
| 216 | music_channel = nullptr; |
| 217 | } |
| 218 | break; |
| 219 | } |
| 220 | chan->status = CHANNEL_IDLE; |
| 221 | chan->sdl_channel = -1; |
| 222 | chan->music = nullptr; |
| 223 | |
| 224 | if (chan->timer != 0) { |
| 225 | SDL_RemoveTimer(chan->timer); |
| 226 | } |
| 227 | |
| 228 | chan->timer = 0; |
| 229 | } |
| 230 | |
| 231 | void glk_schannel_destroy(schanid_t chan) |
| 232 | { |
| 233 | channel_t *prev, *next; |
| 234 | |
| 235 | if (chan == nullptr) { |
| 236 | gli_strict_warning("schannel_destroy: invalid id."); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | glk_schannel_stop(chan); |
| 241 | cleanup_channel(chan); |
| 242 | if (gli_unregister_obj != nullptr) { |
| 243 | (*gli_unregister_obj)(chan, gidisp_Class_Schannel, chan->disprock); |
| 244 | } |
| 245 | |
| 246 | prev = chan->chain_prev; |
| 247 | next = chan->chain_next; |
| 248 | chan->chain_prev = nullptr; |
| 249 | chan->chain_next = nullptr; |
| 250 | |
| 251 | if (prev != nullptr) { |
| 252 | prev->chain_next = next; |
| 253 | } else { |
| 254 | gli_channellist = next; |
| 255 | } |
| 256 | |
| 257 | if (next != nullptr) { |
| 258 | next->chain_prev = prev; |
| 259 | } |
| 260 | |
| 261 | delete chan; |
| 262 | } |
| 263 | |
| 264 | schanid_t glk_schannel_iterate(schanid_t chan, glui32 *rock) |
| 265 | { |
| 266 | if (chan == nullptr) { |
| 267 | chan = gli_channellist; |
| 268 | } else { |
| 269 | chan = chan->chain_next; |
| 270 | } |
| 271 | |
| 272 | if (chan != nullptr) { |
| 273 | if (rock != nullptr) { |
| 274 | *rock = chan->rock; |
| 275 | } |
| 276 | return chan; |
| 277 | } |
| 278 | |
| 279 | if (rock != nullptr) { |
| 280 | *rock = 0; |
| 281 | } |
| 282 | return nullptr; |
| 283 | } |
| 284 | |
| 285 | glui32 glk_schannel_get_rock(schanid_t chan) |
| 286 | { |
| 287 | if (chan == nullptr) { |
| 288 | gli_strict_warning("schannel_get_rock: invalid id."); |
| 289 | return 0; |
| 290 | } |
| 291 | return chan->rock; |
| 292 | } |
| 293 | |
| 294 | glui32 glk_schannel_play(schanid_t chan, glui32 snd) |
| 295 | { |
| 296 | return glk_schannel_play_ext(chan, snd, 1, 0); |
| 297 | } |
| 298 | |
| 299 | glui32 glk_schannel_play_multi(schanid_t *chanarray, glui32 chancount, |
| 300 | glui32 *sndarray, glui32 soundcount, glui32 notify) |
| 301 | { |
| 302 | int successes = 0; |
| 303 | |
| 304 | for (glui32 i = 0; i < chancount; i++) { |
| 305 | successes += glk_schannel_play_ext(chanarray[i], sndarray[i], 1, notify); |
| 306 | } |
| 307 | |
| 308 | return successes; |
| 309 | } |
| 310 | |
| 311 | void glk_sound_load_hint(glui32 snd, glui32 flag) |
| 312 | { |
| 313 | // nop |
| 314 | } |
| 315 | |
| 316 | // Make an incremental volume change when the fade timer fires |
| 317 | Uint32 volume_timer_callback(Uint32 interval, void *param) |
| 318 | { |
| 319 | schanid_t chan = static_cast<schanid_t>(param); |
| 320 | |
| 321 | if (chan == nullptr) { |
| 322 | gli_strict_warning("volume_timer_callback: invalid channel."); |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | chan->float_volume += chan->volume_delta; |
| 327 | |
| 328 | if (chan->float_volume < 0) { |
| 329 | chan->float_volume = 0; |
| 330 | } |
| 331 | |
| 332 | if (static_cast<int>(chan->float_volume) != chan->volume) { |
| 333 | chan->volume = static_cast<int>(chan->float_volume); |
| 334 | |
| 335 | if (chan->status == CHANNEL_SOUND) { |
| 336 | Mix_Volume(chan->sdl_channel, chan->volume); |
| 337 | } else if (chan->status == CHANNEL_MUSIC) { |
| 338 | Mix_VolumeMusic(chan->volume); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | chan->volume_timeout--; |
| 343 | |
| 344 | // If the timer has fired FADE_GRANULARITY times, kill it |
| 345 | if (chan->volume_timeout <= 0) { |
| 346 | if (chan->volume_notify != 0) { |
| 347 | gli_event_store(evtype_VolumeNotify, nullptr, |
| 348 | 0, chan->volume_notify); |
| 349 | gli_notification_waiting(); |
| 350 | } |
| 351 | |
| 352 | if (chan->timer == 0) { |
| 353 | gli_strict_warning("volume_timer_callback: invalid timer."); |
| 354 | return 0; |
| 355 | } |
| 356 | SDL_RemoveTimer(chan->timer); |
| 357 | chan->timer = 0; |
| 358 | |
| 359 | if (chan->volume != chan->target_volume) { |
| 360 | chan->volume = chan->target_volume; |
| 361 | if (chan->status == CHANNEL_SOUND) { |
| 362 | Mix_Volume(chan->sdl_channel, chan->volume); |
| 363 | } else if (chan->status == CHANNEL_MUSIC) { |
| 364 | Mix_VolumeMusic(chan->volume); |
| 365 | } |
| 366 | } |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | return interval; |
| 371 | } |
| 372 | |
| 373 | // Start a fade timer |
| 374 | void init_fade(schanid_t chan, int vol, int duration, int notify) |
| 375 | { |
| 376 | if (chan == nullptr) { |
| 377 | gli_strict_warning("init_fade: invalid channel."); |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | chan->volume_notify = notify; |
| 382 | |
| 383 | chan->target_volume = GLK_VOLUME_TO_SDL_VOLUME(vol); |
| 384 | |
| 385 | chan->float_volume = static_cast<double>(chan->volume); |
| 386 | chan->volume_delta = static_cast<double>(chan->target_volume - chan->volume) / FADE_GRANULARITY; |
| 387 | |
| 388 | chan->volume_timeout = FADE_GRANULARITY; |
| 389 | |
| 390 | if (chan->timer != 0) { |
| 391 | SDL_RemoveTimer(chan->timer); |
| 392 | } |
| 393 | |
| 394 | chan->timer = SDL_AddTimer(static_cast<Uint32>(duration / FADE_GRANULARITY), volume_timer_callback, chan); |
| 395 | |
| 396 | if (chan->timer == 0) { |
| 397 | gli_strict_warning("init_fade: failed to create volume change timer."); |
| 398 | return; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | void glk_schannel_set_volume(schanid_t chan, glui32 vol) |
| 403 | { |
| 404 | glk_schannel_set_volume_ext(chan, vol, 0, 0); |
| 405 | } |
| 406 | |
| 407 | void glk_schannel_set_volume_ext(schanid_t chan, glui32 vol, |
| 408 | glui32 duration, glui32 notify) |
| 409 | { |
| 410 | if (chan == nullptr) { |
| 411 | gli_strict_warning("schannel_set_volume: invalid id."); |
| 412 | return; |
| 413 | } |
| 414 | |
| 415 | if (duration == 0) { |
| 416 | |
| 417 | chan->volume = GLK_VOLUME_TO_SDL_VOLUME(vol); |
| 418 | |
| 419 | switch (chan->status) { |
| 420 | case CHANNEL_IDLE: |
| 421 | break; |
| 422 | case CHANNEL_SOUND: |
| 423 | Mix_Volume(chan->sdl_channel, chan->volume); |
| 424 | break; |
| 425 | case CHANNEL_MUSIC: |
| 426 | Mix_VolumeMusic(chan->volume); |
| 427 | break; |
| 428 | } |
| 429 | } else { |
| 430 | init_fade(chan, vol, duration, notify); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | // Notify the music channel completion |
| 435 | static void music_completion_callback() |
| 436 | { |
| 437 | if (music_channel == nullptr) { |
| 438 | gli_strict_warning("music callback failed"); |
| 439 | return; |
| 440 | } else { |
| 441 | gli_event_store(evtype_SoundNotify, nullptr, music_channel->resid, |
| 442 | music_channel->notify); |
| 443 | gli_notification_waiting(); |
| 444 | } |
| 445 | cleanup_channel(music_channel); |
| 446 | } |
| 447 | |
| 448 | // Notify the sound channel completion |
| 449 | static void sound_completion_callback(int chan) |
| 450 | { |
| 451 | channel_t *sound_channel = sound_channels[chan]; |
| 452 | |
| 453 | if (sound_channel == nullptr) { |
| 454 | gli_strict_warning("sound completion callback called with invalid channel"); |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | if (sound_channel->notify != 0) { |
| 459 | gli_event_store(evtype_SoundNotify, nullptr, |
| 460 | sound_channel->resid, sound_channel->notify); |
| 461 | gli_notification_waiting(); |
| 462 | } |
| 463 | Mix_ChannelFinished(nullptr); |
| 464 | cleanup_channel(sound_channel); |
| 465 | sound_channels[chan] = nullptr; |
| 466 | return; |
| 467 | } |
| 468 | |
| 469 | static int detect_format(const std::vector<unsigned char> &buf) |
| 470 | { |
| 471 | const std::vector<std::pair<std::pair<long, std::vector<std::string>>, unsigned long>> formats = { |
| 472 | // AIFF |
| 473 | {{0, {"FORM"}}, giblorb_ID_FORM}, |
| 474 | |
| 475 | // WAVE |
| 476 | {{0, {"WAVE", "RIFF"}}, giblorb_ID_WAVE}, |
| 477 | |
| 478 | // midi |
| 479 | {{0, {"MThd"}}, giblorb_ID_MIDI}, |
| 480 | |
| 481 | // s3m |
| 482 | {{44, {"SCRM"}}, giblorb_ID_MOD}, |
| 483 | |
| 484 | // XM |
| 485 | {{0, {"Extended Module: "}}, giblorb_ID_MOD}, |
| 486 | |
| 487 | // IT |
| 488 | {{0, {"IMPM"}}, giblorb_ID_MOD}, |
| 489 | |
| 490 | // MOD |
| 491 | {{1080, {"4CHN", "6CHN", "8CHN", |
| 492 | "16CN", "32CN", "M.K.", |
| 493 | "M!K!", "FLT4", "CD81", |
| 494 | "OKTA", " "}}, giblorb_ID_MOD}, |
| 495 | |
| 496 | // ogg |
| 497 | {{0, {"OggS"}}, giblorb_ID_OGG}, |
| 498 | |
| 499 | // mp3 |
| 500 | {{0, {"ID3"}}, giblorb_ID_MP3}, |
| 501 | {{0, {"\xff\xe2"}}, giblorb_ID_MP3}, |
| 502 | {{0, {"\xff\xe3"}}, giblorb_ID_MP3}, |
| 503 | {{0, {"\xff\xf2"}}, giblorb_ID_MP3}, |
| 504 | {{0, {"\xff\xf3"}}, giblorb_ID_MP3}, |
| 505 | {{0, {"\xff\xfa"}}, giblorb_ID_MP3}, |
| 506 | {{0, {"\xff\xfb"}}, giblorb_ID_MP3}, |
| 507 | }; |
| 508 | |
| 509 | for (const auto &entry : formats) { |
| 510 | auto offset = entry.first.first; |
| 511 | const auto &magics = entry.first.second; |
| 512 | auto format = entry.second; |
| 513 | |
| 514 | if (std::any_of(magics.begin(), magics.end(), [&offset, &buf](const auto &magic) { |
| 515 | return offset + magic.size() <= buf.size() && |
| 516 | std::memcmp(buf.data() + offset, magic.data(), magic.size()) == 0; |
| 517 | })) |
| 518 | { |
| 519 | return format; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | static int load_bleep_resource(glui32 snd, std::vector<unsigned char> &buf) |
| 527 | { |
| 528 | if (snd != 1 && snd != 2) { |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | const auto &bleep = gli_bleeps.at(snd); |
| 533 | buf.assign(bleep.begin(), bleep.end()); |
| 534 | |
| 535 | return detect_format(buf); |
| 536 | } |
| 537 | |
| 538 | static glui32 load_sound_resource(glui32 snd, std::vector<unsigned char> &buf) |
| 539 | { |
| 540 | if (giblorb_get_resource_map() != nullptr) { |
| 541 | glui32 type; |
| 542 | |
| 543 | if (!giblorb_copy_resource(giblorb_ID_Snd, snd, type, buf)) { |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | return type; |
| 548 | } else { |
| 549 | const auto &resource_map = gli_get_resource_map(giblorb_ID_Snd); |
| 550 | if (!resource_map.empty()) { |
| 551 | try { |
| 552 | buf = resource_map.at(snd); |
| 553 | } catch (const std::out_of_range &) { |
| 554 | return 0; |
| 555 | } |
| 556 | } else { |
| 557 | auto filename = Format("{}/SND{}", gli_workdir, snd); |
| 558 | |
| 559 | if (!garglk::read_file(filename, buf)) { |
| 560 | return 0; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | return detect_format(buf); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | // Start a sound channel |
| 569 | static glui32 play_sound(schanid_t chan) |
| 570 | { |
| 571 | int loop; |
| 572 | SDL_LockAudio(); |
| 573 | chan->status = CHANNEL_SOUND; |
| 574 | chan->sdl_channel = Mix_GroupAvailable(FREE); |
| 575 | if (chan->sdl_channel < 0) { |
| 576 | gli_strict_warning("No available sound channels"); |
| 577 | } |
| 578 | Mix_GroupChannel(chan->sdl_channel, BUSY); |
| 579 | SDL_UnlockAudio(); |
| 580 | chan->sample = Mix_LoadWAV_RW(chan->sdl_rwops, false); |
| 581 | if (chan->sdl_channel >= 0 && chan->sample != nullptr) { |
| 582 | SDL_LockAudio(); |
| 583 | sound_channels[chan->sdl_channel] = chan; |
| 584 | SDL_UnlockAudio(); |
| 585 | Mix_Volume(chan->sdl_channel, chan->volume); |
| 586 | Mix_ChannelFinished(&sound_completion_callback); |
| 587 | loop = chan->loop - 1; |
| 588 | if (loop < -1) { |
| 589 | loop = -1; |
| 590 | } |
| 591 | if (Mix_PlayChannel(chan->sdl_channel, chan->sample, loop) >= 0) { |
| 592 | return 1; |
| 593 | } |
| 594 | } |
| 595 | gli_strict_warning("play sound failed"); |
| 596 | gli_strict_warning(Mix_GetError()); |
| 597 | SDL_LockAudio(); |
| 598 | cleanup_channel(chan); |
| 599 | SDL_UnlockAudio(); |
| 600 | return 0; |
| 601 | } |
| 602 | |
| 603 | // Start a mod music channel |
| 604 | static glui32 play_mod(schanid_t chan) |
| 605 | { |
| 606 | bool music_busy; |
| 607 | int loop; |
| 608 | |
| 609 | if (chan == nullptr) { |
| 610 | gli_strict_warning("MOD player called with an invalid channel!"); |
| 611 | return 0; |
| 612 | } |
| 613 | |
| 614 | music_busy = Mix_PlayingMusic(); |
| 615 | |
| 616 | if (music_busy) { |
| 617 | // We already checked for music playing on *this* channel |
| 618 | // in glk_schannel_play_ext |
| 619 | |
| 620 | gli_strict_warning("MOD player already in use on another channel!"); |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | chan->status = CHANNEL_MUSIC; |
| 625 | chan->music = Mix_LoadMUS_RW(chan->sdl_rwops, 0); |
| 626 | if (chan->music != nullptr) { |
| 627 | SDL_LockAudio(); |
| 628 | music_channel = chan; |
| 629 | SDL_UnlockAudio(); |
| 630 | Mix_VolumeMusic(chan->volume); |
| 631 | Mix_HookMusicFinished(&music_completion_callback); |
| 632 | loop = chan->loop; |
| 633 | if (loop < -1) { |
| 634 | loop = -1; |
| 635 | } |
| 636 | if (Mix_PlayMusic(chan->music, loop) >= 0) { |
| 637 | return 1; |
| 638 | } |
| 639 | } |
| 640 | gli_strict_warning("play mod failed"); |
| 641 | gli_strict_warning(Mix_GetError()); |
| 642 | SDL_LockAudio(); |
| 643 | cleanup_channel(chan); |
| 644 | SDL_UnlockAudio(); |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | static glui32 gli_schannel_play_ext(schanid_t chan, glui32 snd, glui32 repeats, glui32 notify, std::function<glui32(glui32, std::vector<unsigned char> &)> load_resource) |
| 649 | { |
| 650 | glui32 type; |
| 651 | glui32 result = 0; |
| 652 | bool paused = false; |
| 653 | |
| 654 | if (chan == nullptr) { |
| 655 | gli_strict_warning("schannel_play_ext: invalid id."); |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | // store paused state of channel |
| 660 | paused = chan->paused; |
| 661 | |
| 662 | // stop previous noise |
| 663 | glk_schannel_stop(chan); |
| 664 | |
| 665 | if (repeats == 0) { |
| 666 | return 1; |
| 667 | } |
| 668 | |
| 669 | // load sound resource into memory |
| 670 | type = load_resource(snd, chan->sdl_memory); |
| 671 | |
| 672 | chan->sdl_rwops = SDL_RWFromConstMem(chan->sdl_memory.data(), chan->sdl_memory.size()); |
| 673 | chan->notify = notify; |
| 674 | chan->resid = snd; |
| 675 | chan->loop = repeats; |
| 676 | |
| 677 | switch (type) { |
| 678 | case giblorb_ID_FORM: |
| 679 | case giblorb_ID_AIFF: |
| 680 | case giblorb_ID_WAVE: |
| 681 | case giblorb_ID_OGG: |
| 682 | case giblorb_ID_MP3: |
| 683 | result = play_sound(chan); |
| 684 | break; |
| 685 | |
| 686 | case giblorb_ID_MOD: |
| 687 | case giblorb_ID_MIDI: |
| 688 | result = play_mod(chan); |
| 689 | break; |
| 690 | |
| 691 | default: |
| 692 | gli_strict_warning("schannel_play_ext: unknown resource type."); |
| 693 | } |
| 694 | |
| 695 | // if channel was paused it should be paused again |
| 696 | if (result != 0 && paused) { |
| 697 | glk_schannel_pause(chan); |
| 698 | } |
| 699 | |
| 700 | return result; |
| 701 | } |
| 702 | |
| 703 | glui32 glk_schannel_play_ext(schanid_t chan, glui32 snd, glui32 repeats, glui32 notify) |
| 704 | { |
| 705 | return gli_schannel_play_ext(chan, snd, repeats, notify, load_sound_resource); |
| 706 | } |
| 707 | |
| 708 | void glk_schannel_pause(schanid_t chan) |
| 709 | { |
| 710 | if (chan == nullptr) { |
| 711 | gli_strict_warning("schannel_pause: invalid id."); |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | switch (chan->status) { |
| 716 | case CHANNEL_SOUND: |
| 717 | Mix_Pause(chan->sdl_channel); |
| 718 | break; |
| 719 | case CHANNEL_MUSIC: |
| 720 | Mix_PauseMusic(); |
| 721 | break; |
| 722 | } |
| 723 | |
| 724 | chan->paused = true; |
| 725 | } |
| 726 | |
| 727 | void glk_schannel_unpause(schanid_t chan) |
| 728 | { |
| 729 | if (chan == nullptr) { |
| 730 | gli_strict_warning("schannel_unpause: invalid id."); |
| 731 | return; |
| 732 | } |
| 733 | switch (chan->status) { |
| 734 | case CHANNEL_SOUND: |
| 735 | Mix_Resume(chan->sdl_channel); |
| 736 | break; |
| 737 | case CHANNEL_MUSIC: |
| 738 | Mix_ResumeMusic(); |
| 739 | break; |
| 740 | } |
| 741 | |
| 742 | chan->paused = false; |
| 743 | } |
| 744 | |
| 745 | void glk_schannel_stop(schanid_t chan) |
| 746 | { |
| 747 | if (chan == nullptr) { |
| 748 | gli_strict_warning("schannel_stop: invalid id."); |
| 749 | return; |
| 750 | } |
| 751 | SDL_LockAudio(); |
| 752 | glk_schannel_unpause(chan); |
| 753 | SDL_UnlockAudio(); |
| 754 | switch (chan->status) { |
| 755 | case CHANNEL_SOUND: |
| 756 | chan->notify = 0; |
| 757 | Mix_HaltChannel(chan->sdl_channel); |
| 758 | break; |
| 759 | case CHANNEL_MUSIC: |
| 760 | if (music_channel == chan) { |
| 761 | Mix_HookMusicFinished(nullptr); |
| 762 | } |
| 763 | Mix_HaltMusic(); |
| 764 | break; |
| 765 | } |
| 766 | SDL_LockAudio(); |
| 767 | cleanup_channel(chan); |
| 768 | SDL_UnlockAudio(); |
| 769 | } |
| 770 | |
| 771 | void garglk_zbleep(glui32 number) |
| 772 | { |
| 773 | if (gli_bleep_channel == nullptr) { |
| 774 | gli_bleep_channel = glk_schannel_create(0); |
| 775 | if (gli_bleep_channel != nullptr) { |
| 776 | glk_schannel_set_volume(gli_bleep_channel, 0x8000); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | if (gli_bleep_channel != nullptr) { |
| 781 | try { |
| 782 | gli_schannel_play_ext(gli_bleep_channel, number, 1, 0, load_bleep_resource); |
| 783 | } catch (const Bleeps::Empty &) { |
| 784 | } |
| 785 | } |
| 786 | } |