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  MT_MACINTOSH // Apple Macintosh
64 };
65 
82  MDT_NONE = 0,
83  MDT_PCSPK = 1 << 0, // PC Speaker: Maps to MT_PCSPK and MT_PCJR
84  MDT_CMS = 1 << 1, // Creative Music System / Gameblaster: Maps to MT_CMS
85  MDT_PCJR = 1 << 2, // Tandy/PC Junior driver
86  MDT_ADLIB = 1 << 3, // AdLib: Maps to MT_ADLIB
87  MDT_C64 = 1 << 4,
88  MDT_AMIGA = 1 << 5,
89  MDT_APPLEIIGS = 1 << 6,
90  MDT_TOWNS = 1 << 7, // FM-TOWNS: Maps to MT_TOWNS
91  MDT_PC98 = 1 << 8, // PC-98: Maps to MT_PC98
92  MDT_SEGACD = 1 << 9,
93  MDT_MIDI = 1 << 10, // Real MIDI
94  MDT_PREFER_MT32 = 1 << 11, // MT-32 output is preferred
95  MDT_PREFER_GM = 1 << 12, // GM output is preferred
96  MDT_PREFER_FLUID = 1 << 13, // FluidSynth driver is preferred
97  MDT_MACINTOSH = 1 << 14,
98 
99  MDT_SUPPLIED_SOUND_FONT = 1 << 15, // Engine will supply sound font (allows checkDevice to pass if it would fail due to missing sound font)
100 };
101 
102 enum MidiDriverCheckFlags {
103  MDCK_NONE = 0,
104 
105  MDCK_SUPPLIED_SOUND_FONT = 1 << 0, // Sound font will be supplied by the engine
106  MDCK_AUTO = 1 << 1, // Driver is being checked for automatic selection (i.e. MIDI device is set to "auto")
107 };
108 
113 public:
114  static const uint8 MIDI_CHANNEL_COUNT = 16;
115  static const uint8 MIDI_RHYTHM_CHANNEL = 9;
116 
117  static const byte MIDI_COMMAND_NOTE_OFF = 0x80;
118  static const byte MIDI_COMMAND_NOTE_ON = 0x90;
119  static const byte MIDI_COMMAND_POLYPHONIC_AFTERTOUCH = 0xA0;
120  static const byte MIDI_COMMAND_CONTROL_CHANGE = 0xB0;
121  static const byte MIDI_COMMAND_PROGRAM_CHANGE = 0xC0;
122  static const byte MIDI_COMMAND_CHANNEL_AFTERTOUCH = 0xD0;
123  static const byte MIDI_COMMAND_PITCH_BEND = 0xE0;
124  static const byte MIDI_COMMAND_SYSTEM = 0xF0;
125 
126  static const byte MIDI_CONTROLLER_BANK_SELECT_MSB = 0x00;
127  static const byte MIDI_CONTROLLER_MODULATION = 0x01;
128  static const byte MIDI_CONTROLLER_DATA_ENTRY_MSB = 0x06;
129  static const byte MIDI_CONTROLLER_VOLUME = 0x07;
130  static const byte MIDI_CONTROLLER_BALANCE = 0x08;
131  static const byte MIDI_CONTROLLER_PANNING = 0x0A;
132  static const byte MIDI_CONTROLLER_EXPRESSION = 0x0B;
133  static const byte MIDI_CONTROLLER_BANK_SELECT_LSB = 0x20;
134  static const byte MIDI_CONTROLLER_DATA_ENTRY_LSB = 0x26;
135  static const byte MIDI_CONTROLLER_SUSTAIN = 0x40;
136  static const byte MIDI_CONTROLLER_PORTAMENTO = 0x41;
137  static const byte MIDI_CONTROLLER_SOSTENUTO = 0x42;
138  static const byte MIDI_CONTROLLER_SOFT = 0x43;
139  static const byte MIDI_CONTROLLER_REVERB = 0x5B;
140  static const byte MIDI_CONTROLLER_CHORUS = 0x5D;
141  static const byte MIDI_CONTROLLER_RPN_LSB = 0x64;
142  static const byte MIDI_CONTROLLER_RPN_MSB = 0x65;
143  static const byte MIDI_CONTROLLER_ALL_SOUND_OFF = 0x78;
144  static const byte MIDI_CONTROLLER_RESET_ALL_CONTROLLERS = 0x79;
145  static const byte MIDI_CONTROLLER_ALL_NOTES_OFF = 0x7B;
146  static const byte MIDI_CONTROLLER_OMNI_ON = 0x7C;
147  static const byte MIDI_CONTROLLER_OMNI_OFF = 0x7D;
148  static const byte MIDI_CONTROLLER_MONO_ON = 0x7E;
149  static const byte MIDI_CONTROLLER_POLY_ON = 0x7F;
150 
151  static const uint16 MIDI_RPN_PITCH_BEND_SENSITIVITY = 0x0000;
152  static const uint16 MIDI_RPN_MASTER_TUNING_FINE = 0x0001;
153  static const uint16 MIDI_RPN_MASTER_TUNING_COARSE = 0x0002;
154  static const uint16 MIDI_RPN_NULL = 0x7F7F;
155 
156  static const uint8 MIDI_META_END_OF_TRACK = 0x2F;
157  static const uint8 MIDI_META_SEQUENCER = 0x7F;
158 
159  static const uint16 MIDI_PITCH_BEND_DEFAULT = 0x2000;
160  static const uint8 MIDI_PANNING_DEFAULT = 0x40;
161  static const uint8 MIDI_EXPRESSION_DEFAULT = 0x7F;
162  static const uint16 MIDI_MASTER_TUNING_FINE_DEFAULT = 0x2000;
163  static const uint8 MIDI_MASTER_TUNING_COARSE_DEFAULT = 0x40;
164 
165  static const uint8 MT32_PITCH_BEND_SENSITIVITY_DEFAULT = 0x0C;
166  static const uint8 GM_PITCH_BEND_SENSITIVITY_DEFAULT = 0x02;
167 
168  static const uint8 GS_RHYTHM_FIRST_NOTE = 0x1B;
169  static const uint8 GS_RHYTHM_LAST_NOTE = 0x58;
170 
171  MidiDriver_BASE();
172 
173  virtual ~MidiDriver_BASE();
174 
181  virtual void send(uint32 b) = 0;
182 
188  virtual void send(int8 source, uint32 b) { send(b); }
189 
197  void send(byte status, byte firstOp, byte secondOp);
198 
204  void send(int8 source, byte status, byte firstOp, byte secondOp);
205 
216  virtual void sysEx(const byte *msg, uint16 length) { }
217 
230  virtual uint16 sysExNoDelay(const byte *msg, uint16 length) { sysEx(msg, length); return 0; }
231 
232  // TODO: Document this.
233  virtual void metaEvent(byte type, byte *data, uint16 length) { }
234 
240  virtual void metaEvent(int8 source, byte type, byte *data, uint16 length) { metaEvent(type, data, length); }
241 
258  virtual void stopAllNotes(bool stopSustainedNotes = false);
259 
268  virtual bool isReady(int8 source = -1) { return true; }
269 
270 protected:
271 
277 
279  uint32 _prevMillis;
280 
283 
285  void midiDumpInit();
286 
288  int midiDumpVarLength(const uint32 &delta);
289 
291  void midiDumpDelta();
292 
294  void midiDumpDo(uint32 b);
295 
297  void midiDumpSysEx(const byte *msg, uint16 length);
298 
300  void midiDumpFinish();
301 
302 };
303 
309 class MidiDriver : public MidiDriver_BASE {
310 public:
318  typedef uint32 DeviceHandle;
319 
320  enum DeviceStringType {
321  kDriverName,
322  kDriverId,
323  kDeviceName,
324  kDeviceId
325  };
326 
327  static Common::String musicType2GUIO(uint32 musicType);
328 
330  static MidiDriver *createMidi(DeviceHandle handle);
331 
333  static DeviceHandle detectDevice(int flags);
334 
336  static DeviceHandle getDeviceHandle(const Common::String &identifier);
337 
347  static bool checkDevice(DeviceHandle handle, int flags, bool quiet);
348 
350  static MusicType getMusicType(DeviceHandle handle);
351 
353  static Common::String getDeviceString(DeviceHandle handle, DeviceStringType type);
354 
356  void midiDriverCommonSend(uint32 b);
357 
359  void midiDriverCommonSysEx(const byte *msg, uint16 length);
360 
361 private:
362  // If detectDevice() detects MT32 and we have a preferred MT32 device
363  // we use this to force getMusicType() to return MT_MT32 so that we don't
364  // have to rely on the 'True Roland MT-32' config manager setting (since nobody
365  // would possibly think about activating 'True Roland MT-32' when he has set
366  // 'Music Driver' to '<default>')
367  static bool _forceTypeMT32;
368 
369 public:
370  virtual ~MidiDriver() { }
371 
372  static const byte _mt32ToGm[128];
373  static const byte _gmToMt32[128];
374 
379  enum {
380  MERR_CANNOT_CONNECT = 1,
381 // MERR_STREAMING_NOT_AVAILABLE = 2,
382  MERR_DEVICE_NOT_AVAILABLE = 3,
383  MERR_ALREADY_OPEN = 4
384  };
385 
386  enum {
387  // PROP_TIMEDIV = 1,
388  PROP_OLD_ADLIB = 2,
389  PROP_CHANNEL_MASK = 3,
390  // HACK: Not so nice, but our SCUMM AdLib code is in audio/
391  PROP_SCUMM_OPL3 = 4,
401  PROP_USER_VOLUME_SCALING = 5,
420  PROP_MIDI_DATA_REVERSE_PANNING = 6,
432  PROP_OPL_ACCURACY_MODE = 7,
452  PROP_OPL_CHANNEL_ALLOCATION_MODE = 8,
463  PROP_MILES_VERSION = 9,
477  PROP_OPL_RHYTHM_MODE_IGNORE_NOTE_OFF = 10
478  };
479 
484  virtual int open() = 0;
485 
489  virtual bool isOpen() const = 0;
490 
492  virtual void close() = 0;
493 
495  virtual uint32 property(int prop, uint32 param) { return 0; }
496 
498  static const char *getErrorName(int error_code);
499 
500  // HIGH-LEVEL SEMANTIC METHODS
501  virtual void setPitchBendRange(byte channel, uint range) {
502  send(MIDI_COMMAND_CONTROL_CHANGE | channel, MIDI_CONTROLLER_RPN_MSB, MIDI_RPN_PITCH_BEND_SENSITIVITY >> 8);
503  send(MIDI_COMMAND_CONTROL_CHANGE | channel, MIDI_CONTROLLER_RPN_LSB, MIDI_RPN_PITCH_BEND_SENSITIVITY & 0xFF);
504  send(MIDI_COMMAND_CONTROL_CHANGE | channel, MIDI_CONTROLLER_DATA_ENTRY_MSB, range); // Semi-tones
505  send(MIDI_COMMAND_CONTROL_CHANGE | channel, MIDI_CONTROLLER_DATA_ENTRY_LSB, 0); // Cents
506  send(MIDI_COMMAND_CONTROL_CHANGE | channel, MIDI_CONTROLLER_RPN_MSB, MIDI_RPN_NULL >> 8);
507  send(MIDI_COMMAND_CONTROL_CHANGE | channel, MIDI_CONTROLLER_RPN_LSB, MIDI_RPN_NULL & 0xFF);
508  }
509 
513  void sendMT32Reset();
514 
518  void sendGMReset();
519 
520  // Timing functions - MidiDriver now operates timers
521  virtual void setTimerCallback(void *timer_param, Common::TimerManager::TimerProc timer_proc) = 0;
522 
524  virtual uint32 getBaseTempo() = 0;
525 
526  // Channel allocation functions
527  virtual MidiChannel *allocateChannel() = 0;
528  virtual MidiChannel *getPercussionChannel() = 0;
529 
530  // Allow an engine to supply its own soundFont data. This stream will be destroyed after use.
531  virtual void setEngineSoundFont(Common::SeekableReadStream *soundFontData) { }
532 
533  // Does this driver accept soundFont data?
534  virtual bool acceptsSoundFontData() { return false; }
535 };
536 
537 class MidiChannel {
538 public:
539  virtual ~MidiChannel() {}
540 
541  virtual MidiDriver *device() = 0;
542  virtual byte getNumber() = 0;
543  virtual void release() = 0;
544 
545  virtual void send(uint32 b) = 0; // 4-bit channel portion is ignored
546 
547  // Regular messages
548  virtual void noteOff(byte note) = 0;
549  virtual void noteOn(byte note, byte velocity) = 0;
550  virtual void programChange(byte program) = 0;
551  virtual void pitchBend(int16 bend) = 0; // -0x2000 to +0x1FFF
552 
553  // Control Change messages
554  virtual void controlChange(byte control, byte value) = 0;
555  virtual void modulationWheel(byte value) { controlChange(MidiDriver::MIDI_CONTROLLER_MODULATION, value); }
556  virtual void volume(byte value) { controlChange(MidiDriver::MIDI_CONTROLLER_VOLUME, value); }
557  virtual void panPosition(byte value) { controlChange(MidiDriver::MIDI_CONTROLLER_PANNING, value); }
558  virtual void pitchBendFactor(byte value) = 0;
559  virtual void transpose(int8 value) {}
560  virtual void detune(int16 value) { controlChange(17, value & 0xff); }
561  virtual void priority(byte value) { }
562  virtual void sustain(bool value) { controlChange(MidiDriver::MIDI_CONTROLLER_SUSTAIN, value ? 1 : 0); }
563  virtual void effectLevel(byte value) { controlChange(MidiDriver::MIDI_CONTROLLER_REVERB, value); }
564  virtual void chorusLevel(byte value) { controlChange(MidiDriver::MIDI_CONTROLLER_CHORUS, value); }
565  virtual void allNotesOff() { controlChange(MidiDriver::MIDI_CONTROLLER_ALL_NOTES_OFF, 0); }
566 
567  // SysEx messages
568  virtual void sysEx_customInstrument(uint32 type, const byte *instr, uint32 dataSize) = 0;
569 };
571 #endif
virtual uint32 property(int prop, uint32 param)
Definition: mididrv.h:495
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:240
bool _midiDumpEnable
Definition: mididrv.h:276
uint32 DeviceHandle
Definition: mididrv.h:318
void midiDumpDelta()
MusicType
Definition: mididrv.h:44
void(* TimerProc)(void *refCon)
Definition: timer.h:42
MidiDriverFlags
Definition: mididrv.h:81
Definition: stream.h:745
virtual void stopAllNotes(bool stopSustainedNotes=false)
Definition: mididrv.h:309
virtual bool isReady(int8 source=-1)
Definition: mididrv.h:268
virtual void send(int8 source, uint32 b)
Definition: mididrv.h:188
Common::Array< byte > _midiDumpCache
Definition: mididrv.h:282
Definition: mididrv.h:112
void midiDumpSysEx(const byte *msg, uint16 length)
Definition: mididrv.h:537
uint32 _prevMillis
Definition: mididrv.h:279
virtual void sysEx(const byte *msg, uint16 length)
Definition: mididrv.h:216
void midiDumpFinish()
virtual void send(uint32 b)=0
virtual uint16 sysExNoDelay(const byte *msg, uint16 length)
Definition: mididrv.h:230