REminiscence/mixer.cpp

218 lines
4.7 KiB
C++
Raw Permalink Normal View History

2016-03-20 17:00:00 +01:00
/*
* REminiscence - Flashback interpreter
2019-10-27 17:00:00 +01:00
* Copyright (C) 2005-2019 Gregory Montoir (cyx@users.sourceforge.net)
2015-08-02 18:00:00 +02:00
*/
#include "mixer.h"
#include "systemstub.h"
2016-03-20 17:00:00 +01:00
#include "util.h"
2015-08-02 18:00:00 +02:00
2023-03-31 15:23:51 +02:00
Mixer::Mixer(FileSystem *fs, SystemStub *stub, int midiDriver)
: _stub(stub), _musicType(MT_NONE), _cpc(this, fs), _mod(this, fs), _ogg(this, fs), _prf(this, fs, midiDriver), _sfx(this) {
2015-08-02 18:00:00 +02:00
_musicTrack = -1;
2019-10-27 17:00:00 +01:00
_backgroundMusicType = MT_NONE;
2015-08-02 18:00:00 +02:00
}
void Mixer::init() {
2023-03-31 15:23:51 +02:00
for (int i = 0; i < NUM_CHANNELS; ++i) {
_channels[i].active = false;
}
2015-08-02 18:00:00 +02:00
_premixHook = 0;
_stub->startAudio(Mixer::mixCallback, this);
}
void Mixer::free() {
setPremixHook(0, 0);
stopAll();
_stub->stopAudio();
}
void Mixer::setPremixHook(PremixHook premixHook, void *userData) {
debug(DBG_SND, "Mixer::setPremixHook()");
LockAudioStack las(_stub);
_premixHook = premixHook;
_premixHookData = userData;
}
2019-10-27 17:00:00 +01:00
void Mixer::play(const uint8_t *data, uint32_t len, uint16_t freq, uint8_t volume) {
2015-08-02 18:00:00 +02:00
debug(DBG_SND, "Mixer::play(%d, %d)", freq, volume);
LockAudioStack las(_stub);
MixerChannel *ch = 0;
for (int i = 0; i < NUM_CHANNELS; ++i) {
MixerChannel *cur = &_channels[i];
if (cur->active) {
2019-10-27 17:00:00 +01:00
if (cur->chunk.data == data) {
2015-08-02 18:00:00 +02:00
cur->chunkPos = 0;
2021-09-05 18:00:00 +02:00
cur->volume = volume;
2015-08-02 18:00:00 +02:00
return;
}
} else {
ch = cur;
break;
}
}
if (ch) {
ch->active = true;
ch->volume = volume;
2019-10-27 17:00:00 +01:00
ch->chunk.data = data;
ch->chunk.len = len;
2015-08-02 18:00:00 +02:00
ch->chunkPos = 0;
ch->chunkInc = (freq << FRAC_BITS) / _stub->getOutputSampleRate();
}
}
2019-10-27 17:00:00 +01:00
bool Mixer::isPlaying(const uint8_t *data) const {
2015-08-02 18:00:00 +02:00
debug(DBG_SND, "Mixer::isPlaying");
LockAudioStack las(_stub);
for (int i = 0; i < NUM_CHANNELS; ++i) {
const MixerChannel *ch = &_channels[i];
2019-10-27 17:00:00 +01:00
if (ch->active && ch->chunk.data == data) {
2015-08-02 18:00:00 +02:00
return true;
}
}
return false;
}
uint32_t Mixer::getSampleRate() const {
return _stub->getOutputSampleRate();
}
void Mixer::stopAll() {
debug(DBG_SND, "Mixer::stopAll()");
LockAudioStack las(_stub);
for (uint8_t i = 0; i < NUM_CHANNELS; ++i) {
_channels[i].active = false;
}
}
static bool isMusicSfx(int num) {
return (num >= 68 && num <= 75);
}
2023-04-15 02:44:11 +02:00
void Mixer::playMusic(int num, int tempo) {
debug(DBG_SND, "Mixer::playMusic(%d, %d)", num, tempo);
2021-09-05 18:00:00 +02:00
int trackNum = -1;
if (num == 1) { // menu screen
trackNum = 2;
} else if (num > MUSIC_TRACK) {
trackNum = num - MUSIC_TRACK;
}
if (trackNum != -1 && trackNum != _musicTrack) {
if (_ogg.playTrack(trackNum)) {
2021-05-01 18:00:00 +02:00
_backgroundMusicType = _musicType = MT_OGG;
2021-09-05 18:00:00 +02:00
_musicTrack = trackNum;
2015-08-02 18:00:00 +02:00
return;
}
2021-09-05 18:00:00 +02:00
if (_cpc.playTrack(trackNum)) {
2019-10-27 17:00:00 +01:00
_backgroundMusicType = _musicType = MT_CPC;
2021-09-05 18:00:00 +02:00
_musicTrack = trackNum;
2015-08-02 18:00:00 +02:00
return;
}
}
2019-10-27 17:00:00 +01:00
if ((_musicType == MT_OGG || _musicType == MT_CPC) && isMusicSfx(num)) { // do not play level action music with background music
2016-05-09 18:00:00 +02:00
return;
}
2015-08-02 18:00:00 +02:00
if (isMusicSfx(num)) { // level action sequence
_sfx.play(num);
2016-05-09 18:00:00 +02:00
if (_sfx._playing) {
_musicType = MT_SFX;
}
2015-08-02 18:00:00 +02:00
} else { // cutscene
2023-04-15 02:44:11 +02:00
_mod.play(num, tempo);
2016-05-09 18:00:00 +02:00
if (_mod._playing) {
_musicType = MT_MOD;
2023-03-31 15:23:51 +02:00
return;
}
if (g_options.use_prf_music) {
_prf.play(num);
if (_prf._playing) {
_musicType = MT_PRF;
return;
}
2016-05-09 18:00:00 +02:00
}
2015-08-02 18:00:00 +02:00
}
}
void Mixer::stopMusic() {
debug(DBG_SND, "Mixer::stopMusic()");
switch (_musicType) {
case MT_NONE:
break;
case MT_MOD:
_mod.stop();
break;
case MT_OGG:
2016-05-09 18:00:00 +02:00
_ogg.pauseTrack();
2015-08-02 18:00:00 +02:00
break;
2023-03-31 15:23:51 +02:00
case MT_PRF:
_prf.stop();
break;
2015-08-02 18:00:00 +02:00
case MT_SFX:
_sfx.stop();
break;
2019-10-27 17:00:00 +01:00
case MT_CPC:
_cpc.pauseTrack();
break;
2015-08-02 18:00:00 +02:00
}
_musicType = MT_NONE;
2023-04-15 02:44:11 +02:00
if (_musicTrack > 2) { // do not resume menu music
2019-10-27 17:00:00 +01:00
switch (_backgroundMusicType) {
case MT_OGG:
_ogg.resumeTrack();
_musicType = MT_OGG;
break;
case MT_CPC:
_cpc.resumeTrack();
_musicType = MT_CPC;
break;
default:
break;
}
2015-08-02 18:00:00 +02:00
}
}
2019-10-27 17:00:00 +01:00
static const bool kUseNr = false;
2019-10-27 17:00:00 +01:00
static void nr(int16_t *buf, int len) {
static int prev = 0;
for (int i = 0; i < len; ++i) {
const int vnr = buf[i] >> 1;
buf[i] = vnr + prev;
prev = vnr;
}
}
2016-05-09 18:00:00 +02:00
void Mixer::mix(int16_t *out, int len) {
2015-08-02 18:00:00 +02:00
if (_premixHook) {
2016-05-09 18:00:00 +02:00
if (!_premixHook(_premixHookData, out, len)) {
2015-08-02 18:00:00 +02:00
_premixHook = 0;
_premixHookData = 0;
}
}
for (uint8_t i = 0; i < NUM_CHANNELS; ++i) {
MixerChannel *ch = &_channels[i];
if (ch->active) {
for (int pos = 0; pos < len; ++pos) {
2023-04-15 02:44:11 +02:00
const uint32_t cpos = ch->chunkPos >> FRAC_BITS;
if (cpos >= ch->chunk.len) {
2015-08-02 18:00:00 +02:00
ch->active = false;
break;
}
2023-04-15 02:44:11 +02:00
const int sample8 = ch->chunk.getPCM(cpos) * ch->volume / Mixer::MAX_VOLUME;
const int sample16 = S8_to_S16(sample8);
out[2 * pos] = ADDC_S16(out[2 * pos], sample16);
out[2 * pos + 1] = ADDC_S16(out[2 * pos + 1], sample16);
2015-08-02 18:00:00 +02:00
ch->chunkPos += ch->chunkInc;
}
}
}
2019-10-27 17:00:00 +01:00
if (kUseNr) {
2023-04-15 02:44:11 +02:00
nr(out, len * 2); // stereo
2019-10-27 17:00:00 +01:00
}
2015-08-02 18:00:00 +02:00
}
2016-05-09 18:00:00 +02:00
void Mixer::mixCallback(void *param, int16_t *buf, int len) {
2015-08-02 18:00:00 +02:00
((Mixer *)param)->mix(buf, len);
}