1
/* ScummVM - Graphic Adventure Engine
3
* ScummVM is the legal property of its developers, whose names
4
* are too numerous to list here. Please refer to the COPYRIGHT
5
* file distributed with this source distribution.
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2
10
* of the License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
* $URL: https://scummvm.svn.sourceforge.net/svnroot/scummvm/scummvm/tags/release-1-2-1/sound/softsynth/emumidi.h $
22
* $Id: emumidi.h 40932 2009-05-27 00:34:31Z lordhoto $
25
#ifndef SOUND_SOFTSYNTH_EMUMIDI_H
26
#define SOUND_SOFTSYNTH_EMUMIDI_H
28
#include "sound/audiostream.h"
29
#include "sound/mididrv.h"
30
#include "sound/mixer.h"
34
class MidiDriver_Emulated : public Audio::AudioStream, public MidiDriver {
38
Audio::SoundHandle _mixerSoundHandle;
41
Common::TimerManager::TimerProc _timerProc;
48
virtual void generateSamples(int16 *buf, int len) = 0;
49
virtual void onTimer() {}
54
MidiDriver_Emulated(Audio::Mixer *mixer) : _mixer(mixer) {
69
int d = getRate() / _baseFreq;
70
int r = getRate() % _baseFreq;
72
// This is equivalent to (getRate() << FIXP_SHIFT) / BASE_FREQ
73
// but less prone to arithmetic overflow.
75
_samplesPerTick = (d << FIXP_SHIFT) + (r << FIXP_SHIFT) / _baseFreq;
79
void setTimerCallback(void *timer_param, Common::TimerManager::TimerProc timer_proc) {
80
_timerProc = timer_proc;
81
_timerParam = timer_param;
84
uint32 getBaseTempo() { return 1000000 / _baseFreq; }
88
int readBuffer(int16 *data, const int numSamples) {
89
const int stereoFactor = isStereo() ? 2 : 1;
90
int len = numSamples / stereoFactor;
95
if (step > (_nextTick >> FIXP_SHIFT))
96
step = (_nextTick >> FIXP_SHIFT);
98
generateSamples(data, step);
100
_nextTick -= step << FIXP_SHIFT;
101
if (!(_nextTick >> FIXP_SHIFT)) {
103
(*_timerProc)(_timerParam);
105
_nextTick += _samplesPerTick;
107
data += step * stereoFactor;
113
bool endOfData() const { return false; }