2003-12-16 02:10:15 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2004-01-06 12:45:34 +00:00
|
|
|
* Copyright (C) 2003-2004 The ScummVM project
|
2003-12-16 02:10:15 +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
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "music.h"
|
|
|
|
#include "sound/mixer.h"
|
|
|
|
#include "common/util.h"
|
|
|
|
#include "common/file.h"
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
namespace Sword1 {
|
|
|
|
|
2004-01-01 16:56:00 +00:00
|
|
|
// This means fading takes 3 seconds.
|
|
|
|
#define FADE_LENGTH 3
|
2004-01-01 15:22:15 +00:00
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
// These functions are only called from Music, so I'm just going to
|
2004-01-01 15:22:15 +00:00
|
|
|
// assume that if locking is needed it has already been taken care of.
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void MusicHandle::fadeDown() {
|
2004-01-01 15:22:15 +00:00
|
|
|
if (_fading < 0)
|
|
|
|
_fading = -_fading;
|
|
|
|
else if (_fading == 0)
|
2004-01-01 16:56:00 +00:00
|
|
|
_fading = FADE_LENGTH * getRate();
|
|
|
|
_fadeSamples = FADE_LENGTH * getRate();
|
2004-01-01 15:22:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void MusicHandle::fadeUp() {
|
2004-01-01 15:22:15 +00:00
|
|
|
if (_fading > 0)
|
|
|
|
_fading = -_fading;
|
|
|
|
else if (_fading == 0)
|
2004-01-14 08:09:03 +00:00
|
|
|
_fading = -1;
|
2004-01-01 16:56:00 +00:00
|
|
|
_fadeSamples = FADE_LENGTH * getRate();
|
2004-01-01 15:22:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
bool MusicHandle::endOfData() const {
|
2004-01-01 15:22:15 +00:00
|
|
|
return !streaming();
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
int MusicHandle::readBuffer(int16 *buffer, const int numSamples) {
|
2004-01-01 15:22:15 +00:00
|
|
|
int samples;
|
2004-01-08 13:05:32 +00:00
|
|
|
for (samples = 0; samples < numSamples && !endOfData(); samples++) {
|
|
|
|
int16 sample = _file.readUint16LE();
|
|
|
|
if (_file.ioFailed()) {
|
|
|
|
if (!_looping) {
|
|
|
|
stop();
|
|
|
|
sample = 0;
|
|
|
|
} else {
|
|
|
|
_file.clearIOFailed();
|
|
|
|
_file.seek(WAVEHEADERSIZE);
|
|
|
|
sample = _file.readUint16LE();
|
|
|
|
}
|
2004-01-01 15:22:15 +00:00
|
|
|
}
|
2004-01-08 13:05:32 +00:00
|
|
|
if (_fading > 0) {
|
|
|
|
if (--_fading == 0) {
|
|
|
|
_looping = false;
|
|
|
|
_file.close();
|
|
|
|
}
|
|
|
|
sample = (sample * _fading) / _fadeSamples;
|
|
|
|
} else if (_fading < 0) {
|
2004-01-14 08:09:03 +00:00
|
|
|
_fading--;
|
|
|
|
sample = -(sample * _fading) / _fadeSamples;
|
|
|
|
if (_fading == -_fadeSamples)
|
|
|
|
_fading = 0;
|
2004-01-01 15:22:15 +00:00
|
|
|
}
|
2004-01-08 13:05:32 +00:00
|
|
|
*buffer++ = sample;
|
2004-01-01 15:22:15 +00:00
|
|
|
}
|
2004-01-08 13:05:32 +00:00
|
|
|
return samples;
|
2004-01-01 15:22:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
bool MusicHandle::play(const char *filename, bool loop) {
|
2004-01-01 15:22:15 +00:00
|
|
|
uint8 wavHeader[WAVEHEADERSIZE];
|
|
|
|
stop();
|
2004-01-11 16:53:33 +00:00
|
|
|
if (!_file.open(filename)) {
|
2004-01-04 16:19:15 +00:00
|
|
|
warning("Music file %s could not be opened", filename);
|
2004-01-01 15:22:15 +00:00
|
|
|
return false;
|
2004-01-04 16:19:15 +00:00
|
|
|
}
|
2004-01-01 15:22:15 +00:00
|
|
|
_file.read(wavHeader, WAVEHEADERSIZE);
|
|
|
|
_stereo = (READ_LE_UINT16(wavHeader + 0x16) == 2);
|
|
|
|
_rate = READ_LE_UINT16(wavHeader + 0x18);
|
|
|
|
_looping = loop;
|
|
|
|
fadeUp();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void MusicHandle::stop() {
|
2004-01-01 15:22:15 +00:00
|
|
|
if (_file.isOpen())
|
|
|
|
_file.close();
|
|
|
|
_fading = 0;
|
|
|
|
_looping = false;
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
Music::Music(OSystem *system, SoundMixer *pMixer) {
|
2003-12-16 02:10:15 +00:00
|
|
|
_system = system;
|
|
|
|
_mixer = pMixer;
|
|
|
|
_mixer->setupPremix(passMixerFunc, this);
|
|
|
|
_mutex = _system->create_mutex();
|
2004-01-01 15:22:15 +00:00
|
|
|
_converter[0] = NULL;
|
|
|
|
_converter[1] = NULL;
|
2004-01-07 17:47:46 +00:00
|
|
|
_volumeL = _volumeR = 192;
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
Music::~Music() {
|
2003-12-19 01:08:30 +00:00
|
|
|
_mixer->setupPremix(0, 0);
|
2004-01-01 15:22:15 +00:00
|
|
|
delete _converter[0];
|
|
|
|
delete _converter[1];
|
2004-01-03 11:12:35 +00:00
|
|
|
if (_mutex)
|
|
|
|
_system->delete_mutex(_mutex);
|
2003-12-19 01:08:30 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Music::passMixerFunc(void *param, int16 *buf, uint len) {
|
|
|
|
((Music*)param)->mixer(buf, len);
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Music::mixer(int16 *buf, uint32 len) {
|
2004-01-01 15:22:15 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
2004-01-06 11:48:30 +00:00
|
|
|
for (int i = 0; i < ARRAYSIZE(_handles); i++)
|
|
|
|
if (_handles[i].streaming() && _converter[i])
|
2004-01-07 17:47:46 +00:00
|
|
|
_converter[i]->flow(_handles[i], buf, len, _volumeL, _volumeR);
|
2004-01-06 11:48:30 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Music::setVolume(uint8 volL, uint8 volR) {
|
2004-01-07 17:47:46 +00:00
|
|
|
_volumeL = (st_volume_t)volL;
|
|
|
|
_volumeR = (st_volume_t)volR;
|
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Music::giveVolume(uint8 *volL, uint8 *volR) {
|
2004-01-07 17:47:46 +00:00
|
|
|
*volL = (uint8)_volumeL;
|
|
|
|
*volR = (uint8)_volumeR;
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Music::startMusic(int32 tuneId, int32 loopFlag) {
|
2004-01-01 15:22:15 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
if (strlen(_tuneList[tuneId]) > 0) {
|
|
|
|
int newStream = 0;
|
|
|
|
if (_handles[0].streaming() && _handles[1].streaming()) {
|
|
|
|
// If both handles are playing, stop the one that's
|
|
|
|
// fading down.
|
|
|
|
if (_handles[0].fading() > 0)
|
|
|
|
_handles[0].stop();
|
|
|
|
else
|
|
|
|
_handles[1].stop();
|
|
|
|
}
|
|
|
|
if (_handles[0].streaming()) {
|
|
|
|
_handles[0].fadeDown();
|
|
|
|
newStream = 1;
|
|
|
|
} else if (_handles[1].streaming()) {
|
|
|
|
_handles[1].fadeDown();
|
|
|
|
newStream = 0;
|
|
|
|
}
|
2003-12-16 02:10:15 +00:00
|
|
|
char fName[20];
|
2003-12-17 09:17:59 +00:00
|
|
|
sprintf(fName, "music/%s.wav", _tuneList[tuneId]);
|
2004-01-04 16:19:15 +00:00
|
|
|
if (_handles[newStream].play(fName, loopFlag != 0)) {
|
|
|
|
delete _converter[newStream];
|
|
|
|
_converter[newStream] = makeRateConverter(_handles[newStream].getRate(), _mixer->getOutputRate(), _handles[newStream].isStereo(), false);
|
|
|
|
}
|
2004-01-01 16:56:00 +00:00
|
|
|
} else {
|
|
|
|
if (_handles[0].streaming())
|
|
|
|
_handles[0].fadeDown();
|
|
|
|
if (_handles[1].streaming())
|
|
|
|
_handles[1].fadeDown();
|
2003-12-30 22:57:52 +00:00
|
|
|
}
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
|
|
|
|
2004-01-11 15:47:41 +00:00
|
|
|
void Music::fadeDown() {
|
2004-01-01 15:22:15 +00:00
|
|
|
Common::StackLock lock(_mutex);
|
|
|
|
for (int i = 0; i < ARRAYSIZE(_handles); i++)
|
|
|
|
if (_handles[i].streaming())
|
|
|
|
_handles[i].fadeDown();
|
2003-12-16 02:10:15 +00:00
|
|
|
}
|
2004-01-11 15:47:41 +00:00
|
|
|
|
|
|
|
} // End of namespace Sword1
|