ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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() const;
72 };
73 
84 
92 
103 
110  uint8 rhythmNote;
116  OplInstrumentRhythmType rhythmType;
117 
123  bool isEmpty() const;
129  uint8 getNumberOfOperators() const;
138  const OplInstrumentOperatorDefinition &getOperatorDefinition(uint8 operatorNum) const;
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) const;
177 } PACKED_STRUCT;
178 
192 
198 
203  uint8 waveformSelect1;
204 
212  void toOplInstrumentDefinition(OplInstrumentDefinition &instrumentDef) const;
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) const;
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 
359  RHYTHM_INSTRUMENT_MODE_RHYTHM_TYPE
360  };
361 
366  NOTE_SELECT_MODE_0,
367  NOTE_SELECT_MODE_1
368  };
369 
381  MODULATION_DEPTH_HIGH
382  };
383 
395  VIBRATO_DEPTH_HIGH
396  };
397 
401  static const uint8 OPL2_NUM_CHANNELS = 9;
402  static const uint8 OPL3_NUM_CHANNELS = 18;
407  static const uint8 MELODIC_CHANNELS_OPL2[9];
412  static const uint8 MELODIC_CHANNELS_OPL2_RHYTHM[6];
417  static const uint8 MELODIC_CHANNELS_OPL3[18];
422  static const uint8 MELODIC_CHANNELS_OPL3_RHYTHM[15];
426  static const uint8 OPL_NUM_RHYTHM_INSTRUMENTS = 5;
431  static const uint8 OPL_RHYTHM_INSTRUMENT_CHANNELS[OPL_NUM_RHYTHM_INSTRUMENTS];
432 
436  static const uint8 OPL_REGISTER_TEST = 0x01;
437  static const uint8 OPL_REGISTER_TIMER1 = 0x02;
438  static const uint8 OPL_REGISTER_TIMER2 = 0x03;
439  static const uint8 OPL_REGISTER_TIMERCONTROL = 0x04;
440 
444  static const uint8 OPL_REGISTER_NOTESELECT_CSM = 0x08;
445  static const uint8 OPL_REGISTER_RHYTHM = 0xBD;
446 
450  static const uint8 OPL_REGISTER_BASE_FREQMULT_MISC = 0x20;
451  static const uint8 OPL_REGISTER_BASE_LEVEL = 0x40;
452  static const uint8 OPL_REGISTER_BASE_DECAY_ATTACK = 0x60;
453  static const uint8 OPL_REGISTER_BASE_RELEASE_SUSTAIN = 0x80;
454  static const uint8 OPL_REGISTER_BASE_WAVEFORMSELECT = 0xE0;
455 
459  static const uint8 OPL_REGISTER_BASE_FNUMLOW = 0xA0;
460  static const uint8 OPL_REGISTER_BASE_FNUMHIGH_BLOCK_KEYON = 0xB0;
461  static const uint8 OPL_REGISTER_BASE_CONNECTION_FEEDBACK_PANNING = 0xC0;
462 
466  static const uint16 OPL3_REGISTER_CONNECTIONSELECT = 0x104;
467  static const uint16 OPL3_REGISTER_NEW = 0x105;
468 
472  static const uint16 OPL_REGISTER_SET_2_OFFSET = 0x100;
473 
477  static const uint8 OPL_REGISTER_RHYTHM_OFFSETS[];
478 
482  static const uint8 OPL_MASK_LEVEL = 0x3F;
483  static const uint8 OPL_MASK_FNUMHIGH_BLOCK = 0x1F;
484  static const uint8 OPL_MASK_KEYON = 0x20;
485  static const uint8 OPL_MASK_PANNING = 0x30;
486 
490  static const uint8 OPL_PANNING_CENTER = 0x30;
491  static const uint8 OPL_PANNING_LEFT = 0x10;
492  static const uint8 OPL_PANNING_RIGHT = 0x20;
493 
497  static const OplInstrumentDefinition OPL_INSTRUMENT_BANK[];
501  static const OplInstrumentDefinition OPL_RHYTHM_BANK[];
502 
503 protected:
507  static const uint8 OPL_LEVEL_DEFAULT = 0x3F;
508 
512  static const uint8 OPL_MIDI_PANNING_LEFT_LIMIT = 0x2F;
516  static const uint8 OPL_MIDI_PANNING_RIGHT_LIMIT = 0x51;
517 
522  static const uint16 OPL_NOTE_FREQUENCIES[];
526  static const uint8 OPL_VOLUME_LOOKUP[];
527 
532  uint8 program;
533  uint8 channelPressure;
534  uint16 pitchBend; // 14 bit value; 0x2000 is neutral
535 
536  uint8 modulation;
537  uint8 volume;
538  uint8 panning; // 0x40 is center
539  uint8 expression;
540  bool sustain;
541  uint16 rpn; // Two 7 bit values stored in 8 bits each
542 
543  uint8 pitchBendSensitivity; // Semitones
544  uint8 pitchBendSensitivityCents;
545  uint16 masterTuningFine; // 14 bit value; 0x2000 is neutral
546  uint8 masterTuningCoarse; // Semitones; 0x40 is neutral
547 
549 
553  void init();
554  };
555 
559  struct ActiveNote {
570 
574  uint8 note;
578  uint8 velocity;
583  uint8 channel;
588  uint8 source;
589 
597  uint8 oplNote;
603  uint16 oplFrequency;
609 
626 
633 
634  ActiveNote();
635 
639  void init();
640  };
641 
645  struct InstrumentInfo {
650  uint8 oplNote;
659  };
660 
661 public:
670  static bool detectOplType(OPL::Config::OplType oplType);
671 
682 
690  int open() override;
691  bool isOpen() const override;
692  void close() override;
693  uint32 property(int prop, uint32 param) override;
694  uint32 getBaseTempo() override;
700  MidiChannel *allocateChannel() override;
706  MidiChannel *getPercussionChannel() override;
707 
709  void send(int8 source, uint32 b) override;
710  void sysEx(const byte *msg, uint16 length) override;
711  void metaEvent(int8 source, byte type, byte *data, uint16 length) override;
712  void stopAllNotes(bool stopSustainedNotes = false) override;
713 
714  void stopAllNotes(uint8 source, uint8 channel) override;
715  void deinitSource(uint8 source) override;
716 
717 protected:
718  void applySourceVolume(uint8 source) override;
719 
723  virtual void initOpl();
724 
733  virtual void noteOff(uint8 channel, uint8 note, uint8 velocity, uint8 source);
742  virtual void noteOn(uint8 channel, uint8 note, uint8 velocity, uint8 source);
753  virtual void polyAftertouch(uint8 channel, uint8 note, uint8 pressure, uint8 source);
763  virtual void controlChange(uint8 channel, uint8 controller, uint8 value, uint8 source);
771  virtual void programChange(uint8 channel, uint8 program, uint8 source);
781  virtual void channelAftertouch(uint8 channel, uint8 pressure, uint8 source);
792  virtual void pitchBend(uint8 channel, uint8 pitchBendLsb, uint8 pitchBendMsb, uint8 source);
793 
803  virtual void modulation(uint8 channel, uint8 modulation, uint8 source);
817  virtual void dataEntry(uint8 channel, uint8 dataMsb, uint8 dataLsb, uint8 source);
825  virtual void volume(uint8 channel, uint8 volume, uint8 source);
835  virtual void panning(uint8 channel, uint8 panning, uint8 source);
843  virtual void expression(uint8 channel, uint8 expression, uint8 source);
851  virtual void sustain(uint8 channel, uint8 sustain, uint8 source);
866  virtual void registeredParameterNumber(uint8 channel, uint8 rpnMsb, uint8 rpnLsb, uint8 source);
878  virtual void allSoundOff(uint8 channel, uint8 source);
894  virtual void resetAllControllers(uint8 channel, uint8 source);
903  virtual void allNotesOff(uint8 channel, uint8 source);
904 
914  virtual void applyControllerDefaults(uint8 source);
915 
925  virtual void recalculateFrequencies(uint8 channel, uint8 source);
936  virtual void recalculateVolumes(uint8 channel, uint8 source);
937 
950  virtual InstrumentInfo determineInstrument(uint8 channel, uint8 source, uint8 note);
973  virtual uint8 allocateOplChannel(uint8 channel, uint8 source, InstrumentInfo &instrumentInfo);
981  void determineMelodicChannels();
1001  virtual uint16 calculateFrequency(uint8 channel, uint8 source, uint8 note);
1016  virtual int32 calculatePitchBend(uint8 channel, uint8 source, uint16 oplFrequency);
1039  virtual uint8 calculateVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
1061  virtual uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
1071  virtual bool isVolumeApplicableToOperator(const OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
1083  virtual uint8 calculatePanning(uint8 channel, uint8 source);
1084 
1097  virtual void setRhythmMode(bool rhythmMode);
1098 
1118  uint16 determineOperatorRegisterOffset(uint8 oplChannel, uint8 operatorNum, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED, bool fourOperator = false);
1129  uint16 determineChannelRegisterOffset(uint8 oplChannel, bool fourOperator = false);
1137  void writeInstrument(uint8 oplChannel, InstrumentInfo instrument);
1152  void writeKeyOff(uint8 oplChannel, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED, bool forceWrite = false);
1158  void writeRhythm(bool forceWrite = false);
1172  virtual void writeVolume(uint8 oplChannel, uint8 operatorNum, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED);
1183  virtual void writePanning(uint8 oplChannel, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED);
1195  virtual void writeFrequency(uint8 oplChannel, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED);
1196 
1209  void writeRegister(uint16 reg, uint8 value, bool forceWrite = false);
1210 
1211  // The type of OPL chip to use.
1212  OPL::Config::OplType _oplType;
1213  // The OPL emulator / hardware interface.
1214  OPL::OPL *_opl;
1215 
1216  // True if the driver has been successfully opened.
1217  bool _isOpen;
1218  // The number of timer callbacks per second.
1219  int _timerFrequency;
1220  // Controls the behavior for calculating note frequency and volume.
1221  AccuracyMode _accuracyMode;
1222  // Controls the OPL channel allocation behavior.
1223  ChannelAllocationMode _allocationMode;
1224  // Controls when the instrument definitions are written.
1225  InstrumentWriteMode _instrumentWriteMode;
1226  // In instrument write mode First Note On or Program Change, this flag controls if the Cx register,
1227  // which is shared between rhythm mode instrument definitions (except bass drum), is rewritten
1228  // before each note on.
1229  bool _rhythmModeRewriteSharedRegister;
1230  // Controls response to rhythm note off events when rhythm mode is active.
1231  bool _rhythmModeIgnoreNoteOffs;
1232  // Controls how rhythm notes are played in OPL rhythm mode and whether MIDI
1233  // channel 10 is treated as the rhythm channel or as a melodic channel.
1234  RhythmInstrumentMode _rhythmInstrumentMode;
1235 
1236  // The default MIDI channel volume (set when opening the driver).
1237  uint8 _defaultChannelVolume;
1238 
1239  // OPL global settings. Set these, then call oplInit or open to apply the
1240  // new values.
1241  NoteSelectMode _noteSelect;
1242  ModulationDepth _modulationDepth;
1243  VibratoDepth _vibratoDepth;
1244  // Current OPL rhythm mode setting. Use setRhythmMode to set and activate.
1245  bool _rhythmMode;
1246 
1247  // Pointer to the melodic instrument definitions.
1248  const OplInstrumentDefinition *_instrumentBank;
1249  // Pointer to the rhythm instrument definitions.
1250  const OplInstrumentDefinition *_rhythmBank;
1251  // The MIDI note value of the first rhythm instrument in the bank.
1252  uint8 _rhythmBankFirstNote;
1253  // The MIDI note value of the last rhythm instrument in the bank.
1254  uint8 _rhythmBankLastNote;
1255 
1256  // The current MIDI controller values for each MIDI channel and source.
1257  MidiChannelControlData _controlData[MAXIMUM_SOURCES][MIDI_CHANNEL_COUNT];
1258  // The active note data for each OPL channel.
1259  ActiveNote _activeNotes[OPL3_NUM_CHANNELS];
1260  // The active note data for the OPL rhythm instruments.
1261  ActiveNote _activeRhythmNotes[5];
1262  // The OPL channel allocated to each MIDI channel and source; 0xFF if a
1263  // MIDI channel has no OPL channel allocated. Note that this is only used by
1264  // the static channel allocation mode.
1265  uint8 _channelAllocations[MAXIMUM_SOURCES][MIDI_CHANNEL_COUNT];
1266  // Array containing the numbers of the available melodic channels.
1267  const uint8 *_melodicChannels;
1268  // The number of available melodic channels (length of _melodicChannels).
1269  uint8 _numMelodicChannels;
1270  // The amount of notes played since the driver was opened / reset.
1271  uint32 _noteCounter;
1272 
1273  // Factor to convert a frequency in Hertz to the format used by the OPL
1274  // registers (F - num).
1275  float _oplFrequencyConversionFactor;
1276  // The values last written to each OPL register.
1277  uint8 _shadowRegisters[0x200];
1278 
1279  Common::Mutex _allocationMutex; // For operations on channel allocations
1280  Common::Mutex _activeNotesMutex; // For operations on active notes
1281 };
1282 
1283 #endif
int8 transpose
Definition: adlib_ms.h:239
uint8 keyScalingLevel
Definition: adlib_ms.h:153
uint8 oplNote
Definition: adlib_ms.h:597
uint8 connectionFeedback1
Definition: adlib_ms.h:102
uint8 instrumentType
Definition: adlib_ms.h:187
int16 lastWrittenInstrumentId
Definition: adlib_ms.h:621
AccuracyMode
Definition: adlib_ms.h:294
Definition: mididrv_ms.h:86
uint8 note
Definition: adlib_ms.h:574
const OplInstrumentDefinition * instrumentDef
Definition: adlib_ms.h:625
OplInstrumentOperatorDefinition operator0
Definition: adlib_ms.h:88
uint8 oplNote
Definition: adlib_ms.h:650
uint8 rhythmVoiceNumber
Definition: adlib_ms.h:191
Definition: adlib_ms.h:147
Definition: adlib_ms.h:183
uint8 instrumentId
Definition: adlib_ms.h:616
uint8 rhythmNote
Definition: adlib_ms.h:110
Definition: adlib_ms.h:289
uint8 instrumentId
Definition: adlib_ms.h:658
uint8 waveformSelect0
Definition: adlib_ms.h:202
uint8 connectionFeedback0
Definition: adlib_ms.h:98
const OplInstrumentDefinition * instrumentDef
Definition: adlib_ms.h:654
RhythmInstrumentMode
Definition: adlib_ms.h:346
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:608
NoteSelectMode
Definition: adlib_ms.h:365
uint8 channel
Definition: adlib_ms.h:583
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:569
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:578
VibratoDepth
Definition: adlib_ms.h:387
uint16 oplFrequency
Definition: adlib_ms.h:603
bool channelAllocated
Definition: adlib_ms.h:632
bool fourOperator
Definition: adlib_ms.h:83
Definition: adlib_ms.h:79
AdLibBnkInstrumentOperatorDefinition operator0
Definition: adlib_ms.h:196
ModulationDepth
Definition: adlib_ms.h:373
Definition: adlib_ms.h:43
bool noteActive
Definition: adlib_ms.h:564
Definition: fmopl.h:115
uint8 source
Definition: adlib_ms.h:588