ScummVM API documentation
sound_driver.h
1 /* ScummVM - Graphic Adventure Engine
2  *
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.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
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.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef MM_SHARED_XEEN_SOUND_DRIVER_H
23 #define MM_SHARED_XEEN_SOUND_DRIVER_H
24 
25 #include "audio/fmopl.h"
26 #include "audio/mixer.h"
27 #include "common/array.h"
28 #include "common/mutex.h"
29 #include "common/queue.h"
30 #include "common/stack.h"
31 #include "mm/shared/xeen/file.h"
32 
33 #define CHANNEL_COUNT 9
34 // interrupt is every ~13.736ms, which is ~72.8 times a second
35 #define CALLBACKS_PER_SECOND 72.8f
36 
37 namespace OPL {
38  class OPL;
39 }
40 
41 namespace MM {
42 namespace Shared {
43 namespace Xeen {
44 
45 enum MusicCommand {
46  STOP_SONG = 0, RESTART_SONG = 1, SET_VOLUME = 0x100, GET_STATUS = 0xFFE0
47 };
48 
49 class SoundDriver;
50 
51 typedef bool (SoundDriver:: *CommandFn)(const byte *&srcP, byte param);
52 
56 class SoundDriver {
57 protected:
58  struct Subroutine {
59  const byte *_returnP;
60  const byte *_jumpP;
61  Subroutine() : _returnP(nullptr), _jumpP(nullptr) {
62  }
63  Subroutine(const byte *returnP, const byte *endP) :
64  _returnP(returnP), _jumpP(endP) {
65  }
66  };
67  struct Channel {
68  bool _changeFrequency;
69  int _freqCtrChange;
70  int _freqChange;
71  int _freqCtr;
72  byte _volume;
73  byte _totalLevel;
74  bool _isFx;
75  uint _frequency;
76  Channel() : _changeFrequency(false), _freqCtr(0), _freqCtrChange(0),
77  _freqChange(0), _volume(0), _totalLevel(0), _frequency(0), _isFx(false) {
78  }
79  };
80  enum StreamType {
81  stMUSIC,
82  stFX,
83 
84  stLAST
85  };
86  class Stream {
87  public:
88  Stream() {
89  }
90  Stream(const CommandFn *commands) : _playing(false), _countdownTimer(0), _dataPtr(nullptr), _startPtr(nullptr), _commands(commands) {
91  }
92 
93  bool _playing;
94  int _countdownTimer;
95  const byte *_dataPtr;
96  const byte *_startPtr;
97  const CommandFn *_commands;
98  };
99 
100 private:
101  static const CommandFn FX_COMMANDS[16];
102  static const CommandFn MUSIC_COMMANDS[16];
103 private:
104  Common::Stack<Subroutine> _musSubroutines, _fxSubroutines;
105  uint _frameCtr;
106 private:
112  bool command(const byte *&srcP);
113 
114  Stream *tickStream();
115 protected:
116  Common::Array<Channel> _channels;
117  Stream _streams[stLAST];
118 
119 protected:
123  void execute();
124 
125  // Music commands (with some also used by FX)
126  virtual bool musCallSubroutine(const byte *&srcP, byte param);
127  virtual bool musSetCountdown(const byte *&srcP, byte param);
128  virtual bool musSetInstrument(const byte *&srcP, byte param) = 0;
129  virtual bool cmdNoOperation(const byte *&srcP, byte param);
130  virtual bool musSetPitchWheel(const byte *&srcP, byte param) = 0;
131  virtual bool musSkipWord(const byte *&srcP, byte param);
132  virtual bool musSetPanning(const byte *&srcP, byte param) = 0;
133  virtual bool musFade(const byte *&srcP, byte param) = 0;
134  virtual bool musStartNote(const byte *&srcP, byte param) = 0;
135  virtual bool musSetVolume(const byte *&srcP, byte param) = 0;
136  virtual bool musInjectMidi(const byte *&srcP, byte param) = 0;
137  virtual bool musPlayInstrument(const byte *&srcP, byte param) = 0;
138  virtual bool cmdFreezeFrequency(const byte *&srcP, byte param) = 0;
139  virtual bool cmdChangeFrequency(const byte *&srcP, byte param) = 0;
140  virtual bool musEndSubroutine(const byte *&srcP, byte param);
141 
142  // FX commands
143  virtual bool fxCallSubroutine(const byte *&srcP, byte param);
144  virtual bool fxSetCountdown(const byte *&srcP, byte param);
145  virtual bool fxSetInstrument(const byte *&srcP, byte param) = 0;
146  virtual bool fxSetVolume(const byte *&srcP, byte param) = 0;
147  virtual bool fxMidiReset(const byte *&srcP, byte param) = 0;
148  virtual bool fxMidiDword(const byte *&srcP, byte param) = 0;
149  virtual bool fxSetPanning(const byte *&srcP, byte param) = 0;
150  virtual bool fxChannelOff(const byte *&srcP, byte param) = 0;
151  virtual bool fxFade(const byte *&srcP, byte param) = 0;
152  virtual bool fxStartNote(const byte *&srcP, byte param) = 0;
153  virtual bool fxInjectMidi(const byte *&srcP, byte param) = 0;
154  virtual bool fxPlayInstrument(const byte *&srcP, byte param) = 0;
155  virtual bool fxEndSubroutine(const byte *&srcP, byte param);
156 
160  virtual void pausePostProcess() = 0;
161 
165  virtual void resetFX() = 0;
166 public:
170  SoundDriver();
171 
175  virtual ~SoundDriver();
176 
180  virtual void playFX(uint effectId, const byte *data);
181 
185  void stopFX(bool force = false);
186 
190  virtual void playSong(const byte *data);
191 
195  virtual int songCommand(uint commandId, byte musicVolume = 0, byte sfxVolume = 0);
196 
200  bool isPlaying() const {
201  return _streams[stMUSIC]._playing;
202  }
203 
207  virtual void sysExMessage(const byte *&data) = 0;
208 };
209 
210 } // namespace Xeen
211 } // namespace Shared
212 } // namespace MM
213 
214 #endif
Definition: array.h:52
Definition: sound_driver.h:58
Definition: sound_driver.h:67
Definition: detection.h:27
Definition: sound_driver.h:86
bool isPlaying() const
Definition: sound_driver.h:200
Definition: fmopl.h:35
Definition: sound_driver.h:56
Definition: stack.h:102