-
Notifications
You must be signed in to change notification settings - Fork 0
/
synththread.cpp
355 lines (311 loc) · 11.1 KB
/
synththread.cpp
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
346
347
348
349
350
351
352
353
354
355
#include "synththread.h"
SynthThread::SynthThread(MainWindow *parent)
: QThread(parent), m_pos(0)
{
memset(NOTE, 0, sizeof(NOTE));
mainWindow = parent;
INSTR.resize(16);
ymf262io = new YMF262IO(this, 0);
synthBuffer = new QBuffer(this);
//audioData = new QByteArray(1024, ' ');
audioData = new char[4096];
//QByteArray data(10240, ' ');
//synthBuffer->setData(data);
//synthBuffer->write(audioData, 4096);
testOPL3Sound();
loadSoundbank();
loadProgramMap();
// for (int c = 0; c < soundBankData.size(); c++) {
// qDebug("Name: %s, Index: %d", soundBankData.at(c).instrHeader.name, c);
// }
}
SynthThread::~SynthThread()
{
synthBuffer->close();
delete synthBuffer;
delete audioData;
delete ymf262io;
}
QBuffer *SynthThread::getSynthBuffer()
{
return synthBuffer;
}
void SynthThread::run()
{
//ymf262io->start();
while (isActive) {
//qDebug("bytesAvailable %ld", synthBuffer->bytesAvailable());
while (synthBuffer->atEnd()) {
m_pos = synthBuffer->pos();
ymf262io->updateOne((INT16*)audioData, 4096>>2);
synthBuffer->write(audioData, 4096);
synthBuffer->seek(m_pos);
//mainWindow->audioBuf->write(audioData, 1024);
//qDebug("%d", m_pos);
//qDebug("%s", synthBuffer->buffer().data());
}
Sleep(10);
}
}
void SynthThread::testOPL3Sound()
{
for (int i = 0; i < OPL_CHANNELS; i++) {
ymf262io->writeReg((i/OPL_CHANNELS), (i/9), 0x20+OPS[i%9], 0x1);
ymf262io->writeReg((i/OPL_CHANNELS), (i/9), 0x40+OPS[i%9], 0x10);
ymf262io->writeReg((i/OPL_CHANNELS), (i/9), 0x60+OPS[i%9], 0xF0);
ymf262io->writeReg((i/OPL_CHANNELS), (i/9), 0x80+OPS[i%9], 0x77);
ymf262io->writeReg((i/OPL_CHANNELS), (i/9), 0x20+OPS[i%9]+3, 0x1);
ymf262io->writeReg((i/OPL_CHANNELS), (i/9), 0x40+OPS[i%9]+3, 0x0);
ymf262io->writeReg((i/OPL_CHANNELS), (i/9), 0x60+OPS[i%9]+3, 0xF0);
ymf262io->writeReg((i/OPL_CHANNELS), (i/9), 0x80+OPS[i%9]+3, 0x77);
}
ymf262io->writeReg((0/OPL_CHANNELS), (0/9), 0xA0, 0x41);
ymf262io->writeReg((0/OPL_CHANNELS), (0/9), 0xB0, 0x12);
}
void SynthThread::opl3_set_instr(int opl_chn, int num)
{
struct soundbank_instr_data instr = soundBankData.at(num).instrData;
int add = 0; // addition to operator's multiple to prevent lower octave
// Validation
//qDebug("%d %d", instr.oplModulator.multiple, instr.oplCarrier.multiple);
if (instr.oplCarrier.multiple == 0 || instr.oplModulator.multiple == 0) {
add = 1;
}
qDebug("%d", opl_chn);
// Modulator Parameters
ymf262io->writeReg((opl_chn/18), ((opl_chn/9)%2), 0x20+OPS[opl_chn%9],
(instr.oplModulator.am << 7) |
(instr.oplModulator.vib << 6) |
(instr.oplModulator.eg << 5) |
(instr.oplModulator.ksr << 4) |
(instr.oplModulator.multiple+add));
ymf262io->writeReg((opl_chn/18), ((opl_chn/9)%2), 0x40+OPS[opl_chn%9], (instr.oplModulator.ksl << 6) |
instr.oplModulator.totalLevel);
ymf262io->writeReg((opl_chn/18), ((opl_chn/9)%2), 0x60+OPS[opl_chn%9], (instr.oplModulator.attack << 4) |
instr.oplModulator.decay);
ymf262io->writeReg((opl_chn/18), ((opl_chn/9)%2), 0x80+OPS[opl_chn%9], (instr.oplModulator.sustain << 4) |
instr.oplModulator.releaseRate);
ymf262io->writeReg((opl_chn/18), ((opl_chn/9)%2), 0xE0+OPS[opl_chn%9], instr.iModWaveSel);
// Carrier Parameters
ymf262io->writeReg((opl_chn/18), ((opl_chn/9)%2), 0x20+OPS[opl_chn%9]+3, (instr.oplCarrier.am << 7) |
(instr.oplCarrier.vib << 6) |
(instr.oplCarrier.eg << 5) |
(instr.oplCarrier.ksr << 4) |
(instr.oplCarrier.multiple+add));
ymf262io->writeReg((opl_chn/18), ((opl_chn/9)%2), 0x40+OPS[opl_chn%9]+3, (instr.oplCarrier.ksl << 6) |
instr.oplCarrier.totalLevel);
ymf262io->writeReg((opl_chn/18), ((opl_chn/9)%2), 0x60+OPS[opl_chn%9]+3, (instr.oplCarrier.attack << 4) |
instr.oplCarrier.decay);
ymf262io->writeReg((opl_chn/18), ((opl_chn/9)%2), 0x80+OPS[opl_chn%9]+3, (instr.oplCarrier.sustain << 4) |
instr.oplCarrier.releaseRate);
ymf262io->writeReg((opl_chn/18), ((opl_chn/9)%2), 0xE0+OPS[opl_chn%9]+3, instr.iCarWaveSel);
// Other Instrument Parameters
ymf262io->writeReg((opl_chn/18), ((opl_chn/9)%2), 0xC0+(opl_chn%9), 0x30 | (instr.oplModulator.feedback << 1) | (!instr.oplModulator.con));
}
void SynthThread::opl3_channel_off(int opl_chn)
{
int fnum = NOTES[USED[opl_chn].second%12];
int oct = (USED[opl_chn].second/12)-1;
//qDebug("%d", chan);
ymf262io->writeReg((opl_chn/18), (opl_chn/9)%2, 0xA0+(opl_chn%9), fnum&0xFF);
ymf262io->writeReg((opl_chn/18), (opl_chn/9)%2, 0xB0+(opl_chn%9), (oct<<2) | fnum>>8);
USED[opl_chn] = QPair<qint32, qint32>(-1, -1);
//chn--;
}
void SynthThread::loadSoundbank()
{
struct soundbank_instr_hdr *nameRecords;
struct soundbank_instr_data *dataRecords;
struct soundbank_data *data;
// Load soundbank file
QFile soundBank("STANDARD.BNK");
// Open soundbank file
soundBank.open(QFile::ReadOnly);
// Read header file
soundBank.read((char*)&soundBankHdr, sizeof(struct soundbank_hdr));
if (QString(soundBankHdr.signature).left(6) != "ADLIB-") {
soundBank.close();
return; // Invalid soundbank format
}
// Read instrument name records from the file
nameRecords = new struct soundbank_instr_hdr[soundBankHdr.numInstr];
soundBank.seek(soundBankHdr.offsetName);
soundBank.read((char*)nameRecords, sizeof(struct soundbank_instr_hdr) * soundBankHdr.numInstr);
// Read instrument name records from the file
dataRecords = new struct soundbank_instr_data[soundBankHdr.numInstr];
soundBank.seek(soundBankHdr.offsetData);
soundBank.read((char*)dataRecords, sizeof(struct soundbank_instr_data) * soundBankHdr.numInstr);
// Copy data from array to the instrument data QVector
soundBankData.resize(soundBankHdr.numInstr);
for (int i = 0; i < soundBankData.size(); i++) {
data = soundBankData.data();
memcpy((void*)&data[i].instrHeader, &nameRecords[i], sizeof(struct soundbank_instr_hdr));
memcpy((void*)&data[i].instrData, &dataRecords[i], sizeof(struct soundbank_instr_data));
}
soundBank.close();
delete[] nameRecords;
delete[] dataRecords;
}
void SynthThread::loadProgramMap()
{
QFile programsMapFile("program_map.txt");
QTextStream programsMapStream(&programsMapFile);
// Open the file
programsMapFile.open(QFile::ReadOnly);
// Read the file into the vector
while (!programsMapStream.atEnd()) {
programMap.append(programsMapStream.readLine().toInt());
}
// Close the file
programsMapFile.close();
}
void SynthThread::note_on(int chn, int n, int v)
{
int fnum = NOTES[n%12];
int oct = (n/12)-1;
//qDebug ("%x %d %x", fnum, oct, fnum&0xFF);
//NOTE[n] = chn+1;
if (v == 0) {
note_off(chn, n, v);
return;
}
// if (midiCtrlValues[chn][64] > 0) {
// midiSustainNotes[chn].insert(n, 0);
// }
int chan = find_note_chn(chn, n);
if (chan == -1) chan = find_next_chn(chn, n);
//chan = (chan + 1) % 9;
qDebug("%d", chan);
opl3_channel_off(chan);
opl3_set_instr(chan, INSTR[chn]);
ymf262io->writeReg((chan/18), (chan/9)%2, 0xA0+(chan%9), fnum&0xFF);
ymf262io->writeReg((chan/18), (chan/9)%2, 0xB0+(chan%9), 0x20 | (oct<<2) | fnum>>8);
USED[chan] = QPair<qint32, qint32>(chn, n);
midiActiveNotes[chn].insert(n, 1);
used_chns = (used_chns+1) > OPL_CHANNELS ? OPL_CHANNELS : (used_chns+1);
}
void SynthThread::program_change(int chn, int c)
{
if (programMap[c] == -1) {
INSTR[chn] = 494; // $-PIANO6
} else {
INSTR[chn] = programMap[c];
}
}
void SynthThread::handleSustain(int chn)
{
int note;
if (midiCtrlValues[chn][64] == 0) {
foreach (note, midiSustainNotes[chn].keys()) {
//qDebug("Note: %d", note);
if (midiActiveNotes[chn].value(note) == 0) {
//note_off(chn, note, 64);
opl3_channel_off(find_note_chn(chn, note));
midiSustainNotes[chn].remove(note);
}
}
midiSustainNotes[chn].clear();
}
}
void SynthThread::control_change(int chn, int c, int v)
{
midiCtrlValues[chn][c] = v;
switch (c) {
case 0x40:
qDebug("Sustain: %d %d", c, v);
handleSustain(chn);
break;
}
}
void SynthThread::start()
{
next_chn = -1;
used_chns = 0;
chan = 0;
last_force_chn = 0;
// Resize MIDI controller vectors to number of MIDI channels
midiCtrlValues.clear();
midiCtrlValues.resize(16);
// Resize sustain notes vector to number of MIDI channels
midiSustainNotes.clear();
midiSustainNotes.resize(16);
midiActiveNotes.clear();
midiActiveNotes.resize(16);
for (int c = 0; c < 16; c++) {
midiSustainNotes[c] = QHash<int, int>();
midiActiveNotes[c] = QHash<int, int>();
// Set size to 128 controller values
midiCtrlValues[c].clear();
midiCtrlValues[c].resize(128);
}
USED.clear();
USED.resize(OPL_CHANNELS);
for (int opl_chn = 0; opl_chn < OPL_CHANNELS; opl_chn++) {
USED[opl_chn] = qMakePair((qint32)-1, (qint32)-1);
}
ymf262io->reset();
synthBuffer->open(QIODevice::ReadWrite);
synthBuffer->seek(0);
isActive = true;
QThread::start();
}
void SynthThread::stop()
{
isActive = false;
while (isRunning()) {
msleep(10);
}
synthBuffer->buffer().clear();
synthBuffer->close();
while (synthBuffer->isOpen()) {
msleep(10);
}
}
void SynthThread::note_off(int chn, int n, int v)
{
if (midiCtrlValues[chn][64] > 0) {
midiSustainNotes[chn].insert(n, 1);
midiActiveNotes[chn].insert(n, 0);
return;
}
int chan = find_note_chn(chn, n);
if (chan == -1) return;
opl3_channel_off(chan);
midiActiveNotes[chn].insert(n, 0);
used_chns--;
}
int SynthThread::force_find_next_chn(int chn, int note)
{
int new_chn = ((++last_force_chn) % OPL_CHANNELS);
if (USED[new_chn] != QPair<qint32, qint32>(-1, -1)) {
//note_off(USED[new_chn].first, USED[new_chn].second, 64);
opl3_channel_off(new_chn);
//USED[new_chn] = QPair<qint32, qint32>(-1, -1);
}
return new_chn;
}
int SynthThread::find_next_chn(int chn, int note)
{
int new_chn = -1;
for (int i = 0; i < OPL_CHANNELS; i++) {
if (USED[i] == QPair<qint32, qint32>(-1, -1)) {
new_chn = i;
break;
}
}
if (new_chn == -1) {
new_chn = force_find_next_chn(chn, note);
}
return new_chn;
}
int SynthThread::find_note_chn(int chn, int n)
{
int ch = -1;
for (int i = 0; i < OPL_CHANNELS; i++) {
if (USED[i] == QPair<qint32, qint32>(chn, n)) {
ch = i;
break;
}
}
return ch;
}