2004-10-17 17:49:45 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2001-2006 The ScummVM project
|
2004-10-17 17:49:45 +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.
|
2004-10-17 17:49:45 +00:00
|
|
|
*
|
2006-02-11 10:05:31 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2004-10-17 17:49:45 +00:00
|
|
|
*/
|
|
|
|
|
2005-06-24 15:23:51 +00:00
|
|
|
#include "common/stdafx.h"
|
2004-10-17 17:49:45 +00:00
|
|
|
#include "sound/audiostream.h"
|
|
|
|
#include "sound/mididrv.h"
|
|
|
|
#include "sound/mixer.h"
|
|
|
|
|
|
|
|
#define FIXP_SHIFT 16
|
|
|
|
|
2006-04-29 22:33:31 +00:00
|
|
|
class MidiDriver_Emulated : public Audio::AudioStream, public MidiDriver {
|
2004-10-17 17:49:45 +00:00
|
|
|
protected:
|
|
|
|
bool _isOpen;
|
2005-05-10 23:48:48 +00:00
|
|
|
Audio::Mixer *_mixer;
|
2004-10-17 17:49:45 +00:00
|
|
|
|
|
|
|
private:
|
2005-05-10 23:17:38 +00:00
|
|
|
Common::Timer::TimerProc _timerProc;
|
2004-11-08 10:16:07 +00:00
|
|
|
void *_timerParam;
|
2004-10-17 17:49:45 +00:00
|
|
|
|
2004-11-08 10:16:07 +00:00
|
|
|
int _nextTick;
|
|
|
|
int _samplesPerTick;
|
2004-10-17 17:49:45 +00:00
|
|
|
|
|
|
|
protected:
|
2004-11-08 10:16:07 +00:00
|
|
|
virtual void generateSamples(int16 *buf, int len) = 0;
|
|
|
|
virtual void onTimer() {}
|
2004-10-17 17:49:45 +00:00
|
|
|
|
2004-10-23 20:33:39 +00:00
|
|
|
int _baseFreq;
|
|
|
|
|
2004-10-17 17:49:45 +00:00
|
|
|
public:
|
2005-05-10 23:48:48 +00:00
|
|
|
MidiDriver_Emulated(Audio::Mixer *mixer) : _mixer(mixer) {
|
2004-10-17 17:49:45 +00:00
|
|
|
_isOpen = false;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-08 10:16:07 +00:00
|
|
|
_timerProc = 0;
|
|
|
|
_timerParam = 0;
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-08 10:16:07 +00:00
|
|
|
_nextTick = 0;
|
|
|
|
_samplesPerTick = 0;
|
2004-10-23 20:33:39 +00:00
|
|
|
|
|
|
|
_baseFreq = 250;
|
2004-10-17 17:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int open() {
|
|
|
|
_isOpen = true;
|
2004-10-19 08:47:10 +00:00
|
|
|
|
2004-10-23 20:33:39 +00:00
|
|
|
int d = getRate() / _baseFreq;
|
|
|
|
int r = getRate() % _baseFreq;
|
2004-10-19 08:47:10 +00:00
|
|
|
|
|
|
|
// This is equivalent to (getRate() << FIXP_SHIFT) / BASE_FREQ
|
|
|
|
// but less prone to arithmetic overflow.
|
|
|
|
|
2004-11-08 10:16:07 +00:00
|
|
|
_samplesPerTick = (d << FIXP_SHIFT) + (r << FIXP_SHIFT) / _baseFreq;
|
2004-10-17 19:34:43 +00:00
|
|
|
return 0;
|
2004-10-17 17:49:45 +00:00
|
|
|
}
|
|
|
|
|
2005-05-10 23:17:38 +00:00
|
|
|
void setTimerCallback(void *timer_param, Common::Timer::TimerProc timer_proc) {
|
2004-11-08 10:16:07 +00:00
|
|
|
_timerProc = timer_proc;
|
|
|
|
_timerParam = timer_param;
|
2004-10-17 17:49:45 +00:00
|
|
|
}
|
|
|
|
|
2004-10-23 20:33:39 +00:00
|
|
|
uint32 getBaseTempo() { return 1000000 / _baseFreq; }
|
2004-10-17 17:49:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
// AudioStream API
|
|
|
|
int readBuffer(int16 *data, const int numSamples) {
|
|
|
|
const int stereoFactor = isStereo() ? 2 : 1;
|
|
|
|
int len = numSamples / stereoFactor;
|
|
|
|
int step;
|
2004-10-19 08:47:10 +00:00
|
|
|
|
2004-10-17 17:49:45 +00:00
|
|
|
do {
|
|
|
|
step = len;
|
2004-11-08 10:16:07 +00:00
|
|
|
if (step > (_nextTick >> FIXP_SHIFT))
|
|
|
|
step = (_nextTick >> FIXP_SHIFT);
|
2004-10-19 08:47:10 +00:00
|
|
|
|
2004-11-08 10:16:07 +00:00
|
|
|
generateSamples(data, step);
|
2005-07-30 21:11:48 +00:00
|
|
|
|
2004-11-08 10:16:07 +00:00
|
|
|
_nextTick -= step << FIXP_SHIFT;
|
|
|
|
if (!(_nextTick >> FIXP_SHIFT)) {
|
|
|
|
if (_timerProc)
|
|
|
|
(*_timerProc)(_timerParam);
|
|
|
|
onTimer();
|
|
|
|
_nextTick += _samplesPerTick;
|
2004-10-17 17:49:45 +00:00
|
|
|
}
|
|
|
|
data += step * stereoFactor;
|
|
|
|
len -= step;
|
|
|
|
} while (len);
|
|
|
|
|
|
|
|
return numSamples;
|
|
|
|
}
|
|
|
|
bool endOfData() const { return false; }
|
|
|
|
};
|