2003-07-28 01:47:41 +00:00
|
|
|
/* Copyright (C) 1994-2003 Revolution Software Ltd
|
|
|
|
*
|
|
|
|
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*/
|
|
|
|
|
2003-09-26 14:19:03 +00:00
|
|
|
// FIXME: One feature still missing is the original's DipMusic() function
|
|
|
|
// which, as far as I can understand, softened the music volume when someone
|
|
|
|
// was speaking, but only if the music was playing loudly at the time.
|
|
|
|
//
|
|
|
|
// I'm not sure if we can implement this in any sensible fashion - I don't
|
|
|
|
// think we have that fine-grained control over the mixer - or if we really
|
|
|
|
// want it anyway.
|
|
|
|
//
|
|
|
|
// Simply adjusting the volume paramters to flow() is not enough. If you
|
|
|
|
// only adjust them a little you won't hear the difference anyway, and if you
|
|
|
|
// adjust them a lot it will sound really bad.
|
|
|
|
//
|
|
|
|
// Does anyone who can run the original interpreter have any
|
|
|
|
// opinions on this?
|
|
|
|
|
2003-07-28 03:12:49 +00:00
|
|
|
#include "stdafx.h"
|
2003-09-25 06:11:07 +00:00
|
|
|
#include "sound/audiostream.h"
|
2003-08-30 18:06:08 +00:00
|
|
|
#include "sound/mixer.h"
|
2003-09-25 06:11:07 +00:00
|
|
|
#include "sound/rate.h"
|
2003-10-29 07:53:05 +00:00
|
|
|
#include "sword2/sword2.h"
|
2003-10-28 19:51:30 +00:00
|
|
|
#include "sword2/driver/driver96.h"
|
|
|
|
#include "sword2/driver/d_sound.h"
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-04 00:52:27 +00:00
|
|
|
namespace Sword2 {
|
|
|
|
|
2003-09-25 06:11:07 +00:00
|
|
|
// Fade-out takes half a second. This may need some tuning.
|
|
|
|
#define FADE_SAMPLES 11025
|
|
|
|
|
|
|
|
static File fpMus;
|
2003-09-21 14:26:25 +00:00
|
|
|
|
2003-07-28 01:47:41 +00:00
|
|
|
// Decompression macros
|
2003-09-25 10:02:52 +00:00
|
|
|
#define GetCompressedShift(n) ((n) >> 4)
|
|
|
|
#define GetCompressedSign(n) (((n) >> 3) & 1)
|
|
|
|
#define GetCompressedAmplitude(n) ((n) & 7)
|
2003-07-29 12:34:46 +00:00
|
|
|
|
2003-10-02 07:01:12 +00:00
|
|
|
static int32 panTable[33] = {
|
2003-09-26 14:19:03 +00:00
|
|
|
-127, -119, -111, -103, -95, -87, -79, -71,
|
|
|
|
-63, -55, -47, -39, -31, -23, -15, -7,
|
|
|
|
0,
|
|
|
|
7, 15, 23, 31, 39, 47, 55, 63,
|
|
|
|
71, 79, 87, 95, 103, 111, 119, 127
|
2003-08-31 20:26:21 +00:00
|
|
|
};
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-02 07:01:12 +00:00
|
|
|
static int32 musicVolTable[17] = {
|
2003-09-26 14:19:03 +00:00
|
|
|
0, 15, 31, 47, 63, 79, 95, 111, 127,
|
|
|
|
143, 159, 175, 191, 207, 223, 239, 255
|
2003-08-31 20:26:21 +00:00
|
|
|
};
|
2003-09-21 14:26:25 +00:00
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int16 MusicHandle::read() {
|
2003-09-25 06:11:07 +00:00
|
|
|
uint8 in;
|
Ok, I'm stupid.
The initial sample is, indeed, two bytes, just like the rest of them, but
it really, really helps if you read it from the correct position in the
file.
After fixing that, it turned out that my changing of signedness of the
sample was also wrong. Funny how those two bugs almost cancelled each other
out. Almost.
I've made a few other changes as well, but they're just to clean things up
a bit. The credits music works for me, and I've played the game up to
arriving in Quaramonte, with no obvious music-related problems.
svn-id: r10412
2003-09-26 06:26:18 +00:00
|
|
|
uint16 delta;
|
2003-09-25 06:11:07 +00:00
|
|
|
int16 out;
|
|
|
|
|
|
|
|
if (!_streaming)
|
|
|
|
return 0;
|
|
|
|
|
Ok, I'm stupid.
The initial sample is, indeed, two bytes, just like the rest of them, but
it really, really helps if you read it from the correct position in the
file.
After fixing that, it turned out that my changing of signedness of the
sample was also wrong. Funny how those two bugs almost cancelled each other
out. Almost.
I've made a few other changes as well, but they're just to clean things up
a bit. The credits music works for me, and I've played the game up to
arriving in Quaramonte, with no obvious music-related problems.
svn-id: r10412
2003-09-26 06:26:18 +00:00
|
|
|
if (_firstTime) {
|
|
|
|
_lastSample = fpMus.readUint16LE();
|
|
|
|
_filePos += 2;
|
|
|
|
_firstTime = false;
|
|
|
|
return _lastSample;
|
|
|
|
}
|
|
|
|
|
2003-09-25 06:11:07 +00:00
|
|
|
// Assume the file handle has been correctly positioned already.
|
|
|
|
|
|
|
|
in = fpMus.readByte();
|
|
|
|
delta = GetCompressedAmplitude(in) << GetCompressedShift(in);
|
|
|
|
|
|
|
|
if (GetCompressedSign(in))
|
Ok, I'm stupid.
The initial sample is, indeed, two bytes, just like the rest of them, but
it really, really helps if you read it from the correct position in the
file.
After fixing that, it turned out that my changing of signedness of the
sample was also wrong. Funny how those two bugs almost cancelled each other
out. Almost.
I've made a few other changes as well, but they're just to clean things up
a bit. The credits music works for me, and I've played the game up to
arriving in Quaramonte, with no obvious music-related problems.
svn-id: r10412
2003-09-26 06:26:18 +00:00
|
|
|
out = _lastSample - delta;
|
2003-09-25 06:11:07 +00:00
|
|
|
else
|
Ok, I'm stupid.
The initial sample is, indeed, two bytes, just like the rest of them, but
it really, really helps if you read it from the correct position in the
file.
After fixing that, it turned out that my changing of signedness of the
sample was also wrong. Funny how those two bugs almost cancelled each other
out. Almost.
I've made a few other changes as well, but they're just to clean things up
a bit. The credits music works for me, and I've played the game up to
arriving in Quaramonte, with no obvious music-related problems.
svn-id: r10412
2003-09-26 06:26:18 +00:00
|
|
|
out = _lastSample + delta;
|
2003-09-25 06:11:07 +00:00
|
|
|
|
|
|
|
_filePos++;
|
Ok, I'm stupid.
The initial sample is, indeed, two bytes, just like the rest of them, but
it really, really helps if you read it from the correct position in the
file.
After fixing that, it turned out that my changing of signedness of the
sample was also wrong. Funny how those two bugs almost cancelled each other
out. Almost.
I've made a few other changes as well, but they're just to clean things up
a bit. The credits music works for me, and I've played the game up to
arriving in Quaramonte, with no obvious music-related problems.
svn-id: r10412
2003-09-26 06:26:18 +00:00
|
|
|
_lastSample = out;
|
2003-09-25 06:11:07 +00:00
|
|
|
|
Ok, I'm stupid.
The initial sample is, indeed, two bytes, just like the rest of them, but
it really, really helps if you read it from the correct position in the
file.
After fixing that, it turned out that my changing of signedness of the
sample was also wrong. Funny how those two bugs almost cancelled each other
out. Almost.
I've made a few other changes as well, but they're just to clean things up
a bit. The credits music works for me, and I've played the game up to
arriving in Quaramonte, with no obvious music-related problems.
svn-id: r10412
2003-09-26 06:26:18 +00:00
|
|
|
if (_looping) {
|
|
|
|
if (_filePos >= _fileEnd) {
|
|
|
|
_firstTime = true;
|
|
|
|
_filePos = _fileStart;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Fade out at the end of the music. Is this really desirable
|
|
|
|
// behaviour?
|
|
|
|
|
|
|
|
if (_fileEnd - _filePos <= FADE_SAMPLES)
|
|
|
|
_fading = _fileEnd - _filePos;
|
|
|
|
}
|
2003-09-25 06:11:07 +00:00
|
|
|
|
|
|
|
if (_fading > 0) {
|
|
|
|
if (--_fading == 0) {
|
|
|
|
_streaming = false;
|
|
|
|
_looping = false;
|
|
|
|
}
|
|
|
|
out = (out * _fading) / FADE_SAMPLES;
|
|
|
|
}
|
2003-09-16 07:11:29 +00:00
|
|
|
|
2003-09-25 06:11:07 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
bool MusicHandle::eos() const {
|
2003-09-25 06:11:07 +00:00
|
|
|
if (!_streaming || _filePos >= _fileEnd)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void premix_proc(void *param, int16 *data, uint len) {
|
2003-10-04 01:09:29 +00:00
|
|
|
((Sound *) param)->fxServer(data, len);
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
Sound::Sound(SoundMixer *mixer) {
|
2003-09-04 10:58:55 +00:00
|
|
|
_mutex = g_system->create_mutex();
|
|
|
|
|
2003-10-29 07:53:05 +00:00
|
|
|
_soundOn = false;
|
|
|
|
_speechStatus = false;
|
|
|
|
_fxPaused = false;
|
|
|
|
_speechPaused = false;
|
2003-10-01 06:36:25 +00:00
|
|
|
_speechVol = 14;
|
|
|
|
_fxVol = 14;
|
|
|
|
_speechMuted = 0;
|
|
|
|
_fxMuted = 0;
|
|
|
|
_musicVol = 16;
|
|
|
|
|
|
|
|
_musicMuted = 0;
|
2003-08-02 02:31:36 +00:00
|
|
|
_mixer = mixer;
|
2003-09-21 14:26:25 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
memset(_fx, 0, sizeof(_fx));
|
2003-09-21 14:26:25 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_soundHandleSpeech = 0;
|
2003-10-29 07:53:05 +00:00
|
|
|
_soundOn = true;
|
2003-09-25 06:11:07 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_converter = makeRateConverter(_music[0].getRate(), _mixer->getOutputRate(), _music[0].isStereo(), false);
|
2003-09-25 06:11:07 +00:00
|
|
|
|
|
|
|
_mixer->setupPremix(premix_proc, this);
|
2003-07-29 12:34:46 +00:00
|
|
|
}
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
Sound::~Sound() {
|
2003-09-04 10:58:55 +00:00
|
|
|
if (_mutex)
|
|
|
|
g_system->delete_mutex(_mutex);
|
|
|
|
}
|
|
|
|
|
2003-08-31 10:38:32 +00:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// This function reverse the pan table, thus reversing the stereo.
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2003-09-16 07:11:29 +00:00
|
|
|
// FIXME: We could probably use the FLAG_REVERSE_STEREO mixer flag here.
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* This function reverses the pan table, thus reversing the stereo.
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::reverseStereo(void) {
|
2003-10-08 17:09:51 +00:00
|
|
|
for (int i = 0; i < 16; i++)
|
|
|
|
SWAP(panTable[i], panTable[32 - i]);
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 16:10:43 +00:00
|
|
|
// Save/Restore information about current music so that we can restore it
|
|
|
|
// after the credits.
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::saveMusicState() {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-27 16:10:43 +00:00
|
|
|
|
|
|
|
int saveStream;
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_music[0]._streaming && !_music[0]._fading) {
|
2003-09-27 16:10:43 +00:00
|
|
|
saveStream = 0;
|
2003-10-01 06:36:25 +00:00
|
|
|
} else if (_music[1]._streaming && !_music[0]._fading) {
|
2003-09-27 16:10:43 +00:00
|
|
|
saveStream = 1;
|
|
|
|
} else {
|
2003-10-01 06:36:25 +00:00
|
|
|
_music[2]._streaming = false;
|
2003-09-27 16:10:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_music[2]._streaming = true;
|
|
|
|
_music[2]._fading = 0;
|
|
|
|
_music[2]._looping = _music[saveStream]._looping;
|
|
|
|
_music[2]._fileStart = _music[saveStream]._fileStart;
|
|
|
|
_music[2]._filePos = _music[saveStream]._filePos;
|
|
|
|
_music[2]._fileEnd = _music[saveStream]._fileEnd;
|
|
|
|
_music[2]._lastSample = _music[saveStream]._lastSample;
|
2003-09-27 16:10:43 +00:00
|
|
|
}
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::restoreMusicState() {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-27 16:10:43 +00:00
|
|
|
|
|
|
|
int restoreStream;
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_music[2]._streaming)
|
2003-09-27 16:10:43 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Fade out any music that happens to be playing
|
|
|
|
|
|
|
|
for (int i = 0; i < MAXMUS; i++) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_music[i]._streaming && !_music[i]._fading)
|
|
|
|
_music[i]._fading = FADE_SAMPLES;
|
2003-09-27 16:10:43 +00:00
|
|
|
}
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_music[0]._streaming && !_music[0]._fading) {
|
2003-09-27 16:10:43 +00:00
|
|
|
restoreStream = 0;
|
|
|
|
} else {
|
|
|
|
restoreStream = 1;
|
|
|
|
}
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_music[restoreStream]._streaming = true;
|
|
|
|
_music[restoreStream]._fading = 0;
|
|
|
|
_music[restoreStream]._looping = _music[2]._looping;
|
|
|
|
_music[restoreStream]._fileStart = _music[2]._fileStart;
|
|
|
|
_music[restoreStream]._filePos = _music[2]._filePos;
|
|
|
|
_music[restoreStream]._fileEnd = _music[2]._fileEnd;
|
|
|
|
_music[restoreStream]._lastSample = _music[2]._lastSample;
|
2003-09-27 16:10:43 +00:00
|
|
|
}
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::playLeadOut(uint8 *leadOut) {
|
2003-09-28 14:13:57 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!leadOut)
|
|
|
|
return;
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
playFx(0, leadOut, 0, 0, RDSE_FXLEADOUT);
|
2003-09-28 14:13:57 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
i = getFxIndex(-1);
|
2003-09-28 14:13:57 +00:00
|
|
|
|
|
|
|
if (i == MAXFX) {
|
|
|
|
warning("playLeadOut: Can't find lead-out sound handle");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
while (_fx[i]._handle) {
|
2003-10-15 06:40:31 +00:00
|
|
|
g_display->updateDisplay();
|
2003-09-28 14:13:57 +00:00
|
|
|
g_system->delay_msecs(30);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-16 07:11:29 +00:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// This function returns the index of the sound effect with the ID passed in.
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int32 Sound::getFxIndex(int32 id) {
|
2003-09-27 17:00:15 +00:00
|
|
|
for (int i = 0; i < MAXFX; i++) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_fx[i]._id == id)
|
2003-09-27 17:00:15 +00:00
|
|
|
return i;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
return MAXFX;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int32 Sound::isFxOpen(int32 id) {
|
2003-09-21 14:26:25 +00:00
|
|
|
// FIXME: This seems backwards to me, but changing it breaks sound.
|
2003-10-01 06:36:25 +00:00
|
|
|
return getFxIndex(id) == MAXFX;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::fxServer(int16 *data, uint len) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-03 18:59:02 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_soundOn)
|
2003-07-28 01:47:41 +00:00
|
|
|
return;
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
updateCompSampleStreaming(data, len);
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_music[0]._streaming && !_music[1]._streaming && fpMus.isOpen())
|
2003-09-05 15:56:43 +00:00
|
|
|
fpMus.close();
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* Returns either RDSE_QUIET or RDSE_SPEAKING
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int32 Sound::amISpeaking() {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_speechMuted && !_speechPaused && _soundHandleSpeech != 0)
|
2003-09-16 07:11:29 +00:00
|
|
|
return RDSE_SPEAKING;
|
|
|
|
|
|
|
|
return RDSE_QUIET;
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* This function loads and decompresses a list of speech from a cluster, but
|
|
|
|
* does not play it. This is primarily used by PlayCompSpeech(), but also to
|
|
|
|
* store the voice-overs for the animated cutscenes until they are played.
|
|
|
|
* @param filename the file name of the speech cluster file
|
|
|
|
* @param speechid the text line id used to reference the speech
|
|
|
|
* @param buf a pointer to the buffer that will be allocated for the sound
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
uint32 Sound::preFetchCompSpeech(const char *filename, uint32 speechid, uint16 **buf) {
|
2003-09-22 06:36:38 +00:00
|
|
|
uint32 i;
|
|
|
|
uint8 *data8;
|
2003-09-16 07:11:29 +00:00
|
|
|
uint32 speechIndex[2];
|
|
|
|
File fp;
|
2003-09-22 06:36:38 +00:00
|
|
|
uint32 bufferSize;
|
|
|
|
|
|
|
|
*buf = NULL;
|
|
|
|
|
2003-09-16 07:11:29 +00:00
|
|
|
// Open the speech cluster and find the data offset & size
|
2003-09-17 21:06:16 +00:00
|
|
|
fp.open(filename);
|
2003-09-22 06:36:38 +00:00
|
|
|
if (!fp.isOpen())
|
2003-09-16 07:11:29 +00:00
|
|
|
return 0;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-22 06:36:38 +00:00
|
|
|
fp.seek((speechid + 1) * 8, SEEK_SET);
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-08-30 18:06:08 +00:00
|
|
|
if (fp.read(speechIndex, sizeof(uint32) * 2) != (sizeof(uint32) * 2)) {
|
|
|
|
fp.close();
|
2003-09-16 07:11:29 +00:00
|
|
|
return 0;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
2003-09-16 07:11:29 +00:00
|
|
|
|
2003-09-13 14:32:40 +00:00
|
|
|
#ifdef SCUMM_BIG_ENDIAN
|
|
|
|
speechIndex[0] = SWAP_BYTES_32(speechIndex[0]);
|
|
|
|
speechIndex[1] = SWAP_BYTES_32(speechIndex[1]);
|
|
|
|
#endif
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-22 06:36:38 +00:00
|
|
|
if (!speechIndex[0] || !speechIndex[1]) {
|
|
|
|
fp.close();
|
2003-09-16 07:11:29 +00:00
|
|
|
return 0;
|
2003-09-22 06:36:38 +00:00
|
|
|
}
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-22 06:36:38 +00:00
|
|
|
// Create a temporary buffer for compressed speech
|
|
|
|
if ((data8 = (uint8 *) malloc(speechIndex[1])) == NULL) {
|
2003-08-30 18:06:08 +00:00
|
|
|
fp.close();
|
2003-09-22 06:36:38 +00:00
|
|
|
return 0;
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
2003-09-16 07:11:29 +00:00
|
|
|
|
2003-09-22 06:36:38 +00:00
|
|
|
fp.seek(speechIndex[0], SEEK_SET);
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-22 06:36:38 +00:00
|
|
|
if (fp.read(data8, speechIndex[1]) != speechIndex[1]) {
|
2003-08-30 18:06:08 +00:00
|
|
|
fp.close();
|
2003-09-22 06:36:38 +00:00
|
|
|
free(data8);
|
|
|
|
return 0;
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-22 06:36:38 +00:00
|
|
|
fp.close();
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-22 06:36:38 +00:00
|
|
|
// Decompress data into speech buffer.
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-22 06:36:38 +00:00
|
|
|
bufferSize = (speechIndex[1] - 1) * 2;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-22 06:36:38 +00:00
|
|
|
*buf = (uint16 *) malloc(bufferSize);
|
|
|
|
if (!*buf) {
|
|
|
|
free(data8);
|
|
|
|
return 0;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-22 06:36:38 +00:00
|
|
|
uint16 *data16 = *buf;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-16 07:11:29 +00:00
|
|
|
// Starting Value
|
|
|
|
data16[0] = READ_LE_UINT16(data8);
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-22 06:36:38 +00:00
|
|
|
for (i = 1; i < speechIndex[1] - 1; i++) {
|
2003-08-30 18:06:08 +00:00
|
|
|
if (GetCompressedSign(data8[i + 1]))
|
|
|
|
data16[i] = data16[i - 1] - (GetCompressedAmplitude(data8[i + 1]) << GetCompressedShift(data8[i + 1]));
|
2003-07-28 01:47:41 +00:00
|
|
|
else
|
2003-08-31 20:26:21 +00:00
|
|
|
data16[i] = data16[i - 1] + (GetCompressedAmplitude(data8[i + 1]) << GetCompressedShift(data8[i + 1]));
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-22 06:36:38 +00:00
|
|
|
free(data8);
|
|
|
|
|
|
|
|
#ifndef SCUMM_BIG_ENDIAN
|
|
|
|
// Until the mixer supports LE samples natively, we need to convert
|
|
|
|
// our LE ones to BE
|
|
|
|
for (uint j = 0; j < bufferSize / 2; j++)
|
|
|
|
data16[j] = SWAP_BYTES_16(data16[j]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return bufferSize;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* This function loads, decompresses and plays a line of speech. An error
|
|
|
|
* occurs if speech is already playing.
|
|
|
|
* @param filename the name of the speech cluster file
|
|
|
|
* @param speechid the text line id used to reference the speech
|
|
|
|
* @param vol volume, 0 (minimum) to 16 (maximum)
|
|
|
|
* @param pan panning, -16 (full left) to 16 (full right)
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int32 Sound::playCompSpeech(const char *filename, uint32 speechid, uint8 vol, int8 pan) {
|
2003-09-16 07:11:29 +00:00
|
|
|
uint16 *data16;
|
|
|
|
uint32 bufferSize;
|
2003-08-27 00:36:01 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_speechMuted) {
|
|
|
|
if (getSpeechStatus() == RDERR_SPEECHPLAYING)
|
2003-07-28 01:47:41 +00:00
|
|
|
return RDERR_SPEECHPLAYING;
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
bufferSize = preFetchCompSpeech(filename, speechid, &data16);
|
2003-09-16 07:11:29 +00:00
|
|
|
|
2003-09-22 06:36:38 +00:00
|
|
|
// We don't know exactly what went wrong here.
|
|
|
|
if (bufferSize == 0)
|
|
|
|
return RDERR_OUTOFMEMORY;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-12 23:22:45 +00:00
|
|
|
// Modify the volume according to the master volume
|
2003-09-21 14:26:25 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
byte volume = _speechMuted ? 0 : vol * _speechVol;
|
2003-09-21 14:26:25 +00:00
|
|
|
int8 p = panTable[pan + 16];
|
2003-08-30 18:06:08 +00:00
|
|
|
|
2003-09-12 23:22:45 +00:00
|
|
|
// Start the speech playing
|
2003-09-21 14:26:25 +00:00
|
|
|
|
2003-10-29 07:53:05 +00:00
|
|
|
_speechPaused = true;
|
2003-08-27 00:36:01 +00:00
|
|
|
|
2003-09-16 07:11:29 +00:00
|
|
|
uint32 flags = SoundMixer::FLAG_16BITS | SoundMixer::FLAG_AUTOFREE;
|
2003-08-27 00:36:01 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_mixer->playRaw(&_soundHandleSpeech, data16, bufferSize, 22050, flags, -1, volume, p);
|
2003-08-31 20:26:21 +00:00
|
|
|
|
2003-10-29 07:53:05 +00:00
|
|
|
_speechStatus = true;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-21 14:26:25 +00:00
|
|
|
// DipMusic();
|
|
|
|
|
2003-09-16 07:11:29 +00:00
|
|
|
return RD_OK;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* Stops the speech from playing.
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int32 Sound::stopSpeech(void) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_soundOn)
|
2003-09-16 07:11:29 +00:00
|
|
|
return RD_OK;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_speechStatus) {
|
|
|
|
g_engine->_mixer->stopHandle(_soundHandleSpeech);
|
2003-10-29 07:53:05 +00:00
|
|
|
_speechStatus = false;
|
2003-09-16 07:11:29 +00:00
|
|
|
return RD_OK;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
2003-09-16 07:11:29 +00:00
|
|
|
return RDERR_SPEECHNOTPLAYING;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* @return Either RDSE_SAMPLEPLAYING or RDSE_SAMPLEFINISHED
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int32 Sound::getSpeechStatus(void) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_soundOn || !_speechStatus)
|
2003-09-16 07:11:29 +00:00
|
|
|
return RDSE_SAMPLEFINISHED;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_speechPaused)
|
2003-09-16 07:11:29 +00:00
|
|
|
return RDSE_SAMPLEPLAYING;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_soundHandleSpeech) {
|
2003-10-29 07:53:05 +00:00
|
|
|
_speechStatus = false;
|
2003-09-16 07:11:29 +00:00
|
|
|
return RDSE_SAMPLEFINISHED;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
2003-09-16 07:11:29 +00:00
|
|
|
return RDSE_SAMPLEPLAYING;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* Set the volume of any future as well as playing speech samples.
|
|
|
|
* @param volume volume, from 0 (silent) to 14 (max)
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::setSpeechVolume(uint8 volume) {
|
2003-10-29 07:53:05 +00:00
|
|
|
if (volume > 14)
|
|
|
|
volume = 14;
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_speechVol = volume;
|
2003-10-29 07:53:05 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_soundHandleSpeech != 0 && !_speechMuted && getSpeechStatus() == RDSE_SAMPLEPLAYING) {
|
|
|
|
g_engine->_mixer->setChannelVolume(_soundHandleSpeech, 16 * _speechVol);
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* @return the volume setting for speech
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
uint8 Sound::getSpeechVolume() {
|
2003-10-01 06:36:25 +00:00
|
|
|
return _speechVol;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* Mutes/Unmutes the speech.
|
2003-10-29 07:53:05 +00:00
|
|
|
* @param mute If mute is false, restore the volume to the last set master
|
|
|
|
* level. Otherwise the speech is muted (volume 0).
|
2003-09-27 17:00:15 +00:00
|
|
|
*/
|
|
|
|
|
2003-10-29 07:53:05 +00:00
|
|
|
void Sound::muteSpeech(bool mute) {
|
2003-10-01 06:36:25 +00:00
|
|
|
_speechMuted = mute;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (getSpeechStatus() == RDSE_SAMPLEPLAYING) {
|
|
|
|
byte volume = mute ? 0 : 16 * _speechVol;
|
2003-09-21 14:26:25 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
g_engine->_mixer->setChannelVolume(_soundHandleSpeech, volume);
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
2003-10-29 07:53:05 +00:00
|
|
|
* @return the speech's mute state, true if mute, false if not mute
|
2003-09-27 17:00:15 +00:00
|
|
|
*/
|
|
|
|
|
2003-10-29 07:53:05 +00:00
|
|
|
bool Sound::isSpeechMute(void) {
|
2003-10-01 06:36:25 +00:00
|
|
|
return _speechMuted;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* Stops the speech dead in its tracks.
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::pauseSpeech(void) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (getSpeechStatus() == RDSE_SAMPLEPLAYING) {
|
2003-10-29 07:53:05 +00:00
|
|
|
_speechPaused = true;
|
2003-10-01 06:36:25 +00:00
|
|
|
g_engine->_mixer->pauseHandle(_soundHandleSpeech, true);
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* Restarts the speech from where it was stopped.
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::unpauseSpeech(void) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_speechPaused) {
|
2003-10-29 07:53:05 +00:00
|
|
|
_speechPaused = false;
|
2003-10-01 06:36:25 +00:00
|
|
|
g_engine->_mixer->pauseHandle(_soundHandleSpeech, false);
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* This function opens a sound effect ready for playing. A unique id should be
|
|
|
|
* passed in so that each effect can be referenced individually.
|
|
|
|
* @param id the unique sound id
|
2003-10-03 13:59:44 +00:00
|
|
|
* @param data the WAV data
|
2003-09-27 17:00:15 +00:00
|
|
|
* @warning Zero is not a valid id
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int32 Sound::openFx(int32 id, uint8 *data) {
|
2003-09-16 07:11:29 +00:00
|
|
|
int32 i, fxi;
|
|
|
|
uint32 *data32 = NULL;
|
|
|
|
_wavHeader *wav;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
|
|
|
wav = (_wavHeader *) data;
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_soundOn) {
|
2003-07-28 01:47:41 +00:00
|
|
|
// Check for a valid id.
|
|
|
|
if (id == 0)
|
2003-09-16 07:11:29 +00:00
|
|
|
return RDERR_INVALIDID;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
|
|
|
// Check that the fx is not already open
|
2003-08-30 18:06:08 +00:00
|
|
|
for (i = 0; i < MAXFX; i++) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_fx[i]._id == id)
|
2003-09-16 07:11:29 +00:00
|
|
|
return RDERR_FXALREADYOPEN;
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
2003-07-28 01:47:41 +00:00
|
|
|
|
|
|
|
// Now choose a free slot for the fx
|
2003-09-21 14:26:25 +00:00
|
|
|
for (fxi = 0; fxi < MAXFX; fxi++) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_fx[fxi]._id == 0)
|
2003-07-28 01:47:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-09-04 10:58:55 +00:00
|
|
|
if (fxi == MAXFX) {
|
|
|
|
// Expire the first sound effect that isn't currently
|
2003-10-02 07:01:12 +00:00
|
|
|
// playing. This usually shouldn't happen since the
|
|
|
|
// game engine manually clears all sound effects (at
|
|
|
|
// least except for lead-ins and lead-outs) when moving
|
|
|
|
// between rooms.
|
2003-09-04 10:58:55 +00:00
|
|
|
|
2003-09-21 14:26:25 +00:00
|
|
|
for (fxi = 0; fxi < MAXFX; fxi++) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_fx[fxi]._handle)
|
2003-09-04 10:58:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Still no dice? I give up!
|
|
|
|
|
2003-09-21 14:26:25 +00:00
|
|
|
if (fxi == MAXFX) {
|
2003-10-01 06:36:25 +00:00
|
|
|
warning("openFx: No free sound slots");
|
2003-09-16 07:11:29 +00:00
|
|
|
return RDERR_NOFREEBUFFERS;
|
2003-09-21 14:26:25 +00:00
|
|
|
}
|
2003-09-04 10:58:55 +00:00
|
|
|
}
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-16 07:11:29 +00:00
|
|
|
// Set the sample size - search for the size of the data.
|
2003-07-28 01:47:41 +00:00
|
|
|
i = 0;
|
2003-08-30 18:06:08 +00:00
|
|
|
while (i < 100) {
|
|
|
|
if (*data == 'd') {
|
2003-09-21 14:26:25 +00:00
|
|
|
data32 = (uint32 *) data;
|
2003-09-13 11:39:25 +00:00
|
|
|
if (READ_UINT32(data32) == MKID('data'))
|
2003-08-30 18:06:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2003-09-21 14:26:25 +00:00
|
|
|
i++;
|
2003-08-30 18:06:08 +00:00
|
|
|
data++;
|
|
|
|
}
|
2003-09-22 06:36:38 +00:00
|
|
|
|
2003-08-31 10:38:32 +00:00
|
|
|
if (!data32)
|
2003-09-16 07:11:29 +00:00
|
|
|
return RDERR_INVALIDWAV;
|
2003-08-30 18:06:08 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[fxi]._bufSize = READ_LE_UINT32(data32 + 1);
|
2003-08-30 18:06:08 +00:00
|
|
|
|
2003-09-16 07:11:29 +00:00
|
|
|
// Fill the speech buffer with data
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_fx[fxi]._buf != NULL)
|
|
|
|
free(_fx[fxi]._buf);
|
|
|
|
_fx[fxi]._buf = (uint16 *) malloc(_fx[fxi]._bufSize);
|
|
|
|
memcpy(_fx[fxi]._buf, (uint8 *) (data32 + 2), _fx[fxi]._bufSize);
|
|
|
|
_fx[fxi]._flags = SoundMixer::FLAG_16BITS;
|
2003-09-13 01:59:11 +00:00
|
|
|
if (FROM_LE_16(wav->channels) == 2)
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[fxi]._flags |= SoundMixer::FLAG_STEREO;
|
2003-08-30 18:06:08 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[fxi]._rate = FROM_LE_16(wav->samplesPerSec);
|
2003-08-31 10:45:14 +00:00
|
|
|
|
2003-09-16 07:11:29 +00:00
|
|
|
// Until the mixer supports LE samples natively, we need to
|
|
|
|
// convert our LE ones to BE
|
2003-10-01 06:36:25 +00:00
|
|
|
for (int32 j = 0; j < _fx[fxi]._bufSize / 2; j++)
|
|
|
|
_fx[fxi]._buf[j] = SWAP_BYTES_16(_fx[fxi]._buf[j]);
|
2003-08-30 18:06:08 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[fxi]._id = id;
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
2003-09-16 07:11:29 +00:00
|
|
|
return RD_OK;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* This function plays a sound effect. If the effect has already been opened
|
|
|
|
* then 'data' should be NULL, and the sound effect will simply be obtained
|
|
|
|
* from the id passed in. If the effect has not been opened, then the WAV data
|
|
|
|
* should be passed in 'data'. The sound effect will be closed when it has
|
|
|
|
* finished playing.
|
|
|
|
* @param id the sound id
|
|
|
|
* @param data either NULL or the WAV data
|
|
|
|
* @param vol volume, 0 (minimum) to 16 (maximum)
|
|
|
|
* @param pan panning, -16 (full left) to 16 (full right)
|
|
|
|
* @param type either RDSE_FXSPOT or RDSE_FXLOOP
|
|
|
|
* @warning Zero is not a valid id
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int32 Sound::playFx(int32 id, uint8 *data, uint8 vol, int8 pan, uint8 type) {
|
2003-08-30 18:06:08 +00:00
|
|
|
int32 i, loop;
|
2003-08-31 00:00:09 +00:00
|
|
|
uint32 hr;
|
2003-08-30 20:27:48 +00:00
|
|
|
|
2003-08-30 18:06:08 +00:00
|
|
|
if (type == RDSE_FXLOOP)
|
|
|
|
loop = 1;
|
|
|
|
else
|
|
|
|
loop = 0;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_soundOn) {
|
2003-08-30 18:06:08 +00:00
|
|
|
if (data == NULL) {
|
2003-10-01 06:36:25 +00:00
|
|
|
i = getFxIndex(id);
|
2003-09-28 14:13:57 +00:00
|
|
|
if (i == MAXFX) {
|
2003-10-01 06:36:25 +00:00
|
|
|
warning("playFx(%d, %d, %d, %d) - Not open", id, vol, pan, type);
|
2003-09-28 14:13:57 +00:00
|
|
|
return RDERR_FXNOTOPEN;
|
|
|
|
}
|
|
|
|
if (loop == 1)
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[i]._flags |= SoundMixer::FLAG_LOOP;
|
2003-09-28 14:13:57 +00:00
|
|
|
else
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[i]._flags &= ~SoundMixer::FLAG_LOOP;
|
2003-10-29 07:53:05 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[i]._volume = vol;
|
2003-08-30 18:06:08 +00:00
|
|
|
|
2003-09-28 14:13:57 +00:00
|
|
|
// Start the sound effect playing
|
2003-08-30 18:06:08 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
byte volume = _fxMuted ? 0 : vol * _fxVol;
|
2003-09-28 14:13:57 +00:00
|
|
|
int8 p = panTable[pan + 16];
|
2003-09-21 14:26:25 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
g_engine->_mixer->playRaw(&_fx[i]._handle, _fx[i]._buf, _fx[i]._bufSize, _fx[i]._rate, _fx[i]._flags, -1, volume, p);
|
2003-08-30 18:06:08 +00:00
|
|
|
} else {
|
2003-09-28 14:13:57 +00:00
|
|
|
if (type == RDSE_FXLEADIN || type == RDSE_FXLEADOUT) {
|
|
|
|
if (type == RDSE_FXLEADIN)
|
|
|
|
id = -2;
|
|
|
|
else
|
|
|
|
id = -1;
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
hr = openFx(id, data);
|
2003-09-28 14:13:57 +00:00
|
|
|
if (hr != RD_OK)
|
2003-08-30 18:06:08 +00:00
|
|
|
return hr;
|
2003-09-28 14:13:57 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
i = getFxIndex(id);
|
2003-08-30 18:06:08 +00:00
|
|
|
if (i == MAXFX) {
|
2003-10-01 06:36:25 +00:00
|
|
|
warning("playFx(%d, %d, %d, %d) - Not found", id, vol, pan, type);
|
2003-08-30 18:06:08 +00:00
|
|
|
return RDERR_FXFUCKED;
|
|
|
|
}
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[i]._flags &= ~SoundMixer::FLAG_LOOP;
|
2003-08-31 20:26:21 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
byte volume = _musicMuted ? 0 : musicVolTable[_musicVol];
|
2003-09-21 14:26:25 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
g_engine->_mixer->playRaw(&_fx[i]._handle, _fx[i]._buf, _fx[i]._bufSize, _fx[i]._rate, _fx[i]._flags, -1, volume, 0);
|
2003-08-30 18:06:08 +00:00
|
|
|
} else {
|
2003-10-01 06:36:25 +00:00
|
|
|
hr = openFx(id, data);
|
2003-08-30 18:06:08 +00:00
|
|
|
if (hr != RD_OK) {
|
2003-09-16 07:11:29 +00:00
|
|
|
return hr;
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
i = getFxIndex(id);
|
2003-08-30 18:06:08 +00:00
|
|
|
if (i == MAXFX) {
|
2003-10-01 06:36:25 +00:00
|
|
|
warning("playFx(%d, %d, %d, %d) - Not found", id, vol, pan, type);
|
2003-09-16 07:11:29 +00:00
|
|
|
return RDERR_FXFUCKED;
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
|
|
|
if (loop == 1)
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[i]._flags |= SoundMixer::FLAG_LOOP;
|
2003-08-30 18:06:08 +00:00
|
|
|
else
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[i]._flags &= ~SoundMixer::FLAG_LOOP;
|
|
|
|
_fx[i]._volume = vol;
|
2003-08-30 18:06:08 +00:00
|
|
|
|
2003-09-16 07:11:29 +00:00
|
|
|
// Start the sound effect playing
|
2003-09-21 14:26:25 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
byte volume = _fxMuted ? 0 : vol * _fxVol;
|
2003-09-21 14:26:25 +00:00
|
|
|
int8 p = panTable[pan + 16];
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
g_engine->_mixer->playRaw(&_fx[i]._handle, _fx[i]._buf, _fx[i]._bufSize, _fx[i]._rate, _fx[i]._flags, -1, volume, p);
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-10-01 06:36:25 +00:00
|
|
|
|
2003-09-16 07:11:29 +00:00
|
|
|
return RD_OK;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* Sets the volume and pan of the sample which is currently playing
|
|
|
|
* @param id the id of the sample
|
|
|
|
* @param vol volume
|
|
|
|
* @param pan panning
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int32 Sound::setFxIdVolumePan(int32 id, uint8 vol, int8 pan) {
|
2003-10-01 06:36:25 +00:00
|
|
|
int32 i = getFxIndex(id);
|
|
|
|
|
2003-07-28 01:47:41 +00:00
|
|
|
if (i == MAXFX)
|
|
|
|
return RDERR_FXNOTOPEN;
|
|
|
|
|
2003-10-29 07:53:05 +00:00
|
|
|
if (vol > 14)
|
|
|
|
vol = 14;
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[i]._volume = vol;
|
2003-10-29 07:53:05 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_fxMuted) {
|
2003-10-29 07:53:05 +00:00
|
|
|
g_engine->_mixer->setChannelVolume(_fx[i]._handle, _fx[i]._volume * _fxVol);
|
2003-10-01 06:36:25 +00:00
|
|
|
g_engine->_mixer->setChannelPan(_fx[i]._handle, panTable[pan + 16]);
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
2003-10-29 07:53:05 +00:00
|
|
|
|
2003-07-28 01:47:41 +00:00
|
|
|
return RD_OK;
|
|
|
|
}
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int32 Sound::setFxIdVolume(int32 id, uint8 vol) {
|
2003-10-01 06:36:25 +00:00
|
|
|
int32 i = getFxIndex(id);
|
2003-09-21 14:26:25 +00:00
|
|
|
|
2003-07-28 01:47:41 +00:00
|
|
|
if (i == MAXFX)
|
|
|
|
return RDERR_FXNOTOPEN;
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[i]._volume = vol;
|
|
|
|
if (!_fxMuted)
|
|
|
|
g_engine->_mixer->setChannelVolume(_fx[i]._handle, vol * _fxVol);
|
2003-09-21 14:26:25 +00:00
|
|
|
|
2003-07-28 01:47:41 +00:00
|
|
|
return RD_OK;
|
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* This function clears all of the sound effects which are currently open or
|
|
|
|
* playing, irrespective of type.
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::clearAllFx(void) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_soundOn)
|
|
|
|
return;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-09-21 14:26:25 +00:00
|
|
|
for (int i = 0; i < MAXFX; i++) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_fx[i]._id && _fx[i]._id != -1 && _fx[i]._id != -2) {
|
|
|
|
g_engine->_mixer->stopHandle(_fx[i]._handle);
|
|
|
|
_fx[i]._id = 0;
|
|
|
|
_fx[i]._paused = false;
|
|
|
|
if (_fx[i]._buf != NULL) {
|
|
|
|
free(_fx[i]._buf);
|
|
|
|
_fx[i]._buf = NULL;
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[i]._bufSize = 0;
|
|
|
|
_fx[i]._flags = 0;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* This function closes a sound effect which has been previously opened for
|
|
|
|
* playing. Sound effects must be closed when they are finished with, otherwise
|
|
|
|
* you will run out of sound effect buffers.
|
|
|
|
* @param id the id of the sound to close
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int32 Sound::closeFx(int32 id) {
|
2003-08-30 18:06:08 +00:00
|
|
|
int i;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_soundOn)
|
2003-09-16 07:11:29 +00:00
|
|
|
return RD_OK;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
i = getFxIndex(id);
|
|
|
|
|
|
|
|
if (i == MAXFX)
|
|
|
|
return RDERR_FXNOTOPEN;
|
|
|
|
|
|
|
|
g_engine->_mixer->stopHandle(_fx[i]._handle);
|
|
|
|
_fx[i]._id = 0;
|
|
|
|
_fx[i]._paused = false;
|
|
|
|
if (_fx[i]._buf != NULL) {
|
|
|
|
free(_fx[i]._buf);
|
|
|
|
_fx[i]._buf = NULL;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[i]._bufSize = 0;
|
|
|
|
_fx[i]._flags = 0;
|
2003-09-21 14:26:25 +00:00
|
|
|
|
2003-09-16 07:11:29 +00:00
|
|
|
return RD_OK;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::pauseFx(void) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_fxPaused) {
|
2003-09-21 14:26:25 +00:00
|
|
|
for (int i = 0; i < MAXFX; i++) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_fx[i]._id) {
|
|
|
|
g_engine->_mixer->pauseHandle(_fx[i]._handle, true);
|
|
|
|
_fx[i]._paused = true;
|
2003-09-21 14:26:25 +00:00
|
|
|
} else
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[i]._paused = false;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
2003-10-29 07:53:05 +00:00
|
|
|
_fxPaused = true;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::pauseFxForSequence(void) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_fxPaused) {
|
2003-09-21 14:26:25 +00:00
|
|
|
for (int i = 0; i < MAXFX; i++) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_fx[i]._id && _fx[i]._id != -2) {
|
|
|
|
g_engine->_mixer->pauseHandle(_fx[i]._handle, true);
|
|
|
|
_fx[i]._paused = true;
|
2003-08-30 18:06:08 +00:00
|
|
|
} else {
|
2003-10-01 06:36:25 +00:00
|
|
|
_fx[i]._paused = false;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
2003-10-29 07:53:05 +00:00
|
|
|
_fxPaused = true;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::unpauseFx(void) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_fxPaused) {
|
2003-09-21 14:26:25 +00:00
|
|
|
for (int i = 0; i < MAXFX; i++) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_fx[i]._paused && _fx[i]._id) {
|
|
|
|
g_engine->_mixer->pauseHandle(_fx[i]._handle, false);
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
2003-10-29 07:53:05 +00:00
|
|
|
_fxPaused = false;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* @return the master volume setting for sound effects
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
uint8 Sound::getFxVolume() {
|
2003-10-01 06:36:25 +00:00
|
|
|
return _fxVol;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* Set the master volume of all sound effects. The effects still have their
|
|
|
|
* own volume setting as well as the master volume.
|
|
|
|
* @param volume volume, from 0 (silent) to 14 (max)
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::setFxVolume(uint8 volume) {
|
2003-10-29 07:53:05 +00:00
|
|
|
if (volume > 14)
|
|
|
|
volume = 14;
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_fxVol = volume;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
|
|
|
// Now update the volume of any fxs playing
|
2003-09-21 14:26:25 +00:00
|
|
|
for (int i = 0; i < MAXFX; i++) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_fx[i]._id && !_fxMuted)
|
|
|
|
g_engine->_mixer->setChannelVolume(_fx[i]._handle, _fx[i]._volume * _fxVol);
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* Mutes/Unmutes the sound effects.
|
2003-10-29 07:53:05 +00:00
|
|
|
* @param mute If mute is false, restore the volume to the last set master
|
|
|
|
* level. Otherwise the sound effects are muted (volume 0).
|
2003-09-27 17:00:15 +00:00
|
|
|
*/
|
|
|
|
|
2003-10-29 07:53:05 +00:00
|
|
|
void Sound::muteFx(bool mute) {
|
2003-10-01 06:36:25 +00:00
|
|
|
_fxMuted = mute;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
|
|
|
// Now update the volume of any fxs playing
|
2003-09-21 14:26:25 +00:00
|
|
|
for (int i = 0; i < MAXFX; i++) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_fx[i]._id) {
|
|
|
|
byte volume = mute ? 0 : _fx[i]._volume * _fxVol;
|
2003-09-21 14:26:25 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
g_engine->_mixer->setChannelVolume(_fx[i]._handle, volume);
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
2003-10-29 07:53:05 +00:00
|
|
|
* @return the sound effects's mute state, true if mute, false if not mute
|
2003-09-27 17:00:15 +00:00
|
|
|
*/
|
|
|
|
|
2003-10-29 07:53:05 +00:00
|
|
|
bool Sound::isFxMute(void) {
|
2003-10-01 06:36:25 +00:00
|
|
|
return _fxMuted;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* Streams music from a cluster file.
|
|
|
|
* @param filename the file name of the music cluster file
|
|
|
|
* @param musicId the id of the music to stream
|
|
|
|
* @param looping true if the music is to loop back to the start
|
|
|
|
* @return RD_OK or an error code
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int32 Sound::streamCompMusic(const char *filename, uint32 musicId, bool looping) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-04 10:58:55 +00:00
|
|
|
|
Ok, I'm stupid.
The initial sample is, indeed, two bytes, just like the rest of them, but
it really, really helps if you read it from the correct position in the
file.
After fixing that, it turned out that my changing of signedness of the
sample was also wrong. Funny how those two bugs almost cancelled each other
out. Almost.
I've made a few other changes as well, but they're just to clean things up
a bit. The credits music works for me, and I've played the game up to
arriving in Quaramonte, with no obvious music-related problems.
svn-id: r10412
2003-09-26 06:26:18 +00:00
|
|
|
uint32 len;
|
2003-08-31 10:38:32 +00:00
|
|
|
int32 primaryStream = -1;
|
|
|
|
int32 secondaryStream = -1;
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-08-31 10:38:32 +00:00
|
|
|
// If both music streams are playing, that should mean one of them is
|
|
|
|
// fading out. Pick that one.
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_music[0]._streaming && _music[1]._streaming) {
|
|
|
|
if (_music[0]._fading)
|
2003-08-31 10:38:32 +00:00
|
|
|
primaryStream = 0;
|
|
|
|
else
|
|
|
|
primaryStream = 1;
|
2003-08-02 02:31:36 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_music[primaryStream]._fading = 0;
|
|
|
|
_music[primaryStream]._streaming = false;
|
2003-08-31 10:38:32 +00:00
|
|
|
}
|
2003-08-02 02:31:36 +00:00
|
|
|
|
2003-08-31 10:38:32 +00:00
|
|
|
// Pick the available music stream. If no music is playing it doesn't
|
|
|
|
// matter which we use, so pick the first one.
|
2003-08-30 18:06:08 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_music[0]._streaming || _music[1]._streaming) {
|
|
|
|
if (_music[0]._streaming) {
|
2003-08-31 10:38:32 +00:00
|
|
|
primaryStream = 1;
|
|
|
|
secondaryStream = 0;
|
2003-08-30 18:06:08 +00:00
|
|
|
} else {
|
2003-08-31 10:38:32 +00:00
|
|
|
primaryStream = 0;
|
|
|
|
secondaryStream = 1;
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
2003-08-31 10:38:32 +00:00
|
|
|
} else
|
|
|
|
primaryStream = 0;
|
2003-08-30 18:06:08 +00:00
|
|
|
|
2003-08-31 10:38:32 +00:00
|
|
|
// Save looping info and tune id
|
2003-10-01 06:36:25 +00:00
|
|
|
_music[primaryStream]._looping = looping;
|
|
|
|
_music[primaryStream]._id = musicId;
|
2003-08-30 18:06:08 +00:00
|
|
|
|
2003-08-31 10:38:32 +00:00
|
|
|
// Don't start streaming if the volume is off.
|
2003-10-01 06:36:25 +00:00
|
|
|
if (isMusicMute())
|
2003-08-31 10:38:32 +00:00
|
|
|
return RD_OK;
|
2003-08-02 02:31:36 +00:00
|
|
|
|
2003-09-26 14:19:03 +00:00
|
|
|
// The assumption here is that we are never playing music from two
|
|
|
|
// different files at the same time.
|
|
|
|
|
2003-08-31 10:38:32 +00:00
|
|
|
if (!fpMus.isOpen())
|
2003-09-17 21:06:16 +00:00
|
|
|
fpMus.open(filename);
|
2003-08-30 18:06:08 +00:00
|
|
|
|
2003-08-31 10:38:32 +00:00
|
|
|
if (!fpMus.isOpen())
|
|
|
|
return RDERR_INVALIDFILENAME;
|
2003-08-02 02:31:36 +00:00
|
|
|
|
2003-08-31 10:38:32 +00:00
|
|
|
// Start other music stream fading out
|
2003-10-01 06:36:25 +00:00
|
|
|
if (secondaryStream != -1 && !_music[secondaryStream]._fading)
|
|
|
|
_music[secondaryStream]._fading = FADE_SAMPLES;
|
2003-08-02 02:31:36 +00:00
|
|
|
|
2003-08-31 10:38:32 +00:00
|
|
|
fpMus.seek((musicId + 1) * 8, SEEK_SET);
|
2003-10-01 06:36:25 +00:00
|
|
|
_music[primaryStream]._fileStart = fpMus.readUint32LE();
|
Ok, I'm stupid.
The initial sample is, indeed, two bytes, just like the rest of them, but
it really, really helps if you read it from the correct position in the
file.
After fixing that, it turned out that my changing of signedness of the
sample was also wrong. Funny how those two bugs almost cancelled each other
out. Almost.
I've made a few other changes as well, but they're just to clean things up
a bit. The credits music works for me, and I've played the game up to
arriving in Quaramonte, with no obvious music-related problems.
svn-id: r10412
2003-09-26 06:26:18 +00:00
|
|
|
len = fpMus.readUint32LE();
|
2003-08-02 02:31:36 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_music[primaryStream]._fileStart || !len)
|
2003-08-31 10:38:32 +00:00
|
|
|
return RDERR_INVALIDID;
|
2003-08-02 02:31:36 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_music[primaryStream]._fileEnd = _music[primaryStream]._fileStart + len;
|
|
|
|
_music[primaryStream]._filePos = _music[primaryStream]._fileStart;
|
|
|
|
_music[primaryStream]._streaming = true;
|
|
|
|
_music[primaryStream]._firstTime = true;
|
2003-09-25 06:11:07 +00:00
|
|
|
|
2003-08-31 10:38:32 +00:00
|
|
|
return RD_OK;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::updateCompSampleStreaming(int16 *data, uint len) {
|
Ok, I'm stupid.
The initial sample is, indeed, two bytes, just like the rest of them, but
it really, really helps if you read it from the correct position in the
file.
After fixing that, it turned out that my changing of signedness of the
sample was also wrong. Funny how those two bugs almost cancelled each other
out. Almost.
I've made a few other changes as well, but they're just to clean things up
a bit. The credits music works for me, and I've played the game up to
arriving in Quaramonte, with no obvious music-related problems.
svn-id: r10412
2003-09-26 06:26:18 +00:00
|
|
|
for (int i = 0; i < MAXMUS; i++) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (!_music[i]._streaming || _music[i]._paused)
|
2003-09-05 11:12:40 +00:00
|
|
|
continue;
|
2003-08-30 18:06:08 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
st_sample_t volume = _musicMuted ? 0 : musicVolTable[_musicVol];
|
2003-09-05 11:12:40 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
fpMus.seek(_music[i]._filePos, SEEK_SET);
|
|
|
|
_converter->flow(_music[i], data, len, volume, volume);
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
2003-09-05 16:07:03 +00:00
|
|
|
|
|
|
|
// DipMusic();
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int32 Sound::dipMusic() {
|
2003-08-30 18:06:08 +00:00
|
|
|
// disable this func for now
|
|
|
|
return RD_OK;
|
|
|
|
|
2003-07-28 01:47:41 +00:00
|
|
|
/*
|
|
|
|
int32 len;
|
|
|
|
int32 readCursor, writeCursor;
|
|
|
|
int32 dwBytes1, dwBytes2;
|
|
|
|
int16 *sample;
|
|
|
|
int32 total = 0;
|
|
|
|
int32 i;
|
|
|
|
int32 status;
|
|
|
|
LPVOID lpv1, lpv2;
|
|
|
|
HRESULT hr = DS_OK;
|
|
|
|
LPDIRECTSOUNDBUFFER dsbMusic = NULL;
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
int32 currentMusicVol = musicVolTable[musicVol];
|
2003-07-28 01:47:41 +00:00
|
|
|
int32 minMusicVol;
|
|
|
|
|
|
|
|
// Find which music buffer is currently playing
|
|
|
|
for (i = 0; i<MAXMUS && !dsbMusic; i++)
|
|
|
|
{
|
|
|
|
if (musStreaming[i] && musFading[i] == 0)
|
|
|
|
dsbMusic = lpDsbMus[i];
|
|
|
|
}
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if ((!_musicMuted) && dsbMusic && (!_speechMuted) && (musicVol>2))
|
2003-07-28 01:47:41 +00:00
|
|
|
{
|
2003-10-01 06:36:25 +00:00
|
|
|
minMusicVol = musicVolTable[musicVol - 3];
|
2003-07-28 01:47:41 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_speechStatus)
|
2003-07-28 01:47:41 +00:00
|
|
|
{
|
|
|
|
IDirectSoundBuffer_GetStatus(dsbSpeech, &status);
|
|
|
|
if ((hr = IDirectSoundBuffer_GetCurrentPosition(dsbMusic, &readCursor, &writeCursor)) != DS_OK)
|
|
|
|
return hr;
|
|
|
|
|
|
|
|
len = 44100 / 12 ;// 12th of a second
|
|
|
|
|
|
|
|
if ((hr = IDirectSoundBuffer_Lock(dsbMusic, readCursor, len, &lpv1, &dwBytes1, &lpv2, &dwBytes2, 0)) != DS_OK)
|
|
|
|
return hr;
|
|
|
|
|
|
|
|
for (i = 0, sample = (int16*)lpv1; sample<(int16*)((int8*)lpv1+dwBytes1); sample+= 30, i++) // 60 samples
|
|
|
|
{
|
|
|
|
if (*sample>0)
|
|
|
|
total += *sample;
|
|
|
|
else
|
|
|
|
total -= *sample;
|
|
|
|
}
|
|
|
|
|
|
|
|
total /= i;
|
|
|
|
|
|
|
|
total = minMusicVol + ( ( (currentMusicVol - minMusicVol) * total ) / 8000);
|
|
|
|
|
|
|
|
if (total > currentMusicVol)
|
|
|
|
total = currentMusicVol;
|
|
|
|
|
|
|
|
IDirectSoundBuffer_SetVolume(dsbMusic, total);
|
|
|
|
|
|
|
|
IDirectSoundBuffer_Unlock(dsbMusic,lpv1,dwBytes1,lpv2,dwBytes2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IDirectSoundBuffer_GetVolume(dsbMusic, &total);
|
|
|
|
total += 50;
|
|
|
|
if (total > currentMusicVol)
|
|
|
|
total = currentMusicVol;
|
|
|
|
|
|
|
|
IDirectSoundBuffer_SetVolume(dsbMusic, total);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-08 06:58:34 +00:00
|
|
|
return hr;
|
2003-07-28 01:47:41 +00:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* @return the time left for the current music, in seconds.
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
int32 Sound::musicTimeRemaining() {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-04 10:58:55 +00:00
|
|
|
|
2003-09-21 14:26:25 +00:00
|
|
|
for (int i = 0; i < MAXMUS; i++) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_music[i]._streaming && !_music[i]._fading)
|
|
|
|
return (_music[i]._fileEnd - _music[i]._filePos) / 22050;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-21 14:26:25 +00:00
|
|
|
return 0;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* Fades out and stops the music.
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::stopMusic(void) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-04 10:58:55 +00:00
|
|
|
|
2003-09-21 14:26:25 +00:00
|
|
|
for (int i = 0; i < MAXMUS; i++) {
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_music[i]._streaming)
|
|
|
|
_music[i]._fading = FADE_SAMPLES;
|
2003-08-30 18:06:08 +00:00
|
|
|
else
|
2003-10-01 06:36:25 +00:00
|
|
|
_music[i]._looping = false;
|
2003-08-30 18:06:08 +00:00
|
|
|
}
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
2003-10-08 17:09:51 +00:00
|
|
|
* Stops the music dead in its tracks. Any music that is currently being
|
|
|
|
* streamed is paued.
|
2003-09-27 17:00:15 +00:00
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::pauseMusic(void) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-04 10:58:55 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_soundOn) {
|
2003-10-08 17:09:51 +00:00
|
|
|
for (int i = 0; i < MAXMUS; i++)
|
|
|
|
_music[i]._paused = _music[i]._streaming;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* Restarts the music from where it was stopped.
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::unpauseMusic(void) {
|
2003-10-04 11:50:21 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2003-09-04 10:58:55 +00:00
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
if (_soundOn) {
|
2003-09-25 06:11:07 +00:00
|
|
|
for (int i = 0; i < MAXMUS; i++)
|
2003-10-01 06:36:25 +00:00
|
|
|
_music[i]._paused = false;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* Set the volume of any future as well as playing music.
|
|
|
|
* @param volume volume, from 0 (silent) to 16 (max)
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
void Sound::setMusicVolume(uint8 volume) {
|
2003-10-29 07:53:05 +00:00
|
|
|
if (volume > 16)
|
|
|
|
volume = 16;
|
|
|
|
|
2003-10-01 06:36:25 +00:00
|
|
|
_musicVol = volume;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* @return the volume setting for music
|
|
|
|
*/
|
|
|
|
|
2003-10-04 01:09:29 +00:00
|
|
|
uint8 Sound::getMusicVolume() {
|
2003-10-01 06:36:25 +00:00
|
|
|
return _musicVol;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
|
|
|
* Mutes/Unmutes the music.
|
2003-10-29 07:53:05 +00:00
|
|
|
* @param mute If mute is false, restore the volume to the last set master
|
|
|
|
* level. Otherwise the music is muted (volume 0).
|
2003-09-27 17:00:15 +00:00
|
|
|
*/
|
|
|
|
|
2003-10-29 07:53:05 +00:00
|
|
|
void Sound::muteMusic(bool mute) {
|
2003-10-01 06:36:25 +00:00
|
|
|
_musicMuted = mute;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
|
|
|
|
2003-09-27 17:00:15 +00:00
|
|
|
/**
|
2003-10-29 07:53:05 +00:00
|
|
|
* @return the music's mute state, true if mute, false if not mute
|
2003-09-27 17:00:15 +00:00
|
|
|
*/
|
|
|
|
|
2003-10-29 07:53:05 +00:00
|
|
|
bool Sound::isMusicMute(void) {
|
2003-10-01 06:36:25 +00:00
|
|
|
return _musicMuted;
|
2003-07-28 01:47:41 +00:00
|
|
|
}
|
2003-10-04 00:52:27 +00:00
|
|
|
|
|
|
|
} // End of namespace Sword2
|