2007-05-30 21:56:52 +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.
|
2003-09-24 06:56:30 +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
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2003-09-24 06:56:30 +00:00
|
|
|
*
|
2006-02-11 09:55:41 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2003-09-24 06:56:30 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-09-19 08:40:12 +00:00
|
|
|
|
2003-10-03 18:33:57 +00:00
|
|
|
#include "scumm/player_mod.h"
|
2003-12-24 00:25:18 +00:00
|
|
|
#include "sound/mixer.h"
|
|
|
|
#include "sound/rate.h"
|
2003-10-03 18:33:57 +00:00
|
|
|
|
|
|
|
namespace Scumm {
|
2003-09-24 06:56:30 +00:00
|
|
|
|
2006-10-21 12:44:10 +00:00
|
|
|
Player_MOD::Player_MOD(Audio::Mixer *mixer) {
|
2003-09-24 06:56:30 +00:00
|
|
|
int i;
|
2006-10-21 12:44:10 +00:00
|
|
|
_mixer = mixer;
|
2004-11-27 15:58:18 +00:00
|
|
|
_samplerate = _mixer->getOutputRate();
|
2003-09-24 06:56:30 +00:00
|
|
|
_mixamt = 0;
|
|
|
|
_mixpos = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < MOD_MAXCHANS; i++) {
|
|
|
|
_channels[i].id = 0;
|
|
|
|
_channels[i].vol = 0;
|
|
|
|
_channels[i].freq = 0;
|
|
|
|
_channels[i].input = NULL;
|
2008-02-12 03:26:05 +00:00
|
|
|
_channels[i].ctr = 0;
|
|
|
|
_channels[i].pos = 0;
|
2003-09-24 06:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_playproc = NULL;
|
|
|
|
_playparam = NULL;
|
|
|
|
|
2007-02-20 18:50:17 +00:00
|
|
|
_mixer->playInputStream(Audio::Mixer::kPlainSoundType, &_soundHandle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, false, true);
|
2003-09-24 06:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Player_MOD::~Player_MOD() {
|
2007-02-20 18:50:17 +00:00
|
|
|
_mixer->stopHandle(_soundHandle);
|
2003-09-24 06:56:30 +00:00
|
|
|
for (int i = 0; i < MOD_MAXCHANS; i++) {
|
|
|
|
if (!_channels[i].id)
|
|
|
|
continue;
|
|
|
|
delete _channels[i].input;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-28 21:24:02 +00:00
|
|
|
void Player_MOD::setMusicVolume(int vol) {
|
2003-09-24 06:56:30 +00:00
|
|
|
_maxvol = vol;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player_MOD::setUpdateProc(ModUpdateProc *proc, void *param, int freq) {
|
|
|
|
_playproc = proc;
|
|
|
|
_playparam = param;
|
|
|
|
_mixamt = _samplerate / freq;
|
|
|
|
}
|
|
|
|
void Player_MOD::clearUpdateProc() {
|
|
|
|
_playproc = NULL;
|
|
|
|
_playparam = NULL;
|
|
|
|
_mixamt = 0;
|
|
|
|
}
|
|
|
|
|
2003-12-28 00:19:56 +00:00
|
|
|
void Player_MOD::startChannel(int id, void *data, int size, int rate, uint8 vol, int loopStart, int loopEnd, int8 pan) {
|
2003-09-24 06:56:30 +00:00
|
|
|
int i;
|
|
|
|
if (id == 0)
|
|
|
|
error("player_mod - attempted to start channel id 0");
|
|
|
|
|
|
|
|
for (i = 0; i < MOD_MAXCHANS; i++) {
|
|
|
|
if (!_channels[i].id)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == MOD_MAXCHANS) {
|
|
|
|
warning("player_mod - too many music channels playing (%i max)",MOD_MAXCHANS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_channels[i].id = id;
|
|
|
|
_channels[i].vol = vol;
|
|
|
|
_channels[i].pan = pan;
|
|
|
|
_channels[i].freq = rate;
|
2008-02-12 03:26:05 +00:00
|
|
|
_channels[i].ctr = 0;
|
2007-03-02 15:26:22 +00:00
|
|
|
_channels[i].input = Audio::makeLinearInputStream((const byte*)data, size, rate, Audio::Mixer::FLAG_AUTOFREE | (loopStart != loopEnd ? Audio::Mixer::FLAG_LOOP : 0), loopStart, loopEnd);
|
2008-02-12 03:26:05 +00:00
|
|
|
// read the first sample
|
|
|
|
_channels[i].input->readBuffer(&_channels[i].pos, 1);
|
2003-09-24 06:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Player_MOD::stopChannel(int id) {
|
|
|
|
if (id == 0)
|
|
|
|
error("player_mod - attempted to stop channel id 0");
|
|
|
|
for (int i = 0; i < MOD_MAXCHANS; i++) {
|
|
|
|
if (_channels[i].id == id) {
|
|
|
|
delete _channels[i].input;
|
|
|
|
_channels[i].input = NULL;
|
|
|
|
_channels[i].id = 0;
|
|
|
|
_channels[i].vol = 0;
|
|
|
|
_channels[i].freq = 0;
|
2008-02-12 03:26:05 +00:00
|
|
|
_channels[i].ctr = 0;
|
|
|
|
_channels[i].pos = 0;
|
2003-09-24 06:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Player_MOD::setChannelVol(int id, uint8 vol) {
|
|
|
|
if (id == 0)
|
|
|
|
error("player_mod - attempted to set volume for channel id 0");
|
|
|
|
for (int i = 0; i < MOD_MAXCHANS; i++) {
|
|
|
|
if (_channels[i].id == id) {
|
|
|
|
_channels[i].vol = vol;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player_MOD::setChannelPan(int id, int8 pan) {
|
|
|
|
if (id == 0)
|
|
|
|
error("player_mod - attempted to set pan for channel id 0");
|
|
|
|
for (int i = 0; i < MOD_MAXCHANS; i++) {
|
|
|
|
if (_channels[i].id == id) {
|
|
|
|
_channels[i].pan = pan;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Player_MOD::setChannelFreq(int id, int freq) {
|
|
|
|
if (id == 0)
|
|
|
|
error("player_mod - attempted to set frequency for channel id 0");
|
|
|
|
for (int i = 0; i < MOD_MAXCHANS; i++) {
|
|
|
|
if (_channels[i].id == id) {
|
2008-02-12 03:26:05 +00:00
|
|
|
if (freq > 31400) // this is about as high as WinUAE goes
|
|
|
|
freq = 31400; // can't easily verify on my own Amiga
|
2003-09-24 06:56:30 +00:00
|
|
|
_channels[i].freq = freq;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-27 15:58:18 +00:00
|
|
|
void Player_MOD::do_mix(int16 *data, uint len) {
|
2003-09-24 06:56:30 +00:00
|
|
|
int i;
|
|
|
|
int dpos = 0;
|
|
|
|
uint dlen = 0;
|
|
|
|
memset(data, 0, 2 * len * sizeof(int16));
|
|
|
|
while (len) {
|
|
|
|
if (_playproc) {
|
|
|
|
dlen = _mixamt - _mixpos;
|
|
|
|
if (!_mixpos)
|
|
|
|
_playproc(_playparam);
|
|
|
|
if (dlen <= len) {
|
|
|
|
_mixpos = 0;
|
|
|
|
len -= dlen;
|
|
|
|
} else {
|
2008-10-11 02:25:55 +00:00
|
|
|
_mixpos = len;
|
2003-09-24 06:56:30 +00:00
|
|
|
dlen = len;
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dlen = len;
|
|
|
|
len = 0;
|
|
|
|
}
|
2008-02-12 09:34:38 +00:00
|
|
|
for (i = 0; i < MOD_MAXCHANS; i++) {
|
2003-09-24 06:56:30 +00:00
|
|
|
if (_channels[i].id) {
|
2005-05-11 00:01:44 +00:00
|
|
|
Audio::st_volume_t vol_l = (127 - _channels[i].pan) * _channels[i].vol / 127;
|
|
|
|
Audio::st_volume_t vol_r = (127 + _channels[i].pan) * _channels[i].vol / 127;
|
2008-02-12 09:34:38 +00:00
|
|
|
for (uint j = 0; j < dlen; j++) {
|
2008-02-12 03:26:05 +00:00
|
|
|
// simple linear resample, unbuffered
|
|
|
|
int delta = (uint32)(_channels[i].freq * 0x10000) / _samplerate;
|
|
|
|
uint16 cfrac = ~_channels[i].ctr & 0xFFFF;
|
|
|
|
if (_channels[i].ctr + delta < 0x10000)
|
|
|
|
cfrac = delta;
|
|
|
|
_channels[i].ctr += delta;
|
|
|
|
int32 cpos = _channels[i].pos * cfrac / 0x10000;
|
2008-02-12 09:34:38 +00:00
|
|
|
while (_channels[i].ctr >= 0x10000) {
|
|
|
|
if (_channels[i].input->readBuffer(&_channels[i].pos, 1) != 1) { // out of data
|
2008-02-12 03:26:05 +00:00
|
|
|
stopChannel(_channels[i].id);
|
|
|
|
goto skipchan; // exit 2 loops at once
|
|
|
|
}
|
|
|
|
_channels[i].ctr -= 0x10000;
|
|
|
|
if (_channels[i].ctr > 0x10000)
|
|
|
|
cpos += _channels[i].pos;
|
2008-02-12 09:34:38 +00:00
|
|
|
else
|
|
|
|
cpos += (int32)(_channels[i].pos * (_channels[i].ctr & 0xFFFF)) / 0x10000;
|
2008-02-12 03:26:05 +00:00
|
|
|
}
|
|
|
|
int16 pos = 0;
|
|
|
|
// if too many samples play in a row, the calculation below will overflow and clip
|
|
|
|
// so try and split it up into pieces it can manage comfortably
|
2008-02-12 09:34:38 +00:00
|
|
|
while (cpos < -0x8000) {
|
2008-02-12 03:26:05 +00:00
|
|
|
pos -= 0x80000000 / delta;
|
|
|
|
cpos += 0x8000;
|
|
|
|
}
|
2008-02-12 09:34:38 +00:00
|
|
|
while (cpos > 0x7FFF) {
|
2008-02-12 03:26:05 +00:00
|
|
|
pos += 0x7FFF0000 / delta;
|
|
|
|
cpos -= 0x7FFF;
|
|
|
|
}
|
|
|
|
pos += cpos * 0x10000 / delta;
|
|
|
|
Audio::clampedAdd(data[(dpos + j) * 2 + 0], pos * vol_l / Audio::Mixer::kMaxMixerVolume);
|
|
|
|
Audio::clampedAdd(data[(dpos + j) * 2 + 1], pos * vol_r / Audio::Mixer::kMaxMixerVolume);
|
|
|
|
}
|
2003-09-24 06:56:30 +00:00
|
|
|
}
|
2008-02-12 03:26:05 +00:00
|
|
|
skipchan: ; // channel ran out of data
|
|
|
|
}
|
2003-09-24 06:56:30 +00:00
|
|
|
dpos += dlen;
|
|
|
|
}
|
|
|
|
}
|
2003-10-03 18:33:57 +00:00
|
|
|
|
|
|
|
} // End of namespace Scumm
|