2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
2005-01-09 16:06:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-01-09 16:06:29 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2005-08-19 22:12:09 +00:00
|
|
|
#include "kyra/sound.h"
|
2011-04-28 13:39:17 +00:00
|
|
|
#include "kyra/resource.h"
|
2004-11-11 13:37:35 +00:00
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/mixer.h"
|
|
|
|
#include "audio/audiostream.h"
|
|
|
|
|
|
|
|
#include "audio/decoders/flac.h"
|
|
|
|
#include "audio/decoders/mp3.h"
|
|
|
|
#include "audio/decoders/raw.h"
|
2011-04-28 13:39:17 +00:00
|
|
|
#include "audio/decoders/voc.h"
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/decoders/vorbis.h"
|
2006-02-10 16:39:56 +00:00
|
|
|
|
2004-11-11 13:37:35 +00:00
|
|
|
namespace Kyra {
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2008-05-11 23:16:50 +00:00
|
|
|
Sound::Sound(KyraEngine_v1 *vm, Audio::Mixer *mixer)
|
2008-03-21 16:18:27 +00:00
|
|
|
: _vm(vm), _mixer(mixer), _soundChannels(), _musicEnabled(1),
|
|
|
|
_sfxEnabled(true), _soundDataList(0) {
|
2006-02-27 22:39:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Sound::~Sound() {
|
|
|
|
}
|
|
|
|
|
2011-10-28 22:37:08 +00:00
|
|
|
Sound::kType Sound::getSfxType() const {
|
|
|
|
return getMusicType();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::setSoundList(const AudioDataStruct *list) {
|
|
|
|
_soundDataList = list;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sound::hasSoundFile(uint file) const {
|
|
|
|
return (fileListEntry(file) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sound::isPlaying() const {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-28 22:42:18 +00:00
|
|
|
bool Sound::isVoicePresent(const char *file) const {
|
2011-10-28 22:46:47 +00:00
|
|
|
Common::String filename;
|
2009-05-29 17:12:45 +00:00
|
|
|
|
|
|
|
for (int i = 0; _supportedCodecs[i].fileext; ++i) {
|
2011-10-28 22:46:47 +00:00
|
|
|
filename = file;
|
|
|
|
filename += _supportedCodecs[i].fileext;
|
2009-05-29 17:12:45 +00:00
|
|
|
|
2011-10-28 22:46:47 +00:00
|
|
|
if (_vm->resource()->exists(filename.c_str()))
|
2009-05-29 17:12:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-05-24 01:37:51 +00:00
|
|
|
int32 Sound::voicePlay(const char *file, Audio::SoundHandle *handle, uint8 volume, bool isSfx) {
|
2010-01-05 20:14:28 +00:00
|
|
|
Audio::SeekableAudioStream *audioStream = getVoiceStream(file);
|
2006-02-27 22:39:55 +00:00
|
|
|
|
2009-05-23 23:33:31 +00:00
|
|
|
if (!audioStream) {
|
2008-05-24 22:47:08 +00:00
|
|
|
return 0;
|
2009-05-23 23:33:31 +00:00
|
|
|
}
|
2008-03-21 16:18:27 +00:00
|
|
|
|
2010-01-05 20:14:28 +00:00
|
|
|
int playTime = audioStream->getLength().msecs();
|
2009-05-24 01:29:09 +00:00
|
|
|
playVoiceStream(audioStream, handle, volume, isSfx);
|
2009-05-23 23:58:40 +00:00
|
|
|
return playTime;
|
2009-05-23 23:33:31 +00:00
|
|
|
}
|
|
|
|
|
2011-10-28 22:42:18 +00:00
|
|
|
Audio::SeekableAudioStream *Sound::getVoiceStream(const char *file) const {
|
2011-10-28 22:46:47 +00:00
|
|
|
Common::String filename;
|
2008-02-17 02:06:04 +00:00
|
|
|
|
2010-01-05 20:14:28 +00:00
|
|
|
Audio::SeekableAudioStream *audioStream = 0;
|
2008-06-03 18:12:21 +00:00
|
|
|
for (int i = 0; _supportedCodecs[i].fileext; ++i) {
|
2011-10-28 22:46:47 +00:00
|
|
|
filename = file;
|
|
|
|
filename += _supportedCodecs[i].fileext;
|
2006-02-27 22:39:55 +00:00
|
|
|
|
2011-10-28 22:46:47 +00:00
|
|
|
Common::SeekableReadStream *stream = _vm->resource()->createReadStream(filename);
|
2008-02-07 23:13:13 +00:00
|
|
|
if (!stream)
|
2006-02-27 22:39:55 +00:00
|
|
|
continue;
|
2009-05-29 17:12:45 +00:00
|
|
|
|
2010-01-16 21:36:08 +00:00
|
|
|
audioStream = _supportedCodecs[i].streamFunc(stream, DisposeAfterUse::YES);
|
2006-02-27 22:39:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-05-29 17:12:45 +00:00
|
|
|
if (!audioStream) {
|
|
|
|
warning("Couldn't load sound file '%s'", file);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return audioStream;
|
|
|
|
}
|
2006-02-27 22:39:55 +00:00
|
|
|
}
|
|
|
|
|
2009-05-24 01:29:09 +00:00
|
|
|
bool Sound::playVoiceStream(Audio::AudioStream *stream, Audio::SoundHandle *handle, uint8 volume, bool isSfx) {
|
2009-02-01 19:27:01 +00:00
|
|
|
int h = 0;
|
2010-03-20 20:25:37 +00:00
|
|
|
while (h < kNumChannelHandles && _mixer->isSoundHandleActive(_soundChannels[h]))
|
|
|
|
++h;
|
|
|
|
|
2011-10-22 17:37:49 +00:00
|
|
|
if (h >= kNumChannelHandles) {
|
|
|
|
// When we run out of handles we need to destroy the stream object,
|
|
|
|
// this is to avoid memory leaks in some scenes where too many sfx
|
|
|
|
// are started.
|
|
|
|
// See bug #3427240 "LOL-CD: Memory leak in caves level 3".
|
|
|
|
delete stream;
|
2009-05-24 01:29:09 +00:00
|
|
|
return false;
|
2011-10-22 17:37:49 +00:00
|
|
|
}
|
2009-05-24 01:29:09 +00:00
|
|
|
|
2010-04-12 09:14:17 +00:00
|
|
|
_mixer->playStream(isSfx ? Audio::Mixer::kSFXSoundType : Audio::Mixer::kSpeechSoundType, &_soundChannels[h], stream, -1, volume);
|
2009-05-24 01:29:09 +00:00
|
|
|
if (handle)
|
|
|
|
*handle = _soundChannels[h];
|
2009-05-21 13:13:09 +00:00
|
|
|
|
2009-05-24 01:29:09 +00:00
|
|
|
return true;
|
2009-02-01 19:27:01 +00:00
|
|
|
}
|
|
|
|
|
2009-05-24 01:29:09 +00:00
|
|
|
void Sound::voiceStop(const Audio::SoundHandle *handle) {
|
|
|
|
if (!handle) {
|
2010-03-20 20:25:37 +00:00
|
|
|
for (int h = 0; h < kNumChannelHandles; ++h) {
|
2009-05-24 01:29:09 +00:00
|
|
|
if (_mixer->isSoundHandleActive(_soundChannels[h]))
|
|
|
|
_mixer->stopHandle(_soundChannels[h]);
|
2008-03-21 16:18:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-05-24 01:29:09 +00:00
|
|
|
_mixer->stopHandle(*handle);
|
2008-02-17 02:06:04 +00:00
|
|
|
}
|
2006-05-28 04:46:34 +00:00
|
|
|
}
|
|
|
|
|
2011-10-28 22:42:18 +00:00
|
|
|
bool Sound::voiceIsPlaying(const Audio::SoundHandle *handle) const {
|
2009-05-24 01:29:09 +00:00
|
|
|
if (!handle) {
|
2010-03-20 20:25:37 +00:00
|
|
|
for (int h = 0; h < kNumChannelHandles; ++h) {
|
2009-05-24 01:29:09 +00:00
|
|
|
if (_mixer->isSoundHandleActive(_soundChannels[h]))
|
|
|
|
return true;
|
2008-03-21 16:18:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-05-24 01:29:09 +00:00
|
|
|
return _mixer->isSoundHandleActive(*handle);
|
2008-02-17 02:06:04 +00:00
|
|
|
}
|
2009-05-24 01:29:09 +00:00
|
|
|
|
|
|
|
return false;
|
2006-02-27 22:39:55 +00:00
|
|
|
}
|
|
|
|
|
2011-10-28 22:42:18 +00:00
|
|
|
bool Sound::allVoiceChannelsPlaying() const {
|
2009-05-10 13:40:28 +00:00
|
|
|
for (int i = 0; i < kNumChannelHandles; ++i)
|
2009-05-24 01:29:09 +00:00
|
|
|
if (!_mixer->isSoundHandleActive(_soundChannels[i]))
|
2009-05-10 13:40:28 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-02-27 22:39:55 +00:00
|
|
|
#pragma mark -
|
|
|
|
|
2008-05-11 23:16:50 +00:00
|
|
|
void KyraEngine_v1::snd_playTheme(int file, int track) {
|
2008-04-06 14:11:43 +00:00
|
|
|
if (_curMusicTheme == file)
|
|
|
|
return;
|
|
|
|
|
2007-10-10 09:06:15 +00:00
|
|
|
_curSfxFile = _curMusicTheme = file;
|
|
|
|
_sound->loadSoundFile(_curMusicTheme);
|
2008-11-30 02:47:20 +00:00
|
|
|
|
|
|
|
// Kyrandia 2 uses a special file for
|
|
|
|
// MIDI sound effects, so we load
|
|
|
|
// this here
|
|
|
|
if (_flags.gameID == GI_KYRA2)
|
|
|
|
_sound->loadSfxFile("K2SFX");
|
|
|
|
|
2008-04-10 15:41:06 +00:00
|
|
|
if (track != -1)
|
|
|
|
_sound->playTrack(track);
|
2007-10-10 09:06:15 +00:00
|
|
|
}
|
|
|
|
|
2008-05-11 23:16:50 +00:00
|
|
|
void KyraEngine_v1::snd_playSoundEffect(int track, int volume) {
|
2007-10-10 09:06:15 +00:00
|
|
|
_sound->playSoundEffect(track);
|
|
|
|
}
|
|
|
|
|
2008-05-11 23:16:50 +00:00
|
|
|
void KyraEngine_v1::snd_playWanderScoreViaMap(int command, int restart) {
|
2007-10-10 09:06:15 +00:00
|
|
|
if (restart)
|
|
|
|
_lastMusicCommand = -1;
|
|
|
|
|
|
|
|
// no track mapping given
|
|
|
|
// so don't do anything here
|
|
|
|
if (!_trackMap || !_trackMapSize)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//if (!_disableSound) {
|
|
|
|
// XXX
|
|
|
|
//}
|
2008-01-27 19:47:41 +00:00
|
|
|
|
2009-04-21 11:26:19 +00:00
|
|
|
if (_flags.platform == Common::kPlatformPC || _flags.platform == Common::kPlatformMacintosh) {
|
2008-01-12 08:04:00 +00:00
|
|
|
assert(command*2+1 < _trackMapSize);
|
|
|
|
if (_curMusicTheme != _trackMap[command*2]) {
|
|
|
|
if (_trackMap[command*2] != -1 && _trackMap[command*2] != -2)
|
2008-04-10 15:41:06 +00:00
|
|
|
snd_playTheme(_trackMap[command*2], -1);
|
2008-01-12 08:04:00 +00:00
|
|
|
}
|
2007-10-10 09:06:15 +00:00
|
|
|
|
2008-01-12 08:04:00 +00:00
|
|
|
if (command != 1) {
|
|
|
|
if (_lastMusicCommand != command) {
|
|
|
|
_sound->haltTrack();
|
|
|
|
_sound->playTrack(_trackMap[command*2+1]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_sound->beginFadeOut();
|
|
|
|
}
|
|
|
|
} else if (_flags.platform == Common::kPlatformFMTowns || _flags.platform == Common::kPlatformPC98) {
|
|
|
|
if (command == -1) {
|
2007-10-10 09:06:15 +00:00
|
|
|
_sound->haltTrack();
|
2008-01-12 08:04:00 +00:00
|
|
|
} else {
|
|
|
|
assert(command*2+1 < _trackMapSize);
|
|
|
|
if (_trackMap[command*2] != -2 && command != _lastMusicCommand) {
|
|
|
|
_sound->haltTrack();
|
|
|
|
_sound->playTrack(command);
|
|
|
|
}
|
2007-10-10 09:06:15 +00:00
|
|
|
}
|
2009-08-08 18:23:14 +00:00
|
|
|
} else if (_flags.platform == Common::kPlatformAmiga) {
|
|
|
|
if (_curMusicTheme != 1)
|
|
|
|
snd_playTheme(1, -1);
|
|
|
|
|
|
|
|
assert(command < _trackMapSize);
|
|
|
|
if (_trackMap[_lastMusicCommand] != _trackMap[command])
|
|
|
|
_sound->playTrack(_trackMap[command]);
|
2008-01-27 19:47:41 +00:00
|
|
|
}
|
2007-10-10 09:06:15 +00:00
|
|
|
|
|
|
|
_lastMusicCommand = command;
|
|
|
|
}
|
|
|
|
|
2008-05-11 23:16:50 +00:00
|
|
|
void KyraEngine_v1::snd_stopVoice() {
|
2009-05-24 01:29:09 +00:00
|
|
|
_sound->voiceStop(&_speechHandle);
|
2007-10-13 06:57:47 +00:00
|
|
|
}
|
|
|
|
|
2008-05-11 23:16:50 +00:00
|
|
|
bool KyraEngine_v1::snd_voiceIsPlaying() {
|
2009-05-24 01:29:09 +00:00
|
|
|
return _sound->voiceIsPlaying(&_speechHandle);
|
2007-10-13 06:57:47 +00:00
|
|
|
}
|
|
|
|
|
2006-02-10 16:39:56 +00:00
|
|
|
// static res
|
|
|
|
|
2009-05-23 23:58:40 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// A simple wrapper to create VOC streams the way like creating MP3, OGG/Vorbis and FLAC streams.
|
|
|
|
// Possible TODO: Think of making this complete and moving it to sound/voc.cpp ?
|
2010-01-16 21:36:08 +00:00
|
|
|
Audio::SeekableAudioStream *makeVOCStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse) {
|
2009-08-13 21:46:41 +00:00
|
|
|
|
|
|
|
#ifdef STREAM_AUDIO_FROM_DISK
|
2010-03-08 10:27:42 +00:00
|
|
|
Audio::SeekableAudioStream *as = Audio::makeVOCStream(stream, Audio::FLAG_UNSIGNED, disposeAfterUse);
|
2009-08-13 21:46:41 +00:00
|
|
|
#else
|
2010-03-08 10:27:42 +00:00
|
|
|
Audio::SeekableAudioStream *as = Audio::makeVOCStream(stream, Audio::FLAG_UNSIGNED, DisposeAfterUse::NO);
|
2009-05-23 23:58:40 +00:00
|
|
|
|
|
|
|
if (disposeAfterUse)
|
|
|
|
delete stream;
|
2009-08-13 21:46:41 +00:00
|
|
|
#endif
|
2009-05-23 23:58:40 +00:00
|
|
|
|
|
|
|
return as;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
2008-06-03 18:12:21 +00:00
|
|
|
const Sound::SpeechCodecs Sound::_supportedCodecs[] = {
|
2009-05-24 00:11:28 +00:00
|
|
|
{ ".VOC", makeVOCStream },
|
|
|
|
{ "", makeVOCStream },
|
|
|
|
|
2007-02-24 23:40:28 +00:00
|
|
|
#ifdef USE_MAD
|
|
|
|
{ ".VO3", Audio::makeMP3Stream },
|
2009-05-24 00:11:28 +00:00
|
|
|
{ ".MP3", Audio::makeMP3Stream },
|
2007-02-24 23:40:28 +00:00
|
|
|
#endif // USE_MAD
|
2009-05-23 23:58:40 +00:00
|
|
|
|
2009-05-24 00:11:28 +00:00
|
|
|
#ifdef USE_VORBIS
|
|
|
|
{ ".VOG", Audio::makeVorbisStream },
|
|
|
|
{ ".OGG", Audio::makeVorbisStream },
|
|
|
|
#endif // USE_VORBIS
|
|
|
|
|
|
|
|
#ifdef USE_FLAC
|
2010-02-03 09:42:11 +00:00
|
|
|
{ ".VOF", Audio::makeFLACStream },
|
|
|
|
{ ".FLA", Audio::makeFLACStream },
|
2009-05-24 00:11:28 +00:00
|
|
|
#endif // USE_FLAC
|
2009-05-23 23:58:40 +00:00
|
|
|
|
2006-02-10 16:39:56 +00:00
|
|
|
{ 0, 0 }
|
|
|
|
};
|
|
|
|
|
2009-10-04 21:26:33 +00:00
|
|
|
} // End of namespace Kyra
|