DIRECTOR: Add flexible number of sound channels

This commit is contained in:
Scott Percival 2020-03-28 16:12:41 +08:00
parent 4a918a1fcd
commit 88d8577cb3
3 changed files with 55 additions and 42 deletions

View File

@ -32,6 +32,7 @@
#include "director/sound.h"
#include "director/sprite.h"
#include "director/stxt.h"
#include "director/util.h"
#include "graphics/macgui/macwindowmanager.h"
#include "graphics/macgui/macmenu.h"
@ -1800,7 +1801,7 @@ void LB::b_soundPlayFile(int nargs) {
return;
}
sound->playFile(*whichFile.u.s, whichChannel.u.i);
sound->playFile(pathMakeRelative(*whichFile.u.s), whichChannel.u.i);
}
void LB::b_soundStop(int nargs) {

View File

@ -35,8 +35,15 @@
namespace Director {
DirectorSound::DirectorSound() {
_sound1 = new Audio::SoundHandle();
_sound2 = new Audio::SoundHandle();
uint numChannels = 2;
if (g_director->getVersion() >= 4) {
numChannels = 4;
}
for (uint i = 0; i < numChannels; i++) {
_channels.push_back(new Audio::SoundHandle());
}
_scriptSound = new Audio::SoundHandle();
_mixer = g_system->getMixer();
@ -47,8 +54,9 @@ DirectorSound::DirectorSound() {
}
DirectorSound::~DirectorSound() {
delete _sound1;
delete _sound2;
for (uint i = 0; i < _channels.size(); i++) {
delete _channels[i];
}
delete _scriptSound;
}
@ -82,6 +90,11 @@ void DirectorSound::playFile(Common::String filename, uint8 soundChannel) {
void DirectorSound::playWAV(Common::String filename, uint8 soundChannel) {
Common::File *file = new Common::File();
if (soundChannel == 0 || soundChannel > _channels.size()) {
warning("Invalid sound channel %d", soundChannel);
return;
}
if (!file->open(filename)) {
warning("Failed to open %s", filename.c_str());
@ -92,15 +105,17 @@ void DirectorSound::playWAV(Common::String filename, uint8 soundChannel) {
Audio::RewindableAudioStream *sound = Audio::makeWAVStream(file, DisposeAfterUse::YES);
if (soundChannel == 1)
_mixer->playStream(Audio::Mixer::kSFXSoundType, _sound1, sound);
else
_mixer->playStream(Audio::Mixer::kSFXSoundType, _sound2, sound);
_mixer->playStream(Audio::Mixer::kSFXSoundType, _channels[soundChannel - 1], sound);
}
void DirectorSound::playAIFF(Common::String filename, uint8 soundChannel) {
Common::File *file = new Common::File();
if (soundChannel == 0 || soundChannel > _channels.size()) {
warning("Invalid sound channel %d", soundChannel);
return;
}
if (!file->open(filename)) {
warning("Failed to open %s", filename.c_str());
delete file;
@ -109,10 +124,7 @@ void DirectorSound::playAIFF(Common::String filename, uint8 soundChannel) {
Audio::RewindableAudioStream *sound = Audio::makeAIFFStream(file, DisposeAfterUse::YES);
if (soundChannel == 1)
_mixer->playStream(Audio::Mixer::kSFXSoundType, _sound1, sound);
else
_mixer->playStream(Audio::Mixer::kSFXSoundType, _sound2, sound);
_mixer->playStream(Audio::Mixer::kSFXSoundType, _channels[soundChannel - 1], sound);
}
void DirectorSound::playMCI(Audio::AudioStream &stream, uint32 from, uint32 to) {
@ -128,36 +140,38 @@ void DirectorSound::playStream(Audio::AudioStream &stream, uint8 soundChannel) {
}
void DirectorSound::playStream(Audio::SeekableAudioStream &stream, uint8 soundChannel) {
if (soundChannel == 1)
_mixer->playStream(Audio::Mixer::kSFXSoundType, _sound1, &stream);
else
_mixer->playStream(Audio::Mixer::kSFXSoundType, _sound2, &stream);
}
bool DirectorSound::isChannelActive(uint8 channelID) {
if (channelID == 1) {
return _mixer->isSoundHandleActive(*_sound1);
} else if (channelID == 2) {
return _mixer->isSoundHandleActive(*_sound2);
if (soundChannel == 0 || soundChannel > _channels.size()) {
warning("Invalid sound channel %d", soundChannel);
return;
}
error("Incorrect sound channel");
return false;
_mixer->playStream(Audio::Mixer::kSFXSoundType, _channels[soundChannel - 1], &stream);
}
void DirectorSound::stopSound(uint8 channelID) {
if (channelID == 1) {
_mixer->stopHandle(*_sound1);
} else if (channelID == 2) {
_mixer->stopHandle(*_sound2);
bool DirectorSound::isChannelActive(uint8 soundChannel) {
if (soundChannel == 0 || soundChannel > _channels.size()) {
warning("Invalid sound channel %d", soundChannel);
return false;
}
return _mixer->isSoundHandleActive(*_channels[soundChannel - 1]);
}
void DirectorSound::stopSound(uint8 soundChannel) {
if (soundChannel == 0 || soundChannel > _channels.size()) {
warning("Invalid sound channel %d", soundChannel);
return;
}
_mixer->stopHandle(*_channels[soundChannel - 1]);
return;
}
void DirectorSound::stopSound() {
_mixer->stopHandle(*_sound1);
_mixer->stopHandle(*_sound2);
for (uint i = 0; i < _channels.size(); i++) {
_mixer->stopHandle(*_channels[i]);
}
_mixer->stopHandle(*_scriptSound);
_mixer->stopHandle(*_pcSpeakerHandle);
}
@ -179,7 +193,6 @@ SNDDecoder::~SNDDecoder() {
}
}
bool SNDDecoder::loadStream(Common::SeekableSubReadStreamEndian &stream) {
if (_data) {
free(_data);

View File

@ -35,8 +35,7 @@ namespace Director {
class DirectorSound {
private:
Audio::SoundHandle *_sound1;
Audio::SoundHandle *_sound2;
Common::Array<Audio::SoundHandle *> _channels;
Audio::SoundHandle *_scriptSound;
Audio::Mixer *_mixer;
Audio::PCSpeaker *_speaker;
@ -46,15 +45,15 @@ public:
DirectorSound();
~DirectorSound();
void playWAV(Common::String filename, uint8 channelID);
void playAIFF(Common::String filename, uint8 channelID);
void playFile(Common::String filename, uint8 channelID);
void playWAV(Common::String filename, uint8 soundChannel);
void playAIFF(Common::String filename, uint8 soundChannel);
void playFile(Common::String filename, uint8 soundChannel);
void playMCI(Audio::AudioStream &stream, uint32 from, uint32 to);
void playStream(Audio::AudioStream &stream, uint8 soundChannel);
void playStream(Audio::SeekableAudioStream &stream, uint8 soundChannel);
void systemBeep();
bool isChannelActive(uint8 channelID);
void stopSound(uint8 channelID);
bool isChannelActive(uint8 soundChannel);
void stopSound(uint8 soundChannel);
void stopSound();
};