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() const;
72 };
73 
84 
92 
103 
110  uint8 rhythmNote;
116  OplInstrumentRhythmType rhythmType;
117 
122  int8 transpose;
123 
129  bool isEmpty() const;
135  uint8 getNumberOfOperators() const;
144  const OplInstrumentOperatorDefinition &getOperatorDefinition(uint8 operatorNum) const;
145 };
146 
147 #include "common/pack-start.h" // START STRUCT PACKING
148 
160  uint8 frequencyMultiplier;
161  uint8 feedback; // ignored for operator 1
162  uint8 attack;
163  uint8 sustain;
164  uint8 envelopeGainType; // 0x00: not sustained, >= 0x01: sustained
165  uint8 decay;
166  uint8 release;
167  uint8 level;
168  uint8 amplitudeModulation; // 0x00: off, >= 0x01: on
169  uint8 vibrato; // 0x00: off, >= 0x01: on
170  uint8 keyScalingRate; // 0x00: low, >= 0x01: high
171  uint8 connection; // 0x00: additive, >= 0x01: FM; ignored for operator 1
172 
182  void toOplInstrumentOperatorDefinition(OplInstrumentOperatorDefinition &operatorDef, uint8 waveformSelect) const;
183 } PACKED_STRUCT;
184 
198 
204 
209  uint8 waveformSelect1;
210 
218  void toOplInstrumentDefinition(OplInstrumentDefinition &instrumentDef) const;
219 } PACKED_STRUCT;
220 
227  uint8 o0FreqMultMisc;
228  uint8 o1FreqMultMisc;
229  uint8 o0Level;
230  uint8 o1Level;
231  uint8 o0DecayAttack;
232  uint8 o1DecayAttack;
233  uint8 o0ReleaseSustain;
234  uint8 o1ReleaseSustain;
235  uint8 o0WaveformSelect;
236  uint8 o1WaveformSelect;
237  uint8 connectionFeedback;
241  uint8 rhythmType;
245  int8 transpose;
246  uint8 rhythmNote;
247  uint8 padding1;
248  uint8 padding2;
249 
257  void toOplInstrumentDefinition(OplInstrumentDefinition &instrumentDef) const;
258 } PACKED_STRUCT;
259 
260 #include "common/pack-end.h" // END STRUCT PACKING
261 
296 public:
308  ACCURACY_MODE_GM
309  };
310 
324  ALLOCATION_MODE_STATIC
325  };
326 
349  INSTRUMENT_WRITE_MODE_FIRST_NOTE_ON
350  };
351 
365  RHYTHM_INSTRUMENT_MODE_RHYTHM_TYPE
366  };
367 
372  NOTE_SELECT_MODE_0,
373  NOTE_SELECT_MODE_1
374  };
375 
387  MODULATION_DEPTH_HIGH
388  };
389 
401  VIBRATO_DEPTH_HIGH
402  };
403 
407  static const uint8 OPL2_NUM_CHANNELS = 9;
408  static const uint8 OPL3_NUM_CHANNELS = 18;
413  static const uint8 MELODIC_CHANNELS_OPL2[9];
418  static const uint8 MELODIC_CHANNELS_OPL2_RHYTHM[6];
423  static const uint8 MELODIC_CHANNELS_OPL3[18];
428  static const uint8 MELODIC_CHANNELS_OPL3_RHYTHM[15];
432  static const uint8 OPL_NUM_RHYTHM_INSTRUMENTS = 5;
437  static const uint8 OPL_RHYTHM_INSTRUMENT_CHANNELS[OPL_NUM_RHYTHM_INSTRUMENTS];
438 
442  static const uint8 OPL_REGISTER_TEST = 0x01;
443  static const uint8 OPL_REGISTER_TIMER1 = 0x02;
444  static const uint8 OPL_REGISTER_TIMER2 = 0x03;
445  static const uint8 OPL_REGISTER_TIMERCONTROL = 0x04;
446 
450  static const uint8 OPL_REGISTER_NOTESELECT_CSM = 0x08;
451  static const uint8 OPL_REGISTER_RHYTHM = 0xBD;
452 
456  static const uint8 OPL_REGISTER_BASE_FREQMULT_MISC = 0x20;
457  static const uint8 OPL_REGISTER_BASE_LEVEL = 0x40;
458  static const uint8 OPL_REGISTER_BASE_DECAY_ATTACK = 0x60;
459  static const uint8 OPL_REGISTER_BASE_RELEASE_SUSTAIN = 0x80;
460  static const uint8 OPL_REGISTER_BASE_WAVEFORMSELECT = 0xE0;
461 
465  static const uint8 OPL_REGISTER_BASE_FNUMLOW = 0xA0;
466  static const uint8 OPL_REGISTER_BASE_FNUMHIGH_BLOCK_KEYON = 0xB0;
467  static const uint8 OPL_REGISTER_BASE_CONNECTION_FEEDBACK_PANNING = 0xC0;
468 
472  static const uint16 OPL3_REGISTER_CONNECTIONSELECT = 0x104;
473  static const uint16 OPL3_REGISTER_NEW = 0x105;
474 
478  static const uint16 OPL_REGISTER_SET_2_OFFSET = 0x100;
479 
483  static const uint8 OPL_REGISTER_RHYTHM_OFFSETS[];
484 
488  static const uint8 OPL_MASK_LEVEL = 0x3F;
489  static const uint8 OPL_MASK_FNUMHIGH_BLOCK = 0x1F;
490  static const uint8 OPL_MASK_KEYON = 0x20;
491  static const uint8 OPL_MASK_PANNING = 0xF0;
492 
496  static const uint8 OPL_PANNING_CENTER = 0x30;
497  static const uint8 OPL_PANNING_LEFT = 0x10;
498  static const uint8 OPL_PANNING_RIGHT = 0x20;
499 
503  static const OplInstrumentDefinition OPL_INSTRUMENT_BANK[];
507  static const OplInstrumentDefinition OPL_RHYTHM_BANK[];
508 
509 protected:
513  static const uint8 OPL_LEVEL_DEFAULT = 0x3F;
514 
518  static const uint8 OPL_MIDI_PANNING_LEFT_LIMIT = 0x2F;
522  static const uint8 OPL_MIDI_PANNING_RIGHT_LIMIT = 0x51;
523 
528  static const uint16 OPL_NOTE_FREQUENCIES[];
532  static const uint8 OPL_VOLUME_LOOKUP[];
533 
538  uint8 program;
539  uint8 channelPressure;
540  uint16 pitchBend; // 14 bit value; 0x2000 is neutral
541 
542  uint8 modulation;
543  uint8 volume;
544  uint8 panning; // 0x40 is center
545  uint8 expression;
546  bool sustain;
547  uint16 rpn; // Two 7 bit values stored in 8 bits each
548 
549  uint8 pitchBendSensitivity; // Semitones
550  uint8 pitchBendSensitivityCents;
551  uint16 masterTuningFine; // 14 bit value; 0x2000 is neutral
552  uint8 masterTuningCoarse; // Semitones; 0x40 is neutral
553 
555 
559  void init();
560  };
561 
565  struct ActiveNote {
576 
580  uint8 note;
584  uint8 velocity;
589  uint8 channel;
594  uint8 source;
595 
603  uint8 oplNote;
609  uint16 oplFrequency;
615 
632 
639 
640  ActiveNote();
641 
645  void init();
646  };
647 
651  struct InstrumentInfo {
656  uint8 oplNote;
665  };
666 
667 public:
676  static bool detectOplType(OPL::Config::OplType oplType);
677 
688 
696  int open() override;
697  bool isOpen() const override;
698  void close() override;
699  uint32 property(int prop, uint32 param) override;
700  uint32 getBaseTempo() override;
706  MidiChannel *allocateChannel() override;
712  MidiChannel *getPercussionChannel() override;
713 
715  void send(int8 source, uint32 b) override;
716  void sysEx(const byte *msg, uint16 length) override;
717  void metaEvent(int8 source, byte type, const byte *data, uint16 length) override;
718  void stopAllNotes(bool stopSustainedNotes = false) override;
719 
720  void stopAllNotes(uint8 source, uint8 channel) override;
721  void deinitSource(uint8 source) override;
722 
723 protected:
724  void applySourceVolume(uint8 source) override;
725 
729  virtual void initOpl();
730 
739  virtual void noteOff(uint8 channel, uint8 note, uint8 velocity, uint8 source);
748  virtual void noteOn(uint8 channel, uint8 note, uint8 velocity, uint8 source);
759  virtual void polyAftertouch(uint8 channel, uint8 note, uint8 pressure, uint8 source);
769  virtual void controlChange(uint8 channel, uint8 controller, uint8 value, uint8 source);
777  virtual void programChange(uint8 channel, uint8 program, uint8 source);
787  virtual void channelAftertouch(uint8 channel, uint8 pressure, uint8 source);
798  virtual void pitchBend(uint8 channel, uint8 pitchBendLsb, uint8 pitchBendMsb, uint8 source);
799 
809  virtual void modulation(uint8 channel, uint8 modulation, uint8 source);
823  virtual void dataEntry(uint8 channel, uint8 dataMsb, uint8 dataLsb, uint8 source);
831  virtual void volume(uint8 channel, uint8 volume, uint8 source);
841  virtual void panning(uint8 channel, uint8 panning, uint8 source);
849  virtual void expression(uint8 channel, uint8 expression, uint8 source);
857  virtual void sustain(uint8 channel, uint8 sustain, uint8 source);
872  virtual void registeredParameterNumber(uint8 channel, uint8 rpnMsb, uint8 rpnLsb, uint8 source);
884  virtual void allSoundOff(uint8 channel, uint8 source);
900  virtual void resetAllControllers(uint8 channel, uint8 source);
909  virtual void allNotesOff(uint8 channel, uint8 source);
910 
920  virtual void applyControllerDefaults(uint8 source);
921 
931  virtual void recalculateFrequencies(uint8 channel, uint8 source);
942  virtual void recalculateVolumes(uint8 channel, uint8 source);
943 
956  virtual InstrumentInfo determineInstrument(uint8 channel, uint8 source, uint8 note);
979  virtual uint8 allocateOplChannel(uint8 channel, uint8 source, InstrumentInfo &instrumentInfo);
987  void determineMelodicChannels();
1008  virtual uint16 calculateFrequency(uint8 channel, uint8 source, uint8 note);
1023  virtual int32 calculatePitchBend(uint8 channel, uint8 source, uint16 oplFrequency);
1046  virtual uint8 calculateVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
1068  virtual uint8 calculateUnscaledVolume(uint8 channel, uint8 source, uint8 velocity, const OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
1078  virtual bool isVolumeApplicableToOperator(const OplInstrumentDefinition &instrumentDef, uint8 operatorNum);
1090  virtual uint8 calculatePanning(uint8 channel, uint8 source);
1091 
1104  virtual void setRhythmMode(bool rhythmMode);
1105 
1125  uint16 determineOperatorRegisterOffset(uint8 oplChannel, uint8 operatorNum, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED, bool fourOperator = false);
1136  uint16 determineChannelRegisterOffset(uint8 oplChannel, bool fourOperator = false);
1144  void writeInstrument(uint8 oplChannel, InstrumentInfo instrument);
1159  void writeKeyOff(uint8 oplChannel, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED, bool forceWrite = false);
1165  void writeRhythm(bool forceWrite = false);
1179  virtual void writeVolume(uint8 oplChannel, uint8 operatorNum, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED);
1190  virtual void writePanning(uint8 oplChannel, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED);
1202  virtual void writeFrequency(uint8 oplChannel, OplInstrumentRhythmType rhythmType = RHYTHM_TYPE_UNDEFINED);
1203 
1216  void writeRegister(uint16 reg, uint8 value, bool forceWrite = false);
1217 
1218  // The type of OPL chip to use.
1219  OPL::Config::OplType _oplType;
1220  // The OPL emulator / hardware interface.
1221  OPL::OPL *_opl;
1222 
1223  // True if the driver has been successfully opened.
1224  bool _isOpen;
1225  // The number of timer callbacks per second.
1226  int _timerFrequency;
1227  // Controls the behavior for calculating note frequency and volume.
1228  AccuracyMode _accuracyMode;
1229  // Controls the OPL channel allocation behavior.
1230  ChannelAllocationMode _allocationMode;
1231  // Controls when the instrument definitions are written.
1232  InstrumentWriteMode _instrumentWriteMode;
1233  // In instrument write mode First Note On or Program Change, this flag controls if the Cx register,
1234  // which is shared between rhythm mode instrument definitions (except bass drum), is rewritten
1235  // before each note on.
1236  bool _rhythmModeRewriteSharedRegister;
1237  // Controls response to rhythm note off events when rhythm mode is active.
1238  bool _rhythmModeIgnoreNoteOffs;
1239  // Controls how rhythm notes are played in OPL rhythm mode and whether MIDI
1240  // channel 10 is treated as the rhythm channel or as a melodic channel.
1241  RhythmInstrumentMode _rhythmInstrumentMode;
1242 
1243  // The default MIDI channel volume (set when opening the driver).
1244  uint8 _defaultChannelVolume;
1245 
1246  // OPL global settings. Set these, then call oplInit or open to apply the
1247  // new values.
1248  NoteSelectMode _noteSelect;
1249  ModulationDepth _modulationDepth;
1250  VibratoDepth _vibratoDepth;
1251  // Current OPL rhythm mode setting. Use setRhythmMode to set and activate.
1252  bool _rhythmMode;
1253 
1254  // Pointer to the melodic instrument definitions.
1255  const OplInstrumentDefinition *_instrumentBank;
1256  // Pointer to the rhythm instrument definitions.
1257  const OplInstrumentDefinition *_rhythmBank;
1258  // The MIDI note value of the first rhythm instrument in the bank.
1259  uint8 _rhythmBankFirstNote;
1260  // The MIDI note value of the last rhythm instrument in the bank.
1261  uint8 _rhythmBankLastNote;
1262 
1263  // The current MIDI controller values for each MIDI channel and source.
1264  MidiChannelControlData _controlData[MAXIMUM_SOURCES][MIDI_CHANNEL_COUNT];
1265  // The active note data for each OPL channel.
1266  ActiveNote _activeNotes[OPL3_NUM_CHANNELS];
1267  // The active note data for the OPL rhythm instruments.
1268  ActiveNote _activeRhythmNotes[5];
1269  // The OPL channel allocated to each MIDI channel and source; 0xFF if a
1270  // MIDI channel has no OPL channel allocated. Note that this is only used by
1271  // the static channel allocation mode.
1272  uint8 _channelAllocations[MAXIMUM_SOURCES][MIDI_CHANNEL_COUNT];
1273  // Array containing the numbers of the available melodic channels.
1274  const uint8 *_melodicChannels;
1275  // The number of available melodic channels (length of _melodicChannels).
1276  uint8 _numMelodicChannels;
1277  // The amount of notes played since the driver was opened / reset.
1278  uint32 _noteCounter;
1279 
1280  // Factor to convert a frequency in Hertz to the format used by the OPL
1281  // registers (F - num).
1282  float _oplFrequencyConversionFactor;
1283  // The values last written to each OPL register.
1284  uint8 _shadowRegisters[0x200];
1285 
1286  Common::Mutex _allocationMutex; // For operations on channel allocations
1287  Common::Mutex _activeNotesMutex; // For operations on active notes
1288 };
1289 
1290 #endif
int8 transpose
Definition: adlib_ms.h:245
uint8 keyScalingLevel
Definition: adlib_ms.h:159
uint8 oplNote
Definition: adlib_ms.h:603
uint8 connectionFeedback1
Definition: adlib_ms.h:102
uint8 instrumentType
Definition: adlib_ms.h:193
int16 lastWrittenInstrumentId
Definition: adlib_ms.h:627
AccuracyMode
Definition: adlib_ms.h:300
Definition: mididrv_ms.h:86
uint8 note
Definition: adlib_ms.h:580
const OplInstrumentDefinition * instrumentDef
Definition: adlib_ms.h:631
OplInstrumentOperatorDefinition operator0
Definition: adlib_ms.h:88
uint8 oplNote
Definition: adlib_ms.h:656
uint8 rhythmVoiceNumber
Definition: adlib_ms.h:197
Definition: adlib_ms.h:153
Definition: adlib_ms.h:189
uint8 instrumentId
Definition: adlib_ms.h:622
uint8 rhythmNote
Definition: adlib_ms.h:110
Definition: adlib_ms.h:295
uint8 instrumentId
Definition: adlib_ms.h:664
uint8 waveformSelect0
Definition: adlib_ms.h:208
uint8 connectionFeedback0
Definition: adlib_ms.h:98
const OplInstrumentDefinition * instrumentDef
Definition: adlib_ms.h:660
RhythmInstrumentMode
Definition: adlib_ms.h:352
uint8 rhythmType
Definition: adlib_ms.h:241
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:614
NoteSelectMode
Definition: adlib_ms.h:371
uint8 channel
Definition: adlib_ms.h:589
InstrumentWriteMode
Definition: adlib_ms.h:330
Definition: mutex.h:67
uint8 freqMultMisc
Definition: adlib_ms.h:48
uint8 waveformSelect
Definition: adlib_ms.h:64
ChannelAllocationMode
Definition: adlib_ms.h:314
bool noteSustained
Definition: adlib_ms.h:575
Definition: mididrv.h:539
Definition: adlib_ms.h:226
void send(uint32 b) override
uint8 releaseSustain
Definition: adlib_ms.h:60
uint8 velocity
Definition: adlib_ms.h:584
int8 transpose
Definition: adlib_ms.h:122
VibratoDepth
Definition: adlib_ms.h:393
uint16 oplFrequency
Definition: adlib_ms.h:609
bool channelAllocated
Definition: adlib_ms.h:638
bool fourOperator
Definition: adlib_ms.h:83
Definition: adlib_ms.h:79
AdLibBnkInstrumentOperatorDefinition operator0
Definition: adlib_ms.h:202
ModulationDepth
Definition: adlib_ms.h:379
Definition: adlib_ms.h:43
bool noteActive
Definition: adlib_ms.h:570
Definition: fmopl.h:115
uint8 source
Definition: adlib_ms.h:594