2007-05-30 21:56:52 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
2006-02-22 22:40:53 +00:00
|
|
|
*
|
2007-05-30 21:56:52 +00:00
|
|
|
* 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.
|
2006-02-22 22:40:53 +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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-03-29 15:59:37 +00:00
|
|
|
#include "common/endian.h"
|
2007-05-19 12:08:41 +00:00
|
|
|
#include "common/file.h"
|
|
|
|
#include "common/system.h"
|
2006-03-29 15:59:37 +00:00
|
|
|
|
2006-02-22 22:40:53 +00:00
|
|
|
#include "cine/cine.h"
|
2007-05-19 12:08:41 +00:00
|
|
|
#include "cine/sound.h"
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
#include "sound/audiostream.h"
|
|
|
|
#include "sound/fmopl.h"
|
|
|
|
#include "sound/mods/soundfx.h"
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-02-25 00:26:14 +00:00
|
|
|
namespace Cine {
|
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
class PCSoundDriver {
|
|
|
|
public:
|
|
|
|
typedef void (*UpdateCallback)(void *);
|
|
|
|
|
|
|
|
virtual ~PCSoundDriver() {}
|
|
|
|
|
|
|
|
virtual void setupChannel(int channel, const byte *data, int instrument, int volume) = 0;
|
|
|
|
virtual void setChannelFrequency(int channel, int frequency) = 0;
|
|
|
|
virtual void stopChannel(int channel) = 0;
|
|
|
|
virtual void playSample(const byte *data, int size, int channel, int volume) = 0;
|
|
|
|
virtual void stopAll() = 0;
|
|
|
|
virtual const char *getInstrumentExtension() const { return ""; }
|
|
|
|
|
|
|
|
void setUpdateCallback(UpdateCallback upCb, void *ref);
|
|
|
|
void resetChannel(int channel);
|
|
|
|
void findNote(int freq, int *note, int *oct) const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
UpdateCallback _upCb;
|
|
|
|
void *_upRef;
|
|
|
|
|
|
|
|
static const int _noteTable[];
|
|
|
|
static const int _noteTableCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
const int PCSoundDriver::_noteTable[] = {
|
|
|
|
0xEEE, 0xE17, 0xD4D, 0xC8C, 0xBD9, 0xB2F, 0xA8E, 0x9F7,
|
|
|
|
0x967, 0x8E0, 0x861, 0x7E8, 0x777, 0x70B, 0x6A6, 0x647,
|
|
|
|
0x5EC, 0x597, 0x547, 0x4FB, 0x4B3, 0x470, 0x430, 0x3F4,
|
|
|
|
0x3BB, 0x385, 0x353, 0x323, 0x2F6, 0x2CB, 0x2A3, 0x27D,
|
|
|
|
0x259, 0x238, 0x218, 0x1FA, 0x1DD, 0x1C2, 0x1A9, 0x191,
|
|
|
|
0x17B, 0x165, 0x151, 0x13E, 0x12C, 0x11C, 0x10C, 0x0FD,
|
|
|
|
0x0EE, 0x0E1, 0x0D4, 0x0C8, 0x0BD, 0x0B2, 0x0A8, 0x09F,
|
|
|
|
0x096, 0x08E, 0x086, 0x07E, 0x077, 0x070, 0x06A, 0x064,
|
|
|
|
0x05E, 0x059, 0x054, 0x04F, 0x04B, 0x047, 0x043, 0x03F,
|
|
|
|
0x03B, 0x038, 0x035, 0x032, 0x02F, 0x02C, 0x02A, 0x027,
|
|
|
|
0x025, 0x023, 0x021, 0x01F, 0x01D, 0x01C, 0x01A, 0x019,
|
|
|
|
0x017, 0x016, 0x015, 0x013, 0x012, 0x011, 0x010, 0x00F
|
|
|
|
};
|
|
|
|
|
|
|
|
const int PCSoundDriver::_noteTableCount = ARRAYSIZE(_noteTable);
|
|
|
|
|
|
|
|
struct AdlibRegisterSoundInstrument {
|
2008-08-16 11:38:43 +00:00
|
|
|
uint8 vibrato;
|
|
|
|
uint8 attackDecay;
|
|
|
|
uint8 sustainRelease;
|
|
|
|
uint8 feedbackStrength;
|
|
|
|
uint8 keyScaling;
|
|
|
|
uint8 outputLevel;
|
|
|
|
uint8 freqMod;
|
2007-05-19 12:08:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct AdlibSoundInstrument {
|
|
|
|
byte mode;
|
|
|
|
byte channel;
|
|
|
|
AdlibRegisterSoundInstrument regMod;
|
|
|
|
AdlibRegisterSoundInstrument regCar;
|
|
|
|
byte waveSelectMod;
|
|
|
|
byte waveSelectCar;
|
|
|
|
byte amDepth;
|
|
|
|
};
|
|
|
|
|
|
|
|
class AdlibSoundDriver : public PCSoundDriver, Audio::AudioStream {
|
|
|
|
public:
|
|
|
|
AdlibSoundDriver(Audio::Mixer *mixer);
|
|
|
|
virtual ~AdlibSoundDriver();
|
|
|
|
|
|
|
|
// PCSoundDriver interface
|
|
|
|
virtual void setupChannel(int channel, const byte *data, int instrument, int volume);
|
|
|
|
virtual void stopChannel(int channel);
|
|
|
|
virtual void stopAll();
|
|
|
|
|
|
|
|
// AudioStream interface
|
|
|
|
virtual int readBuffer(int16 *buffer, const int numSamples);
|
|
|
|
virtual bool isStereo() const { return false; }
|
|
|
|
virtual bool endOfData() const { return false; }
|
|
|
|
virtual int getRate() const { return _sampleRate; }
|
|
|
|
|
|
|
|
void initCard();
|
|
|
|
void update(int16 *buf, int len);
|
|
|
|
void setupInstrument(const byte *data, int channel);
|
|
|
|
void loadRegisterInstrument(const byte *data, AdlibRegisterSoundInstrument *reg);
|
|
|
|
virtual void loadInstrument(const byte *data, AdlibSoundInstrument *asi) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
FM_OPL *_opl;
|
|
|
|
int _sampleRate;
|
|
|
|
Audio::Mixer *_mixer;
|
|
|
|
Audio::SoundHandle _soundHandle;
|
|
|
|
|
|
|
|
byte _vibrato;
|
|
|
|
int _channelsVolumeTable[4];
|
|
|
|
AdlibSoundInstrument _instrumentsTable[4];
|
|
|
|
|
|
|
|
static const int _freqTable[];
|
|
|
|
static const int _freqTableCount;
|
|
|
|
static const int _operatorsTable[];
|
|
|
|
static const int _operatorsTableCount;
|
|
|
|
static const int _voiceOperatorsTable[];
|
|
|
|
static const int _voiceOperatorsTableCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
const int AdlibSoundDriver::_freqTable[] = {
|
|
|
|
0x157, 0x16C, 0x181, 0x198, 0x1B1, 0x1CB,
|
|
|
|
0x1E6, 0x203, 0x222, 0x243, 0x266, 0x28A
|
|
|
|
};
|
|
|
|
|
|
|
|
const int AdlibSoundDriver::_freqTableCount = ARRAYSIZE(_freqTable);
|
|
|
|
|
|
|
|
const int AdlibSoundDriver::_operatorsTable[] = {
|
|
|
|
0, 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 16, 17, 18, 19, 20, 21
|
|
|
|
};
|
|
|
|
|
|
|
|
const int AdlibSoundDriver::_operatorsTableCount = ARRAYSIZE(_operatorsTable);
|
|
|
|
|
|
|
|
const int AdlibSoundDriver::_voiceOperatorsTable[] = {
|
|
|
|
0, 3, 1, 4, 2, 5, 6, 9, 7, 10, 8, 11, 12, 15, 16, 16, 14, 14, 17, 17, 13, 13
|
|
|
|
};
|
|
|
|
|
|
|
|
const int AdlibSoundDriver::_voiceOperatorsTableCount = ARRAYSIZE(_voiceOperatorsTable);
|
|
|
|
|
|
|
|
// Future Wars Adlib driver
|
|
|
|
class AdlibSoundDriverINS : public AdlibSoundDriver {
|
|
|
|
public:
|
|
|
|
AdlibSoundDriverINS(Audio::Mixer *mixer) : AdlibSoundDriver(mixer) {}
|
|
|
|
virtual const char *getInstrumentExtension() const { return ".INS"; }
|
|
|
|
virtual void loadInstrument(const byte *data, AdlibSoundInstrument *asi);
|
|
|
|
virtual void setChannelFrequency(int channel, int frequency);
|
|
|
|
virtual void playSample(const byte *data, int size, int channel, int volume);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Operation Stealth Adlib driver
|
|
|
|
class AdlibSoundDriverADL : public AdlibSoundDriver {
|
|
|
|
public:
|
|
|
|
AdlibSoundDriverADL(Audio::Mixer *mixer) : AdlibSoundDriver(mixer) {}
|
|
|
|
virtual const char *getInstrumentExtension() const { return ".ADL"; }
|
|
|
|
virtual void loadInstrument(const byte *data, AdlibSoundInstrument *asi);
|
|
|
|
virtual void setChannelFrequency(int channel, int frequency);
|
|
|
|
virtual void playSample(const byte *data, int size, int channel, int volume);
|
|
|
|
};
|
|
|
|
|
|
|
|
class PCSoundFxPlayer {
|
|
|
|
public:
|
|
|
|
|
|
|
|
PCSoundFxPlayer(PCSoundDriver *driver);
|
|
|
|
~PCSoundFxPlayer();
|
|
|
|
|
|
|
|
bool load(const char *song);
|
|
|
|
void play();
|
|
|
|
void stop();
|
|
|
|
void fadeOut();
|
|
|
|
|
|
|
|
static void updateCallback(void *ref);
|
|
|
|
|
|
|
|
enum {
|
|
|
|
NUM_INSTRUMENTS = 15,
|
|
|
|
NUM_CHANNELS = 4
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void update();
|
|
|
|
void handleEvents();
|
|
|
|
void handlePattern(int channel, const byte *patternData);
|
|
|
|
void unload();
|
|
|
|
|
|
|
|
bool _playing;
|
|
|
|
int _currentPos;
|
|
|
|
int _currentOrder;
|
|
|
|
int _numOrders;
|
|
|
|
int _eventsDelay;
|
|
|
|
int _fadeOutCounter;
|
|
|
|
int _updateTicksCounter;
|
|
|
|
int _instrumentsChannelTable[NUM_CHANNELS];
|
|
|
|
byte *_sfxData;
|
|
|
|
byte *_instrumentsData[NUM_INSTRUMENTS];
|
|
|
|
PCSoundDriver *_driver;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void PCSoundDriver::setUpdateCallback(UpdateCallback upCb, void *ref) {
|
2006-03-09 22:37:19 +00:00
|
|
|
_upCb = upCb;
|
|
|
|
_upRef = ref;
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
void PCSoundDriver::findNote(int freq, int *note, int *oct) const {
|
2006-03-09 22:37:19 +00:00
|
|
|
*note = _noteTableCount - 1;
|
|
|
|
for (int i = 0; i < _noteTableCount; ++i) {
|
|
|
|
if (_noteTable[i] <= freq) {
|
|
|
|
*note = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*oct = *note / 12;
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
void PCSoundDriver::resetChannel(int channel) {
|
2006-03-09 22:37:19 +00:00
|
|
|
stopChannel(channel);
|
2007-05-19 12:08:41 +00:00
|
|
|
stopAll();
|
2006-03-09 22:37:19 +00:00
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
AdlibSoundDriver::AdlibSoundDriver(Audio::Mixer *mixer)
|
|
|
|
: _mixer(mixer) {
|
|
|
|
_sampleRate = _mixer->getOutputRate();
|
|
|
|
_opl = makeAdlibOPL(_sampleRate);
|
|
|
|
memset(_channelsVolumeTable, 0, sizeof(_channelsVolumeTable));
|
|
|
|
memset(_instrumentsTable, 0, sizeof(_instrumentsTable));
|
|
|
|
initCard();
|
2007-02-20 18:50:17 +00:00
|
|
|
_mixer->playInputStream(Audio::Mixer::kPlainSoundType, &_soundHandle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, false, true);
|
2006-03-09 22:37:19 +00:00
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
AdlibSoundDriver::~AdlibSoundDriver() {
|
2007-02-20 18:50:17 +00:00
|
|
|
_mixer->stopHandle(_soundHandle);
|
2008-05-27 23:26:48 +00:00
|
|
|
OPLDestroy(_opl);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2006-03-23 03:45:52 +00:00
|
|
|
void AdlibSoundDriver::setupChannel(int channel, const byte *data, int instrument, int volume) {
|
2006-03-09 22:37:19 +00:00
|
|
|
assert(channel < 4);
|
|
|
|
if (data) {
|
|
|
|
if (volume > 80) {
|
|
|
|
volume = 80;
|
|
|
|
} else if (volume < 0) {
|
|
|
|
volume = 0;
|
|
|
|
}
|
|
|
|
volume += volume / 4;
|
|
|
|
if (volume > 127) {
|
|
|
|
volume = 127;
|
|
|
|
}
|
|
|
|
_channelsVolumeTable[channel] = volume;
|
|
|
|
setupInstrument(data, channel);
|
|
|
|
}
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
void AdlibSoundDriver::stopChannel(int channel) {
|
|
|
|
assert(channel < 4);
|
|
|
|
AdlibSoundInstrument *ins = &_instrumentsTable[channel];
|
2007-05-13 02:11:38 +00:00
|
|
|
if (ins->mode != 0 && ins->channel == 6) {
|
|
|
|
channel = 6;
|
|
|
|
}
|
|
|
|
if (ins->mode == 0 || channel == 6) {
|
|
|
|
OPLWriteReg(_opl, 0xB0 | channel, 0);
|
|
|
|
}
|
|
|
|
if (ins->mode != 0) {
|
|
|
|
_vibrato &= ~(1 << (10 - ins->channel));
|
|
|
|
OPLWriteReg(_opl, 0xBD, _vibrato);
|
2006-03-09 22:37:19 +00:00
|
|
|
}
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
void AdlibSoundDriver::stopAll() {
|
2006-03-09 22:37:19 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 18; ++i) {
|
|
|
|
OPLWriteReg(_opl, 0x40 | _operatorsTable[i], 63);
|
|
|
|
}
|
|
|
|
for (i = 0; i < 9; ++i) {
|
|
|
|
OPLWriteReg(_opl, 0xB0 | i, 0);
|
|
|
|
}
|
|
|
|
OPLWriteReg(_opl, 0xBD, 0);
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
int AdlibSoundDriver::readBuffer(int16 *buffer, const int numSamples) {
|
2006-04-06 18:55:39 +00:00
|
|
|
update(buffer, numSamples);
|
2006-03-09 22:37:19 +00:00
|
|
|
return numSamples;
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
void AdlibSoundDriver::initCard() {
|
|
|
|
_vibrato = 0x20;
|
|
|
|
OPLWriteReg(_opl, 0xBD, _vibrato);
|
|
|
|
OPLWriteReg(_opl, 0x08, 0x40);
|
2007-05-19 12:08:41 +00:00
|
|
|
|
2007-12-13 19:07:10 +00:00
|
|
|
static const int oplRegs[] = { 0x40, 0x60, 0x80, 0x20, 0xE0 };
|
2007-12-13 12:33:31 +00:00
|
|
|
|
2007-12-13 19:07:10 +00:00
|
|
|
for (int i = 0; i < 9; ++i) {
|
2006-03-09 22:37:19 +00:00
|
|
|
OPLWriteReg(_opl, 0xB0 | i, 0);
|
|
|
|
}
|
2007-12-13 19:07:10 +00:00
|
|
|
for (int i = 0; i < 9; ++i) {
|
2006-03-09 22:37:19 +00:00
|
|
|
OPLWriteReg(_opl, 0xC0 | i, 0);
|
|
|
|
}
|
2007-12-13 12:33:31 +00:00
|
|
|
|
2007-12-13 19:07:10 +00:00
|
|
|
for (int j = 0; j < 5; j++) {
|
|
|
|
for (int i = 0; i < 18; ++i) {
|
2007-12-13 12:33:31 +00:00
|
|
|
OPLWriteReg(_opl, oplRegs[j] | _operatorsTable[i], 0);
|
|
|
|
}
|
2006-03-09 22:37:19 +00:00
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
OPLWriteReg(_opl, 1, 0x20);
|
|
|
|
OPLWriteReg(_opl, 1, 0);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
void AdlibSoundDriver::update(int16 *buf, int len) {
|
2006-02-22 22:40:53 +00:00
|
|
|
static int samplesLeft = 0;
|
|
|
|
while (len != 0) {
|
2006-03-09 22:37:19 +00:00
|
|
|
int count = samplesLeft;
|
|
|
|
if (count > len) {
|
2006-02-22 22:40:53 +00:00
|
|
|
count = len;
|
2006-03-09 22:37:19 +00:00
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
samplesLeft -= count;
|
|
|
|
len -= count;
|
2006-03-09 22:37:19 +00:00
|
|
|
YM3812UpdateOne(_opl, buf, count);
|
|
|
|
if (samplesLeft == 0) {
|
|
|
|
if (_upCb) {
|
|
|
|
(*_upCb)(_upRef);
|
|
|
|
}
|
|
|
|
samplesLeft = _sampleRate / 50;
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
buf += count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-23 03:45:52 +00:00
|
|
|
void AdlibSoundDriver::setupInstrument(const byte *data, int channel) {
|
2006-03-09 22:37:19 +00:00
|
|
|
assert(channel < 4);
|
|
|
|
AdlibSoundInstrument *ins = &_instrumentsTable[channel];
|
|
|
|
loadInstrument(data, ins);
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
int mod, car, tmp;
|
|
|
|
const AdlibRegisterSoundInstrument *reg;
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
if (ins->mode != 0) {
|
|
|
|
mod = _operatorsTable[_voiceOperatorsTable[2 * ins->channel + 0]];
|
|
|
|
car = _operatorsTable[_voiceOperatorsTable[2 * ins->channel + 1]];
|
|
|
|
} else {
|
|
|
|
mod = _operatorsTable[_voiceOperatorsTable[2 * channel + 0]];
|
|
|
|
car = _operatorsTable[_voiceOperatorsTable[2 * channel + 1]];
|
|
|
|
}
|
2007-05-19 12:08:41 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
if (ins->mode == 0 || ins->channel == 6) {
|
|
|
|
reg = &ins->regMod;
|
|
|
|
OPLWriteReg(_opl, 0x20 | mod, reg->vibrato);
|
|
|
|
if (reg->freqMod) {
|
|
|
|
tmp = reg->outputLevel & 0x3F;
|
|
|
|
} else {
|
|
|
|
tmp = (63 - (reg->outputLevel & 0x3F)) * _channelsVolumeTable[channel];
|
|
|
|
tmp = 63 - (2 * tmp + 127) / (2 * 127);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
2006-03-09 22:37:19 +00:00
|
|
|
OPLWriteReg(_opl, 0x40 | mod, tmp | (reg->keyScaling << 6));
|
|
|
|
OPLWriteReg(_opl, 0x60 | mod, reg->attackDecay);
|
|
|
|
OPLWriteReg(_opl, 0x80 | mod, reg->sustainRelease);
|
|
|
|
if (ins->mode != 0) {
|
|
|
|
OPLWriteReg(_opl, 0xC0 | ins->channel, reg->feedbackStrength);
|
|
|
|
} else {
|
|
|
|
OPLWriteReg(_opl, 0xC0 | channel, reg->feedbackStrength);
|
|
|
|
}
|
|
|
|
OPLWriteReg(_opl, 0xE0 | mod, ins->waveSelectMod);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
reg = &ins->regCar;
|
|
|
|
OPLWriteReg(_opl, 0x20 | car, reg->vibrato);
|
|
|
|
tmp = (63 - (reg->outputLevel & 0x3F)) * _channelsVolumeTable[channel];
|
|
|
|
tmp = 63 - (2 * tmp + 127) / (2 * 127);
|
|
|
|
OPLWriteReg(_opl, 0x40 | car, tmp | (reg->keyScaling << 6));
|
|
|
|
OPLWriteReg(_opl, 0x60 | car, reg->attackDecay);
|
|
|
|
OPLWriteReg(_opl, 0x80 | car, reg->sustainRelease);
|
|
|
|
OPLWriteReg(_opl, 0xE0 | car, ins->waveSelectCar);
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-03-23 03:45:52 +00:00
|
|
|
void AdlibSoundDriver::loadRegisterInstrument(const byte *data, AdlibRegisterSoundInstrument *reg) {
|
2006-03-09 22:37:19 +00:00
|
|
|
reg->vibrato = 0;
|
|
|
|
if (READ_LE_UINT16(data + 18)) { // amplitude vibrato
|
|
|
|
reg->vibrato |= 0x80;
|
|
|
|
}
|
|
|
|
if (READ_LE_UINT16(data + 20)) { // frequency vibrato
|
|
|
|
reg->vibrato |= 0x40;
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
2006-03-09 22:37:19 +00:00
|
|
|
if (READ_LE_UINT16(data + 10)) { // sustaining sound
|
|
|
|
reg->vibrato |= 0x20;
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
2006-03-09 22:37:19 +00:00
|
|
|
if (READ_LE_UINT16(data + 22)) { // envelope scaling
|
|
|
|
reg->vibrato |= 0x10;
|
|
|
|
}
|
|
|
|
reg->vibrato |= READ_LE_UINT16(data + 2) & 0xF; // frequency multiplier
|
2007-05-19 12:08:41 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
reg->attackDecay = READ_LE_UINT16(data + 6) << 4; // attack rate
|
|
|
|
reg->attackDecay |= READ_LE_UINT16(data + 12) & 0xF; // decay rate
|
2007-05-19 12:08:41 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
reg->sustainRelease = READ_LE_UINT16(data + 8) << 4; // sustain level
|
|
|
|
reg->sustainRelease |= READ_LE_UINT16(data + 14) & 0xF; // release rate
|
2007-05-19 12:08:41 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
reg->feedbackStrength = READ_LE_UINT16(data + 4) << 1; // feedback
|
|
|
|
if (READ_LE_UINT16(data + 24) == 0) { // frequency modulation
|
|
|
|
reg->feedbackStrength |= 1;
|
|
|
|
}
|
2007-05-19 12:08:41 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
reg->keyScaling = READ_LE_UINT16(data);
|
|
|
|
reg->outputLevel = READ_LE_UINT16(data + 16);
|
|
|
|
reg->freqMod = READ_LE_UINT16(data + 24);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2006-03-23 03:45:52 +00:00
|
|
|
void AdlibSoundDriverINS::loadInstrument(const byte *data, AdlibSoundInstrument *asi) {
|
2006-03-09 22:37:19 +00:00
|
|
|
asi->mode = *data++;
|
|
|
|
asi->channel = *data++;
|
|
|
|
loadRegisterInstrument(data, &asi->regMod); data += 26;
|
|
|
|
loadRegisterInstrument(data, &asi->regCar); data += 26;
|
|
|
|
asi->waveSelectMod = data[0] & 3; data += 2;
|
|
|
|
asi->waveSelectCar = data[0] & 3; data += 2;
|
|
|
|
asi->amDepth = data[0]; data += 2;
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
void AdlibSoundDriverINS::setChannelFrequency(int channel, int frequency) {
|
|
|
|
assert(channel < 4);
|
|
|
|
AdlibSoundInstrument *ins = &_instrumentsTable[channel];
|
2007-05-17 22:18:51 +00:00
|
|
|
if (ins->mode != 0 && ins->channel == 6) {
|
|
|
|
channel = 6;
|
|
|
|
}
|
|
|
|
if (ins->mode == 0 || ins->channel == 6) {
|
|
|
|
int freq, note, oct;
|
|
|
|
findNote(frequency, ¬e, &oct);
|
|
|
|
if (channel == 6) {
|
|
|
|
note %= 12;
|
2006-03-09 22:37:19 +00:00
|
|
|
}
|
2007-05-17 22:18:51 +00:00
|
|
|
freq = _freqTable[note % 12];
|
|
|
|
OPLWriteReg(_opl, 0xA0 | channel, freq);
|
|
|
|
freq = ((note / 12) << 2) | ((freq & 0x300) >> 8);
|
|
|
|
if (ins->mode == 0) {
|
|
|
|
freq |= 0x20;
|
2006-03-09 22:37:19 +00:00
|
|
|
}
|
2007-05-17 22:18:51 +00:00
|
|
|
OPLWriteReg(_opl, 0xB0 | channel, freq);
|
|
|
|
}
|
|
|
|
if (ins->mode != 0) {
|
|
|
|
_vibrato |= 1 << (10 - ins->channel);
|
|
|
|
OPLWriteReg(_opl, 0xBD, _vibrato);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
void AdlibSoundDriverINS::playSample(const byte *data, int size, int channel, int volume) {
|
2006-03-09 22:37:19 +00:00
|
|
|
assert(channel < 4);
|
|
|
|
_channelsVolumeTable[channel] = 127;
|
|
|
|
resetChannel(channel);
|
|
|
|
setupInstrument(data + 257, channel);
|
|
|
|
AdlibSoundInstrument *ins = &_instrumentsTable[channel];
|
|
|
|
if (ins->mode != 0 && ins->channel == 6) {
|
|
|
|
channel = 6;
|
|
|
|
}
|
|
|
|
if (ins->mode == 0 || channel == 6) {
|
2007-05-13 02:11:38 +00:00
|
|
|
uint16 note = 12;
|
|
|
|
int freq = _freqTable[note % 12];
|
2006-03-09 22:37:19 +00:00
|
|
|
OPLWriteReg(_opl, 0xA0 | channel, freq);
|
2007-05-13 02:11:38 +00:00
|
|
|
freq = ((note / 12) << 2) | ((freq & 0x300) >> 8);
|
2006-03-09 22:37:19 +00:00
|
|
|
if (ins->mode == 0) {
|
|
|
|
freq |= 0x20;
|
|
|
|
}
|
|
|
|
OPLWriteReg(_opl, 0xB0 | channel, freq);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
2006-03-09 22:37:19 +00:00
|
|
|
if (ins->mode != 0) {
|
2007-05-13 02:11:38 +00:00
|
|
|
_vibrato |= 1 << (10 - ins->channel);
|
2006-03-09 22:37:19 +00:00
|
|
|
OPLWriteReg(_opl, 0xBD, _vibrato);
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-23 03:45:52 +00:00
|
|
|
void AdlibSoundDriverADL::loadInstrument(const byte *data, AdlibSoundInstrument *asi) {
|
2006-03-09 22:37:19 +00:00
|
|
|
asi->mode = *data++;
|
|
|
|
asi->channel = *data++;
|
|
|
|
asi->waveSelectMod = *data++ & 3;
|
|
|
|
asi->waveSelectCar = *data++ & 3;
|
|
|
|
asi->amDepth = *data++;
|
|
|
|
++data;
|
|
|
|
loadRegisterInstrument(data, &asi->regMod); data += 26;
|
|
|
|
loadRegisterInstrument(data, &asi->regCar); data += 26;
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2006-03-09 22:37:19 +00:00
|
|
|
void AdlibSoundDriverADL::setChannelFrequency(int channel, int frequency) {
|
|
|
|
assert(channel < 4);
|
|
|
|
AdlibSoundInstrument *ins = &_instrumentsTable[channel];
|
2007-05-17 22:18:51 +00:00
|
|
|
if (ins->mode != 0) {
|
|
|
|
channel = ins->channel;
|
|
|
|
if (channel == 9) {
|
|
|
|
channel = 8;
|
|
|
|
} else if (channel == 10) {
|
|
|
|
channel = 7;
|
2006-03-09 22:37:19 +00:00
|
|
|
}
|
2007-05-17 22:18:51 +00:00
|
|
|
}
|
|
|
|
int freq, note, oct;
|
|
|
|
findNote(frequency, ¬e, &oct);
|
2007-05-19 12:08:41 +00:00
|
|
|
|
2007-05-17 22:18:51 +00:00
|
|
|
note += oct * 12;
|
|
|
|
if (ins->amDepth) {
|
|
|
|
note = ins->amDepth;
|
|
|
|
}
|
|
|
|
if (note < 0) {
|
|
|
|
note = 0;
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2007-05-17 22:18:51 +00:00
|
|
|
freq = _freqTable[note % 12];
|
|
|
|
OPLWriteReg(_opl, 0xA0 | channel, freq);
|
|
|
|
freq = ((note / 12) << 2) | ((freq & 0x300) >> 8);
|
|
|
|
if (ins->mode == 0) {
|
|
|
|
freq |= 0x20;
|
|
|
|
}
|
|
|
|
OPLWriteReg(_opl, 0xB0 | channel, freq);
|
|
|
|
if (ins->mode != 0) {
|
|
|
|
_vibrato |= 1 << (10 - channel);
|
|
|
|
OPLWriteReg(_opl, 0xBD, _vibrato);
|
2006-03-09 22:37:19 +00:00
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
void AdlibSoundDriverADL::playSample(const byte *data, int size, int channel, int volume) {
|
2006-03-09 22:37:19 +00:00
|
|
|
assert(channel < 4);
|
|
|
|
_channelsVolumeTable[channel] = 127;
|
|
|
|
setupInstrument(data, channel);
|
|
|
|
AdlibSoundInstrument *ins = &_instrumentsTable[channel];
|
|
|
|
if (ins->mode != 0 && ins->channel == 6) {
|
|
|
|
OPLWriteReg(_opl, 0xB0 | channel, 0);
|
|
|
|
}
|
|
|
|
if (ins->mode != 0) {
|
2007-05-13 02:11:38 +00:00
|
|
|
_vibrato &= ~(1 << (10 - ins->channel));
|
2006-03-09 22:37:19 +00:00
|
|
|
OPLWriteReg(_opl, 0xBD, _vibrato);
|
|
|
|
}
|
|
|
|
if (ins->mode != 0) {
|
|
|
|
channel = ins->channel;
|
|
|
|
if (channel == 9) {
|
|
|
|
channel = 8;
|
|
|
|
} else if (channel == 10) {
|
|
|
|
channel = 7;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint16 note = 48;
|
|
|
|
if (ins->amDepth) {
|
|
|
|
note = ins->amDepth;
|
|
|
|
}
|
|
|
|
int freq = _freqTable[note % 12];
|
|
|
|
OPLWriteReg(_opl, 0xA0 | channel, freq);
|
|
|
|
freq = ((note / 12) << 2) | ((freq & 0x300) >> 8);
|
|
|
|
if (ins->mode == 0) {
|
|
|
|
freq |= 0x20;
|
|
|
|
}
|
|
|
|
OPLWriteReg(_opl, 0xB0 | channel, freq);
|
|
|
|
if (ins->mode != 0) {
|
2007-05-13 02:11:38 +00:00
|
|
|
_vibrato |= 1 << (10 - channel);
|
2006-03-09 22:37:19 +00:00
|
|
|
OPLWriteReg(_opl, 0xBD, _vibrato);
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
}
|
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
PCSoundFxPlayer::PCSoundFxPlayer(PCSoundDriver *driver)
|
|
|
|
: _playing(false), _driver(driver) {
|
|
|
|
memset(_instrumentsData, 0, sizeof(_instrumentsData));
|
|
|
|
_sfxData = NULL;
|
|
|
|
_fadeOutCounter = 0;
|
|
|
|
_driver->setUpdateCallback(updateCallback, this);
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
PCSoundFxPlayer::~PCSoundFxPlayer() {
|
|
|
|
_driver->setUpdateCallback(NULL, NULL);
|
|
|
|
if (_playing) {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
bool PCSoundFxPlayer::load(const char *song) {
|
|
|
|
debug(9, "PCSoundFxPlayer::load('%s')", song);
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
/* stop (w/ fade out) the previous song */
|
|
|
|
while (_fadeOutCounter != 0 && _fadeOutCounter < 100) {
|
|
|
|
g_system->delayMillis(50);
|
|
|
|
}
|
|
|
|
_fadeOutCounter = 0;
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
if (_playing) {
|
|
|
|
stop();
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
_sfxData = readBundleSoundFile(song);
|
|
|
|
if (!_sfxData) {
|
|
|
|
warning("Unable to load soundfx module '%s'", song);
|
|
|
|
return 0;
|
|
|
|
}
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
for (int i = 0; i < NUM_INSTRUMENTS; ++i) {
|
|
|
|
_instrumentsData[i] = NULL;
|
2006-02-22 22:40:53 +00:00
|
|
|
|
2008-04-07 20:24:40 +00:00
|
|
|
char instrument[64];
|
2008-08-12 23:56:13 +00:00
|
|
|
memset(instrument, 0, 64); // Clear the data first
|
2007-05-19 12:08:41 +00:00
|
|
|
memcpy(instrument, _sfxData + 20 + i * 30, 12);
|
2008-04-07 20:24:40 +00:00
|
|
|
instrument[63] = '\0';
|
2006-02-25 00:26:14 +00:00
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
if (strlen(instrument) != 0) {
|
|
|
|
char *dot = strrchr(instrument, '.');
|
|
|
|
if (dot) {
|
|
|
|
*dot = '\0';
|
|
|
|
}
|
|
|
|
strcat(instrument, _driver->getInstrumentExtension());
|
|
|
|
_instrumentsData[i] = readBundleSoundFile(instrument);
|
|
|
|
if (!_instrumentsData[i]) {
|
|
|
|
warning("Unable to load soundfx instrument '%s'", instrument);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2007-05-13 16:11:19 +00:00
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
void PCSoundFxPlayer::play() {
|
|
|
|
debug(9, "PCSoundFxPlayer::play()");
|
|
|
|
if (_sfxData) {
|
|
|
|
for (int i = 0; i < NUM_CHANNELS; ++i) {
|
|
|
|
_instrumentsChannelTable[i] = -1;
|
|
|
|
}
|
|
|
|
_currentPos = 0;
|
|
|
|
_currentOrder = 0;
|
|
|
|
_numOrders = _sfxData[470];
|
|
|
|
_eventsDelay = (252 - _sfxData[471]) * 50 / 1060;
|
|
|
|
_updateTicksCounter = 0;
|
|
|
|
_playing = true;
|
|
|
|
}
|
2007-05-13 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
void PCSoundFxPlayer::stop() {
|
|
|
|
if (_playing || _fadeOutCounter != 0) {
|
|
|
|
_fadeOutCounter = 0;
|
|
|
|
_playing = false;
|
|
|
|
for (int i = 0; i < NUM_CHANNELS; ++i) {
|
|
|
|
_driver->stopChannel(i);
|
|
|
|
}
|
|
|
|
_driver->stopAll();
|
|
|
|
unload();
|
|
|
|
}
|
2007-05-13 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
void PCSoundFxPlayer::fadeOut() {
|
|
|
|
if (_playing) {
|
|
|
|
_fadeOutCounter = 1;
|
|
|
|
_playing = false;
|
|
|
|
}
|
2007-05-13 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
void PCSoundFxPlayer::updateCallback(void *ref) {
|
|
|
|
((PCSoundFxPlayer *)ref)->update();
|
2007-05-13 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
void PCSoundFxPlayer::update() {
|
|
|
|
if (_playing || (_fadeOutCounter != 0 && _fadeOutCounter < 100)) {
|
|
|
|
++_updateTicksCounter;
|
|
|
|
if (_updateTicksCounter > _eventsDelay) {
|
|
|
|
handleEvents();
|
|
|
|
_updateTicksCounter = 0;
|
|
|
|
}
|
2007-05-13 16:11:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
void PCSoundFxPlayer::handleEvents() {
|
|
|
|
const byte *patternData = _sfxData + 600;
|
|
|
|
const byte *orderTable = _sfxData + 472;
|
|
|
|
uint16 patternNum = orderTable[_currentOrder] * 1024;
|
|
|
|
|
|
|
|
for (int i = 0; i < 4; ++i) {
|
|
|
|
handlePattern(i, patternData + patternNum + _currentPos);
|
|
|
|
patternData += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_fadeOutCounter != 0 && _fadeOutCounter < 100) {
|
|
|
|
_fadeOutCounter += 2;
|
|
|
|
}
|
|
|
|
_currentPos += 16;
|
|
|
|
if (_currentPos >= 1024) {
|
|
|
|
_currentPos = 0;
|
|
|
|
++_currentOrder;
|
|
|
|
if (_currentOrder == _numOrders) {
|
|
|
|
_currentOrder = 0;
|
|
|
|
}
|
2007-05-13 16:11:19 +00:00
|
|
|
}
|
2007-05-19 12:08:41 +00:00
|
|
|
debug(7, "_currentOrder=%d/%d _currentPos=%d", _currentOrder, _numOrders, _currentPos);
|
2007-05-13 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
void PCSoundFxPlayer::handlePattern(int channel, const byte *patternData) {
|
|
|
|
int instrument = patternData[2] >> 4;
|
|
|
|
if (instrument != 0) {
|
|
|
|
--instrument;
|
|
|
|
if (_instrumentsChannelTable[channel] != instrument || _fadeOutCounter != 0) {
|
|
|
|
_instrumentsChannelTable[channel] = instrument;
|
|
|
|
const int volume = _sfxData[instrument] - _fadeOutCounter;
|
|
|
|
_driver->setupChannel(channel, _instrumentsData[instrument], instrument, volume);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int16 freq = (int16)READ_BE_UINT16(patternData);
|
|
|
|
if (freq > 0) {
|
|
|
|
_driver->stopChannel(channel);
|
|
|
|
_driver->setChannelFrequency(channel, freq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCSoundFxPlayer::unload() {
|
|
|
|
for (int i = 0; i < NUM_INSTRUMENTS; ++i) {
|
|
|
|
free(_instrumentsData[i]);
|
|
|
|
_instrumentsData[i] = NULL;
|
|
|
|
}
|
|
|
|
free(_sfxData);
|
|
|
|
_sfxData = NULL;
|
2007-05-13 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
2007-05-19 12:08:41 +00:00
|
|
|
|
|
|
|
PCSound::PCSound(Audio::Mixer *mixer, CineEngine *vm)
|
|
|
|
: Sound(mixer, vm) {
|
|
|
|
if (_vm->getGameType() == GType_FW) {
|
|
|
|
_soundDriver = new AdlibSoundDriverINS(_mixer);
|
|
|
|
} else {
|
|
|
|
_soundDriver = new AdlibSoundDriverADL(_mixer);
|
|
|
|
}
|
|
|
|
_player = new PCSoundFxPlayer(_soundDriver);
|
|
|
|
}
|
|
|
|
|
|
|
|
PCSound::~PCSound() {
|
|
|
|
delete _player;
|
|
|
|
delete _soundDriver;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCSound::loadMusic(const char *name) {
|
2007-12-09 13:41:59 +00:00
|
|
|
debugC(5, kCineDebugSound, "PCSound::loadMusic('%s')", name);
|
2007-05-19 12:08:41 +00:00
|
|
|
_player->load(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCSound::playMusic() {
|
2007-12-09 13:41:59 +00:00
|
|
|
debugC(5, kCineDebugSound, "PCSound::playMusic()");
|
2007-05-19 12:08:41 +00:00
|
|
|
_player->play();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCSound::stopMusic() {
|
2007-12-09 13:41:59 +00:00
|
|
|
debugC(5, kCineDebugSound, "PCSound::stopMusic()");
|
2007-05-19 12:08:41 +00:00
|
|
|
_player->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCSound::fadeOutMusic() {
|
2007-12-09 13:41:59 +00:00
|
|
|
debugC(5, kCineDebugSound, "PCSound::fadeOutMusic()");
|
2007-05-19 12:08:41 +00:00
|
|
|
_player->fadeOut();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCSound::playSound(int channel, int frequency, const uint8 *data, int size, int volumeStep, int stepCount, int volume, int repeat) {
|
2007-12-09 13:41:59 +00:00
|
|
|
debugC(5, kCineDebugSound, "PCSound::playSound() channel %d size %d", channel, size);
|
2007-05-19 12:08:41 +00:00
|
|
|
_soundDriver->playSample(data, size, channel, volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PCSound::stopSound(int channel) {
|
2007-12-09 13:41:59 +00:00
|
|
|
debugC(5, kCineDebugSound, "PCSound::stopSound() channel %d", channel);
|
2007-05-19 12:08:41 +00:00
|
|
|
_soundDriver->resetChannel(channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
PaulaSound::PaulaSound(Audio::Mixer *mixer, CineEngine *vm)
|
|
|
|
: Sound(mixer, vm) {
|
|
|
|
_moduleStream = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PaulaSound::~PaulaSound() {
|
2007-12-08 15:24:42 +00:00
|
|
|
for (int i = 0; i < NUM_CHANNELS; ++i) {
|
|
|
|
stopSound(i);
|
|
|
|
}
|
|
|
|
stopMusic();
|
2007-05-19 12:08:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PaulaSound::loadMusic(const char *name) {
|
2007-12-09 13:41:59 +00:00
|
|
|
debugC(5, kCineDebugSound, "PaulaSound::loadMusic('%s')", name);
|
2007-05-20 11:50:06 +00:00
|
|
|
if (_vm->getGameType() == GType_FW) {
|
|
|
|
// look for separate files
|
|
|
|
Common::File f;
|
|
|
|
if (f.open(name)) {
|
|
|
|
_moduleStream = Audio::makeSoundFxStream(&f, 0, _mixer->getOutputRate());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// look in bundle files
|
|
|
|
uint32 size;
|
|
|
|
byte *buf = readBundleSoundFile(name, &size);
|
|
|
|
if (buf) {
|
|
|
|
Common::MemoryReadStream s(buf, size);
|
|
|
|
_moduleStream = Audio::makeSoundFxStream(&s, readBundleSoundFile, _mixer->getOutputRate());
|
|
|
|
free(buf);
|
|
|
|
}
|
2007-05-19 12:08:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaulaSound::playMusic() {
|
2007-12-09 13:41:59 +00:00
|
|
|
debugC(5, kCineDebugSound, "PaulaSound::playMusic()");
|
2007-05-19 12:08:41 +00:00
|
|
|
_mixer->stopHandle(_moduleHandle);
|
|
|
|
if (_moduleStream) {
|
2007-12-08 15:24:42 +00:00
|
|
|
_mixer->playInputStream(Audio::Mixer::kMusicSoundType, &_moduleHandle, _moduleStream);
|
2007-05-19 12:08:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaulaSound::stopMusic() {
|
2007-12-09 13:41:59 +00:00
|
|
|
debugC(5, kCineDebugSound, "PaulaSound::stopMusic()");
|
2007-05-19 12:08:41 +00:00
|
|
|
_mixer->stopHandle(_moduleHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaulaSound::fadeOutMusic() {
|
2007-12-09 13:41:59 +00:00
|
|
|
debugC(5, kCineDebugSound, "PaulaSound::fadeOutMusic()");
|
2007-05-19 12:08:41 +00:00
|
|
|
// TODO
|
|
|
|
stopMusic();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaulaSound::playSound(int channel, int frequency, const uint8 *data, int size, int volumeStep, int stepCount, int volume, int repeat) {
|
2007-12-09 13:41:59 +00:00
|
|
|
// TODO: handle volume slides and repeat
|
|
|
|
debugC(5, kCineDebugSound, "PaulaSound::playSound() channel %d size %d", channel, size);
|
2007-12-08 15:24:42 +00:00
|
|
|
stopSound(channel);
|
|
|
|
size = MIN<int>(size - SPL_HDR_SIZE, READ_BE_UINT16(data + 4));
|
2007-12-09 13:41:59 +00:00
|
|
|
// TODO: consider skipping the header in loadSpl directly
|
2007-12-08 15:24:42 +00:00
|
|
|
if (size > 0) {
|
2007-12-09 13:41:59 +00:00
|
|
|
byte *sound = (byte *)malloc(size);
|
|
|
|
if (sound) {
|
|
|
|
memcpy(sound, data + SPL_HDR_SIZE, size);
|
|
|
|
playSoundChannel(channel, frequency, sound, size, volume);
|
2007-12-08 15:24:42 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-19 12:08:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PaulaSound::stopSound(int channel) {
|
2007-12-09 13:41:59 +00:00
|
|
|
debugC(5, kCineDebugSound, "PaulaSound::stopSound() channel %d", channel);
|
2007-05-19 12:08:41 +00:00
|
|
|
_mixer->stopHandle(_channelsTable[channel]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PaulaSound::update() {
|
2007-05-13 16:11:19 +00:00
|
|
|
// process volume slides and start sound playback
|
2007-12-09 13:41:59 +00:00
|
|
|
// TODO
|
2007-05-13 16:11:19 +00:00
|
|
|
}
|
|
|
|
|
2007-12-08 15:24:42 +00:00
|
|
|
void PaulaSound::playSoundChannel(int channel, int frequency, uint8 *data, int size, int volume) {
|
2007-05-19 12:08:41 +00:00
|
|
|
assert(frequency > 0);
|
|
|
|
frequency = PAULA_FREQ / frequency;
|
2007-12-09 13:41:59 +00:00
|
|
|
_mixer->playRaw(Audio::Mixer::kSFXSoundType, &_channelsTable[channel], data, size, frequency, Audio::Mixer::FLAG_AUTOFREE);
|
2007-12-08 15:24:42 +00:00
|
|
|
_mixer->setChannelVolume(_channelsTable[channel], volume * Audio::Mixer::kMaxChannelVolume / 63);
|
2007-05-19 12:08:41 +00:00
|
|
|
}
|
|
|
|
|
2006-02-25 00:26:14 +00:00
|
|
|
} // End of namespace Cine
|