2009-05-05 21:47:12 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2021-12-26 17:47:58 +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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2009-05-05 21:47:12 +00:00
|
|
|
*
|
|
|
|
* 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
|
2021-12-26 17:47:58 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-02-18 01:34:17 +00:00
|
|
|
*
|
2009-05-05 21:47:12 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/fmopl.h"
|
2009-05-05 21:47:12 +00:00
|
|
|
|
2015-04-03 05:38:14 +00:00
|
|
|
#include "audio/mixer.h"
|
2022-01-29 16:33:29 +00:00
|
|
|
#ifdef USE_RETROWAVE
|
|
|
|
#include "audio/rwopl3.h"
|
|
|
|
#endif
|
2011-02-09 01:09:01 +00:00
|
|
|
#include "audio/softsynth/opl/dosbox.h"
|
|
|
|
#include "audio/softsynth/opl/mame.h"
|
2018-03-21 18:15:40 +00:00
|
|
|
#include "audio/softsynth/opl/nuked.h"
|
2009-05-05 21:47:12 +00:00
|
|
|
|
2009-05-12 18:42:44 +00:00
|
|
|
#include "common/config-manager.h"
|
2015-04-03 05:38:14 +00:00
|
|
|
#include "common/system.h"
|
2011-04-24 08:34:27 +00:00
|
|
|
#include "common/textconsole.h"
|
2015-05-15 01:07:46 +00:00
|
|
|
#include "common/timer.h"
|
2010-06-26 15:55:53 +00:00
|
|
|
#include "common/translation.h"
|
2009-05-12 18:42:44 +00:00
|
|
|
|
2009-05-05 21:47:12 +00:00
|
|
|
namespace OPL {
|
|
|
|
|
2015-03-17 09:23:25 +00:00
|
|
|
// Factory functions
|
|
|
|
|
|
|
|
#ifdef USE_ALSA
|
|
|
|
namespace ALSA {
|
|
|
|
OPL *create(Config::OplType type);
|
|
|
|
} // End of namespace ALSA
|
|
|
|
#endif // USE_ALSA
|
|
|
|
|
2018-02-17 11:50:19 +00:00
|
|
|
#ifdef ENABLE_OPL2LPT
|
|
|
|
namespace OPL2LPT {
|
2019-08-22 04:43:50 +00:00
|
|
|
OPL *create(Config::OplType type);
|
2018-02-17 11:50:19 +00:00
|
|
|
} // End of namespace OPL2LPT
|
|
|
|
#endif // ENABLE_OPL2LPT
|
|
|
|
|
2022-01-29 16:33:29 +00:00
|
|
|
#ifdef USE_RETROWAVE
|
|
|
|
namespace RetroWaveOPL3 {
|
|
|
|
OPL *create(Config::OplType type);
|
|
|
|
} // End of namespace RetroWaveOPL3
|
|
|
|
#endif // ENABLE_RETROWAVE_OPL3
|
|
|
|
|
2009-05-12 18:42:44 +00:00
|
|
|
// Config implementation
|
2009-05-05 21:47:12 +00:00
|
|
|
|
2009-05-12 18:42:44 +00:00
|
|
|
enum OplEmulator {
|
2009-05-12 19:03:54 +00:00
|
|
|
kAuto = 0,
|
2009-05-12 18:53:44 +00:00
|
|
|
kMame = 1,
|
2015-03-17 09:23:25 +00:00
|
|
|
kDOSBox = 2,
|
2018-03-21 18:15:40 +00:00
|
|
|
kALSA = 3,
|
2018-02-17 11:50:19 +00:00
|
|
|
kNuked = 4,
|
2019-08-22 04:43:50 +00:00
|
|
|
kOPL2LPT = 5,
|
2022-01-29 16:33:29 +00:00
|
|
|
kOPL3LPT = 6,
|
|
|
|
kRWOPL3 = 7
|
2009-05-12 18:42:44 +00:00
|
|
|
};
|
2009-05-05 21:47:12 +00:00
|
|
|
|
2010-03-08 00:54:05 +00:00
|
|
|
OPL::OPL() {
|
|
|
|
if (_hasInstance)
|
2010-09-18 10:55:16 +00:00
|
|
|
error("There are multiple OPL output instances running");
|
2010-03-08 00:54:05 +00:00
|
|
|
_hasInstance = true;
|
|
|
|
}
|
|
|
|
|
2009-05-12 18:42:44 +00:00
|
|
|
const Config::EmulatorDescription Config::_drivers[] = {
|
2009-05-12 19:03:54 +00:00
|
|
|
{ "auto", "<default>", kAuto, kFlagOpl2 | kFlagDualOpl2 | kFlagOpl3 },
|
2010-06-26 15:48:03 +00:00
|
|
|
{ "mame", _s("MAME OPL emulator"), kMame, kFlagOpl2 },
|
2009-05-12 18:42:44 +00:00
|
|
|
#ifndef DISABLE_DOSBOX_OPL
|
2010-06-26 15:48:03 +00:00
|
|
|
{ "db", _s("DOSBox OPL emulator"), kDOSBox, kFlagOpl2 | kFlagDualOpl2 | kFlagOpl3 },
|
2015-03-17 09:23:25 +00:00
|
|
|
#endif
|
2018-03-21 18:15:40 +00:00
|
|
|
#ifndef DISABLE_NUKED_OPL
|
|
|
|
{ "nuked", _s("Nuked OPL emulator"), kNuked, kFlagOpl2 | kFlagDualOpl2 | kFlagOpl3 },
|
|
|
|
#endif
|
2015-03-17 09:23:25 +00:00
|
|
|
#ifdef USE_ALSA
|
2015-03-17 12:39:44 +00:00
|
|
|
{ "alsa", _s("ALSA Direct FM"), kALSA, kFlagOpl2 | kFlagDualOpl2 | kFlagOpl3 },
|
2018-02-17 11:50:19 +00:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_OPL2LPT
|
2019-08-22 04:43:50 +00:00
|
|
|
{ "opl2lpt", _s("OPL2LPT"), kOPL2LPT, kFlagOpl2},
|
|
|
|
{ "opl3lpt", _s("OPL3LPT"), kOPL3LPT, kFlagOpl2 | kFlagOpl3 },
|
2022-01-29 16:33:29 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USE_RETROWAVE
|
|
|
|
{"rwopl3", _s("RetroWave OPL3"), kRWOPL3, kFlagOpl2 | kFlagOpl3},
|
2009-05-05 21:47:12 +00:00
|
|
|
#endif
|
2021-11-13 19:23:55 +00:00
|
|
|
{ nullptr, nullptr, 0, 0 }
|
2009-05-12 18:42:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Config::DriverId Config::parse(const Common::String &name) {
|
|
|
|
for (int i = 0; _drivers[i].name; ++i) {
|
|
|
|
if (name.equalsIgnoreCase(_drivers[i].name))
|
|
|
|
return _drivers[i].id;
|
|
|
|
}
|
|
|
|
|
2009-05-12 19:03:54 +00:00
|
|
|
return -1;
|
2009-05-05 21:47:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 22:19:01 +00:00
|
|
|
const Config::EmulatorDescription *Config::findDriver(DriverId id) {
|
|
|
|
for (int i = 0; _drivers[i].name; ++i) {
|
|
|
|
if (_drivers[i].id == id)
|
|
|
|
return &_drivers[i];
|
|
|
|
}
|
|
|
|
|
2021-11-13 19:23:55 +00:00
|
|
|
return nullptr;
|
2015-05-19 22:19:01 +00:00
|
|
|
}
|
|
|
|
|
2009-05-12 18:42:44 +00:00
|
|
|
Config::DriverId Config::detect(OplType type) {
|
|
|
|
uint32 flags = 0;
|
|
|
|
switch (type) {
|
|
|
|
case kOpl2:
|
|
|
|
flags = kFlagOpl2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kDualOpl2:
|
|
|
|
flags = kFlagDualOpl2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case kOpl3:
|
|
|
|
flags = kFlagOpl3;
|
|
|
|
break;
|
2019-11-17 08:20:01 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2009-05-12 18:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
DriverId drv = parse(ConfMan.get("opl_driver"));
|
2015-05-30 17:18:29 +00:00
|
|
|
if (drv == kAuto) {
|
|
|
|
// Since the "auto" can be explicitly set for a game, and this
|
|
|
|
// driver shows up in the GUI as "<default>", check if there is
|
|
|
|
// a global setting for it before resorting to auto-detection.
|
|
|
|
drv = parse(ConfMan.get("opl_driver", Common::ConfigManager::kApplicationDomain));
|
|
|
|
}
|
2009-05-12 18:42:44 +00:00
|
|
|
|
|
|
|
// When a valid driver is selected, check whether it supports
|
|
|
|
// the requested OPL chip.
|
2009-05-12 19:03:54 +00:00
|
|
|
if (drv != -1 && drv != kAuto) {
|
2015-05-19 22:19:01 +00:00
|
|
|
const EmulatorDescription *driverDesc = findDriver(drv);
|
2009-05-12 18:42:44 +00:00
|
|
|
// If the chip is supported, just use the driver.
|
2015-05-19 22:19:01 +00:00
|
|
|
if (!driverDesc) {
|
|
|
|
warning("The selected OPL driver %d could not be found", drv);
|
|
|
|
} else if ((flags & driverDesc->flags)) {
|
2009-05-12 18:42:44 +00:00
|
|
|
return drv;
|
2009-11-25 23:02:03 +00:00
|
|
|
} else {
|
|
|
|
// Else we will output a warning and just
|
|
|
|
// return that no valid driver is found.
|
2010-09-18 10:55:16 +00:00
|
|
|
warning("Your selected OPL driver \"%s\" does not support type %d emulation, which is requested by your game", _drivers[drv].description, type);
|
2009-11-25 23:02:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2009-05-12 18:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Detect the first matching emulator
|
2010-02-03 09:11:55 +00:00
|
|
|
drv = -1;
|
|
|
|
|
2009-05-12 18:42:44 +00:00
|
|
|
for (int i = 1; _drivers[i].name; ++i) {
|
|
|
|
if (_drivers[i].flags & flags) {
|
|
|
|
drv = _drivers[i].id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return drv;
|
|
|
|
}
|
|
|
|
|
2010-02-03 09:11:55 +00:00
|
|
|
OPL *Config::create(OplType type) {
|
|
|
|
return create(kAuto, type);
|
|
|
|
}
|
|
|
|
|
2009-05-12 18:42:44 +00:00
|
|
|
OPL *Config::create(DriverId driver, OplType type) {
|
2009-05-12 19:03:54 +00:00
|
|
|
// On invalid driver selection, we try to do some fallback detection
|
|
|
|
if (driver == -1) {
|
|
|
|
warning("Invalid OPL driver selected, trying to detect a fallback emulator");
|
|
|
|
driver = kAuto;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If autodetection is selected, we search for a matching
|
|
|
|
// driver.
|
|
|
|
if (driver == kAuto) {
|
2009-05-12 18:42:44 +00:00
|
|
|
driver = detect(type);
|
|
|
|
|
2009-05-12 19:03:54 +00:00
|
|
|
// No emulator for the specified OPL chip could
|
|
|
|
// be found, thus stop here.
|
|
|
|
if (driver == -1) {
|
|
|
|
warning("No OPL emulator available for type %d", type);
|
2021-11-13 19:23:55 +00:00
|
|
|
return nullptr;
|
2009-05-12 19:03:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (driver) {
|
2009-05-12 18:42:44 +00:00
|
|
|
case kMame:
|
|
|
|
if (type == kOpl2)
|
|
|
|
return new MAME::OPL();
|
|
|
|
else
|
2010-09-18 10:55:16 +00:00
|
|
|
warning("MAME OPL emulator only supports OPL2 emulation");
|
2021-11-13 19:23:55 +00:00
|
|
|
return nullptr;
|
2009-05-12 18:42:44 +00:00
|
|
|
|
|
|
|
#ifndef DISABLE_DOSBOX_OPL
|
|
|
|
case kDOSBox:
|
|
|
|
return new DOSBox::OPL(type);
|
|
|
|
#endif
|
|
|
|
|
2018-03-21 18:15:40 +00:00
|
|
|
#ifndef DISABLE_NUKED_OPL
|
|
|
|
case kNuked:
|
|
|
|
return new NUKED::OPL(type);
|
|
|
|
#endif
|
|
|
|
|
2015-03-17 09:23:25 +00:00
|
|
|
#ifdef USE_ALSA
|
|
|
|
case kALSA:
|
|
|
|
return ALSA::create(type);
|
|
|
|
#endif
|
|
|
|
|
2018-02-17 11:50:19 +00:00
|
|
|
#ifdef ENABLE_OPL2LPT
|
|
|
|
case kOPL2LPT:
|
2019-08-22 04:43:50 +00:00
|
|
|
if (type == kOpl2) {
|
|
|
|
return OPL2LPT::create(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
warning("OPL2LPT only supprts OPL2");
|
|
|
|
return 0;
|
|
|
|
case kOPL3LPT:
|
|
|
|
if (type == kOpl2 || type == kOpl3) {
|
|
|
|
return OPL2LPT::create(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
warning("OPL3LPT does not support dual OPL2");
|
2018-02-17 11:50:19 +00:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2022-01-29 16:33:29 +00:00
|
|
|
#ifdef USE_RETROWAVE
|
|
|
|
case kRWOPL3:
|
|
|
|
if (type == kDualOpl2) {
|
|
|
|
warning("RetroWave OPL3 does not support dual OPL2");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return RetroWaveOPL3::create(type);
|
|
|
|
#endif
|
|
|
|
|
2009-05-12 18:42:44 +00:00
|
|
|
default:
|
|
|
|
warning("Unsupported OPL emulator %d", driver);
|
|
|
|
// TODO: Maybe we should add some dummy emulator too, which just outputs
|
|
|
|
// silence as sound?
|
2021-11-13 19:23:55 +00:00
|
|
|
return nullptr;
|
2009-05-12 18:42:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 05:38:14 +00:00
|
|
|
void OPL::start(TimerCallback *callback, int timerFrequency) {
|
|
|
|
_callback.reset(callback);
|
|
|
|
startCallbacks(timerFrequency);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OPL::stop() {
|
|
|
|
stopCallbacks();
|
|
|
|
_callback.reset();
|
|
|
|
}
|
|
|
|
|
2009-05-12 18:42:44 +00:00
|
|
|
bool OPL::_hasInstance = false;
|
|
|
|
|
2015-05-15 01:07:46 +00:00
|
|
|
RealOPL::RealOPL() : _baseFreq(0), _remainingTicks(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
RealOPL::~RealOPL() {
|
|
|
|
// Stop callbacks, just in case. If it's still playing at this
|
|
|
|
// point, there's probably a bigger issue, though. The subclass
|
|
|
|
// needs to call stop() or the pointer can still use be used in
|
|
|
|
// the mixer thread at the same time.
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RealOPL::setCallbackFrequency(int timerFrequency) {
|
|
|
|
stopCallbacks();
|
|
|
|
startCallbacks(timerFrequency);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RealOPL::startCallbacks(int timerFrequency) {
|
|
|
|
_baseFreq = timerFrequency;
|
|
|
|
assert(_baseFreq > 0);
|
|
|
|
|
|
|
|
// We can't request more a timer faster than 100Hz. We'll handle this by calling
|
|
|
|
// the proc multiple times in onTimer() later on.
|
|
|
|
if (timerFrequency > kMaxFreq)
|
|
|
|
timerFrequency = kMaxFreq;
|
|
|
|
|
|
|
|
_remainingTicks = 0;
|
|
|
|
g_system->getTimerManager()->installTimerProc(timerProc, 1000000 / timerFrequency, this, "RealOPL");
|
|
|
|
}
|
|
|
|
|
|
|
|
void RealOPL::stopCallbacks() {
|
|
|
|
g_system->getTimerManager()->removeTimerProc(timerProc);
|
|
|
|
_baseFreq = 0;
|
|
|
|
_remainingTicks = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RealOPL::timerProc(void *refCon) {
|
|
|
|
static_cast<RealOPL *>(refCon)->onTimer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RealOPL::onTimer() {
|
|
|
|
uint callbacks = 1;
|
|
|
|
|
|
|
|
if (_baseFreq > kMaxFreq) {
|
|
|
|
// We run faster than our max, so run the callback multiple
|
|
|
|
// times to approximate the actual timer callback frequency.
|
2016-10-09 13:02:02 +00:00
|
|
|
uint totalTicks = _baseFreq + _remainingTicks;
|
2015-05-15 01:07:46 +00:00
|
|
|
callbacks = totalTicks / kMaxFreq;
|
|
|
|
_remainingTicks = totalTicks % kMaxFreq;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call the callback multiple times. The if is on the inside of the
|
|
|
|
// loop in case the callback removes itself.
|
|
|
|
for (uint i = 0; i < callbacks; i++)
|
|
|
|
if (_callback && _callback->isValid())
|
|
|
|
(*_callback)();
|
|
|
|
}
|
|
|
|
|
2015-04-03 05:38:14 +00:00
|
|
|
EmulatedOPL::EmulatedOPL() :
|
|
|
|
_nextTick(0),
|
|
|
|
_samplesPerTick(0),
|
2015-04-30 04:01:30 +00:00
|
|
|
_baseFreq(0),
|
|
|
|
_handle(new Audio::SoundHandle()) {
|
2015-04-03 05:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EmulatedOPL::~EmulatedOPL() {
|
|
|
|
// Stop callbacks, just in case. If it's still playing at this
|
2015-04-30 03:42:01 +00:00
|
|
|
// point, there's probably a bigger issue, though. The subclass
|
|
|
|
// needs to call stop() or the pointer can still use be used in
|
|
|
|
// the mixer thread at the same time.
|
|
|
|
stop();
|
2015-04-30 04:01:30 +00:00
|
|
|
|
|
|
|
delete _handle;
|
2015-04-03 05:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int EmulatedOPL::readBuffer(int16 *buffer, const int numSamples) {
|
|
|
|
const int stereoFactor = isStereo() ? 2 : 1;
|
|
|
|
int len = numSamples / stereoFactor;
|
|
|
|
int step;
|
|
|
|
|
|
|
|
do {
|
|
|
|
step = len;
|
|
|
|
if (step > (_nextTick >> FIXP_SHIFT))
|
|
|
|
step = (_nextTick >> FIXP_SHIFT);
|
|
|
|
|
|
|
|
generateSamples(buffer, step * stereoFactor);
|
|
|
|
|
|
|
|
_nextTick -= step << FIXP_SHIFT;
|
|
|
|
if (!(_nextTick >> FIXP_SHIFT)) {
|
|
|
|
if (_callback && _callback->isValid())
|
|
|
|
(*_callback)();
|
|
|
|
|
|
|
|
_nextTick += _samplesPerTick;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer += step * stereoFactor;
|
|
|
|
len -= step;
|
|
|
|
} while (len);
|
|
|
|
|
|
|
|
return numSamples;
|
|
|
|
}
|
|
|
|
|
|
|
|
int EmulatedOPL::getRate() const {
|
|
|
|
return g_system->getMixer()->getOutputRate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EmulatedOPL::startCallbacks(int timerFrequency) {
|
2015-04-30 03:45:25 +00:00
|
|
|
setCallbackFrequency(timerFrequency);
|
2015-04-30 04:01:30 +00:00
|
|
|
g_system->getMixer()->playStream(Audio::Mixer::kPlainSoundType, _handle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO, true);
|
2015-04-30 03:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EmulatedOPL::stopCallbacks() {
|
2015-04-30 04:01:30 +00:00
|
|
|
g_system->getMixer()->stopHandle(*_handle);
|
2015-04-30 03:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EmulatedOPL::setCallbackFrequency(int timerFrequency) {
|
2015-04-03 05:38:14 +00:00
|
|
|
_baseFreq = timerFrequency;
|
|
|
|
assert(_baseFreq != 0);
|
|
|
|
|
|
|
|
int d = getRate() / _baseFreq;
|
|
|
|
int r = getRate() % _baseFreq;
|
|
|
|
|
|
|
|
// This is equivalent to (getRate() << FIXP_SHIFT) / BASE_FREQ
|
|
|
|
// but less prone to arithmetic overflow.
|
|
|
|
|
|
|
|
_samplesPerTick = (d << FIXP_SHIFT) + (r << FIXP_SHIFT) / _baseFreq;
|
2015-04-21 04:43:43 +00:00
|
|
|
}
|
|
|
|
|
2009-10-04 21:26:33 +00:00
|
|
|
} // End of namespace OPL
|