| 1 | // Copyright 2011-2025 Chris Spiegel. |
| 2 | // |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #include <array> |
| 6 | #include <exception> |
| 7 | #include <memory> |
| 8 | #include <set> |
| 9 | #include <stdexcept> |
| 10 | #include <unordered_map> |
| 11 | |
| 12 | #include "sound.h" |
| 13 | #include "iff.h" |
| 14 | #include "process.h" |
| 15 | #include "types.h" |
| 16 | #include "zterp.h" |
| 17 | |
| 18 | #ifdef ZTERP_GLK |
| 19 | extern "C" { |
| 20 | #include <glk.h> |
| 21 | #if defined(GLK_MODULE_SOUND2) && defined(ZTERP_GLK_BLORB) |
| 22 | #include <gi_blorb.h> |
| 23 | #endif |
| 24 | } |
| 25 | #endif |
| 26 | |
| 27 | #ifdef GLK_MODULE_SOUND2 |
| 28 | struct Channel { |
| 29 | class Error : public std::exception { |
| 30 | }; |
| 31 | |
| 32 | Channel() { |
| 33 | if (channel == nullptr) { |
| 34 | throw Error(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | ~Channel() { |
| 39 | glk_schannel_destroy(channel); |
| 40 | } |
| 41 | |
| 42 | Channel(const Channel &other) = delete; |
| 43 | Channel &operator=(const Channel &other) = delete; |
| 44 | |
| 45 | schanid_t channel = glk_schannel_create(0); |
| 46 | bool playing = false; |
| 47 | uint16_t routine = 0; |
| 48 | |
| 49 | struct { |
| 50 | uint16_t number; |
| 51 | uint8_t volume; |
| 52 | } queued = {0, 0}; |
| 53 | }; |
| 54 | |
| 55 | class Channels { |
| 56 | public: |
| 57 | static constexpr glui32 Effects = 1; |
| 58 | static constexpr glui32 Music = 2; |
| 59 | |
| 60 | void create() { |
| 61 | try { |
| 62 | m_channels.emplace(Effects, std::make_shared<Channel>()); |
| 63 | } catch (const Channel::Error &) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | // Try to create a music channel, but if that fails, alias it to |
| 68 | // the effects channel. This will allow music and effects to |
| 69 | // interrupt each other, but that’s better than no music. |
| 70 | try { |
| 71 | m_channels.emplace(Music, std::make_shared<Channel>()); |
| 72 | } catch (const Channel::Error &) { |
| 73 | m_channels.emplace(Music, m_channels.at(Effects)); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | bool loaded() const { |
| 78 | return !m_channels.empty(); |
| 79 | } |
| 80 | |
| 81 | std::shared_ptr<Channel> at(glui32 chantype) { |
| 82 | return m_channels.at(chantype); |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | std::unordered_map<glui32, std::shared_ptr<Channel>> m_channels; |
| 87 | }; |
| 88 | |
| 89 | constexpr glui32 Channels::Effects; |
| 90 | constexpr glui32 Channels::Music; |
| 91 | |
| 92 | static Channels channels; |
| 93 | |
| 94 | static std::set<uint32_t> looping_sounds; |
| 95 | static std::unordered_map<uint32_t, bool> music_sounds; |
| 96 | #endif |
| 97 | |
| 98 | void init_sound() |
| 99 | { |
| 100 | #ifdef GLK_MODULE_SOUND2 |
| 101 | if (sound_loaded()) { |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | if (glk_gestalt(gestalt_Sound2, 0)) { |
| 106 | channels.create(); |
| 107 | } |
| 108 | |
| 109 | #ifdef ZTERP_GLK_BLORB |
| 110 | // Blorb files support a “Loop” chunk which is used to specify which |
| 111 | // sound resources, when played, loop indefinitely. This exists |
| 112 | // solely to support The Lurking Horror’s sounds, some of which are |
| 113 | // looping. The Lurking Horror embedded the looping information |
| 114 | // directly into the sound files themselves, which is why Blorbs |
| 115 | // include the looping information. This is only used in V3 games. |
| 116 | // See §11.4 of the Blorb specification. |
| 117 | auto *map = giblorb_get_resource_map(); |
| 118 | if (zversion == 3 && sound_loaded() && map != nullptr) { |
| 119 | giblorb_result_t res; |
| 120 | glui32 chunktype = IFF::TypeID("Loop").val(); |
| 121 | if (giblorb_load_chunk_by_type(map, giblorb_method_Memory, &res, chunktype, 0) == giblorb_err_None) { |
| 122 | for (size_t i = 0; i < res.length; i += 8) { |
| 123 | auto read32 = [&res](size_t offset) { |
| 124 | const auto *p = static_cast<unsigned char *>(res.data.ptr) + offset; |
| 125 | return (static_cast<uint32_t>(p[0]) << 24) | |
| 126 | (static_cast<uint32_t>(p[1]) << 16) | |
| 127 | (static_cast<uint32_t>(p[2]) << 8) | |
| 128 | (static_cast<uint32_t>(p[3]) << 0); |
| 129 | }; |
| 130 | uint32_t n = read32(i); |
| 131 | uint32_t repeats = read32(i + 4); |
| 132 | if (repeats == 0) { |
| 133 | looping_sounds.insert(n); |
| 134 | } |
| 135 | } |
| 136 | giblorb_unload_chunk(map, res.chunknum); |
| 137 | } |
| 138 | } |
| 139 | #endif |
| 140 | #endif |
| 141 | } |
| 142 | |
| 143 | bool sound_loaded() |
| 144 | { |
| 145 | #ifdef GLK_MODULE_SOUND2 |
| 146 | return channels.loaded(); |
| 147 | #else |
| 148 | return false; |
| 149 | #endif |
| 150 | } |
| 151 | |
| 152 | #ifdef GLK_MODULE_SOUND2 |
| 153 | static void start_sound(glui32 chantype, Channel *channel, uint16_t number, uint8_t repeats, uint8_t volume) |
| 154 | { |
| 155 | static const std::array<uint32_t, 8> vols = { |
| 156 | 0x02000, 0x04000, 0x06000, 0x08000, |
| 157 | 0x0a000, 0x0c000, 0x0e000, 0x10000 |
| 158 | }; |
| 159 | |
| 160 | channel->queued.number = 0; |
| 161 | |
| 162 | try { |
| 163 | glk_schannel_set_volume(channel->channel, vols.at(volume - 1)); |
| 164 | } catch (const std::out_of_range &) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | if (glk_schannel_play_ext(channel->channel, number, repeats == 255 ? -1 : repeats, chantype)) { |
| 169 | channel->playing = true; |
| 170 | } else { |
| 171 | channel->playing = false; |
| 172 | channel->routine = 0; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | void sound_stopped(glui32 chantype) |
| 177 | { |
| 178 | try { |
| 179 | auto channel = channels.at(chantype); |
| 180 | |
| 181 | channel->playing = false; |
| 182 | |
| 183 | if (channel->queued.number != 0) { |
| 184 | start_sound(chantype, channel.get(), channel->queued.number, 1, channel->queued.volume); |
| 185 | channel->queued.number = 0; |
| 186 | } |
| 187 | } catch (const std::out_of_range &) { |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | uint16_t sound_get_routine(glui32 chantype) |
| 192 | { |
| 193 | try { |
| 194 | auto channel = channels.at(chantype); |
| 195 | auto routine = channel->routine; |
| 196 | channel->routine = 0; |
| 197 | return routine; |
| 198 | } catch (const std::out_of_range &) { |
| 199 | return 0; |
| 200 | } |
| 201 | } |
| 202 | #endif |
| 203 | |
| 204 | void zsound_effect() |
| 205 | { |
| 206 | uint16_t number = znargs == 0 ? 1 : zargs[0]; |
| 207 | |
| 208 | if (number == 1 || number == 2) { |
| 209 | #ifdef GLK_MODULE_GARGLKBLEEP |
| 210 | garglk_zbleep(number); |
| 211 | #endif |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | if (znargs < 2) { |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | #ifdef GLK_MODULE_SOUND2 |
| 220 | if (!channels.loaded()) { |
| 221 | return; |
| 222 | } |
| 223 | |
| 224 | glui32 chantype = Channels::Effects; |
| 225 | |
| 226 | #ifdef ZTERP_GLK_BLORB |
| 227 | // According to §9: “Only one sound effect of each type can play at |
| 228 | // any given time, so that starting a new music sound effect stops |
| 229 | // any current music playing, and starting any new sample sound |
| 230 | // effect stops any current sample sound playing. Samples and music |
| 231 | // do not interrupt each other.” |
| 232 | // |
| 233 | // The Standard doesn’t go into the difference between sound effects |
| 234 | // and music, but the Blorb standard (in §14.3) indicates that MOD |
| 235 | // and Ogg Vorbis are music, while AIFF is a sound effect. One can |
| 236 | // assume that SONGs (Blorb §3.4), although deprecated, count as |
| 237 | // music as well. |
| 238 | // |
| 239 | // The Blorb standard (§14.5) notes that Adrift Blorbs are allowed |
| 240 | // more sound formats than standard Blorb: WAV, MIDI, and MP3. While |
| 241 | // there’s likely never going to be a Blorb file with these sound |
| 242 | // formats meant for use with the Z-Machine, there’s no harm in |
| 243 | // adding MIDI and MP3 as music types here. |
| 244 | if (music_sounds.find(number) == music_sounds.end()) { |
| 245 | auto *map = giblorb_get_resource_map(); |
| 246 | if (map != nullptr) { |
| 247 | giblorb_result_t res; |
| 248 | if (giblorb_load_resource(map, giblorb_method_DontLoad, &res, giblorb_ID_Snd, number) == giblorb_err_None) { |
| 249 | switch (res.chunktype) { |
| 250 | case 0x4d4f4420: /* MOD */ |
| 251 | case 0x4f474756: /* OGGV */ |
| 252 | case 0x534f4e47: /* SONG */ |
| 253 | case 0x4d494449: /* MIDI (non-standard) */ |
| 254 | case 0x4d503320: /* MP3 (non-standard) */ |
| 255 | music_sounds.emplace(number, true); |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (music_sounds[number]) { |
| 263 | chantype = Channels::Music; |
| 264 | } |
| 265 | #endif |
| 266 | |
| 267 | std::shared_ptr<Channel> channel; |
| 268 | try { |
| 269 | channel = channels.at(chantype); |
| 270 | } catch (const std::out_of_range &) { |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | constexpr uint16_t SOUND_EFFECT_PREPARE = 1; |
| 275 | constexpr uint16_t SOUND_EFFECT_START = 2; |
| 276 | constexpr uint16_t SOUND_EFFECT_STOP = 3; |
| 277 | constexpr uint16_t SOUND_EFFECT_FINISH = 4; |
| 278 | |
| 279 | uint16_t effect = zargs[1]; |
| 280 | |
| 281 | // Sound effect 0 is invalid, except that it can be used to mean |
| 282 | // “stop all sounds”. |
| 283 | if (number == 0 && effect != SOUND_EFFECT_STOP) { |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | switch (effect) { |
| 288 | case SOUND_EFFECT_PREPARE: |
| 289 | glk_sound_load_hint(number, 1); |
| 290 | break; |
| 291 | case SOUND_EFFECT_START: { |
| 292 | if (znargs < 3) { |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | uint8_t repeats = zargs[2] >> 8; |
| 297 | uint8_t volume = zargs[2] & 0xff; |
| 298 | |
| 299 | if (looping_sounds.find(number) != looping_sounds.end()) { |
| 300 | repeats = 255; |
| 301 | } |
| 302 | |
| 303 | // Illegal, but recommended by standard 1.1. |
| 304 | if (repeats == 0) { |
| 305 | repeats = 1; |
| 306 | } |
| 307 | |
| 308 | if (volume == 0) { |
| 309 | volume = 1; |
| 310 | } else if (volume > 8) { |
| 311 | volume = 8; |
| 312 | } |
| 313 | |
| 314 | // Implement the hack described in the remarks to §9 for The |
| 315 | // Lurking Horror. Moreover, only impelement this for the two |
| 316 | // samples where it is relevant, since this behavior is so |
| 317 | // idiosyncratic that it should be ignored everywhere else. In |
| 318 | // addition, this doesn’t bother checking whether player input |
| 319 | // has occurred since the previous sound was started, because |
| 320 | // for the two specific samples in The Lurking Horror for which |
| 321 | // this hack applies, it’s known that there will not have been |
| 322 | // any input. |
| 323 | // |
| 324 | // Don’t queue the number of repeats: always play a queued sound |
| 325 | // only once. This is because one of the queued sounds |
| 326 | // (S-CRETIN, number 16) has a built-in loop; the game knows |
| 327 | // this so explicitly stops the sample in the same game round as |
| 328 | // it was started to prevent it from looping indefinitely. |
| 329 | // Modern systems are so fast that this stop event comes in |
| 330 | // roughly simultaneously with the start event, so the sound |
| 331 | // doesn’t play at all, if the stop event is honored. As such, |
| 332 | // stop events are ignored below if a queued sound exists, so |
| 333 | // the “stopping” must happen here by only playing the sample |
| 334 | // once. |
| 335 | if (is_game(Game::LurkingHorror) && chantype == Channels::Effects && channel->playing && (number == 9 || number == 16)) { |
| 336 | channel->queued.number = number; |
| 337 | channel->queued.volume = volume; |
| 338 | return; |
| 339 | } |
| 340 | |
| 341 | channel->routine = znargs >= 4 ? zargs[3] : 0; |
| 342 | start_sound(chantype, channel.get(), number, repeats, volume); |
| 343 | |
| 344 | break; |
| 345 | } |
| 346 | case SOUND_EFFECT_STOP: |
| 347 | // If there is a queued sound, ignore stop requests. This is |
| 348 | // because The Lurking Horror does the following in one single |
| 349 | // game round: |
| 350 | // |
| 351 | // @sound_effect 11 2 8 |
| 352 | // @sound_effect 16 2 8 |
| 353 | // @sound_effect 16 3 |
| 354 | // |
| 355 | // Per the remarks to §9, this is because Infocom knew the Amiga |
| 356 | // interpreter (the only system where a sound-enabled game was |
| 357 | // available) was slow, so sound effects would be able to play |
| 358 | // while the interpreter was running between user inputs. Thus |
| 359 | // there was presumably a fair amount of time between the second |
| 360 | // and third @sound_effect calls, meaning that the stop event |
| 361 | // came in after the sample had played, or at least partially |
| 362 | // played. On fast modern systems, the stop call will be |
| 363 | // processed before either sound effect has had any time to |
| 364 | // play. A queued sound will only play once, so ignoring this |
| 365 | // stop request is effectively just delaying it till the queued |
| 366 | // sound finishes. |
| 367 | if (channel->queued.number == 0) { |
| 368 | glk_schannel_stop(channel->channel); |
| 369 | channel->routine = 0; |
| 370 | } |
| 371 | break; |
| 372 | case SOUND_EFFECT_FINISH: |
| 373 | glk_sound_load_hint(number, 0); |
| 374 | break; |
| 375 | } |
| 376 | #endif |
| 377 | } |