ScummVM API documentation
sfxparser_accolade.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 AGOS_SFXPARSER_ACCOLADE_H
23 #define AGOS_SFXPARSER_ACCOLADE_H
24 
25 #include "agos/drivers/accolade/adlib.h"
26 #include "agos/drivers/accolade/mt32.h"
27 
28 #include "common/mutex.h"
29 #include "common/stream.h"
30 
31 namespace AGOS {
32 
34 public:
35  // Size in bytes of MT-32 instrument data in the SFX data.
36  static const byte INSTRUMENT_SIZE_MT32 = 0xF9;
37  // Number of script ticks per second.
38  static const uint16 SCRIPT_TIMER_FREQUENCY = 292;
39  // Number of microseconds per script tick.
40  static const uint16 SCRIPT_TIMER_RATE = 1000000 / SCRIPT_TIMER_FREQUENCY;
41 
42 protected:
43  // Size in bytes of AdLib instrument data in the SFX data.
44  static const byte INSTRUMENT_SIZE_ADLIB = 0x09;
45  // Maximum number of words in an SFX script.
46  static const byte MAX_SCRIPT_SIZE = 0x30;
47  // Maximum number of simultaneous sources for OPL2.
48  static const byte OPL2_NUM_SOURCES = 2;
49  // Maximum number of simultaneous sources for OPL3.
50  static const byte OPL3_NUM_SOURCES = 4;
51 
52  // Data for a single SFX. Taken from the game's SFX bank.
53  struct SfxData {
54  // The instrument data for the used sound device (OPL or MT-32).
55  byte instrumentDefinition[INSTRUMENT_SIZE_MT32];
56  // The SFX script.
57  int16 scriptData[MAX_SCRIPT_SIZE];
58  // The size in words of the SFX script.
59  int scriptSize;
60  };
61 
62  // State data a SFX playback slot.
63  struct SfxSlot {
64  SfxSlot();
65 
66  // The data of the SFX currently played by this slot.
67  SfxData *sfxData;
68 
69  // True if this slot has been allocated to playing a SFX.
70  bool allocated;
71  // True if SFX playback is active.
72  bool active;
73  // The source used to send data to the MIDI driver.
74  int8 source;
75  // Current position in the SFX script.
76  byte scriptPos;
77 
78  // Current playback time in microseconds.
79  uint32 playTime;
80  // The timestamp of the last processed script tick.
81  uint32 lastEventTime;
82  // The last MIDI note that was sent as a note on event.
83  int16 lastPlayedNote;
84  // The current MIDI note (upper byte) and note fraction (1/256th notes;
85  // lower byte) value.
86  uint16 currentNoteFraction;
87  // True if the allocated channel on the MIDI device has been changed to
88  // the SFX instrument.
89  bool programChanged;
90 
91  // Delta to the note fraction. This is added to/subtracted from the
92  // note fraction every script tick.
93  int16 noteFractionDelta;
94  // The vibrato time. The number of script ticks it takes for the note
95  // difference to go from lowest to highest (or the other way around).
96  int16 vibratoTime;
97  // The number of script ticks that have passed since the vibrato has
98  // started.
99  int16 vibratoCounter;
100  // Vibrato delta to the note fraction. This is added to/subtracted
101  // from the note fraction every script tick.
102  int16 vibratoDelta;
103  // The number of ticks that remain before the next script event is
104  // processed.
105  int16 waitCounter;
106  // The script position at which the current loop has started.
107  byte loopStart;
108  // The number of times the looped section will be repeated (-1 for
109  // infinite loop).
110  int16 loopCounter;
111 
112  // Completely clears the SFX slot data.
113  void clear();
114  // Resets the SFX slot data as needed by the reset opcode.
115  void reset();
116  // True if the current position is at the end of the script.
117  bool atEndOfScript();
118  // Reads the next script word. Specify the opCode flag to return a
119  // valid opcode.
120  int16 readScript(bool opCode);
121  };
122 
123 public:
125  virtual ~SfxParser_Accolade();
126 
127  // Loads the specified sound effects bank (FXB file).
128  void load(Common::SeekableReadStream *in, int32 size);
129 
130  // Sets the MIDI driver that should be used to output the SFX.
131  virtual void setMidiDriver(MidiDriver_Multisource *driver) = 0;
132  // Sets the number of microseconds between timer callbacks.
133  void setTimerRate(uint32 rate);
134 
135  // Starts playback of the specified sound effect.
136  void play(uint8 sfxNumber);
137  // Stops all active SFX.
138  void stopAll();
139  // Pauses or unpauses all active SFX.
140  void pauseAll(bool paused);
141 
142  void onTimer();
143  static void timerCallback(void *data);
144 
145 protected:
146  // Stops the sound effect playing in the specified slot.
147  void stop(SfxSlot *sfxSlot);
148  // Processes the specified opcode for the specified slot.
149  void processOpCode(SfxSlot *sfxSlot, byte opCode);
150 
151  // Returns the number of sources available for SFX playback.
152  virtual byte getNumberOfSfxSources() = 0;
153  // Reads the SFX instrument data into the specified SfxData from the
154  // specified SFX bank data. This is positioned at the start of the data of
155  // a sound effect in the bank; when the function returns is should be
156  // positioned right after all instrument data for the sound effect.
157  virtual void readInstrument(SfxData *sfxData, Common::SeekableReadStream *in) = 0;
158 
159  // Loads the SFX instrument for the specified slot into the channel
160  // allocated to the sound effect. Returns true if the channel needs to be
161  // changed to the new instrument when the driver is ready.
162  virtual bool loadInstrument(SfxSlot *sfxSlot) = 0;
163  // Changes the channel allocated to the sound effect to the SFX instrument.
164  virtual void changeInstrument(SfxSlot *sfxSlot) { };
165  // Starts a note at the current note / note fraction for the slot.
166  virtual void noteOn(SfxSlot *sfxSlot);
167  // Stops the current note for the slot.
168  virtual void noteOff(SfxSlot *sfxSlot);
169  // Updates the note / note fraction for the slot.
170  virtual void updateNote(SfxSlot *sfxSlot) { };
171 
172  Common::Mutex _mutex;
173 
174  MidiDriver_Multisource *_driver;
175  uint32 _timerRate;
176 
177  // Array of SFX data loaded from the SFX bank.
178  SfxData *_sfxData;
179  // The number of SFX data loaded.
180  uint16 _numSfx;
181  // The slots available for SFX playback.
182  SfxSlot _sfxSlots[4];
183  // The slot numbers allocated to the available SFX sources. -1 if no slot
184  // is using the source.
185  int8 _sourceAllocations[4];
186 
187  // True if SFX playback is paused.
188  bool _paused;
189 };
190 
192 public:
193  SfxParser_Accolade_AdLib() : _adLibDriver(nullptr) { }
194 
195 protected:
196  void setMidiDriver(MidiDriver_Multisource *driver) override;
197  byte getNumberOfSfxSources() override;
198  void readInstrument(SfxData *sfxData, Common::SeekableReadStream *in) override;
199  bool loadInstrument(SfxSlot *sfxSlot) override;
200  void noteOn(SfxSlot *sfxSlot) override;
201  void updateNote(SfxSlot *sfxSlot) override;
202 
203  MidiDriver_Accolade_AdLib *_adLibDriver;
204 };
205 
207 public:
208  SfxParser_Accolade_MT32() : _mt32Driver(nullptr) { }
209 
210 protected:
211  void setMidiDriver(MidiDriver_Multisource *driver) override;
212  byte getNumberOfSfxSources() override;
213  void readInstrument(SfxData *sfxData, Common::SeekableReadStream *in) override;
214  bool loadInstrument(SfxSlot *sfxSlot) override;
215  void changeInstrument(SfxSlot *sfxSlot) override;
216 
217  MidiDriver_Accolade_MT32 *_mt32Driver;
218 };
219 
220 } // End of namespace AGOS
221 
222 #endif
Definition: sfxparser_accolade.h:63
Definition: mididrv_ms.h:86
Definition: stream.h:745
Definition: sfxparser_accolade.h:206
Definition: agos.h:70
Definition: mt32.h:29
Definition: mutex.h:67
Definition: sfxparser_accolade.h:53
Definition: sfxparser_accolade.h:33
Definition: sfxparser_accolade.h:191
Definition: adlib.h:29