2008-04-20 14:47:37 +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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "m4/m4.h"
|
|
|
|
#include "m4/sound.h"
|
|
|
|
#include "m4/compression.h"
|
|
|
|
|
2010-01-19 11:20:58 +00:00
|
|
|
#include "common/stream.h"
|
|
|
|
|
2008-04-20 14:47:37 +00:00
|
|
|
#include "sound/audiostream.h"
|
|
|
|
#include "sound/mixer.h"
|
2010-01-19 11:20:58 +00:00
|
|
|
#include "sound/raw.h"
|
2008-04-20 14:47:37 +00:00
|
|
|
|
|
|
|
namespace M4 {
|
|
|
|
|
|
|
|
Sound::Sound(M4Engine *vm, Audio::Mixer *mixer, int volume) :
|
|
|
|
_vm(vm), _mixer(mixer) {
|
|
|
|
|
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++)
|
|
|
|
_handles[i].type = kFreeHandle;
|
|
|
|
|
|
|
|
_dsrFileLoaded = false;
|
|
|
|
|
|
|
|
setVolume(volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
Sound::~Sound() {
|
|
|
|
unloadDSRFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
SndHandle *Sound::getHandle() {
|
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++) {
|
|
|
|
if (_handles[i].type == kFreeHandle)
|
|
|
|
return &_handles[i];
|
|
|
|
|
|
|
|
if (!_mixer->isSoundHandleActive(_handles[i].handle)) {
|
|
|
|
_handles[i].type = kFreeHandle;
|
|
|
|
return &_handles[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
error("Sound::getHandle(): Too many sound handles");
|
2009-09-24 17:52:53 +00:00
|
|
|
return NULL; // for compilers that don't support NORETURN
|
2008-04-20 14:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Sound::isHandleActive(SndHandle *handle) {
|
|
|
|
return (_mixer->isSoundHandleActive(handle->handle));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::playSound(const char *soundName, int volume, bool loop, int channel) {
|
|
|
|
byte flags;
|
|
|
|
Common::SeekableReadStream *soundStream = _vm->res()->get(soundName);
|
|
|
|
SndHandle *handle;
|
|
|
|
if (channel < 0) {
|
|
|
|
handle = getHandle();
|
|
|
|
} else {
|
|
|
|
if (_handles[channel].type == kFreeHandle) {
|
|
|
|
handle = &_handles[channel];
|
|
|
|
} else {
|
|
|
|
warning("Attempted to play a sound on a channel that isn't free");
|
|
|
|
return;
|
2009-01-01 15:06:43 +00:00
|
|
|
}
|
2008-04-20 14:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int bufferSize = soundStream->size();
|
|
|
|
byte *buffer = new byte[bufferSize];
|
|
|
|
soundStream->read(buffer, bufferSize);
|
|
|
|
_vm->res()->toss(soundName);
|
|
|
|
|
|
|
|
handle->type = kEffectHandle;
|
2010-01-19 00:56:29 +00:00
|
|
|
flags = Audio::Mixer::FLAG_UNSIGNED;
|
2008-04-20 14:47:37 +00:00
|
|
|
|
|
|
|
if (loop)
|
|
|
|
flags |= Audio::Mixer::FLAG_LOOP;
|
|
|
|
|
|
|
|
_vm->res()->toss(soundName);
|
|
|
|
|
|
|
|
// Sound format is 8bit mono, unsigned, 11025kHz
|
2010-01-19 11:20:58 +00:00
|
|
|
Audio::AudioStream *stream = Audio::makeRawMemoryStream(buffer, bufferSize, DisposeAfterUse::YES, 11025, flags, 0, 0);
|
|
|
|
_mixer->playInputStream(Audio::Mixer::kSFXSoundType, &handle->handle, stream, -1, volume);
|
2008-04-20 14:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::pauseSound() {
|
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++) {
|
|
|
|
if (_handles[i].type == kEffectHandle)
|
|
|
|
_mixer->pauseHandle(_handles[i].handle, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::resumeSound() {
|
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++) {
|
|
|
|
if (_handles[i].type == kEffectHandle)
|
|
|
|
_mixer->pauseHandle(_handles[i].handle, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::stopSound(int channel) {
|
|
|
|
if (channel >= 0) {
|
|
|
|
if (_handles[channel].type == kEffectHandle) {
|
|
|
|
_mixer->stopHandle(_handles[channel].handle);
|
|
|
|
_handles[channel].type = kFreeHandle;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
warning("Attempted to stop a sound on a channel that is already free");
|
|
|
|
return;
|
2009-01-01 15:06:43 +00:00
|
|
|
}
|
2008-04-20 14:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++) {
|
|
|
|
if (_handles[i].type == kEffectHandle) {
|
|
|
|
_mixer->stopHandle(_handles[i].handle);
|
|
|
|
_handles[i].type = kFreeHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::playVoice(const char *soundName, int volume) {
|
|
|
|
byte flags;
|
|
|
|
Common::SeekableReadStream *soundStream = _vm->res()->get(soundName);
|
|
|
|
SndHandle *handle = getHandle();
|
|
|
|
byte *buffer;
|
|
|
|
|
2010-01-19 11:20:58 +00:00
|
|
|
buffer = (byte *)malloc(soundStream->size());
|
2008-04-20 14:47:37 +00:00
|
|
|
soundStream->read(buffer, soundStream->size());
|
|
|
|
|
|
|
|
handle->type = kEffectHandle;
|
2010-01-19 00:56:29 +00:00
|
|
|
flags = Audio::Mixer::FLAG_UNSIGNED;
|
2008-04-20 14:47:37 +00:00
|
|
|
|
|
|
|
_vm->res()->toss(soundName);
|
|
|
|
|
|
|
|
// Voice format is 8bit mono, unsigned, 11025kHz
|
2010-01-19 11:20:58 +00:00
|
|
|
Audio::AudioStream *stream = Audio::makeRawMemoryStream(buffer, soundStream->size(), DisposeAfterUse::YES, 11025, flags);
|
|
|
|
_mixer->playInputStream(Audio::Mixer::kSFXSoundType, &handle->handle, stream, -1, volume);
|
2008-04-20 14:47:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::pauseVoice() {
|
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++)
|
|
|
|
if (_handles[i].type == kVoiceHandle)
|
|
|
|
_mixer->pauseHandle(_handles[i].handle, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::resumeVoice() {
|
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++)
|
|
|
|
if (_handles[i].type == kVoiceHandle)
|
|
|
|
_mixer->pauseHandle(_handles[i].handle, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::stopVoice() {
|
|
|
|
for (int i = 0; i < SOUND_HANDLES; i++)
|
|
|
|
if (_handles[i].type == kVoiceHandle) {
|
|
|
|
_mixer->stopHandle(_handles[i].handle);
|
|
|
|
_handles[i].type = kFreeHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::stopAll() {
|
|
|
|
stopVoice();
|
|
|
|
stopSound();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::setVolume(int volume) {
|
|
|
|
_mixer->setVolumeForSoundType(Audio::Mixer::kSFXSoundType, volume);
|
|
|
|
_mixer->setVolumeForSoundType(Audio::Mixer::kSpeechSoundType, volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::loadDSRFile(const char *fileName) {
|
|
|
|
if (_dsrFileLoaded)
|
|
|
|
unloadDSRFile();
|
|
|
|
|
|
|
|
Common::SeekableReadStream *fileStream = _vm->res()->get(fileName);
|
|
|
|
|
|
|
|
sprintf(_dsrFile.fileName, "%s", fileName);
|
|
|
|
|
|
|
|
// Read header
|
|
|
|
_dsrFile.entryCount = fileStream->readUint16LE();
|
|
|
|
//printf("DSR has %i entries\n", _dsrFile.entryCount);
|
|
|
|
|
|
|
|
for (int i = 0; i < _dsrFile.entryCount; i++) {
|
|
|
|
DSREntry* newEntry = new DSREntry();
|
|
|
|
newEntry->frequency = fileStream->readUint16LE();
|
|
|
|
newEntry->channels = fileStream->readUint32LE();
|
|
|
|
newEntry->compSize = fileStream->readUint32LE();
|
|
|
|
newEntry->uncompSize = fileStream->readUint32LE();
|
|
|
|
newEntry->offset = fileStream->readUint32LE();
|
|
|
|
_dsrFile.dsrEntries.push_back(newEntry);
|
|
|
|
|
|
|
|
/*
|
|
|
|
printf("%i: ", i);
|
|
|
|
printf("frequency: %i ", newEntry->frequency);
|
|
|
|
printf("channels: %i ", newEntry->channels);
|
|
|
|
printf("comp: %i ", newEntry->compSize);
|
|
|
|
printf("uncomp: %i ", newEntry->uncompSize);
|
|
|
|
printf("offset: %i ", newEntry->offset);
|
|
|
|
printf("\n");
|
|
|
|
*/
|
|
|
|
}
|
2009-01-01 15:06:43 +00:00
|
|
|
|
2008-04-20 14:47:37 +00:00
|
|
|
_vm->res()->toss(fileName);
|
|
|
|
|
|
|
|
_dsrFileLoaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::unloadDSRFile() {
|
|
|
|
if (!_dsrFileLoaded)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (int i = 0; i < _dsrFile.entryCount; i++) {
|
|
|
|
_dsrFile.dsrEntries.remove_at(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
_dsrFile.entryCount = 0;
|
|
|
|
strcpy(_dsrFile.fileName, "");
|
|
|
|
_dsrFileLoaded = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Sound::playDSRSound(int soundIndex, int volume, bool loop) {
|
|
|
|
if (!_dsrFileLoaded) {
|
|
|
|
warning("DSR file not loaded, not playing sound");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (soundIndex < 0 || soundIndex > _dsrFile.entryCount - 1) {
|
|
|
|
warning("Invalid sound index: %i, not playing sound", soundIndex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte flags;
|
|
|
|
SndHandle *handle = getHandle();
|
|
|
|
|
|
|
|
handle->type = kEffectHandle;
|
2010-01-19 00:56:29 +00:00
|
|
|
flags = Audio::Mixer::FLAG_UNSIGNED;
|
2008-04-20 14:47:37 +00:00
|
|
|
|
|
|
|
if (loop)
|
|
|
|
flags |= Audio::Mixer::FLAG_LOOP;
|
|
|
|
|
|
|
|
// Get sound data
|
|
|
|
FabDecompressor fab;
|
|
|
|
byte *compData = new byte[_dsrFile.dsrEntries[soundIndex]->compSize];
|
|
|
|
byte *buffer = new byte[_dsrFile.dsrEntries[soundIndex]->uncompSize];
|
|
|
|
Common::SeekableReadStream *fileStream = _vm->res()->get(_dsrFile.fileName);
|
|
|
|
fileStream->seek(_dsrFile.dsrEntries[soundIndex]->offset, SEEK_SET);
|
|
|
|
fileStream->read(compData, _dsrFile.dsrEntries[soundIndex]->compSize);
|
|
|
|
_vm->res()->toss(_dsrFile.fileName);
|
|
|
|
|
2009-01-01 15:06:43 +00:00
|
|
|
fab.decompress(compData, _dsrFile.dsrEntries[soundIndex]->compSize,
|
2008-04-20 14:47:37 +00:00
|
|
|
buffer, _dsrFile.dsrEntries[soundIndex]->uncompSize);
|
|
|
|
|
|
|
|
// Play sound
|
2010-01-19 11:20:58 +00:00
|
|
|
Audio::AudioStream *stream = Audio::makeRawMemoryStream(buffer,
|
2010-01-19 00:56:29 +00:00
|
|
|
_dsrFile.dsrEntries[soundIndex]->uncompSize, DisposeAfterUse::YES,
|
2010-01-19 11:20:58 +00:00
|
|
|
_dsrFile.dsrEntries[soundIndex]->frequency, flags, 0, 0);
|
|
|
|
_mixer->playInputStream(Audio::Mixer::kSFXSoundType, &handle->handle, stream, -1, volume);
|
2008-04-20 14:47:37 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
// Dump the sound file
|
|
|
|
FILE *destFile = fopen("sound.raw", "wb");
|
|
|
|
fwrite(_dsrFile.dsrEntries[soundIndex]->data, _dsrFile.dsrEntries[soundIndex]->uncompSize, 1, destFile);
|
|
|
|
fclose(destFile);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace M4
|