1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#include "sci/sound/drivers/mididriver.h"
#include "sound/softsynth/emumidi.h"
namespace Sci {
#define VOLUME_SHIFT 3
#define BASE_NOTE 129 // A10
#define BASE_OCTAVE 10 // A10, as I said
static const int freq_table[12] = { // A4 is 440Hz, halftone map is x |-> ** 2^(x/12)
28160, // A10
29834,
31608,
33488,
35479,
37589,
39824,
42192,
44701,
47359,
50175,
53159
};
static inline int get_freq(int note) {
int halftone_delta = note - BASE_NOTE;
int oct_diff = ((halftone_delta + BASE_OCTAVE * 12) / 12) - BASE_OCTAVE;
int halftone_index = (halftone_delta + (12 * 100)) % 12 ;
int freq = (!note) ? 0 : freq_table[halftone_index] / (1 << (-oct_diff));
return freq;
}
class MidiDriver_PCJr : public MidiDriver_Emulated {
public:
friend class MidiPlayer_PCJr;
enum {
kMaxChannels = 3
};
MidiDriver_PCJr(Audio::Mixer *mixer) : MidiDriver_Emulated(mixer) { }
~MidiDriver_PCJr() { }
// MidiDriver
int open() { return open(kMaxChannels); }
void close();
void send(uint32 b);
MidiChannel *allocateChannel() { return NULL; }
MidiChannel *getPercussionChannel() { return NULL; }
// AudioStream
bool isStereo() const { return false; }
int getRate() const { return _mixer->getOutputRate(); }
// MidiDriver_Emulated
void generateSamples(int16 *buf, int len);
int open(int channels);
private:
int _channels_nr;
int _global_volume; // Base volume
int _volumes[kMaxChannels];
int _notes[kMaxChannels]; // Current halftone, or 0 if off
int _freq_count[kMaxChannels];
int _channel_assigner;
int _channels_assigned;
int _chan_nrs[kMaxChannels];
};
void MidiDriver_PCJr::send(uint32 b) {
byte command = b & 0xff;
byte op1 = (b >> 8) & 0xff;
byte op2 = (b >> 16) & 0xff;
int i;
int mapped_chan = -1;
int chan_nr = command & 0xf;
// First, test for channel having been assigned already
if (_channels_assigned & (1 << chan_nr)) {
// Already assigned this channel number:
for (i = 0; i < _channels_nr; i++)
if (_chan_nrs[i] == chan_nr) {
mapped_chan = i;
break;
}
} else if ((command & 0xe0) == 0x80) {
// Assign new channel round-robin
// Mark channel as unused:
if (_chan_nrs[_channel_assigner] >= 0)
_channels_assigned &= ~(1 << _chan_nrs[_channel_assigner]);
// Remember channel:
_chan_nrs[_channel_assigner] = chan_nr;
// Mark channel as used
_channels_assigned |= (1 << _chan_nrs[_channel_assigner]);
// Save channel for use later in this call:
mapped_chan = _channel_assigner;
// Round-ropin iterate channel assigner:
_channel_assigner = (_channel_assigner + 1) % _channels_nr;
}
if (mapped_chan == -1)
return;
switch (command & 0xf0) {
case 0x80:
if (op1 == _notes[mapped_chan])
_notes[mapped_chan] = 0;
break;
case 0x90:
if (!op2) {
if (op1 == _notes[mapped_chan])
_notes[mapped_chan] = 0;
} else {
_notes[mapped_chan] = op1;
_volumes[mapped_chan] = op2;
}
break;
case 0xb0:
if ((op1 == SCI_MIDI_CHANNEL_NOTES_OFF) || (op1 == SCI_MIDI_CHANNEL_SOUND_OFF))
_notes[mapped_chan] = 0;
break;
default:
debug(2, "Unused MIDI command %02x %02x %02x", command, op1, op2);
break; /* ignore */
}
}
void MidiDriver_PCJr::generateSamples(int16 *data, int len) {
int i;
int chan;
int freq[kMaxChannels];
int frequency = getRate();
for (chan = 0; chan < _channels_nr; chan++)
freq[chan] = get_freq(_notes[chan]);
for (i = 0; i < len; i++) {
int16 result = 0;
for (chan = 0; chan < _channels_nr; chan++)
if (_notes[chan]) {
int volume = (_global_volume * _volumes[chan])
>> VOLUME_SHIFT;
_freq_count[chan] += freq[chan];
while (_freq_count[chan] >= (frequency << 1))
_freq_count[chan] -= (frequency << 1);
if (_freq_count[chan] - freq[chan] < 0) {
/* Unclean rising edge */
int l = volume << 1;
result += -volume + (l * _freq_count[chan]) / freq[chan];
} else if (_freq_count[chan] >= frequency
&& _freq_count[chan] - freq[chan] < frequency) {
/* Unclean falling edge */
int l = volume << 1;
result += volume - (l * (_freq_count[chan] - frequency)) / freq[chan];
} else {
if (_freq_count[chan] < frequency)
result += volume;
else
result += -volume;
}
}
data[i] = result;
}
}
int MidiDriver_PCJr::open(int channels) {
if (_isOpen)
return MERR_ALREADY_OPEN;
if (channels > kMaxChannels)
return -1;
_channels_nr = channels;
_global_volume = 100;
for (int i = 0; i < _channels_nr; i++) {
_volumes[i] = 100;
_notes[i] = 0;
_freq_count[i] = 0;
_chan_nrs[i] = -1;
}
_channel_assigner = 0;
_channels_assigned = 0;
MidiDriver_Emulated::open();
_mixer->playStream(Audio::Mixer::kPlainSoundType, &_mixerSoundHandle, this, -1, _mixer->kMaxChannelVolume, 0, DisposeAfterUse::NO);
return 0;
}
void MidiDriver_PCJr::close() {
_mixer->stopHandle(_mixerSoundHandle);
}
class MidiPlayer_PCJr : public MidiPlayer {
public:
MidiPlayer_PCJr(SciVersion version) : MidiPlayer(version) { _driver = new MidiDriver_PCJr(g_system->getMixer()); }
int open(ResourceManager *resMan) { return static_cast<MidiDriver_PCJr *>(_driver)->open(getPolyphony()); }
byte getPlayId();
int getPolyphony() const { return 3; }
bool hasRhythmChannel() const { return false; }
void setVolume(byte volume) { static_cast<MidiDriver_PCJr *>(_driver)->_global_volume = volume; }
};
byte MidiPlayer_PCJr::getPlayId() {
switch (_version) {
case SCI_VERSION_0_EARLY:
return 0x02;
case SCI_VERSION_0_LATE:
return 0x10;
default:
return 0x13;
}
}
MidiPlayer *MidiPlayer_PCJr_create(SciVersion version) {
return new MidiPlayer_PCJr(version);
}
class MidiPlayer_PCSpeaker : public MidiPlayer_PCJr {
public:
MidiPlayer_PCSpeaker(SciVersion version) : MidiPlayer_PCJr(version) { }
byte getPlayId();
int getPolyphony() const { return 1; }
};
byte MidiPlayer_PCSpeaker::getPlayId() {
switch (_version) {
case SCI_VERSION_0_EARLY:
return 0x04;
case SCI_VERSION_0_LATE:
return 0x20;
default:
return 0x12;
}
}
MidiPlayer *MidiPlayer_PCSpeaker_create(SciVersion version) {
return new MidiPlayer_PCSpeaker(version);
}
} // End of namespace Sci
|