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 
221  uint8 o0FreqMultMisc;
222  uint8 o1FreqMultMisc;
223  uint8 o0Level;
224  uint8 o1Level;
225  uint8 o0DecayAttack;
226  uint8 o1DecayAttack;
227  uint8 o0ReleaseSustain;
228  uint8 o1ReleaseSustain;
229  uint8 o0WaveformSelect;
230  uint8 o1WaveformSelect;
231  uint8 connectionFeedback;
235  uint8 rhythmType;
239  int8 transpose;
240  uint8 rhythmNote;
241  uint8 padding1;
242  uint8 padding2;
243 
251  void toOplInstrumentDefinition(OplInstrumentDefinition &instrumentDef);
252 } PACKED_STRUCT;
253 
254 #include "common/pack-end.h" // END STRUCT PACKING
255 
290 public:
302  ACCURACY_MODE_GM
303  };
304 
318  ALLOCATION_MODE_STATIC
319  };
320 
343  INSTRUMENT_WRITE_MODE_FIRST_NOTE_ON
344  };
345 
350  NOTE_SELECT_MODE_0,
351  NOTE_SELECT_MODE_1
352  };
353 
365  MODULATION_DEPTH_HIGH
366  };
367 
379  VIBRATO_DEPTH_HIGH
380  };
381 
385  static const uint8 OPL2_NUM_CHANNELS = 9;
386  static const uint8 OPL3_NUM_CHANNELS = 18;
391  static const uint8 MELODIC_CHANNELS_OPL2[9];
396  static const uint8 MELODIC_CHANNELS_OPL2_RHYTHM[6];
401  static const uint8 MELODIC_CHANNELS_OPL3[18];
406  static const uint8 MELODIC_CHANNELS_OPL3_RHYTHM[15];
410  static const uint8 OPL_NUM_RHYTHM_INSTRUMENTS = 5;
415  static const uint8 OPL_RHYTHM_INSTRUMENT_CHANNELS[OPL_NUM_RHYTHM_INSTRUMENTS];
416 
420  static const uint8 OPL_REGISTER_TEST = 0x01;
421  static const uint8 OPL_REGISTER_TIMER1 = 0x02;
422  static const uint8 OPL_REGISTER_TIMER2 = 0x03;
423  static const uint8 OPL_REGISTER_TIMERCONTROL = 0x04;
424 
428  static const uint8 OPL_REGISTER_NOTESELECT_CSM = 0x08;
429  static const uint8 OPL_REGISTER_RHYTHM = 0xBD;
430 
434  static const uint8 OPL_REGISTER_BASE_FREQMULT_MISC = 0x20;
435  static const uint8 OPL_REGISTER_BASE_LEVEL = 0x40;
436  static const uint8 OPL_REGISTER_BASE_DECAY_ATTACK = 0x60;
437  static const uint8 OPL_REGISTER_BASE_RELEASE_SUSTAIN = 0x80;
438  static const uint8 OPL_REGISTER_BASE_WAVEFORMSELECT = 0xE0;
439 
443  static const uint8 OPL_REGISTER_BASE_FNUMLOW = 0xA0;
444  static const uint8 OPL_REGISTER_BASE_FNUMHIGH_BLOCK_KEYON = 0xB0;
445  static const uint8 OPL_REGISTER_BASE_CONNECTION_FEEDBACK_PANNING = 0xC0;
446 
450  static const uint16 OPL3_REGISTER_CONNECTIONSELECT = 0x104;
451  static const uint16 OPL3_REGISTER_NEW = 0x105;
452 
456  static const uint16 OPL_REGISTER_SET_2_OFFSET = 0x100;
457 
461  static const uint8 OPL_REGISTER_RHYTHM_OFFSETS[];
462 
466  static const uint8 OPL_MASK_LEVEL = 0x3F;
467  static const uint8 OPL_MASK_FNUMHIGH_BLOCK = 0x1F;
468  static const uint8 OPL_MASK_KEYON = 0x20;
469  static const uint8 OPL_MASK_PANNING = 0x30;
470 
474  static const uint8 OPL_PANNING_CENTER = 0x30;
475  static const uint8 OPL_PANNING_LEFT = 0x10;
476  static const uint8 OPL_PANNING_RIGHT = 0x20;
477 
481  static OplInstrumentDefinition OPL_INSTRUMENT_BANK[];
485  static OplInstrumentDefinition OPL_RHYTHM_BANK[];
486 
487 protected:
491  static const uint8 OPL_LEVEL_DEFAULT = 0x3F;
492 
496  static const uint8 OPL_MIDI_PANNING_LEFT_LIMIT = 0x2F;
500  static const uint8 OPL_MIDI_PANNING_RIGHT_LIMIT = 0x51;
501 
506  static const uint16 OPL_NOTE_FREQUENCIES[];
510  static const uint8 OPL_VOLUME_LOOKUP[];
511 
516  uint8 program;
517  uint8 channelPressure;
518  uint16 pitchBend; // 14 bit value; 0x2000 is neutral
519 
520  uint8 modulation;
521  uint8 volume;
522  uint8 panning; // 0x40 is center
523  uint8 expression;
524  bool sustain;
525  uint16 rpn; // Two 7 bit values stored in 8 bits each
526 
527  uint8 pitchBendSensitivity; // Semitones
528  uint8 pitchBendSensitivityCents;
529  uint16 masterTuningFine; // 14 bit value; 0x2000 is neutral
530  uint8 masterTuningCoarse; // Semitones; 0x40 is neutral
531 
533 
537  void init();
538  };
539 
543  struct ActiveNote {
554 
558  uint8 note;
562  uint8 velocity;
567  uint8 channel;
572  uint8 source;
573 
581  uint8 oplNote;
587  uint16 oplFrequency;
593 
610 
617 
618  ActiveNote();
619 
623  void init();
624  };
625 
629  struct InstrumentInfo {
634  uint8 oplNote;
643  };
644 
645 public:
654  static bool detectOplType(OPL::Config::OplType oplType);
655 
666 
674  int open() override;
675  bool isOpen() const override;
676  void close() override;
677  uint32 property(int prop, uint32 param) override;
678  uint32 getBaseTempo() override;
684  MidiChannel *allocateChannel() override;
690  MidiChannel *getPercussionChannel() override;
691 
693  void send(int8 source, uint32 b) override;
694  void sysEx(const byte *msg, uint16 length) override;
695  void metaEvent(int8 source, byte type, byte *data, uint16 length) override;
696  void stopAllNotes(bool stopSustainedNotes = false) override;
697 
698  void stopAllNotes(uint8 source, uint8 channel) override;
699  void deinitSource(uint8 source) override;
700 
701 protected:
702  void applySourceVolume(uint8 source) override;
703 
707  virtual void initOpl();
708 
717  virtual void noteOff(uint8 channel, uint8 note, uint8 velocity, uint8 source);
726  virtual void noteOn(uint8 channel, uint8 note, uint8 velocity, uint8 source);
737  virtual void polyAftertouch(uint8 channel, uint8 note, uint8 pressure, uint8 source);
747  virtual void controlChange(uint8 channel, uint8 controller, uint8 value, uint8 source);
755  virtual void programChange(uint8 channel, uint8 program, uint8 source);
765  virtual void channelAftertouch(uint8 channel, uint8 pressure, uint8 source);
776  virtual void pitchBend(uint8 channel, uint8 pitchBendLsb, uint8 pitchBendMsb, uint8 source);
777 
787  virtual void modulation(uint8 channel, uint8 modulation, uint8 source);
801  virtual void dataEntry(uint8 channel, uint8 dataMsb, uint8 dataLsb, uint8 source);
809  virtual void volume(uint8 channel, uint8 volume, uint8 source);
819  virtual void panning(uint8 channel, uint8 panning, uint8 source);
827  virtual void expression(uint8 channel, uint8 expression, uint8 source);
835  virtual void sustain(uint8 channel, uint8 sustain, uint8 source);
850  virtual void registeredParameterNumber(uint8 channel, uint8 rpnMsb, uint8 rpnLsb, uint8 source);
862  virtual void allSoundOff(uint8 channel, uint8 source);
878  virtual void resetAllControllers(uint8 channel, uint8 source);
887  virtual void allNotesOff(uint8 channel, uint8 source);
888 
898  virtual void applyControllerDefaults(uint8 source);
899 
909  virtual void recalculateFrequencies(uint8 channel, uint8 source);
920  virtual void recalculateVolumes(uint8 channel, uint8 source);
921 
934  virtual InstrumentInfo determineInstrument(uint8 channel, uint8 source, uint8 note);
957  virtual uint8 allocateOplChannel(uint8 channel, uint8 source, uint8 instrumentId);
965  void determineMelodicChannels();
985  virtual uint16 calculateFrequency(uint8 channel, uint8 source, uint8 note);
1000  virtual int32 calculatePitchBend(uint8 channel, uint8 source, uint16 oplFrequency);
1023  virtual uint8 calculateVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
1045  virtual uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
1055  virtual bool isVolumeApplicableToOperator(OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
1067  virtual uint8 calculatePanning(uint8 channel, uint8 source);
1068 
1081  virtual void setRhythmMode(bool rhythmMode);
1082 
1102  uint16 determineOperatorRegisterOffset(uint8 oplChannel, uint8 operatorNum, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED, bool fourOperator = false);
1113  uint16 determineChannelRegisterOffset(uint8 oplChannel, bool fourOperator = false);
1121  void writeInstrument(uint8 oplChannel, InstrumentInfo instrument);
1136  void writeKeyOff(uint8 oplChannel, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED, bool forceWrite = false);
1142  void writeRhythm(bool forceWrite = false);
1156  virtual void writeVolume(uint8 oplChannel, uint8 operatorNum, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED);
1167  virtual void writePanning(uint8 oplChannel, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED);
1179  virtual void writeFrequency(uint8 oplChannel, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED);
1180 
1193  void writeRegister(uint16 reg, uint8 value, bool forceWrite = false);
1194 
1195  // The type of OPL chip to use.
1196  OPL::Config::OplType _oplType;
1197  // The OPL emulator / hardware interface.
1198  OPL::OPL *_opl;
1199 
1200  // True if the driver has been successfully opened.
1201  bool _isOpen;
1202  // The number of timer callbacks per second.
1203  int _timerFrequency;
1204  // Controls the behavior for calculating note frequency and volume.
1205  AccuracyMode _accuracyMode;
1206  // Controls the OPL channel allocation behavior.
1207  ChannelAllocationMode _allocationMode;
1208  // Controls when the instrument definitions are written.
1209  InstrumentWriteMode _instrumentWriteMode;
1210  // Controls response to rhythm note off events when rhythm mode is active.
1211  bool _rhythmModeIgnoreNoteOffs;
1212  // Controls whether MIDI channel 10 is treated as the rhythm channel or as
1213  // a melodic channel.
1214  bool _channel10Melodic;
1215 
1216  // The default MIDI channel volume (set when opening the driver).
1217  uint8 _defaultChannelVolume;
1218 
1219  // OPL global settings. Set these, then call oplInit or open to apply the
1220  // new values.
1221  NoteSelectMode _noteSelect;
1222  ModulationDepth _modulationDepth;
1223  VibratoDepth _vibratoDepth;
1224  // Current OPL rhythm mode setting. Use setRhythmMode to set and activate.
1225  bool _rhythmMode;
1226 
1227  // Pointer to the melodic instrument definitions.
1228  OplInstrumentDefinition *_instrumentBank;
1229  // Pointer to the rhythm instrument definitions.
1230  OplInstrumentDefinition *_rhythmBank;
1231  // The MIDI note value of the first rhythm instrument in the bank.
1232  uint8 _rhythmBankFirstNote;
1233  // The MIDI note value of the last rhythm instrument in the bank.
1234  uint8 _rhythmBankLastNote;
1235 
1236  // The current MIDI controller values for each MIDI channel and source.
1237  MidiChannelControlData _controlData[MAXIMUM_SOURCES][MIDI_CHANNEL_COUNT];
1238  // The active note data for each OPL channel.
1239  ActiveNote _activeNotes[OPL3_NUM_CHANNELS];
1240  // The active note data for the OPL rhythm instruments.
1241  ActiveNote _activeRhythmNotes[5];
1242  // The OPL channel allocated to each MIDI channel and source; 0xFF if a
1243  // MIDI channel has no OPL channel allocated. Note that this is only used by
1244  // the static channel allocation mode.
1245  uint8 _channelAllocations[MAXIMUM_SOURCES][MIDI_CHANNEL_COUNT];
1246  // Array containing the numbers of the available melodic channels.
1247  const uint8 *_melodicChannels;
1248  // The number of available melodic channels (length of _melodicChannels).
1249  uint8 _numMelodicChannels;
1250  // The amount of notes played since the driver was opened / reset.
1251  uint32 _noteCounter;
1252 
1253  // Factor to convert a frequency in Hertz to the format used by the OPL
1254  // registers (F - num).
1255  float _oplFrequencyConversionFactor;
1256  // The values last written to each OPL register.
1257  uint8 _shadowRegisters[0x200];
1258 
1259  Common::Mutex _allocationMutex; // For operations on channel allocations
1260  Common::Mutex _activeNotesMutex; // For operations on active notes
1261 };
1262 
1263 #endif
OplInstrumentDefinition * instrumentDef
Definition: adlib_ms.h:638
int8 transpose
Definition: adlib_ms.h:239
uint8 keyScalingLevel
Definition: adlib_ms.h:153
uint8 oplNote
Definition: adlib_ms.h:581
uint8 connectionFeedback1
Definition: adlib_ms.h:102
uint8 instrumentType
Definition: adlib_ms.h:187
int16 lastWrittenInstrumentId
Definition: adlib_ms.h:605
AccuracyMode
Definition: adlib_ms.h:294
Definition: mididrv_ms.h:86
uint8 note
Definition: adlib_ms.h:558
OplInstrumentOperatorDefinition operator0
Definition: adlib_ms.h:88
uint8 oplNote
Definition: adlib_ms.h:634
uint8 rhythmVoiceNumber
Definition: adlib_ms.h:191
Definition: adlib_ms.h:147
Definition: adlib_ms.h:183
uint8 instrumentId
Definition: adlib_ms.h:600
uint8 rhythmNote
Definition: adlib_ms.h:110
Definition: adlib_ms.h:289
uint8 instrumentId
Definition: adlib_ms.h:642
uint8 waveformSelect0
Definition: adlib_ms.h:202
uint8 connectionFeedback0
Definition: adlib_ms.h:98
uint8 rhythmType
Definition: adlib_ms.h:235
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:592
NoteSelectMode
Definition: adlib_ms.h:349
uint8 channel
Definition: adlib_ms.h:567
InstrumentWriteMode
Definition: adlib_ms.h:324
Definition: mutex.h:67
uint8 freqMultMisc
Definition: adlib_ms.h:48
uint8 waveformSelect
Definition: adlib_ms.h:64
ChannelAllocationMode
Definition: adlib_ms.h:308
bool noteSustained
Definition: adlib_ms.h:553
Definition: mididrv.h:537
Definition: adlib_ms.h:220
void send(uint32 b) override
uint8 releaseSustain
Definition: adlib_ms.h:60
uint8 velocity
Definition: adlib_ms.h:562
OplInstrumentDefinition * instrumentDef
Definition: adlib_ms.h:609
VibratoDepth
Definition: adlib_ms.h:371
uint16 oplFrequency
Definition: adlib_ms.h:587
bool channelAllocated
Definition: adlib_ms.h:616
bool fourOperator
Definition: adlib_ms.h:83
Definition: adlib_ms.h:79
AdLibBnkInstrumentOperatorDefinition operator0
Definition: adlib_ms.h:196
ModulationDepth
Definition: adlib_ms.h:357
Definition: adlib_ms.h:43
bool noteActive
Definition: adlib_ms.h:548
Definition: fmopl.h:115
uint8 source
Definition: adlib_ms.h:572