diff options
Diffstat (limited to 'engines/cruise/sound.cpp')
-rw-r--r-- | engines/cruise/sound.cpp | 114 |
1 files changed, 44 insertions, 70 deletions
diff --git a/engines/cruise/sound.cpp b/engines/cruise/sound.cpp index 0b0fab8c4a..f57435f4f7 100644 --- a/engines/cruise/sound.cpp +++ b/engines/cruise/sound.cpp @@ -108,7 +108,7 @@ struct VolumeEntry { int adjusted; }; -class AdLibSoundDriver : public PCSoundDriver, Audio::AudioStream { +class AdLibSoundDriver : public PCSoundDriver { public: AdLibSoundDriver(Audio::Mixer *mixer); virtual ~AdLibSoundDriver(); @@ -118,14 +118,8 @@ public: 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 onTimer(); void setupInstrument(const byte *data, int channel); void setupInstrument(const AdLibSoundInstrument *ins, int channel); void loadRegisterInstrument(const byte *data, AdLibRegisterSoundInstrument *reg); @@ -135,10 +129,8 @@ public: void adjustVolume(int channel, int volume); protected: - FM_OPL *_opl; - int _sampleRate; + OPL::OPL *_opl; Audio::Mixer *_mixer; - Audio::SoundHandle _soundHandle; byte _vibrato; VolumeEntry _channelsVolumeTable[5]; @@ -302,8 +294,9 @@ void PCSoundDriver::syncSounds() { AdLibSoundDriver::AdLibSoundDriver(Audio::Mixer *mixer) : _mixer(mixer) { - _sampleRate = _mixer->getOutputRate(); - _opl = makeAdLibOPL(_sampleRate); + _opl = OPL::Config::create(); + if (!_opl || !_opl->init()) + error("Failed to create OPL"); for (int i = 0; i < 5; ++i) { _channelsVolumeTable[i].original = 0; @@ -311,15 +304,15 @@ AdLibSoundDriver::AdLibSoundDriver(Audio::Mixer *mixer) } memset(_instrumentsTable, 0, sizeof(_instrumentsTable)); initCard(); - _mixer->playStream(Audio::Mixer::kPlainSoundType, &_soundHandle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO, true); _musicVolume = ConfMan.getBool("music_mute") ? 0 : MIN(255, ConfMan.getInt("music_volume")); _sfxVolume = ConfMan.getBool("sfx_mute") ? 0 : MIN(255, ConfMan.getInt("sfx_volume")); + + _opl->start(new Common::Functor0Mem<void, AdLibSoundDriver>(this, &AdLibSoundDriver::onTimer), 50); } AdLibSoundDriver::~AdLibSoundDriver() { - _mixer->stopHandle(_soundHandle); - OPLDestroy(_opl); + delete _opl; } void AdLibSoundDriver::syncSounds() { @@ -368,70 +361,51 @@ void AdLibSoundDriver::stopChannel(int channel) { channel = 6; } if (ins->mode == 0 || channel == 6) { - OPLWriteReg(_opl, 0xB0 | channel, 0); + _opl->writeReg(0xB0 | channel, 0); } if (ins->mode != 0) { _vibrato &= ~(1 << (10 - ins->channel)); - OPLWriteReg(_opl, 0xBD, _vibrato); + _opl->writeReg(0xBD, _vibrato); } } void AdLibSoundDriver::stopAll() { for (int i = 0; i < 18; ++i) - OPLWriteReg(_opl, 0x40 | _operatorsTable[i], 63); + _opl->writeReg(0x40 | _operatorsTable[i], 63); for (int i = 0; i < 9; ++i) - OPLWriteReg(_opl, 0xB0 | i, 0); - - OPLWriteReg(_opl, 0xBD, 0); -} + _opl->writeReg(0xB0 | i, 0); -int AdLibSoundDriver::readBuffer(int16 *buffer, const int numSamples) { - update(buffer, numSamples); - return numSamples; + _opl->writeReg(0xBD, 0); } void AdLibSoundDriver::initCard() { _vibrato = 0x20; - OPLWriteReg(_opl, 0xBD, _vibrato); - OPLWriteReg(_opl, 0x08, 0x40); + _opl->writeReg(0xBD, _vibrato); + _opl->writeReg(0x08, 0x40); static const int oplRegs[] = { 0x40, 0x60, 0x80, 0x20, 0xE0 }; for (int i = 0; i < 9; ++i) { - OPLWriteReg(_opl, 0xB0 | i, 0); + _opl->writeReg(0xB0 | i, 0); } for (int i = 0; i < 9; ++i) { - OPLWriteReg(_opl, 0xC0 | i, 0); + _opl->writeReg(0xC0 | i, 0); } for (int j = 0; j < 5; j++) { for (int i = 0; i < 18; ++i) { - OPLWriteReg(_opl, oplRegs[j] | _operatorsTable[i], 0); + _opl->writeReg(oplRegs[j] | _operatorsTable[i], 0); } } - OPLWriteReg(_opl, 1, 0x20); - OPLWriteReg(_opl, 1, 0); + _opl->writeReg(1, 0x20); + _opl->writeReg(1, 0); } -void AdLibSoundDriver::update(int16 *buf, int len) { - static int samplesLeft = 0; - while (len != 0) { - int count = samplesLeft; - if (count > len) { - count = len; - } - samplesLeft -= count; - len -= count; - YM3812UpdateOne(_opl, buf, count); - if (samplesLeft == 0) { - if (_upCb) { - (*_upCb)(_upRef); - } - samplesLeft = _sampleRate / 50; - } - buf += count; +void AdLibSoundDriver::onTimer() { + if (_upCb) { + (*_upCb)(_upRef); } } @@ -457,32 +431,32 @@ void AdLibSoundDriver::setupInstrument(const AdLibSoundInstrument *ins, int chan if (ins->mode == 0 || ins->channel == 6) { reg = &ins->regMod; - OPLWriteReg(_opl, 0x20 | mod, reg->vibrato); + _opl->writeReg(0x20 | mod, reg->vibrato); if (reg->freqMod) { tmp = reg->outputLevel & 0x3F; } else { tmp = (63 - (reg->outputLevel & 0x3F)) * _channelsVolumeTable[channel].adjusted; tmp = 63 - (2 * tmp + 127) / (2 * 127); } - OPLWriteReg(_opl, 0x40 | mod, tmp | (reg->keyScaling << 6)); - OPLWriteReg(_opl, 0x60 | mod, reg->attackDecay); - OPLWriteReg(_opl, 0x80 | mod, reg->sustainRelease); + _opl->writeReg(0x40 | mod, tmp | (reg->keyScaling << 6)); + _opl->writeReg(0x60 | mod, reg->attackDecay); + _opl->writeReg(0x80 | mod, reg->sustainRelease); if (ins->mode != 0) { - OPLWriteReg(_opl, 0xC0 | ins->channel, reg->feedbackStrength); + _opl->writeReg(0xC0 | ins->channel, reg->feedbackStrength); } else { - OPLWriteReg(_opl, 0xC0 | channel, reg->feedbackStrength); + _opl->writeReg(0xC0 | channel, reg->feedbackStrength); } - OPLWriteReg(_opl, 0xE0 | mod, ins->waveSelectMod); + _opl->writeReg(0xE0 | mod, ins->waveSelectMod); } reg = &ins->regCar; - OPLWriteReg(_opl, 0x20 | car, reg->vibrato); + _opl->writeReg(0x20 | car, reg->vibrato); tmp = (63 - (reg->outputLevel & 0x3F)) * _channelsVolumeTable[channel].adjusted; 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); + _opl->writeReg(0x40 | car, tmp | (reg->keyScaling << 6)); + _opl->writeReg(0x60 | car, reg->attackDecay); + _opl->writeReg(0x80 | car, reg->sustainRelease); + _opl->writeReg(0xE0 | car, ins->waveSelectCar); } void AdLibSoundDriver::loadRegisterInstrument(const byte *data, AdLibRegisterSoundInstrument *reg) { @@ -551,15 +525,15 @@ void AdLibSoundDriverADL::setChannelFrequency(int channel, int frequency) { } freq = _freqTable[note % 12]; - OPLWriteReg(_opl, 0xA0 | channel, freq); + _opl->writeReg(0xA0 | channel, freq); freq = ((note / 12) << 2) | ((freq & 0x300) >> 8); if (ins->mode == 0) { freq |= 0x20; } - OPLWriteReg(_opl, 0xB0 | channel, freq); + _opl->writeReg(0xB0 | channel, freq); if (ins->mode != 0) { _vibrato |= 1 << (10 - channel); - OPLWriteReg(_opl, 0xBD, _vibrato); + _opl->writeReg(0xBD, _vibrato); } } @@ -570,11 +544,11 @@ void AdLibSoundDriverADL::playSample(const byte *data, int size, int channel, in setupInstrument(data, channel); AdLibSoundInstrument *ins = &_instrumentsTable[channel]; if (ins->mode != 0 && ins->channel == 6) { - OPLWriteReg(_opl, 0xB0 | channel, 0); + _opl->writeReg(0xB0 | channel, 0); } if (ins->mode != 0) { _vibrato &= ~(1 << (10 - ins->channel)); - OPLWriteReg(_opl, 0xBD, _vibrato); + _opl->writeReg(0xBD, _vibrato); } if (ins->mode != 0) { channel = ins->channel; @@ -589,15 +563,15 @@ void AdLibSoundDriverADL::playSample(const byte *data, int size, int channel, in note = ins->amDepth; } int freq = _freqTable[note % 12]; - OPLWriteReg(_opl, 0xA0 | channel, freq); + _opl->writeReg(0xA0 | channel, freq); freq = ((note / 12) << 2) | ((freq & 0x300) >> 8); if (ins->mode == 0) { freq |= 0x20; } - OPLWriteReg(_opl, 0xB0 | channel, freq); + _opl->writeReg(0xB0 | channel, freq); if (ins->mode != 0) { _vibrato |= 1 << (10 - channel); - OPLWriteReg(_opl, 0xBD, _vibrato); + _opl->writeReg(0xBD, _vibrato); } } |