ScummVM API documentation
music.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 MACS2_MUSIC_H
23 #define MACS2_MUSIC_H
24 
25 #include "audio/mididrv.h"
26 #include "common/array.h"
27 #include "common/scummsys.h"
28 
29 class MidiParser;
30 
31 namespace Common {
32 class MemoryReadStream;
33 }
34 
35 namespace OPL {
36 class OPL;
37 }
38 
39 namespace Macs2 {
40 
48 class Music : public MidiDriver_BASE {
49 public:
50  Music();
51  ~Music();
52 
53  void init();
54  void deinit();
55 
56  void playSongData(const Common::Array<uint8> &data);
57  void stopMusic();
58  void setVolume(uint16 volume);
59  bool isPlaybackReady() const { return _adlibPlaybackReady; }
60 
61  void readDataFromExecutable(Common::MemoryReadStream *fileStream);
62 
63  // MidiDriver_BASE interface
64  void send(uint32 b) override;
65  void metaEvent(byte type, const byte *data, uint16 length) override;
66 
67  // Debug state for ImGui visualization
68  static constexpr int kDebugRingSize = 512;
69  struct VoiceDebugState {
70  uint8 note = 0xFF;
71  uint8 channel = 0xFF;
72  uint8 volume = 0;
73  bool active = false;
74  };
75  struct DebugState {
76  VoiceDebugState voices[9];
77  uint8 masterVolume = 0;
78  uint16 activeMusicSlot = 0;
79  uint8 statusFlags = 0;
80  uint32 nextEventTimer = 0;
81  uint16 numOplChannels = 0;
82  float regHistory[9][kDebugRingSize] = {};
83  int ringPos = 0;
84  };
85  DebugState _debug;
86 
87 private:
88  void onTimer();
89  void writeReg(byte reg, byte value);
90  uint8 readReg(byte reg) const { return _regShadow[reg]; }
91 
92  void noteOn(byte channel, byte note, byte velocity);
93  void noteOff(byte channel, byte note);
94  void programChange(byte channel, byte program);
95  void controlChange(byte channel, byte control, byte value);
96 
97  void loadInstrument(uint8 voice, uint8 program);
98  void setFrequency(uint8 voice, uint8 note, uint8 pitchBend);
99  void silenceAll();
100  void updateDebugState();
101 
102  OPL::OPL *_opl;
103  MidiParser *_parser;
104  bool _playing;
105  bool _adlibPlaybackReady = true;
106 
107  // OPL state
108  byte _regShadow[256];
109  uint8 _masterVolume;
110  uint8 _numOplChannels;
111 
112  // Voice allocation (age-based, matching original)
113  uint8 _voiceAge[9];
114  uint8 _voiceMidiChannel[9];
115  uint8 _voiceInstrument[9];
116  uint8 _voiceNote[9];
117 
118  // Channel state
119  uint8 _channelPrograms[16];
120  uint8 _channelPitchBend[16];
121 
122  // Song data (kept alive for MidiParser which doesn't copy)
123  Common::Array<uint8> _songData;
124 
125  // Instrument data from EXE (16 bytes per instrument, 11 used)
126  Common::Array<uint8> _instrumentData;
127  uint16 _instrumentDataOffset;
128 
129  // Lookup tables from EXE
130  Common::Array<uint8> _opSlotTable;
131  Common::Array<uint8> _opMap1;
132  Common::Array<uint8> _opMap2;
133  Common::Array<uint8> _freqTableLo;
134  Common::Array<uint8> _freqTableHi;
135  Common::Array<uint8> _percVolTable;
136  Common::Array<uint8> _percOpMap;
137  Common::Array<uint8> _percFreqChannel;
138 
139  void loadData(Common::MemoryReadStream *stream, int64 pos, uint16 size, void *target);
140 };
141 
142 } // End of namespace Macs2
143 
144 #endif
Definition: music.h:48
Definition: mididrv.h:114
Definition: music.h:75
Definition: algorithm.h:29
Definition: memstream.h:43
Definition: fmopl.h:35
Definition: music.h:69
Definition: debugtools.h:25
Definition: midiparser.h:354
Definition: fmopl.h:116