2005-04-05 18:08:02 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
|
|
|
* Copyright (C) 2004 Ivan Dubrov
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2004-2006 The ScummVM project
|
2005-04-05 18:08:02 +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
|
2005-04-09 19:19:54 +00:00
|
|
|
* 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-04-05 18:08:02 +00:00
|
|
|
*
|
2006-02-11 10:11:37 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2005-04-05 18:08:02 +00:00
|
|
|
*
|
|
|
|
*/
|
2005-05-06 15:59:33 +00:00
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
#include "common/stdafx.h"
|
|
|
|
#include "common/endian.h"
|
|
|
|
|
2005-04-05 15:07:40 +00:00
|
|
|
#include "gob/gob.h"
|
|
|
|
#include "gob/sound.h"
|
2007-03-20 14:51:57 +00:00
|
|
|
#include "gob/global.h"
|
2006-05-11 19:43:30 +00:00
|
|
|
#include "gob/util.h"
|
2007-03-20 14:51:57 +00:00
|
|
|
#include "gob/dataio.h"
|
|
|
|
#include "gob/game.h"
|
2005-05-06 15:59:33 +00:00
|
|
|
|
2005-04-05 15:07:40 +00:00
|
|
|
namespace Gob {
|
2005-04-29 10:07:00 +00:00
|
|
|
|
2007-03-29 17:55:39 +00:00
|
|
|
void SoundDesc::set(SoundType type, SoundSource src,
|
|
|
|
byte *data, uint32 dSize) {
|
|
|
|
|
|
|
|
free();
|
|
|
|
|
|
|
|
_type = type;
|
|
|
|
_source = src;
|
|
|
|
_data = _dataPtr = data;
|
|
|
|
_size = dSize;
|
|
|
|
}
|
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
void SoundDesc::load(SoundType type, SoundSource src,
|
|
|
|
byte *data, uint32 dSize) {
|
|
|
|
|
|
|
|
free();
|
|
|
|
|
|
|
|
_source = src;
|
|
|
|
switch (type) {
|
|
|
|
case SOUND_ADL:
|
|
|
|
loadADL(data, dSize);
|
|
|
|
break;
|
|
|
|
case SOUND_SND:
|
|
|
|
loadSND(data, dSize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundDesc::free() {
|
|
|
|
if (_source != SOUND_TOT)
|
|
|
|
delete[] _data;
|
|
|
|
_data = _dataPtr = 0;
|
|
|
|
_id = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundDesc::flip() {
|
|
|
|
if ((_type == SOUND_SND) && _data && _dataPtr) {
|
|
|
|
_frequency = -_frequency;
|
|
|
|
for (uint32 i = 0; i < _size; i++)
|
|
|
|
_dataPtr[i] ^= 0x80;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundDesc::loadSND(byte *data, uint32 dSize) {
|
|
|
|
assert(dSize > 6);
|
|
|
|
|
|
|
|
_type = SOUND_SND;
|
|
|
|
_data = data;
|
|
|
|
_dataPtr = data + 6;
|
|
|
|
_frequency = MAX((int16) READ_BE_UINT16(data + 4), (int16) 4700);
|
|
|
|
_flag = data[0] ? (data[0] & 0x7F) : 8;
|
|
|
|
data[0] = 0;
|
|
|
|
_size = MIN(READ_BE_UINT32(data), dSize - 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoundDesc::loadADL(byte *data, uint32 dSize) {
|
|
|
|
_type = SOUND_ADL;
|
|
|
|
_data = _dataPtr = data;
|
|
|
|
_size = dSize;
|
|
|
|
}
|
|
|
|
|
2007-02-14 16:36:17 +00:00
|
|
|
Snd::SquareWaveStream::SquareWaveStream() {
|
|
|
|
_rate = 44100;
|
|
|
|
_beepForever = false;
|
|
|
|
_periodLength = 0;
|
|
|
|
_periodSamples = 0;
|
|
|
|
_remainingSamples = 0;
|
|
|
|
_sampleValue = 0;
|
2007-02-15 22:07:44 +00:00
|
|
|
_mixedSamples = 0;
|
2007-02-14 16:36:17 +00:00
|
|
|
}
|
|
|
|
|
2006-01-03 23:14:39 +00:00
|
|
|
void Snd::SquareWaveStream::playNote(int freq, int32 ms, uint rate) {
|
|
|
|
_rate = rate;
|
2005-05-06 15:59:33 +00:00
|
|
|
_periodLength = _rate / (2 * freq);
|
|
|
|
_periodSamples = 0;
|
|
|
|
_sampleValue = 6000;
|
|
|
|
if (ms == -1) {
|
|
|
|
_remainingSamples = 1;
|
|
|
|
_beepForever = true;
|
|
|
|
} else {
|
|
|
|
_remainingSamples = (_rate * ms) / 1000;
|
|
|
|
_beepForever = false;
|
|
|
|
}
|
2007-02-15 22:07:44 +00:00
|
|
|
_mixedSamples = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Snd::SquareWaveStream::stop(uint32 milis) {
|
|
|
|
if (!_beepForever)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (milis)
|
|
|
|
update(milis);
|
|
|
|
else
|
|
|
|
_remainingSamples = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Snd::SquareWaveStream::update(uint32 milis) {
|
|
|
|
uint32 neededSamples;
|
|
|
|
|
|
|
|
if (!_beepForever || !_remainingSamples)
|
|
|
|
return;
|
|
|
|
|
|
|
|
neededSamples = (_rate * milis) / 1000;
|
|
|
|
_remainingSamples =
|
|
|
|
neededSamples > _mixedSamples ? neededSamples - _mixedSamples : 0;
|
|
|
|
_beepForever = false;
|
2005-05-06 15:59:33 +00:00
|
|
|
}
|
|
|
|
|
2006-01-03 23:14:39 +00:00
|
|
|
int Snd::SquareWaveStream::readBuffer(int16 *buffer, const int numSamples) {
|
2007-02-26 20:41:52 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; _remainingSamples && i < numSamples; i++) {
|
2007-02-14 16:36:17 +00:00
|
|
|
buffer[i] = _sampleValue;
|
2005-05-06 15:59:33 +00:00
|
|
|
if (_periodSamples++ > _periodLength) {
|
|
|
|
_periodSamples = 0;
|
|
|
|
_sampleValue = -_sampleValue;
|
|
|
|
}
|
|
|
|
if (!_beepForever)
|
|
|
|
_remainingSamples--;
|
2007-02-15 22:07:44 +00:00
|
|
|
_mixedSamples++;
|
2005-05-06 15:59:33 +00:00
|
|
|
}
|
2007-02-26 20:41:52 +00:00
|
|
|
|
|
|
|
// Clear the rest of the buffer
|
|
|
|
if (i < numSamples)
|
|
|
|
memset(buffer + i, 0, (numSamples - i) * sizeof(int16));
|
2005-05-06 15:59:33 +00:00
|
|
|
|
2007-02-14 16:36:17 +00:00
|
|
|
return numSamples;
|
2005-05-06 15:59:33 +00:00
|
|
|
}
|
|
|
|
|
2006-01-03 23:14:39 +00:00
|
|
|
Snd::Snd(GobEngine *vm) : _vm(vm) {
|
2006-01-04 01:48:15 +00:00
|
|
|
_playingSound = 0;
|
2007-02-17 09:56:09 +00:00
|
|
|
_curSoundDesc = 0;
|
2005-04-30 11:32:18 +00:00
|
|
|
|
2007-01-30 22:19:55 +00:00
|
|
|
_rate = _vm->_mixer->getOutputRate();
|
|
|
|
_end = true;
|
|
|
|
_data = 0;
|
|
|
|
_length = 0;
|
|
|
|
_freq = 0;
|
|
|
|
_repCount = 0;
|
|
|
|
_offset = 0.0;
|
|
|
|
|
2007-01-31 13:17:50 +00:00
|
|
|
_frac = 0.0;
|
|
|
|
_cur = 0;
|
|
|
|
_last = 0;
|
|
|
|
|
|
|
|
_fade = false;
|
|
|
|
_fadeVol = 255.0;
|
|
|
|
_fadeVolStep = 0.0;
|
|
|
|
_fadeSamples = 0;
|
|
|
|
_curFadeSamples = 0;
|
|
|
|
|
2007-02-04 15:45:15 +00:00
|
|
|
_compositionSamples = 0;
|
|
|
|
_compositionSampleCount = 0;
|
2007-01-30 22:19:55 +00:00
|
|
|
_compositionPos = -1;
|
|
|
|
|
|
|
|
_vm->_mixer->playInputStream(Audio::Mixer::kSFXSoundType, &_handle,
|
|
|
|
this, -1, 255, 0, false, true);
|
2007-02-14 16:36:17 +00:00
|
|
|
_vm->_mixer->playInputStream(Audio::Mixer::kSFXSoundType, &_speakerHandle,
|
|
|
|
&_speakerStream, -1, 255, 0, false, true);
|
2005-04-30 11:32:18 +00:00
|
|
|
}
|
2005-04-29 10:07:00 +00:00
|
|
|
|
2007-02-26 20:41:52 +00:00
|
|
|
Snd::~Snd() {
|
|
|
|
// stop permanent streams manually:
|
|
|
|
|
|
|
|
// First the speaker stream
|
2007-02-26 18:52:28 +00:00
|
|
|
_vm->_mixer->stopHandle(_speakerHandle);
|
|
|
|
|
2007-02-26 20:41:52 +00:00
|
|
|
// Next, this stream (class Snd is an AudioStream, too)
|
|
|
|
_vm->_mixer->stopHandle(_handle);
|
2007-02-26 18:52:28 +00:00
|
|
|
}
|
|
|
|
|
2006-01-03 23:14:39 +00:00
|
|
|
void Snd::speakerOn(int16 frequency, int32 length) {
|
2006-01-04 01:48:15 +00:00
|
|
|
_speakerStream.playNote(frequency, length, _vm->_mixer->getOutputRate());
|
2007-02-15 22:07:44 +00:00
|
|
|
_speakerStartTimeKey = _vm->_util->getTimeKey();
|
2005-05-06 15:59:33 +00:00
|
|
|
}
|
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
void Snd::speakerOff() {
|
2007-02-15 22:07:44 +00:00
|
|
|
_speakerStream.stop(_vm->_util->getTimeKey() - _speakerStartTimeKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Snd::speakerOnUpdate(uint32 milis) {
|
|
|
|
_speakerStream.update(milis);
|
2005-05-06 15:59:33 +00:00
|
|
|
}
|
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
void Snd::stopSound(int16 fadeLength, SoundDesc *sndDesc) {
|
2007-01-30 22:19:55 +00:00
|
|
|
Common::StackLock slock(_mutex);
|
2005-04-30 11:32:18 +00:00
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
if (sndDesc && (sndDesc != _curSoundDesc))
|
|
|
|
return;
|
|
|
|
|
2007-01-31 13:17:50 +00:00
|
|
|
if (fadeLength <= 0) {
|
|
|
|
_data = 0;
|
|
|
|
_end = true;
|
|
|
|
_playingSound = 0;
|
2007-03-20 14:51:57 +00:00
|
|
|
_curSoundDesc = 0;
|
2007-01-31 13:17:50 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_fade = true;
|
|
|
|
_fadeVol = 255.0;
|
|
|
|
_fadeSamples = (int) (fadeLength * (((double) _rate) / 10.0));
|
|
|
|
_fadeVolStep = 255.0 / ((double) _fadeSamples);
|
|
|
|
_curFadeSamples = 0;
|
2007-01-30 22:19:55 +00:00
|
|
|
}
|
|
|
|
|
2007-03-29 17:55:39 +00:00
|
|
|
void Snd::setRepeating(int32 repCount) {
|
|
|
|
Common::StackLock slock(_mutex);
|
|
|
|
|
|
|
|
_repCount = repCount;
|
|
|
|
}
|
|
|
|
|
2007-02-04 15:45:15 +00:00
|
|
|
void Snd::waitEndPlay(bool interruptible, bool stopComp) {
|
|
|
|
if (stopComp)
|
|
|
|
_compositionPos = -1;
|
|
|
|
while (!_end && !_vm->_quitRequested) {
|
|
|
|
if (interruptible && (_vm->_util->checkKey() == 0x11B)) {
|
|
|
|
WRITE_VAR(57, -1);
|
|
|
|
return;
|
|
|
|
}
|
2007-01-30 22:19:55 +00:00
|
|
|
_vm->_util->longDelay(200);
|
2007-02-04 15:45:15 +00:00
|
|
|
}
|
2007-01-30 22:19:55 +00:00
|
|
|
stopSound(0);
|
|
|
|
}
|
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
void Snd::stopComposition() {
|
2007-01-30 22:19:55 +00:00
|
|
|
if (_compositionPos != -1) {
|
|
|
|
stopSound(0);
|
|
|
|
_compositionPos = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
void Snd::nextCompositionPos() {
|
2007-01-30 22:19:55 +00:00
|
|
|
int8 slot;
|
2005-04-30 11:32:18 +00:00
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
while ((++_compositionPos < 50) &&
|
|
|
|
((slot = _composition[_compositionPos]) != -1)) {
|
|
|
|
if ((slot >= 0) && (slot < _compositionSampleCount)) {
|
|
|
|
SoundDesc &sample = _compositionSamples[slot];
|
|
|
|
if (!sample.empty() && (sample.getType() == SOUND_SND)) {
|
|
|
|
setSample(sample, 1, 0, 0);
|
|
|
|
return;
|
|
|
|
}
|
2005-04-30 11:32:18 +00:00
|
|
|
}
|
2007-02-12 19:30:52 +00:00
|
|
|
if (_compositionPos == 49)
|
|
|
|
_compositionPos = -1;
|
2005-04-30 11:32:18 +00:00
|
|
|
}
|
2007-01-30 22:19:55 +00:00
|
|
|
_compositionPos = -1;
|
|
|
|
}
|
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
void Snd::playComposition(int16 *composition, int16 freqVal,
|
|
|
|
SoundDesc *sndDescs, int8 sndCount) {
|
2007-02-04 15:45:15 +00:00
|
|
|
int i;
|
|
|
|
|
2007-01-30 22:19:55 +00:00
|
|
|
waitEndPlay();
|
|
|
|
stopComposition();
|
|
|
|
|
2007-02-04 15:45:15 +00:00
|
|
|
_compositionSamples = sndDescs ? sndDescs : _vm->_game->_soundSamples;
|
|
|
|
_compositionSampleCount = sndCount;
|
|
|
|
|
|
|
|
i = -1;
|
|
|
|
do {
|
|
|
|
i++;
|
2007-01-30 22:19:55 +00:00
|
|
|
_composition[i] = composition[i];
|
2007-02-04 15:45:15 +00:00
|
|
|
} while ((i < 50) && (composition[i] != -1));
|
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
_compositionPos = -1;
|
2007-01-30 22:19:55 +00:00
|
|
|
nextCompositionPos();
|
2005-04-29 10:07:00 +00:00
|
|
|
}
|
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
void Snd::setSample(SoundDesc &sndDesc, int16 repCount, int16 frequency,
|
|
|
|
int16 fadeLength) {
|
2005-04-30 11:32:18 +00:00
|
|
|
|
2007-01-30 22:19:55 +00:00
|
|
|
if (frequency <= 0)
|
2007-03-20 14:51:57 +00:00
|
|
|
frequency = sndDesc._frequency;
|
2005-04-05 15:07:40 +00:00
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
_curSoundDesc = &sndDesc;
|
|
|
|
sndDesc._repCount = repCount - 1;
|
|
|
|
sndDesc._frequency = frequency;
|
2007-01-30 22:19:55 +00:00
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
_data = (int8 *) sndDesc.getData();
|
|
|
|
_length = sndDesc.size();
|
2007-01-30 22:19:55 +00:00
|
|
|
_freq = frequency;
|
2007-03-30 17:52:31 +00:00
|
|
|
|
|
|
|
if ((frequency % 100) == 0)
|
|
|
|
_freq--;
|
|
|
|
|
2007-01-30 22:19:55 +00:00
|
|
|
_ratio = ((double) _freq) / _rate;
|
|
|
|
_offset = 0.0;
|
|
|
|
_frac = 0;
|
2007-01-31 14:53:46 +00:00
|
|
|
_last = _cur;
|
|
|
|
_cur = _data[0];
|
2007-01-30 22:19:55 +00:00
|
|
|
_repCount = repCount;
|
|
|
|
_end = false;
|
2007-01-31 13:17:50 +00:00
|
|
|
_playingSound = 1;
|
|
|
|
|
|
|
|
_curFadeSamples = 0;
|
|
|
|
if (fadeLength == 0) {
|
|
|
|
_fade = false;
|
|
|
|
_fadeVol = 255.0;
|
|
|
|
_fadeSamples = 0;
|
|
|
|
_fadeVolStep = 0.0;
|
|
|
|
} else {
|
|
|
|
_fade = true;
|
|
|
|
_fadeVol = 0.0;
|
|
|
|
_fadeSamples = (int) (fadeLength * (((double) _rate) / 10.0));
|
|
|
|
_fadeVolStep = -(255.0 / ((double) _fadeSamples));
|
|
|
|
}
|
2007-01-30 22:19:55 +00:00
|
|
|
}
|
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
bool Snd::loadSample(SoundDesc &sndDesc, const char *fileName) {
|
|
|
|
byte *data;
|
|
|
|
uint32 size;
|
|
|
|
|
|
|
|
data = (byte *) _vm->_dataIO->getData(fileName);
|
|
|
|
if (!data)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
size = _vm->_dataIO->getDataSize(fileName);
|
|
|
|
sndDesc.load(SOUND_SND, SOUND_FILE, data, size);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Snd::freeSample(SoundDesc &sndDesc) {
|
|
|
|
stopSound(0, &sndDesc);
|
|
|
|
sndDesc.free();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Snd::playSample(SoundDesc &sndDesc, int16 repCount, int16 frequency,
|
|
|
|
int16 fadeLength) {
|
2007-01-30 22:19:55 +00:00
|
|
|
Common::StackLock slock(_mutex);
|
2005-04-05 15:07:40 +00:00
|
|
|
|
2007-01-30 22:19:55 +00:00
|
|
|
if (!_end)
|
|
|
|
return;
|
|
|
|
|
2007-01-31 13:17:50 +00:00
|
|
|
setSample(sndDesc, repCount, frequency, fadeLength);
|
2007-01-30 22:19:55 +00:00
|
|
|
}
|
|
|
|
|
2007-03-20 14:51:57 +00:00
|
|
|
void Snd::checkEndSample() {
|
2007-01-30 22:19:55 +00:00
|
|
|
if (_compositionPos != -1)
|
|
|
|
nextCompositionPos();
|
|
|
|
else if ((_repCount == -1) || (--_repCount > 0)) {
|
|
|
|
_offset = 0.0;
|
2007-01-31 14:53:46 +00:00
|
|
|
_frac = 0.0;
|
2007-01-30 22:19:55 +00:00
|
|
|
_end = false;
|
2007-01-31 13:17:50 +00:00
|
|
|
_playingSound = 1;
|
2007-01-30 22:19:55 +00:00
|
|
|
} else {
|
|
|
|
_end = true;
|
|
|
|
_playingSound = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int Snd::readBuffer(int16 *buffer, const int numSamples) {
|
|
|
|
for (int i = 0; i < numSamples; i++) {
|
|
|
|
Common::StackLock slock(_mutex);
|
|
|
|
|
|
|
|
if (!_data)
|
|
|
|
return i;
|
|
|
|
if (_end || (_offset >= _length))
|
|
|
|
checkEndSample();
|
|
|
|
if (_end)
|
|
|
|
return i;
|
|
|
|
|
2007-01-31 13:17:50 +00:00
|
|
|
*buffer++ = (int16) ((_last + (_cur - _last) * _frac) * _fadeVol);
|
2007-01-30 22:19:55 +00:00
|
|
|
_frac += _ratio;
|
2007-01-31 14:53:46 +00:00
|
|
|
_offset += _ratio;
|
2007-02-04 15:45:15 +00:00
|
|
|
while ((_frac > 1) && (_offset < _length)) {
|
2007-01-30 22:19:55 +00:00
|
|
|
_frac -= 1;
|
|
|
|
_last = _cur;
|
2007-01-31 13:17:50 +00:00
|
|
|
_cur = _data[(int) _offset];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_fade) {
|
2007-01-31 16:23:34 +00:00
|
|
|
if (++_curFadeSamples >= _fadeSamples) {
|
2007-01-31 13:17:50 +00:00
|
|
|
if (_fadeVolStep > 0) {
|
|
|
|
_data = 0;
|
|
|
|
_end = true;
|
|
|
|
_playingSound = 0;
|
2007-01-31 16:23:34 +00:00
|
|
|
_compositionPos = -1;
|
2007-03-20 14:51:57 +00:00
|
|
|
_curSoundDesc = 0;
|
2007-01-31 13:17:50 +00:00
|
|
|
} else {
|
|
|
|
_fadeVol = 255.0;
|
|
|
|
_fade = false;
|
|
|
|
}
|
2007-01-31 16:23:34 +00:00
|
|
|
} else
|
|
|
|
_fadeVol -= _fadeVolStep;
|
2007-01-30 22:19:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return numSamples;
|
|
|
|
}
|
2005-04-05 15:07:40 +00:00
|
|
|
|
2007-01-30 22:19:55 +00:00
|
|
|
} // End of namespace Gob
|