ScummVM API documentation
mididrv.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 AUDIO_MIDIDRV_H
23 #define AUDIO_MIDIDRV_H
24 
25 #include "common/scummsys.h"
26 #include "common/str.h"
27 #include "common/stream.h"
28 #include "common/timer.h"
29 #include "common/array.h"
30 
31 class MidiChannel;
32 
44 enum MusicType {
45  MT_INVALID = -1, // Invalid output
46  MT_AUTO = 0, // Auto
47  MT_NULL, // Null
48  MT_PCSPK, // PC Speaker
49  MT_PCJR, // PCjr
50  MT_CMS, // CMS
51  MT_ADLIB, // AdLib
52  MT_C64, // C64
53  MT_AMIGA, // Amiga
54  MT_APPLEIIGS, // Apple IIGS
55  MT_TOWNS, // FM-TOWNS
56  MT_PC98, // PC98
57  MT_SEGACD, // SegaCD
58  MT_GM, // General MIDI
59  MT_MT32, // MT-32
60  MT_GS, // Roland GS
61  MT_MT540, // Casio MT-540
62  MT_CT460 // Casio CT-460 / CSM-1
63 };
64 
81  MDT_NONE = 0,
82  MDT_PCSPK = 1 << 0, // PC Speaker: Maps to MT_PCSPK and MT_PCJR
83  MDT_CMS = 1 << 1, // Creative Music System / Gameblaster: Maps to MT_CMS
84  MDT_PCJR = 1 << 2, // Tandy/PC Junior driver
85  MDT_ADLIB = 1 << 3, // AdLib: Maps to MT_ADLIB
86  MDT_C64 = 1 << 4,
87  MDT_AMIGA = 1 << 5,
88  MDT_APPLEIIGS = 1 << 6,
89  MDT_TOWNS = 1 << 7, // FM-TOWNS: Maps to MT_TOWNS
90  MDT_PC98 = 1 << 8, // PC-98: Maps to MT_PC98
91  MDT_SEGACD = 1 << 9,
92  MDT_MIDI = 1 << 10, // Real MIDI
93  MDT_PREFER_MT32 = 1 << 11, // MT-32 output is preferred
94  MDT_PREFER_GM = 1 << 12, // GM output is preferred
95  MDT_PREFER_FLUID= 1 << 13, // FluidSynth driver is preferred
96  MDT_MACINTOSH = 1 << 14
97 };
98 
103 public:
104  static const uint8 MIDI_CHANNEL_COUNT = 16;
105  static const uint8 MIDI_RHYTHM_CHANNEL = 9;
106 
107  static const byte MIDI_COMMAND_NOTE_OFF = 0x80;
108  static const byte MIDI_COMMAND_NOTE_ON = 0x90;
109  static const byte MIDI_COMMAND_POLYPHONIC_AFTERTOUCH = 0xA0;
110  static const byte MIDI_COMMAND_CONTROL_CHANGE = 0xB0;
111  static const byte MIDI_COMMAND_PROGRAM_CHANGE = 0xC0;
112  static const byte MIDI_COMMAND_CHANNEL_AFTERTOUCH = 0xD0;
113  static const byte MIDI_COMMAND_PITCH_BEND = 0xE0;
114  static const byte MIDI_COMMAND_SYSTEM = 0xF0;
115 
116  static const byte MIDI_CONTROLLER_BANK_SELECT_MSB = 0x00;
117  static const byte MIDI_CONTROLLER_MODULATION = 0x01;
118  static const byte MIDI_CONTROLLER_DATA_ENTRY_MSB = 0x06;
119  static const byte MIDI_CONTROLLER_VOLUME = 0x07;
120  static const byte MIDI_CONTROLLER_BALANCE = 0x08;
121  static const byte MIDI_CONTROLLER_PANNING = 0x0A;
122  static const byte MIDI_CONTROLLER_EXPRESSION = 0x0B;
123  static const byte MIDI_CONTROLLER_BANK_SELECT_LSB = 0x20;
124  static const byte MIDI_CONTROLLER_DATA_ENTRY_LSB = 0x26;
125  static const byte MIDI_CONTROLLER_SUSTAIN = 0x40;
126  static const byte MIDI_CONTROLLER_PORTAMENTO = 0x41;
127  static const byte MIDI_CONTROLLER_SOSTENUTO = 0x42;
128  static const byte MIDI_CONTROLLER_SOFT = 0x43;
129  static const byte MIDI_CONTROLLER_REVERB = 0x5B;
130  static const byte MIDI_CONTROLLER_CHORUS = 0x5D;
131  static const byte MIDI_CONTROLLER_RPN_LSB = 0x64;
132  static const byte MIDI_CONTROLLER_RPN_MSB = 0x65;
133  static const byte MIDI_CONTROLLER_ALL_SOUND_OFF = 0x78;
134  static const byte MIDI_CONTROLLER_RESET_ALL_CONTROLLERS = 0x79;
135  static const byte MIDI_CONTROLLER_ALL_NOTES_OFF = 0x7B;
136  static const byte MIDI_CONTROLLER_OMNI_ON = 0x7C;
137  static const byte MIDI_CONTROLLER_OMNI_OFF = 0x7D;
138  static const byte MIDI_CONTROLLER_MONO_ON = 0x7E;
139  static const byte MIDI_CONTROLLER_POLY_ON = 0x7F;
140 
141  static const uint16 MIDI_RPN_PITCH_BEND_SENSITIVITY = 0x0000;
142  static const uint16 MIDI_RPN_MASTER_TUNING_FINE = 0x0001;
143  static const uint16 MIDI_RPN_MASTER_TUNING_COARSE = 0x0002;
144  static const uint16 MIDI_RPN_NULL = 0x7F7F;
145 
146  static const uint8 MIDI_META_END_OF_TRACK = 0x2F;
147  static const uint8 MIDI_META_SEQUENCER = 0x7F;
148 
149  static const uint16 MIDI_PITCH_BEND_DEFAULT = 0x2000;
150  static const uint8 MIDI_PANNING_DEFAULT = 0x40;
151  static const uint8 MIDI_EXPRESSION_DEFAULT = 0x7F;
152  static const uint16 MIDI_MASTER_TUNING_FINE_DEFAULT = 0x2000;
153  static const uint8 MIDI_MASTER_TUNING_COARSE_DEFAULT = 0x40;
154 
155  static const uint8 MT32_PITCH_BEND_SENSITIVITY_DEFAULT = 0x0C;
156  static const uint8 GM_PITCH_BEND_SENSITIVITY_DEFAULT = 0x02;
157 
158  static const uint8 GS_RHYTHM_FIRST_NOTE = 0x1B;
159  static const uint8 GS_RHYTHM_LAST_NOTE = 0x58;
160 
161  MidiDriver_BASE();
162 
163  virtual ~MidiDriver_BASE();
164 
171  virtual void send(uint32 b) = 0;
172 
178  virtual void send(int8 source, uint32 b) { send(b); }
179 
187  void send(byte status, byte firstOp, byte secondOp);
188 
194  void send(int8 source, byte status, byte firstOp, byte secondOp);
195 
206  virtual void sysEx(const byte *msg, uint16 length) { }
207 
220  virtual uint16 sysExNoDelay(const byte *msg, uint16 length) { sysEx(msg, length); return 0; }
221 
222  // TODO: Document this.
223  virtual void metaEvent(byte type, byte *data, uint16 length) { }
224 
230  virtual void metaEvent(int8 source, byte type, byte *data, uint16 length) { metaEvent(type, data, length); }
231 
248  virtual void stopAllNotes(bool stopSustainedNotes = false);
249 
258  virtual bool isReady(int8 source = -1) { return true; }
259 
260 protected:
261 
267 
269  uint32 _prevMillis;
270 
273 
275  void midiDumpInit();
276 
278  int midiDumpVarLength(const uint32 &delta);
279 
281  void midiDumpDelta();
282 
284  void midiDumpDo(uint32 b);
285 
287  void midiDumpSysEx(const byte *msg, uint16 length);
288 
290  void midiDumpFinish();
291 
292 };
293 
299 class MidiDriver : public MidiDriver_BASE {
300 public:
308  typedef uint32 DeviceHandle;
309 
310  enum DeviceStringType {
311  kDriverName,
312  kDriverId,
313  kDeviceName,
314  kDeviceId
315  };
316 
317  static Common::String musicType2GUIO(uint32 musicType);
318 
320  static MidiDriver *createMidi(DeviceHandle handle);
321 
323  static DeviceHandle detectDevice(int flags);
324 
326  static DeviceHandle getDeviceHandle(const Common::String &identifier);
327 
329  static bool checkDevice(DeviceHandle handle);
330 
332  static MusicType getMusicType(DeviceHandle handle);
333 
335  static Common::String getDeviceString(DeviceHandle handle, DeviceStringType type);
336 
338  void midiDriverCommonSend(uint32 b);
339 
341  void midiDriverCommonSysEx(const byte *msg, uint16 length);
342 
343 private:
344  // If detectDevice() detects MT32 and we have a preferred MT32 device
345  // we use this to force getMusicType() to return MT_MT32 so that we don't
346  // have to rely on the 'True Roland MT-32' config manager setting (since nobody
347  // would possibly think about activating 'True Roland MT-32' when he has set
348  // 'Music Driver' to '<default>')
349  static bool _forceTypeMT32;
350 
351 public:
352  virtual ~MidiDriver() { }
353 
354  static const byte _mt32ToGm[128];
355  static const byte _gmToMt32[128];
356 
361  enum {
362  MERR_CANNOT_CONNECT = 1,
363 // MERR_STREAMING_NOT_AVAILABLE = 2,
364  MERR_DEVICE_NOT_AVAILABLE = 3,
365  MERR_ALREADY_OPEN = 4
366  };
367 
368  enum {
369  // PROP_TIMEDIV = 1,
370  PROP_OLD_ADLIB = 2,
371  PROP_CHANNEL_MASK = 3,
372  // HACK: Not so nice, but our SCUMM AdLib code is in audio/
373  PROP_SCUMM_OPL3 = 4,
383  PROP_USER_VOLUME_SCALING = 5,
402  PROP_MIDI_DATA_REVERSE_PANNING = 6,
414  PROP_OPL_ACCURACY_MODE = 7,
434  PROP_OPL_CHANNEL_ALLOCATION_MODE = 8,
445  PROP_MILES_VERSION = 9,
459  PROP_OPL_RHYTHM_MODE_IGNORE_NOTE_OFF = 10
460  };
461 
466  virtual int open() = 0;
467 
471  virtual bool isOpen() const = 0;
472 
474  virtual void close() = 0;
475 
477  virtual uint32 property(int prop, uint32 param) { return 0; }
478 
480  static const char *getErrorName(int error_code);
481 
482  // HIGH-LEVEL SEMANTIC METHODS
483  virtual void setPitchBendRange(byte channel, uint range) {
484  send(MIDI_COMMAND_CONTROL_CHANGE | channel, MIDI_CONTROLLER_RPN_MSB, MIDI_RPN_PITCH_BEND_SENSITIVITY >> 8);
485  send(MIDI_COMMAND_CONTROL_CHANGE | channel, MIDI_CONTROLLER_RPN_LSB, MIDI_RPN_PITCH_BEND_SENSITIVITY & 0xFF);
486  send(MIDI_COMMAND_CONTROL_CHANGE | channel, MIDI_CONTROLLER_DATA_ENTRY_MSB, range); // Semi-tones
487  send(MIDI_COMMAND_CONTROL_CHANGE | channel, MIDI_CONTROLLER_DATA_ENTRY_LSB, 0); // Cents
488  send(MIDI_COMMAND_CONTROL_CHANGE | channel, MIDI_CONTROLLER_RPN_MSB, MIDI_RPN_NULL >> 8);
489  send(MIDI_COMMAND_CONTROL_CHANGE | channel, MIDI_CONTROLLER_RPN_LSB, MIDI_RPN_NULL & 0xFF);
490  }
491 
495  void sendMT32Reset();
496 
500  void sendGMReset();
501 
502  // Timing functions - MidiDriver now operates timers
503  virtual void setTimerCallback(void *timer_param, Common::TimerManager::TimerProc timer_proc) = 0;
504 
506  virtual uint32 getBaseTempo() = 0;
507 
508  // Channel allocation functions
509  virtual MidiChannel *allocateChannel() = 0;
510  virtual MidiChannel *getPercussionChannel() = 0;
511 
512  // Allow an engine to supply its own soundFont data. This stream will be destroyed after use.
513  virtual void setEngineSoundFont(Common::SeekableReadStream *soundFontData) { }
514 
515  // Does this driver accept soundFont data?
516  virtual bool acceptsSoundFontData() { return false; }
517 };
518 
519 class MidiChannel {
520 public:
521  virtual ~MidiChannel() {}
522 
523  virtual MidiDriver *device() = 0;
524  virtual byte getNumber() = 0;
525  virtual void release() = 0;
526 
527  virtual void send(uint32 b) = 0; // 4-bit channel portion is ignored
528 
529  // Regular messages
530  virtual void noteOff(byte note) = 0;
531  virtual void noteOn(byte note, byte velocity) = 0;
532  virtual void programChange(byte program) = 0;
533  virtual void pitchBend(int16 bend) = 0; // -0x2000 to +0x1FFF
534 
535  // Control Change messages
536  virtual void controlChange(byte control, byte value) = 0;
537  virtual void modulationWheel(byte value) { controlChange(MidiDriver::MIDI_CONTROLLER_MODULATION, value); }
538  virtual void volume(byte value) { controlChange(MidiDriver::MIDI_CONTROLLER_VOLUME, value); }
539  virtual void panPosition(byte value) { controlChange(MidiDriver::MIDI_CONTROLLER_PANNING, value); }
540  virtual void pitchBendFactor(byte value) = 0;
541  virtual void transpose(int8 value) {}
542  virtual void detune(int16 value) { controlChange(17, value & 0xff); }
543  virtual void priority(byte value) { }
544  virtual void sustain(bool value) { controlChange(MidiDriver::MIDI_CONTROLLER_SUSTAIN, value ? 1 : 0); }
545  virtual void effectLevel(byte value) { controlChange(MidiDriver::MIDI_CONTROLLER_REVERB, value); }
546  virtual void chorusLevel(byte value) { controlChange(MidiDriver::MIDI_CONTROLLER_CHORUS, value); }
547  virtual void allNotesOff() { controlChange(MidiDriver::MIDI_CONTROLLER_ALL_NOTES_OFF, 0); }
548 
549  // SysEx messages
550  virtual void sysEx_customInstrument(uint32 type, const byte *instr, uint32 dataSize) = 0;
551 };
553 #endif
virtual uint32 property(int prop, uint32 param)
Definition: mididrv.h:477
void midiDumpDo(uint32 b)
Definition: str.h:59
int midiDumpVarLength(const uint32 &delta)
virtual void metaEvent(int8 source, byte type, byte *data, uint16 length)
Definition: mididrv.h:230
bool _midiDumpEnable
Definition: mididrv.h:266
uint32 DeviceHandle
Definition: mididrv.h:308
void midiDumpDelta()
MusicType
Definition: mididrv.h:44
void(* TimerProc)(void *refCon)
Definition: timer.h:42
MidiDriverFlags
Definition: mididrv.h:80
Definition: stream.h:745
virtual void stopAllNotes(bool stopSustainedNotes=false)
Definition: mididrv.h:299
virtual bool isReady(int8 source=-1)
Definition: mididrv.h:258
virtual void send(int8 source, uint32 b)
Definition: mididrv.h:178
Common::Array< byte > _midiDumpCache
Definition: mididrv.h:272
Definition: mididrv.h:102
void midiDumpSysEx(const byte *msg, uint16 length)
Definition: mididrv.h:519
uint32 _prevMillis
Definition: mididrv.h:269
virtual void sysEx(const byte *msg, uint16 length)
Definition: mididrv.h:206
void midiDumpFinish()
virtual void send(uint32 b)=0
virtual uint16 sysExNoDelay(const byte *msg, uint16 length)
Definition: mididrv.h:220