1 /* ScummVM - Graphic Adventure Engine 2 * 3 * ScummVM is the legal property of its developers, whose names 4 * are too numerous to list here. Please refer to the COPYRIGHT 5 * file distributed with this source distribution. 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 2 10 * of the License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 * 21 */ 22 23 #ifndef SCI_AUDIO32_H 24 #define SCI_AUDIO32_H 25 #include "audio/audiostream.h" // for AudioStream, SeekableAudioStream (... 26 #include "audio/mixer.h" // for Mixer, SoundHandle 27 #include "audio/rate.h" // for Audio::st_volume_t, RateConverter 28 #include "common/array.h" // for Array 29 #include "common/mutex.h" // for StackLock, Mutex 30 #include "common/scummsys.h" // for int16, uint8, uint32, uint16 31 #include "sci/resource.h" // for ResourceId 32 #include "sci/engine/vm_types.h" // for reg_t, NULL_REG 33 #include "sci/video/robot_decoder.h" // for RobotAudioStream 34 35 namespace Sci { 36 class Console; 37 38 bool detectSolAudio(Common::SeekableReadStream &stream); 39 bool detectWaveAudio(Common::SeekableReadStream &stream); 40 41 #pragma mark AudioChannel 42 43 /** 44 * An audio channel used by the software SCI mixer. 45 */ 46 struct AudioChannel { 47 /** 48 * The ID of the resource loaded into this channel. 49 */ 50 ResourceId id; 51 52 /** 53 * The resource loaded into this channel. The resource is owned by 54 * ResourceManager. 55 */ 56 Resource *resource; 57 58 /** 59 * The audio stream loaded into this channel. Can cast to 60 * `SeekableAudioStream` for normal channels and `RobotAudioStream` for 61 * robot channels. 62 */ 63 Common::ScopedPtr<Audio::AudioStream> stream; 64 65 /** 66 * The converter used to transform and merge the input stream into the 67 * mixer's output buffer. 68 */ 69 Common::ScopedPtr<Audio::RateConverter> converter; 70 71 /** 72 * Duration of the channel, in ticks. 73 */ 74 uint32 duration; 75 76 /** 77 * The tick when the channel was started. 78 */ 79 uint32 startedAtTick; 80 81 /** 82 * The tick when the channel was paused. 83 */ 84 uint32 pausedAtTick; 85 86 /** 87 * The time, in ticks, that the channel fade began. If 0, the channel is not 88 * being faded. 89 */ 90 uint32 fadeStartTick; 91 92 /** 93 * The start volume of a fade. 94 */ 95 int fadeStartVolume; 96 97 /** 98 * The total length of the fade, in ticks. 99 */ 100 uint32 fadeDuration; 101 102 /** 103 * The end volume of a fade. 104 */ 105 int fadeTargetVolume; 106 107 /** 108 * Whether or not the channel should be stopped and freed when the fade is 109 * complete. 110 */ 111 bool stopChannelOnFade; 112 113 /** 114 * Whether or not this channel contains a Robot audio block. 115 */ 116 bool robot; 117 118 /** 119 * For digital sound effects, the related VM Sound::nodePtr object for the 120 * sound. 121 */ 122 reg_t soundNode; 123 124 /** 125 * The playback volume, from 1 to 127 inclusive. 126 */ 127 int volume; 128 129 /** 130 * The amount to pan to the right, from 0 to 100. 50 is centered, -1 is not 131 * panned. 132 */ 133 int pan; 134 135 AudioChannel &operator=(AudioChannel &other) { 136 id = other.id; 137 resource = other.resource; 138 stream.reset(other.stream.release()); 139 converter.reset(other.converter.release()); 140 duration = other.duration; 141 startedAtTick = other.startedAtTick; 142 pausedAtTick = other.pausedAtTick; 143 fadeStartTick = other.fadeStartTick; 144 fadeStartVolume = other.fadeStartVolume; 145 fadeDuration = other.fadeDuration; 146 fadeTargetVolume = other.fadeTargetVolume; 147 stopChannelOnFade = other.stopChannelOnFade; 148 robot = other.robot; 149 soundNode = other.soundNode; 150 volume = other.volume; 151 pan = other.pan; 152 return *this; 153 } 154 }; 155 156 #pragma mark - 157 158 /** 159 * Special audio channel indexes used to select a channel for digital audio 160 * playback. 161 */ 162 enum AudioChannelIndex { 163 kRobotChannel = -3, 164 kNoExistingChannel = -2, 165 kAllChannels = -1 166 }; 167 168 /** 169 * Audio32 acts as a permanent audio stream into the system mixer and provides 170 * digital audio services for the SCI32 engine, since the system mixer does not 171 * support all the features of SCI. 172 */ 173 class Audio32 : public Audio::AudioStream, public Common::Serializable { 174 public: 175 Audio32(ResourceManager *resMan); 176 ~Audio32(); 177 178 virtual void saveLoadWithSerializer(Common::Serializer &s); 179 180 enum { 181 /** 182 * The maximum channel volume. 183 */ 184 kMaxVolume = 127, 185 186 kMonitorAudioFlagSci3 = 0x80 187 }; 188 189 private: 190 ResourceManager *_resMan; 191 Audio::Mixer *_mixer; 192 Audio::SoundHandle _handle; 193 Common::Mutex _mutex; 194 195 #pragma mark - 196 #pragma mark AudioStream implementation 197 public: 198 int readBuffer(Audio::st_sample_t *buffer, const int numSamples); isStereo()199 bool isStereo() const { return true; } getRate()200 int getRate() const { return _mixer->getOutputRate(); } endOfData()201 bool endOfData() const { return _numActiveChannels == 0; } endOfStream()202 bool endOfStream() const { return false; } 203 204 private: 205 /** 206 * Determines the number of channels that will be mixed together during a 207 * call to readBuffer. 208 */ 209 int16 getNumChannelsToMix() const; 210 211 /** 212 * Determines whether or not the given audio channel will be mixed into the 213 * output stream. 214 */ 215 bool channelShouldMix(const AudioChannel &channel) const; 216 217 /** 218 * Mixes audio from the given source stream into the target buffer using the 219 * given rate converter. 220 */ 221 int writeAudioInternal(Audio::AudioStream &sourceStream, Audio::RateConverter &converter, Audio::st_sample_t *targetBuffer, const int numSamples, const Audio::st_volume_t leftVolume, const Audio::st_volume_t rightVolume); 222 223 #pragma mark - 224 #pragma mark Channel management 225 public: 226 /** 227 * Gets the number of currently active channels. 228 */ getNumActiveChannels()229 inline uint8 getNumActiveChannels() const { 230 Common::StackLock lock(_mutex); 231 return _numActiveChannels; 232 } 233 234 /** 235 * Gets the number of currently active channels that are playing from 236 * unlocked resources. 237 * 238 * @note In SSCI, this function would actually return the number of channels 239 * whose audio data were not loaded into memory. In practice, the signal for 240 * placing audio data into memory was a call to kLock, so since we do not 241 * follow how SSCI works when it comes to resource management, the lock 242 * state is used as an (apparently) successful proxy for this information 243 * instead. 244 */ 245 uint8 getNumUnlockedChannels() const; 246 247 /** 248 * Finds a channel that is already configured for the given audio sample. 249 * 250 * @param startIndex The location of the audio resource information in the 251 * arguments list. 252 */ 253 int16 findChannelByArgs(int argc, const reg_t *argv, const int startIndex, const reg_t soundNode) const; 254 255 /** 256 * Finds a channel that is already configured for the given audio sample. 257 */ 258 int16 findChannelById(const ResourceId resourceId, const reg_t soundNode = NULL_REG) const; 259 260 /** 261 * Sets or clears a lock on the given resource ID. 262 */ 263 void lockResource(const ResourceId resourceId, const bool lock); 264 265 private: 266 typedef Common::Array<ResourceId> LockList; 267 typedef Common::Array<Resource *> UnlockList; 268 269 /** 270 * The audio channels. 271 */ 272 Common::Array<AudioChannel> _channels; 273 274 /** 275 * The number of active audio channels in the mixer. Being active is not the 276 * same as playing; active channels may be paused. 277 */ 278 uint8 _numActiveChannels; 279 280 /** 281 * Whether or not we are in the audio thread. 282 * 283 * This flag is used instead of passing a parameter to `freeUnusedChannels` 284 * because a parameter would require forwarding through the public method 285 * `stop`, and there is not currently any reason for this implementation 286 * detail to be exposed. 287 */ 288 bool _inAudioThread; 289 290 /** 291 * The list of resources from freed channels that need to be unlocked from 292 * the main thread. 293 */ 294 UnlockList _resourcesToUnlock; 295 296 /** 297 * The list of resource IDs that have been locked by game scripts. 298 */ 299 LockList _lockedResourceIds; 300 301 /** 302 * Gets the audio channel at the given index. 303 */ getChannel(const int16 channelIndex)304 inline AudioChannel &getChannel(const int16 channelIndex) { 305 Common::StackLock lock(_mutex); 306 assert(channelIndex >= 0 && channelIndex < _numActiveChannels); 307 return _channels[channelIndex]; 308 } 309 310 /** 311 * Gets the audio channel at the given index. 312 */ getChannel(const int16 channelIndex)313 inline const AudioChannel &getChannel(const int16 channelIndex) const { 314 Common::StackLock lock(_mutex); 315 assert(channelIndex >= 0 && channelIndex < _numActiveChannels); 316 return _channels[channelIndex]; 317 } 318 319 /** 320 * Frees all non-looping channels that have reached the end of their stream. 321 */ 322 void freeUnusedChannels(); 323 324 /** 325 * Frees resources allocated to the given channel. 326 */ 327 void freeChannel(const int16 channelIndex); 328 329 /** 330 * Unlocks all resources that were freed by the audio thread. 331 */ 332 void unlockResources(); 333 334 #pragma mark - 335 #pragma mark Script compatibility 336 public: 337 /** 338 * Gets the (fake) sample rate of the hardware DAC. For script compatibility 339 * only. 340 */ getSampleRate()341 inline uint16 getSampleRate() const { 342 return _globalSampleRate; 343 } 344 345 /** 346 * Sets the (fake) sample rate of the hardware DAC. For script compatibility 347 * only. 348 */ 349 void setSampleRate(uint16 rate); 350 351 /** 352 * Gets the (fake) bit depth of the hardware DAC. For script compatibility 353 * only. 354 */ getBitDepth()355 inline uint8 getBitDepth() const { 356 return _globalBitDepth; 357 } 358 359 /** 360 * Sets the (fake) sample rate of the hardware DAC. For script compatibility 361 * only. 362 */ 363 void setBitDepth(uint8 depth); 364 365 /** 366 * Gets the (fake) number of output (speaker) channels of the hardware DAC. 367 * For script compatibility only. 368 */ getNumOutputChannels()369 inline uint8 getNumOutputChannels() const { 370 return _globalNumOutputChannels; 371 } 372 373 /** 374 * Sets the (fake) number of output (speaker) channels of the hardware DAC. 375 * For script compatibility only. 376 */ 377 void setNumOutputChannels(int16 numChannels); 378 379 /** 380 * Gets the (fake) number of preloaded channels. For script compatibility 381 * only. 382 */ getPreload()383 inline uint8 getPreload() const { 384 return _preload; 385 } 386 387 /** 388 * Sets the (fake) number of preloaded channels. For script compatibility 389 * only. 390 */ setPreload(uint8 preload)391 inline void setPreload(uint8 preload) { 392 _preload = preload; 393 } 394 395 private: 396 /** 397 * The hardware DAC sample rate. Stored only for script compatibility. 398 */ 399 uint16 _globalSampleRate; 400 401 /** 402 * The maximum allowed sample rate of the system mixer. Stored only for 403 * script compatibility. 404 */ 405 uint16 _maxAllowedSampleRate; 406 407 /** 408 * The hardware DAC bit depth. Stored only for script compatibility. 409 */ 410 uint8 _globalBitDepth; 411 412 /** 413 * The maximum allowed bit depth of the system mixer. Stored only for script 414 * compatibility. 415 */ 416 uint8 _maxAllowedBitDepth; 417 418 /** 419 * The hardware DAC output (speaker) channel configuration. Stored only for 420 * script compatibility. 421 */ 422 uint8 _globalNumOutputChannels; 423 424 /** 425 * The maximum allowed number of output (speaker) channels of the system 426 * mixer. Stored only for script compatibility. 427 */ 428 uint8 _maxAllowedOutputChannels; 429 430 /** 431 * The number of audio channels that should have their data preloaded into 432 * memory instead of streaming from disk. 1 = all channels, 2 = 2nd active 433 * channel and above, etc. Stored only for script compatibility. 434 */ 435 uint8 _preload; 436 437 #pragma mark - 438 #pragma mark Robot 439 public: 440 bool playRobotAudio(const RobotAudioStream::RobotAudioPacket &packet); 441 bool queryRobotAudio(RobotAudioStream::StreamState &outStatus) const; 442 bool finishRobotAudio(); 443 bool stopRobotAudio(); 444 445 private: 446 /** 447 * Finds a channel that is configured for robot playback. 448 */ 449 int16 findRobotChannel() const; 450 451 /** 452 * When true, channels marked as robot audio will not be played. 453 */ 454 bool _robotAudioPaused; 455 456 #pragma mark - 457 #pragma mark Playback 458 public: 459 /** 460 * Starts or resumes playback of an audio channel. 461 */ 462 uint16 play(int16 channelIndex, const ResourceId resourceId, const bool autoPlay, const bool loop, const int16 volume, const reg_t soundNode, const bool monitor); 463 464 /** 465 * Resumes playback of a paused audio channel, or of the entire audio 466 * player. 467 */ 468 bool resume(const int16 channelIndex); 469 bool resume(const ResourceId resourceId, const reg_t soundNode = NULL_REG) { 470 Common::StackLock lock(_mutex); 471 return resume(findChannelById(resourceId, soundNode)); 472 } 473 474 /** 475 * Pauses an audio channel, or the entire audio player. 476 */ 477 bool pause(const int16 channelIndex); 478 bool pause(const ResourceId resourceId, const reg_t soundNode = NULL_REG) { 479 Common::StackLock lock(_mutex); 480 return pause(findChannelById(resourceId, soundNode)); 481 } 482 483 /** 484 * Stops and unloads an audio channel, or the entire audio player. 485 */ 486 int16 stop(const int16 channelIndex); 487 int16 stop(const ResourceId resourceId, const reg_t soundNode = NULL_REG) { 488 Common::StackLock lock(_mutex); 489 return stop(findChannelById(resourceId, soundNode)); 490 } 491 492 /** 493 * Restarts playback of the given audio resource. 494 */ 495 uint16 restart(const ResourceId resourceId, const bool autoPlay, const bool loop, const int16 volume, const reg_t soundNode, const bool monitor); 496 497 /** 498 * Returns the playback position for the given channel number, in ticks. 499 */ 500 int16 getPosition(const int16 channelIndex) const; 501 int16 getPosition(const ResourceId resourceId, const reg_t soundNode = NULL_REG) { 502 Common::StackLock lock(_mutex); 503 return getPosition(findChannelById(resourceId, soundNode)); 504 } 505 506 /** 507 * Sets whether or not the given channel should loop. 508 */ 509 void setLoop(const int16 channelIndex, const bool loop); setLoop(const ResourceId resourceId,const reg_t soundNode,const bool loop)510 void setLoop(const ResourceId resourceId, const reg_t soundNode, const bool loop) { 511 Common::StackLock lock(_mutex); 512 setLoop(findChannelById(resourceId, soundNode), loop); 513 } 514 515 /** 516 * Sets the stereo panning for the given channel. 517 */ setPan(const int16 channelIndex,const int16 pan)518 void setPan(const int16 channelIndex, const int16 pan) { 519 Common::StackLock lock(_mutex); 520 getChannel(channelIndex).pan = pan; 521 } 522 523 private: 524 /** 525 * The tick when audio was globally paused. 526 */ 527 uint32 _pausedAtTick; 528 529 /** 530 * The tick when audio was globally started. 531 */ 532 uint32 _startedAtTick; 533 534 #pragma mark - 535 #pragma mark Effects 536 public: 537 /** 538 * Gets the volume for a given channel. Passing `kAllChannels` will get the 539 * global volume. 540 */ 541 int16 getVolume(const int16 channelIndex) const; getVolume(const ResourceId resourceId,const reg_t soundNode)542 int16 getVolume(const ResourceId resourceId, const reg_t soundNode) const { 543 Common::StackLock lock(_mutex); 544 return getVolume(findChannelById(resourceId, soundNode)); 545 } 546 547 /** 548 * Sets the volume of an audio channel. Passing `kAllChannels` will set the 549 * global volume. 550 */ 551 void setVolume(const int16 channelIndex, int16 volume); setVolume(const ResourceId resourceId,const reg_t soundNode,const int16 volume)552 void setVolume(const ResourceId resourceId, const reg_t soundNode, const int16 volume) { 553 Common::StackLock lock(_mutex); 554 setVolume(findChannelById(resourceId, soundNode), volume); 555 } 556 557 /** 558 * Sets the master volume for digital audio playback. 559 */ setMasterVolume(const int16 volume)560 void setMasterVolume(const int16 volume) { 561 _mixer->setVolumeForSoundType(Audio::Mixer::kSFXSoundType, volume * Audio::Mixer::kMaxChannelVolume / kMaxVolume); 562 } 563 564 /** 565 * Initiate an immediate fade of the given channel. 566 */ 567 bool fadeChannel(const int16 channelIndex, const int16 targetVolume, const int16 speed, const int16 steps, const bool stopAfterFade); fadeChannel(const ResourceId resourceId,const reg_t soundNode,const int16 targetVolume,const int16 speed,const int16 steps,const bool stopAfterFade)568 bool fadeChannel(const ResourceId resourceId, const reg_t soundNode, const int16 targetVolume, const int16 speed, const int16 steps, const bool stopAfterFade) { 569 Common::StackLock lock(_mutex); 570 return fadeChannel(findChannelById(resourceId, soundNode), targetVolume, speed, steps, stopAfterFade); 571 } 572 573 /** 574 * Gets whether attenuated mixing mode is active. 575 */ getAttenuatedMixing()576 inline bool getAttenuatedMixing() const { 577 return _attenuatedMixing; 578 } 579 580 /** 581 * Sets the attenuated mixing mode. 582 */ setAttenuatedMixing(bool attenuated)583 void setAttenuatedMixing(bool attenuated) { 584 Common::StackLock lock(_mutex); 585 _attenuatedMixing = attenuated; 586 } 587 588 private: 589 /** 590 * If true, audio will be mixed by reducing the target buffer by half every 591 * time a new channel is mixed in. The final channel is not attenuated. 592 */ 593 bool _attenuatedMixing; 594 595 /** 596 * When true, a modified attenuation algorithm is used (`A/4 + B`) instead 597 * of standard linear attenuation (`A/2 + B/2`). 598 */ 599 bool _useModifiedAttenuation; 600 601 /** 602 * Processes an audio fade for the given channel. 603 * 604 * @returns true if the fade was completed and the channel was stopped. 605 */ 606 bool processFade(const int16 channelIndex); 607 608 #pragma mark - 609 #pragma mark Signal monitoring 610 public: 611 /** 612 * Returns whether the currently monitored audio channel contains any signal 613 * within the next audio frame. 614 */ 615 bool hasSignal() const; 616 617 private: 618 /** 619 * The index of the channel being monitored for signal, or -1 if no channel 620 * is monitored. When a channel is monitored, it also causes the engine to 621 * play only the monitored channel. 622 */ 623 int16 _monitoredChannelIndex; 624 625 /** 626 * The data buffer holding decompressed audio data for the channel that will 627 * be monitored for an audio signal. 628 */ 629 Common::Array<Audio::st_sample_t> _monitoredBuffer; 630 631 /** 632 * The number of valid audio samples in the signal monitoring buffer. 633 */ 634 int _numMonitoredSamples; 635 636 #pragma mark - 637 #pragma mark Kernel 638 public: 639 reg_t kernelPlay(const bool autoPlay, const int argc, const reg_t *const argv); 640 reg_t kernelStop(const int argc, const reg_t *const argv); 641 reg_t kernelPause(const int argc, const reg_t *const argv); 642 reg_t kernelResume(const int argc, const reg_t *const argv); 643 reg_t kernelPosition(const int argc, const reg_t *const argv); 644 reg_t kernelVolume(const int argc, const reg_t *const argv); 645 reg_t kernelMixing(const int argc, const reg_t *const argv); 646 reg_t kernelFade(const int argc, const reg_t *const argv); 647 void kernelLoop(const int argc, const reg_t *const argv); 648 void kernelPan(const int argc, const reg_t *const argv); 649 void kernelPanOff(const int argc, const reg_t *const argv); 650 651 #pragma mark - 652 #pragma mark Debugging 653 public: 654 void printAudioList(Console *con) const; 655 }; 656 657 } // End of namespace Sci 658 #endif 659