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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
/*
* OPL.cpp
* -------
* Purpose: Translate data coming from OpenMPT's mixer into OPL commands to be sent to the Opal emulator.
* Notes : (currently none)
* Authors: OpenMPT Devs
* Schism Tracker contributors (bisqwit, JosepMa, Malvineous, code relicensed from GPL to BSD with permission)
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#include "stdafx.h"
#include "../common/misc_util.h"
#include "OPL.h"
#include "opal.h"
OPENMPT_NAMESPACE_BEGIN
OPL::OPL(uint32 samplerate)
{
Initialize(samplerate);
}
OPL::OPL(IRegisterLogger &logger)
: m_logger{&logger}
{
Initialize(OPL_BASERATE);
}
OPL::~OPL()
{
// This destructor is put here so that we can forward-declare the Opal emulator class.
}
void OPL::Initialize(uint32 samplerate)
{
if(m_opl == nullptr)
m_opl = std::make_unique<Opal>(samplerate);
else
m_opl->SetSampleRate(samplerate);
Reset();
}
void OPL::Mix(int32 *target, size_t count, uint32 volumeFactorQ16)
{
if(!m_isActive)
return;
// This factor causes a sample voice to be more or less as loud as an OPL voice
const int32 factor = Util::muldiv_unsigned(volumeFactorQ16, 6169, (1 << 16));
while(count--)
{
int16 l, r;
m_opl->Sample(&l, &r);
target[0] += l * factor;
target[1] += r * factor;
target += 2;
}
}
uint16 OPL::ChannelToRegister(uint8 oplCh)
{
if(oplCh < 9)
return oplCh;
else
return (oplCh - 9) | 0x100;
}
// Translate a channel's first operator address into a register
uint16 OPL::OperatorToRegister(uint8 oplCh)
{
static constexpr uint8 OPLChannelToOperator[] = { 0, 1, 2, 8, 9, 10, 16, 17, 18 };
if(oplCh < 9)
return OPLChannelToOperator[oplCh];
else
return OPLChannelToOperator[oplCh - 9] | 0x100;
}
uint8 OPL::GetVoice(CHANNELINDEX c) const
{
if((m_ChanToOPL[c] & OPL_CHANNEL_CUT) || m_ChanToOPL[c] == OPL_CHANNEL_INVALID)
return OPL_CHANNEL_INVALID;
return m_ChanToOPL[c] & OPL_CHANNEL_MASK;
}
uint8 OPL::AllocateVoice(CHANNELINDEX c)
{
// Can we re-use a previous channel?
if(auto oplCh = m_ChanToOPL[c]; oplCh != OPL_CHANNEL_INVALID)
{
if(!(m_ChanToOPL[c] & OPL_CHANNEL_CUT))
return oplCh;
// Check re-use hint
oplCh &= OPL_CHANNEL_MASK;
if(m_OPLtoChan[oplCh] == CHANNELINDEX_INVALID || m_OPLtoChan[oplCh] == c)
{
m_OPLtoChan[oplCh] = c;
m_ChanToOPL[c] = oplCh;
return oplCh;
}
}
// Search for unused channel or channel with released note
uint8 releasedChn = OPL_CHANNEL_INVALID, releasedCutChn = OPL_CHANNEL_INVALID;
for(uint8 oplCh = 0; oplCh < OPL_CHANNELS; oplCh++)
{
if(m_OPLtoChan[oplCh] == CHANNELINDEX_INVALID)
{
m_OPLtoChan[oplCh] = c;
m_ChanToOPL[c] = oplCh;
return oplCh;
} else if(!(m_KeyOnBlock[oplCh] & KEYON_BIT))
{
releasedChn = oplCh;
if(m_ChanToOPL[m_OPLtoChan[oplCh]] & OPL_CHANNEL_CUT)
releasedCutChn = oplCh;
}
}
if(releasedChn != OPL_CHANNEL_INVALID)
{
// Prefer channel that has been marked as cut over channel that has just been released
if(releasedCutChn != OPL_CHANNEL_INVALID)
releasedChn = releasedCutChn;
m_ChanToOPL[m_OPLtoChan[releasedChn]] = OPL_CHANNEL_INVALID;
m_OPLtoChan[releasedChn] = c;
m_ChanToOPL[c] = releasedChn;
}
return GetVoice(c);
}
void OPL::MoveChannel(CHANNELINDEX from, CHANNELINDEX to)
{
uint8 oplCh = GetVoice(from);
if(oplCh == OPL_CHANNEL_INVALID)
return;
m_OPLtoChan[oplCh] = to;
m_ChanToOPL[from] = OPL_CHANNEL_INVALID;
m_ChanToOPL[to] = oplCh;
}
void OPL::NoteOff(CHANNELINDEX c)
{
uint8 oplCh = GetVoice(c);
if(oplCh == OPL_CHANNEL_INVALID || m_opl == nullptr)
return;
m_KeyOnBlock[oplCh] &= ~KEYON_BIT;
Port(c, KEYON_BLOCK | ChannelToRegister(oplCh), m_KeyOnBlock[oplCh]);
}
void OPL::NoteCut(CHANNELINDEX c, bool unassign)
{
uint8 oplCh = GetVoice(c);
if(oplCh == OPL_CHANNEL_INVALID)
return;
NoteOff(c);
Volume(c, 0, false); // Note that a volume of 0 is not complete silence; the release portion of the sound will still be heard at -48dB
if(unassign)
{
m_OPLtoChan[oplCh] = CHANNELINDEX_INVALID;
m_ChanToOPL[c] |= OPL_CHANNEL_CUT;
}
}
void OPL::Frequency(CHANNELINDEX c, uint32 milliHertz, bool keyOff, bool beatingOscillators)
{
uint8 oplCh = GetVoice(c);
if(oplCh == OPL_CHANNEL_INVALID || m_opl == nullptr)
return;
uint16 fnum = 1023;
uint8 block = 7;
if(milliHertz <= 6208431)
{
if(milliHertz > 3104215) block = 7;
else if(milliHertz > 1552107) block = 6;
else if(milliHertz > 776053) block = 5;
else if(milliHertz > 388026) block = 4;
else if(milliHertz > 194013) block = 3;
else if(milliHertz > 97006) block = 2;
else if(milliHertz > 48503) block = 1;
else block = 0;
fnum = static_cast<uint16>(Util::muldivr_unsigned(milliHertz, 1 << (20 - block), OPL_BASERATE * 1000));
MPT_ASSERT(fnum < 1024);
}
// Evil CDFM hack! Composer 670 slightly detunes each note based on the OPL channel number modulo 4.
// We allocate our OPL channels dynamically, which would result in slightly different beating characteristics,
// but we can just take the pattern channel number instead, as the pattern channel layout is always identical.
if(beatingOscillators)
fnum = std::min(static_cast<uint16>(fnum + (c & 3)), uint16(1023));
fnum |= (block << 10);
uint16 channel = ChannelToRegister(oplCh);
m_KeyOnBlock[oplCh] = (keyOff ? 0 : KEYON_BIT) | (fnum >> 8); // Key on bit + Octave (block) + F-number high 2 bits
Port(c, FNUM_LOW | channel, fnum & 0xFF); // F-Number low 8 bits
Port(c, KEYON_BLOCK | channel, m_KeyOnBlock[oplCh]);
m_isActive = true;
}
uint8 OPL::CalcVolume(uint8 trackerVol, uint8 kslVolume)
{
if(trackerVol >= 63u)
return kslVolume;
if(trackerVol > 0)
trackerVol++;
return (kslVolume & KSL_MASK) | (63u - ((63u - (kslVolume & TOTAL_LEVEL_MASK)) * trackerVol) / 64u);
}
void OPL::Volume(CHANNELINDEX c, uint8 vol, bool applyToModulator)
{
uint8 oplCh = GetVoice(c);
if(oplCh == OPL_CHANNEL_INVALID || m_opl == nullptr)
return;
const auto &patch = m_Patches[oplCh];
const uint16 modulator = OperatorToRegister(oplCh), carrier = modulator + 3;
if((patch[10] & CONNECTION_BIT) || applyToModulator)
{
// Set volume of both operators in additive mode
Port(c, KSL_LEVEL + modulator, CalcVolume(vol, patch[2]));
}
if(!applyToModulator)
{
Port(c, KSL_LEVEL + carrier, CalcVolume(vol, patch[3]));
}
}
int8 OPL::Pan(CHANNELINDEX c, int32 pan)
{
uint8 oplCh = GetVoice(c);
if(oplCh == OPL_CHANNEL_INVALID || m_opl == nullptr)
return 0;
const auto &patch = m_Patches[oplCh];
uint8 fbConn = patch[10] & ~STEREO_BITS;
// OPL3 only knows hard left, center and right, so we need to translate our
// continuous panning range into one of those three states.
// 0...84 = left, 85...170 = center, 171...256 = right
if(pan <= 170)
fbConn |= VOICE_TO_LEFT;
if(pan >= 85)
fbConn |= VOICE_TO_RIGHT;
Port(c, FEEDBACK_CONNECTION | ChannelToRegister(oplCh), fbConn);
return ((fbConn & VOICE_TO_LEFT) ? -1 : 0) + ((fbConn & VOICE_TO_RIGHT) ? 1 : 0);
}
void OPL::Patch(CHANNELINDEX c, const OPLPatch &patch)
{
uint8 oplCh = AllocateVoice(c);
if(oplCh == OPL_CHANNEL_INVALID || m_opl == nullptr)
return;
m_Patches[oplCh] = patch;
const uint16 modulator = OperatorToRegister(oplCh), carrier = modulator + 3;
for(uint8 op = 0; op < 2; op++)
{
const auto opReg = op ? carrier : modulator;
Port(c, AM_VIB | opReg, patch[0 + op]);
Port(c, KSL_LEVEL | opReg, patch[2 + op]);
Port(c, ATTACK_DECAY | opReg, patch[4 + op]);
Port(c, SUSTAIN_RELEASE | opReg, patch[6 + op]);
Port(c, WAVE_SELECT | opReg, patch[8 + op]);
}
Port(c, FEEDBACK_CONNECTION | ChannelToRegister(oplCh), patch[10]);
}
void OPL::Reset()
{
if(m_isActive)
{
for(CHANNELINDEX chn = 0; chn < MAX_CHANNELS; chn++)
{
NoteCut(chn);
}
m_isActive = false;
}
m_KeyOnBlock.fill(0);
m_OPLtoChan.fill(CHANNELINDEX_INVALID);
m_ChanToOPL.fill(OPL_CHANNEL_INVALID);
Port(CHANNELINDEX_INVALID, 0x105, 1); // Enable OPL3
Port(CHANNELINDEX_INVALID, 0x104, 0); // No 4-op voices
}
void OPL::Port(CHANNELINDEX c, uint16 reg, uint8 value)
{
if(!m_logger)
m_opl->Port(reg, value);
else
m_logger->Port(c, reg, value);
}
std::vector<uint16> OPL::AllVoiceRegisters()
{
static constexpr uint8 opRegisters[] = {OPL::AM_VIB, OPL::KSL_LEVEL, OPL::ATTACK_DECAY, OPL::SUSTAIN_RELEASE, OPL::WAVE_SELECT};
static constexpr uint8 chnRegisters[] = {OPL::FNUM_LOW, OPL::KEYON_BLOCK, OPL::FEEDBACK_CONNECTION};
std::vector<uint16> result;
result.reserve(234);
for(uint16 chip = 0; chip < 2; chip++)
{
for(uint8 opReg : opRegisters)
{
for(uint8 op = 0; op < 22; op++)
{
if((op & 7) < 6)
result.push_back((chip << 8) | opReg | op);
}
}
for(uint8 chnReg : chnRegisters)
{
for(uint8 chn = 0; chn < 9; chn++)
{
result.push_back((chip << 8) | chnReg | chn);
}
}
}
return result;
}
OPENMPT_NAMESPACE_END
|