ScummVM API documentation
mac_m68k.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 SCUMM_IMUSE_MAC_M68K_H
23 #define SCUMM_IMUSE_MAC_M68K_H
24 
25 #include "audio/softsynth/emumidi.h"
26 
27 #include "common/hashmap.h"
28 
29 namespace Common {
30 class SeekableReadStream;
31 }
32 
33 namespace Scumm {
34 
36  friend class MidiChannel_MacM68k;
37 public:
39  ~IMuseDriver_MacM68k() override;
40 
41  int open() override;
42  void close() override;
43 
44  void send(uint32 d) override;
45 
46  MidiChannel *allocateChannel() override;
47  MidiChannel *getPercussionChannel() override { return nullptr; }
48 
49  bool isStereo() const override { return false; }
50  int getRate() const override {
51  // The original is using a frequency of approx. 22254.54546 here.
52  // To be precise it uses the 16.16 fixed point value 0x56EE8BA3.
53  return 22254;
54  }
55 
56 protected:
57  void generateSamples(int16 *buf, int len) override;
58  void onTimer() override {}
59 
60 private:
61  int *_mixBuffer;
62  int _mixBufferLength;
63 
64  struct Instrument {
65  Instrument() { reset(); }
66  uint length;
67  uint sampleRate;
68  uint loopStart;
69  uint loopEnd;
70  int baseFrequency;
71 
72  byte *data;
73 
74  void reset() {
75  length = 0;
76  sampleRate = 0;
77  loopStart = 0;
78  loopEnd = 0;
79  baseFrequency = 0;
80  data = nullptr;
81  }
82  };
83 
84  enum {
85  kDefaultInstrument = 0x3E7,
86  kProgramChangeBase = 0x3E8,
87  kSysExBase = 0x7D0
88  };
89 
90  Instrument getInstrument(int idx) const;
92  InstrumentMap _instruments;
93  Instrument _defaultInstrument;
94  void loadAllInstruments();
95  void addInstrument(int idx, Common::SeekableReadStream *data);
96 
97  struct OutputChannel {
98  int pitchModifier;
99 
100  const byte *instrument;
101  uint subPos;
102 
103  const byte *start;
104  const byte *end;
105 
106  const byte *soundStart;
107  const byte *soundEnd;
108  const byte *loopStart;
109  const byte *loopEnd;
110 
111  int frequency;
112  int volume;
113 
114  bool isFinished;
115 
116  int baseFrequency;
117  };
118 
119  void setPitch(OutputChannel *out, int frequency);
120  int _pitchTable[128];
121 
122  byte *_volumeTable;
123  static const int _volumeBaseTable[32];
124 
125  class MidiChannel_MacM68k;
126 
127  struct VoiceChannel {
128  MidiChannel_MacM68k *part;
129  VoiceChannel *prev, *next;
130  int channel;
131  int note;
132  bool sustainNoteOff;
133  OutputChannel out;
134 
135  void off();
136  };
137 
138  class MidiChannel_MacM68k : public MidiChannel {
139  friend class IMuseDriver_MacM68k;
140  public:
141  MidiChannel_MacM68k(IMuseDriver_MacM68k *driver, byte number) : MidiChannel(), _owner(driver), _number(number), _allocated(false),
142  _priority(0), _sustain(0), _pitchBend(0), _pitchBendFactor(2), _transpose(0), _detune(0), _volume(0), _voice(nullptr), _instrument() {}
143  MidiDriver *device() override { return _owner; }
144  byte getNumber() override { return _number; }
145  void release() override;
146 
147  void send(uint32 b) override;
148  void noteOff(byte note) override;
149  void noteOn(byte note, byte velocity) override;
150  void programChange(byte program) override;
151  void pitchBend(int16 bend) override;
152  void controlChange(byte control, byte value) override;
153  void pitchBendFactor(byte value) override;
154  void transpose(int8 value) override;
155  void detune(int16 value) override;
156  void priority(byte value) override;
157  void sysEx_customInstrument(uint32 type, const byte *instr, uint32 dataSize) override;
158 
159  bool allocate();
160 
161  void addVoice(VoiceChannel *voice);
162  void removeVoice(VoiceChannel *voice);
163  private:
164  IMuseDriver_MacM68k *_owner;
165  const byte _number;
166  bool _allocated;
167 
168  VoiceChannel *_voice;
169  int _priority;
170  int _sustain;
171  Instrument _instrument;
172  int _pitchBend;
173  int _pitchBendFactor;
174  int8 _detune;
175  int8 _transpose;
176  int _volume;
177  };
178 
179  MidiChannel_MacM68k *_channels[32];
180 
181  enum {
182  kChannelCount = 8
183  };
184  VoiceChannel _voiceChannels[kChannelCount];
185  int _lastUsedVoiceChannel;
186  VoiceChannel *allocateVoice(int priority);
187 };
188 
189 } // End of namespace Scumm
190 
191 #endif
Definition: stream.h:745
Definition: mac_m68k.h:35
Definition: mididrv.h:309
int getRate() const override
Definition: mac_m68k.h:50
Definition: mixer.h:59
Definition: algorithm.h:29
Definition: mididrv.h:537
bool isStereo() const override
Definition: mac_m68k.h:49
Definition: emumidi.h:29
Definition: actor.h:30