ScummVM API documentation
adlib_ms.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_ADLIB_MS_H
23 #define AUDIO_ADLIB_MS_H
24 
25 #include "audio/mididrv_ms.h"
26 #include "audio/fmopl.h"
27 
31 enum OplInstrumentRhythmType {
32  RHYTHM_TYPE_UNDEFINED,
33  RHYTHM_TYPE_HI_HAT,
34  RHYTHM_TYPE_CYMBAL,
35  RHYTHM_TYPE_TOM_TOM,
36  RHYTHM_TYPE_SNARE_DRUM,
37  RHYTHM_TYPE_BASS_DRUM
38 };
39 
48  uint8 freqMultMisc;
52  uint8 level;
56  uint8 decayAttack;
65 
71  bool isEmpty();
72 };
73 
84 
92 
103 
110  uint8 rhythmNote;
116  OplInstrumentRhythmType rhythmType;
117 
123  bool isEmpty();
129  uint8 getNumberOfOperators();
138  OplInstrumentOperatorDefinition &getOperatorDefinition(uint8 operatorNum);
139 };
140 
141 #include "common/pack-start.h" // START STRUCT PACKING
142 
154  uint8 frequencyMultiplier;
155  uint8 feedback; // ignored for operator 1
156  uint8 attack;
157  uint8 sustain;
158  uint8 envelopeGainType; // 0x00: not sustained, >= 0x01: sustained
159  uint8 decay;
160  uint8 release;
161  uint8 level;
162  uint8 amplitudeModulation; // 0x00: off, >= 0x01: on
163  uint8 vibrato; // 0x00: off, >= 0x01: on
164  uint8 keyScalingRate; // 0x00: low, >= 0x01: high
165  uint8 connection; // 0x00: additive, >= 0x01: FM; ignored for operator 1
166 
176  void toOplInstrumentOperatorDefinition(OplInstrumentOperatorDefinition &operatorDef, uint8 waveformSelect);
177 } PACKED_STRUCT;
178 
192 
198 
203  uint8 waveformSelect1;
204 
212  void toOplInstrumentDefinition(OplInstrumentDefinition &instrumentDef);
213 } PACKED_STRUCT;
214 
215 #include "common/pack-end.h" // END STRUCT PACKING
216 
251 public:
263  ACCURACY_MODE_GM
264  };
265 
279  ALLOCATION_MODE_STATIC
280  };
281 
299  INSTRUMENT_WRITE_MODE_PROGRAM_CHANGE
300  };
301 
306  NOTE_SELECT_MODE_0,
307  NOTE_SELECT_MODE_1
308  };
309 
321  MODULATION_DEPTH_HIGH
322  };
323 
335  VIBRATO_DEPTH_HIGH
336  };
337 
341  static const uint8 OPL2_NUM_CHANNELS = 9;
342  static const uint8 OPL3_NUM_CHANNELS = 18;
347  static const uint8 MELODIC_CHANNELS_OPL2[9];
352  static const uint8 MELODIC_CHANNELS_OPL2_RHYTHM[6];
357  static const uint8 MELODIC_CHANNELS_OPL3[18];
362  static const uint8 MELODIC_CHANNELS_OPL3_RHYTHM[15];
366  static const uint8 OPL_NUM_RHYTHM_INSTRUMENTS = 5;
371  static const uint8 OPL_RHYTHM_INSTRUMENT_CHANNELS[OPL_NUM_RHYTHM_INSTRUMENTS];
372 
376  static const uint8 OPL_REGISTER_TEST = 0x01;
377  static const uint8 OPL_REGISTER_TIMER1 = 0x02;
378  static const uint8 OPL_REGISTER_TIMER2 = 0x03;
379  static const uint8 OPL_REGISTER_TIMERCONTROL = 0x04;
380 
384  static const uint8 OPL_REGISTER_NOTESELECT_CSM = 0x08;
385  static const uint8 OPL_REGISTER_RHYTHM = 0xBD;
386 
390  static const uint8 OPL_REGISTER_BASE_FREQMULT_MISC = 0x20;
391  static const uint8 OPL_REGISTER_BASE_LEVEL = 0x40;
392  static const uint8 OPL_REGISTER_BASE_DECAY_ATTACK = 0x60;
393  static const uint8 OPL_REGISTER_BASE_RELEASE_SUSTAIN = 0x80;
394  static const uint8 OPL_REGISTER_BASE_WAVEFORMSELECT = 0xE0;
395 
399  static const uint8 OPL_REGISTER_BASE_FNUMLOW = 0xA0;
400  static const uint8 OPL_REGISTER_BASE_FNUMHIGH_BLOCK_KEYON = 0xB0;
401  static const uint8 OPL_REGISTER_BASE_CONNECTION_FEEDBACK_PANNING = 0xC0;
402 
406  static const uint16 OPL3_REGISTER_CONNECTIONSELECT = 0x104;
407  static const uint16 OPL3_REGISTER_NEW = 0x105;
408 
412  static const uint16 OPL_REGISTER_SET_2_OFFSET = 0x100;
413 
417  static const uint8 OPL_REGISTER_RHYTHM_OFFSETS[];
418 
422  static const uint8 OPL_MASK_LEVEL = 0x3F;
423  static const uint8 OPL_MASK_FNUMHIGH_BLOCK = 0x1F;
424  static const uint8 OPL_MASK_KEYON = 0x20;
425  static const uint8 OPL_MASK_PANNING = 0x30;
426 
430  static const uint8 OPL_PANNING_CENTER = 0x30;
431  static const uint8 OPL_PANNING_LEFT = 0x10;
432  static const uint8 OPL_PANNING_RIGHT = 0x20;
433 
437  static OplInstrumentDefinition OPL_INSTRUMENT_BANK[];
441  static OplInstrumentDefinition OPL_RHYTHM_BANK[];
442 
443 protected:
447  static const uint8 OPL_LEVEL_DEFAULT = 0x3F;
448 
452  static const uint8 OPL_MIDI_PANNING_LEFT_LIMIT = 0x2F;
456  static const uint8 OPL_MIDI_PANNING_RIGHT_LIMIT = 0x51;
457 
462  static const uint16 OPL_NOTE_FREQUENCIES[];
466  static const uint8 OPL_VOLUME_LOOKUP[];
467 
472  uint8 program;
473  uint8 channelPressure;
474  uint16 pitchBend; // 14 bit value; 0x2000 is neutral
475 
476  uint8 modulation;
477  uint8 volume;
478  uint8 panning; // 0x40 is center
479  uint8 expression;
480  bool sustain;
481  uint16 rpn; // Two 7 bit values stored in 8 bits each
482 
483  uint8 pitchBendSensitivity; // Semitones
484  uint8 pitchBendSensitivityCents;
485  uint16 masterTuningFine; // 14 bit value; 0x2000 is neutral
486  uint8 masterTuningCoarse; // Semitones; 0x40 is neutral
487 
489 
493  void init();
494  };
495 
499  struct ActiveNote {
510 
514  uint8 note;
518  uint8 velocity;
523  uint8 channel;
528  uint8 source;
529 
537  uint8 oplNote;
543  uint16 oplFrequency;
549 
561 
568 
569  ActiveNote();
570 
574  void init();
575  };
576 
580  struct InstrumentInfo {
585  uint8 oplNote;
594  };
595 
596 public:
605  static bool detectOplType(OPL::Config::OplType oplType);
606 
617 
625  int open() override;
626  bool isOpen() const override;
627  void close() override;
628  uint32 property(int prop, uint32 param) override;
629  uint32 getBaseTempo() override;
635  MidiChannel *allocateChannel() override;
641  MidiChannel *getPercussionChannel() override;
642 
644  void send(int8 source, uint32 b) override;
645  void sysEx(const byte *msg, uint16 length) override;
646  void metaEvent(int8 source, byte type, byte *data, uint16 length) override;
647  void stopAllNotes(bool stopSustainedNotes = false) override;
648 
649  void stopAllNotes(uint8 source, uint8 channel) override;
650  void deinitSource(uint8 source) override;
651 
652 protected:
653  void applySourceVolume(uint8 source) override;
654 
658  virtual void initOpl();
659 
668  virtual void noteOff(uint8 channel, uint8 note, uint8 velocity, uint8 source);
677  virtual void noteOn(uint8 channel, uint8 note, uint8 velocity, uint8 source);
688  virtual void polyAftertouch(uint8 channel, uint8 note, uint8 pressure, uint8 source);
698  virtual void controlChange(uint8 channel, uint8 controller, uint8 value, uint8 source);
706  virtual void programChange(uint8 channel, uint8 program, uint8 source);
716  virtual void channelAftertouch(uint8 channel, uint8 pressure, uint8 source);
727  virtual void pitchBend(uint8 channel, uint8 pitchBendLsb, uint8 pitchBendMsb, uint8 source);
728 
738  virtual void modulation(uint8 channel, uint8 modulation, uint8 source);
752  virtual void dataEntry(uint8 channel, uint8 dataMsb, uint8 dataLsb, uint8 source);
760  virtual void volume(uint8 channel, uint8 volume, uint8 source);
770  virtual void panning(uint8 channel, uint8 panning, uint8 source);
778  virtual void expression(uint8 channel, uint8 expression, uint8 source);
786  virtual void sustain(uint8 channel, uint8 sustain, uint8 source);
801  virtual void registeredParameterNumber(uint8 channel, uint8 rpnMsb, uint8 rpnLsb, uint8 source);
813  virtual void allSoundOff(uint8 channel, uint8 source);
829  virtual void resetAllControllers(uint8 channel, uint8 source);
838  virtual void allNotesOff(uint8 channel, uint8 source);
839 
849  virtual void applyControllerDefaults(uint8 source);
850 
860  virtual void recalculateFrequencies(uint8 channel, uint8 source);
871  virtual void recalculateVolumes(uint8 channel, uint8 source);
872 
885  virtual InstrumentInfo determineInstrument(uint8 channel, uint8 source, uint8 note);
908  virtual uint8 allocateOplChannel(uint8 channel, uint8 source, uint8 instrumentId);
916  void determineMelodicChannels();
936  virtual uint16 calculateFrequency(uint8 channel, uint8 source, uint8 note);
951  virtual int32 calculatePitchBend(uint8 channel, uint8 source, uint16 oplFrequency);
974  virtual uint8 calculateVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
996  virtual uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
1008  virtual uint8 calculatePanning(uint8 channel, uint8 source);
1009 
1022  virtual void setRhythmMode(bool rhythmMode);
1023 
1043  uint16 determineOperatorRegisterOffset(uint8 oplChannel, uint8 operatorNum, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED, bool fourOperator = false);
1054  uint16 determineChannelRegisterOffset(uint8 oplChannel, bool fourOperator = false);
1062  void writeInstrument(uint8 oplChannel, InstrumentInfo instrument);
1077  void writeKeyOff(uint8 oplChannel, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED, bool forceWrite = false);
1083  void writeRhythm(bool forceWrite = false);
1097  virtual void writeVolume(uint8 oplChannel, uint8 operatorNum, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED);
1108  virtual void writePanning(uint8 oplChannel, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED);
1120  virtual void writeFrequency(uint8 oplChannel, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED);
1121 
1134  void writeRegister(uint16 reg, uint8 value, bool forceWrite = false);
1135 
1136  // The type of OPL chip to use.
1137  OPL::Config::OplType _oplType;
1138  // The OPL emulator / hardware interface.
1139  OPL::OPL *_opl;
1140 
1141  // True if the driver has been successfully opened.
1142  bool _isOpen;
1143  // The number of timer callbacks per second.
1144  int _timerFrequency;
1145  // Controls the behavior for calculating note frequency and volume.
1146  AccuracyMode _accuracyMode;
1147  // Controls the OPL channel allocation behavior.
1148  ChannelAllocationMode _allocationMode;
1149  // Controls when the instrument definitions are written.
1150  InstrumentWriteMode _instrumentWriteMode;
1151  // Controls response to rhythm note off events when rhythm mode is active.
1152  bool _rhythmModeIgnoreNoteOffs;
1153 
1154  // The default MIDI channel volume (set when opening the driver).
1155  uint8 _defaultChannelVolume;
1156 
1157  // OPL global settings. Set these, then call oplInit or open to apply the
1158  // new values.
1159  NoteSelectMode _noteSelect;
1160  ModulationDepth _modulationDepth;
1161  VibratoDepth _vibratoDepth;
1162  // Current OPL rhythm mode setting. Use setRhythmMode to set and activate.
1163  bool _rhythmMode;
1164 
1165  // Pointer to the melodic instrument definitions.
1166  OplInstrumentDefinition *_instrumentBank;
1167  // Pointer to the rhythm instrument definitions.
1168  OplInstrumentDefinition *_rhythmBank;
1169  // The MIDI note value of the first rhythm instrument in the bank.
1170  uint8 _rhythmBankFirstNote;
1171  // The MIDI note value of the last rhythm instrument in the bank.
1172  uint8 _rhythmBankLastNote;
1173 
1174  // The current MIDI controller values for each MIDI channel and source.
1175  MidiChannelControlData _controlData[MAXIMUM_SOURCES][MIDI_CHANNEL_COUNT];
1176  // The active note data for each OPL channel.
1177  ActiveNote _activeNotes[OPL3_NUM_CHANNELS];
1178  // The active note data for the OPL rhythm instruments.
1179  ActiveNote _activeRhythmNotes[5];
1180  // The OPL channel allocated to each MIDI channel and source; 0xFF if a
1181  // MIDI channel has no OPL channel allocated. Note that this is only used by
1182  // the static channel allocation mode.
1183  uint8 _channelAllocations[MAXIMUM_SOURCES][MIDI_CHANNEL_COUNT];
1184  // Array containing the numbers of the available melodic channels.
1185  const uint8 *_melodicChannels;
1186  // The number of available melodic channels (length of _melodicChannels).
1187  uint8 _numMelodicChannels;
1188  // The amount of notes played since the driver was opened / reset.
1189  uint32 _noteCounter;
1190 
1191  // Factor to convert a frequency in Hertz to the format used by the OPL
1192  // registers (F - num).
1193  float _oplFrequencyConversionFactor;
1194  // The values last written to each OPL register.
1195  uint8 _shadowRegisters[0x200];
1196 
1197  Common::Mutex _allocationMutex; // For operations on channel allocations
1198  Common::Mutex _activeNotesMutex; // For operations on active notes
1199 };
1200 
1201 #endif
OplInstrumentDefinition * instrumentDef
Definition: adlib_ms.h:589
uint8 keyScalingLevel
Definition: adlib_ms.h:153
uint8 oplNote
Definition: adlib_ms.h:537
uint8 connectionFeedback1
Definition: adlib_ms.h:102
uint8 instrumentType
Definition: adlib_ms.h:187
AccuracyMode
Definition: adlib_ms.h:255
Definition: mididrv_ms.h:86
uint8 note
Definition: adlib_ms.h:514
OplInstrumentOperatorDefinition operator0
Definition: adlib_ms.h:88
uint8 oplNote
Definition: adlib_ms.h:585
uint8 rhythmVoiceNumber
Definition: adlib_ms.h:191
Definition: adlib_ms.h:147
Definition: adlib_ms.h:183
uint8 instrumentId
Definition: adlib_ms.h:556
uint8 rhythmNote
Definition: adlib_ms.h:110
Definition: adlib_ms.h:250
uint8 instrumentId
Definition: adlib_ms.h:593
uint8 waveformSelect0
Definition: adlib_ms.h:202
uint8 connectionFeedback0
Definition: adlib_ms.h:98
uint8 decayAttack
Definition: adlib_ms.h:56
uint8 level
Definition: adlib_ms.h:52
OplType
Definition: fmopl.h:58
OplInstrumentRhythmType rhythmType
Definition: adlib_ms.h:116
uint32 noteCounterValue
Definition: adlib_ms.h:548
NoteSelectMode
Definition: adlib_ms.h:305
uint8 channel
Definition: adlib_ms.h:523
InstrumentWriteMode
Definition: adlib_ms.h:285
Definition: mutex.h:67
uint8 freqMultMisc
Definition: adlib_ms.h:48
uint8 waveformSelect
Definition: adlib_ms.h:64
ChannelAllocationMode
Definition: adlib_ms.h:269
bool noteSustained
Definition: adlib_ms.h:509
Definition: mididrv.h:520
void send(uint32 b) override
uint8 releaseSustain
Definition: adlib_ms.h:60
uint8 velocity
Definition: adlib_ms.h:518
OplInstrumentDefinition * instrumentDef
Definition: adlib_ms.h:560
VibratoDepth
Definition: adlib_ms.h:327
uint16 oplFrequency
Definition: adlib_ms.h:543
bool channelAllocated
Definition: adlib_ms.h:567
bool fourOperator
Definition: adlib_ms.h:83
Definition: adlib_ms.h:79
AdLibBnkInstrumentOperatorDefinition operator0
Definition: adlib_ms.h:196
ModulationDepth
Definition: adlib_ms.h:313
Definition: adlib_ms.h:43
bool noteActive
Definition: adlib_ms.h:504
Definition: fmopl.h:115
uint8 source
Definition: adlib_ms.h:528