[go: up one dir, main page]

Menu

[r138]: / trunk / synth.cpp  Maximize  Restore  History

Download this file

476 lines (428 with data), 14.9 kB

  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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include "synth.h"
#include <stdlib.h>
#include <QDebug>
//#define DEBUG_TUNING 1
#ifdef DEBUG_TUNING
#include <math.h>
#endif
static const uint tune12[] = { 44000, 46616, 49388, 52325, 55437, 58733, 62225, 65926, 69846, 73999, 78399, 83061 };
Synthesizer::Synthesizer(QObject *parent) : QIODevice(parent),
forceBuffer(true),
gateState(false),
deviceId(0),
sizeLimit(0)
{
setSampleRate();
int sc = -6;
uint j = 3;
for(uint i = 0; i < sizeof(keytuning) / sizeof(*keytuning); ++i) {
uint tune = tune12[j];
if (sc > 0)
tune <<= sc;
else if (sc < 0)
tune = (tune + (1 << (-sc-1))) >> -sc;
keytuning[i] = tune;
if (++j >= sizeof(tune12) / sizeof(*tune12)) {
j = 0; ++sc;
}
}
}
Synthesizer::~Synthesizer()
{
}
uint Synthesizer::keyTune(uint key)
{
return keytuning[key];
}
void Synthesizer::setSampleRate(uint rate)
{
if (rate == 0)
rate = 44100;
srate = rate;
tuneConst = ((((10 << 27) + srate / 2) / srate) *
(((1U << 23) + 500) / 1000) + (1 << 13)
) >> 14;
}
void Synthesizer::setSizeLimit(uint limit)
{
sizeLimit = limit;
}
void Synthesizer::setKeyTuning(uint key, uint tune)
{
keytuning[key] = tune;
for(int ch = 0; ch < channelData.count(); ++ch) {
Channel &chd = channelData[ch];
for(uint ti = 0; ti < chd.threadsCount; ++ti) {
Thread &thr = threadData[chd.firstThread + ti];
if (thr.currAmp != 0 && thr.assignedKey == key) {
thr.targetFreq = calcTuning(ch, key);
// thr.legatoRate = (thr.targetFreq + (1 << 5)) >> 6;
}
}
}
}
void Synthesizer::setChannelsCount(uint count)
{
channelData.resize(count);
threadData.resize(count);
for(uint i = 0; i < count; ++i) {
channelData[i].firstThread = i;
channelData[i].threadsCount = 1;
}
}
void Synthesizer::setChannelPoly(int channel, uint poly)
{
int channelCount = channelData.count();
if (channel < 0 || channel >= channelCount)
return;
channelData[channel].threadsCount = poly;
uint threadCount = 0;
for(int i = 0; i < channelCount; ++i) {
channelData[i].firstThread = threadCount;
threadCount += channelData[i].threadsCount;
}
threadData.resize(threadCount);
}
void Synthesizer::setChannelMode(int channel, Synthesizer::ChannelMode mode)
{
if (channel < 0 || channel >= channelData.count())
return;
channelData[channel].chmode = mode;
}
void Synthesizer::setChannelSample(int channel, QString name, uint attack, uint decay, uint release, uint periods)
{
if (channel < 0 || channel >= channelData.count())
return;
Channel &chd = channelData[channel];
chd.attackRate = attack;
chd.decayRate = decay;
chd.releaseRate = release;
chd.samplePeriods = (periods == 0 ? 1 : periods);
chd.sampleRes = new QResource(name);
if (chd.sampleRes->isValid()) {
chd.sample = reinterpret_cast<const qint16 *>(chd.sampleRes->data());
chd.sampleLen = chd.sampleRes->size() / 2;
}
mute(channel);
}
void Synthesizer::setChannelVolume(int channel, int volume)
{
if (channel < 0 || channel >= channelData.count())
return;
channelData[channel].volume = volumeToAmp(volume) >> 16;
}
void Synthesizer::setChannelLegatoRate(int channel, int rate)
{
if (channel < 0 || channel >= channelData.count())
return;
uint rr = volumeToAmp(rate);
rr >>= 22;
channelData[channel].legatoRate = static_cast<uint>(rr);
}
void Synthesizer::clearRhythmEvents(int channel)
{
if (channel < 0 || channel >= channelData.count())
return;
channelData[channel].rhythmPos = 0;
channelData[channel].rhythmTime = 0;
channelData[channel].rhythm.clear();
}
void Synthesizer::addRhythmEvent(int channel, uint key, uint timeStep)
{
if (channel < 0 || channel >= channelData.count())
return;
if (timeStep > 0)
channelData[channel].rhythm.append(QPair<uint, int>(timeStep * srate, key));
}
bool Synthesizer::reset()
{
forceBuffer = true;
return true;
}
uint Synthesizer::volumeToAmp(int vol)
{
uint amp = 0;
if (vol > 60) vol = 60;
if (vol > 0) {
static uint ampt6[] = { 287, 323, 362, 406, 456, 512 };
vol += 5;
int vs = vol / 6, vm = vol % 6;
amp = (8388607U * ampt6[vm]) >> (10 - vs);
}
return amp;
}
void Synthesizer::keyDown(int channel, uint key, uint velocity)
{
if (channel < 0 || channel >= channelData.count() || key >= 128)
return;
if (velocity == 0) {
releaseKey(channel, key);
return;
}
Channel &ch = channelData[channel];
uint firstThread = ch.firstThread;
uint threadsCount = ch.threadsCount;
if (velocity > 127)
velocity = 127;
// Select free thread for channel
int thread = -1, prio = 0;
for(uint ti = 0; ti < threadsCount; ++ti) {
int th = static_cast<int>(firstThread + ti);
Thread &thr = threadData[th];
if (thr.assignedKey == key) {
if (prio < 2) {
thread = th;
prio = 2;
}
} else if (thr.targetAmp != 0) {
if (ch.chmode == Legato) {
if (prio < 3) {
thread = th;
prio = 3;
}
ch.unassigned.push(thr.assignedKey);
} else if (ch.chmode == Mono) {
thr.targetAmp = 0;
ch.unassigned.push(thr.assignedKey);
}
} else {
if (prio < 1 || (prio == 1 && thr.currAmp < threadData[thread].currAmp)) {
thread = th;
prio = 1;
}
}
}
if (thread != -1) {
assignKeyToThread(channel, thread, key, velocity);
#ifdef DEBUG_TUNING
static uint lastf = 0;
qDebug() << channel << key << threadData[thread].targetFreq
<< (lastf != 0 ? 72*log2(static_cast<double>(threadData[thread].targetFreq) / lastf) : 0);
lastf = threadData[thread].targetFreq;
#endif
} else {
ch.unassigned.push(key);
}
}
void Synthesizer::keyDown(uint key, uint velocity)
{
keyDown(0, key, velocity);
}
void Synthesizer::mute(int channel)
{
if (channel >= channelData.count())
return;
int t1 = (channel < 0) ? 0 : channelData[channel].firstThread;
int t2 = (channel < 0) ? threadData.count() - 1 : t1 + channelData[channel].threadsCount;
for(int th = t1; th < t2; ++th) {
threadData[th].targetAmp = 0;
threadData[th].ampRate = 8;
}
if (channel < 0) {
for(int ch = 0; ch < channelData.count(); ++ch)
channelData[ch].unassigned.clear();
} else {
channelData[channel].unassigned.clear();
}
}
qint64 Synthesizer::readData(char *data, qint64 maxSize)
{
uint dataOffset = 0;
/*
if (forceBuffer) {
if (sizeLimit != 0 && maxSize > sizeLimit * 2)
maxSize = sizeLimit * 2;
forceBuffer = false;
} else if (sizeLimit != 0 && maxSize > sizeLimit) {
maxSize = sizeLimit;
}
*/
while(dataOffset < maxSize) {
bool gate = false;
for(int ch = 0; dataOffset < maxSize && ch < channelData.count(); ++ch) {
int sample = 0;
uint threadsCount = channelData[ch].threadsCount;
uint sampleLen = channelData[ch].sampleLen;
if (threadsCount == 0 || sampleLen == 0)
continue;
uint volume = channelData[ch].volume;
int rhytmKey = -1;
if (!channelData[ch].rhythm.isEmpty()) {
int pos = channelData[ch].rhythmPos;
uint time = channelData[ch].rhythmTime;
time += 1000;
if (time >= channelData[ch].rhythm[pos].first) {
rhytmKey = channelData[ch].rhythm[pos].second;
time -= channelData[ch].rhythm[pos].first;
if (++pos >= channelData[ch].rhythm.count())
pos = 0;
channelData[ch].rhythmPos = pos;
}
channelData[ch].rhythmTime = time;
}
for(uint ti = 0; ti < threadsCount; ++ti) {
int th = channelData[ch].firstThread + ti;
if (rhytmKey != -1) {
assignKeyToThread(ch, th, rhytmKey, 127);
/*
threadData[th].targetAmp = 4294967295U;
threadData[th].ampRate = channelData[ch].attackRate;
uint z = (tuneConst * keyTuning[rhytmKey] + (1 << 10)) >> 11;
z = (z * channelData[ch].sampleLen / channelData[ch].samplePeriods + (1 << 8)) >> 9;
threadData[th].targetFreq = z;
threadData[th].legatoRate = (z + (1 << 5)) >> 6;
*/
}
uint amp = threadData[th].currAmp;
uint frq = threadData[th].currFreq;
uint toFrq = threadData[th].targetFreq;
// Do legato
if (toFrq != frq) {
uint lstep = threadData[th].legatoStep;
if (amp == 0)
frq = toFrq;
else if (toFrq > frq) {
frq += lstep;
if (frq > toFrq)
frq = toFrq;
} else {
frq -= lstep;
if (frq < toFrq)
frq = toFrq;
}
threadData[th].currFreq = frq;
}
// Do envelope
uint toAmp = threadData[th].targetAmp;
if (toAmp != amp) {
if (toAmp > amp) {
// Attack
uint newAmp = amp + qMax((toAmp - amp) >> (18 - threadData[th].ampRate), 128U);
if (newAmp >= toAmp || newAmp < amp) {
// Decay
amp = toAmp;
if (channelData[ch].decayRate != 0) {
threadData[th].targetAmp = 0;
threadData[th].ampRate = channelData[ch].decayRate;
}
} else {
amp = newAmp;
}
} else {
// Release
uint newAmp = amp - qMax((amp - toAmp) >> (18 - threadData[th].ampRate), 128U);
if (newAmp < toAmp || newAmp > amp) {
amp = toAmp;
} else
amp = newAmp;
if (amp == 0) {
threadData[th].samplePtr = 0;
emit keyStateChanged(ch, threadData[th].assignedKey, false);
}
}
threadData[th].currAmp = amp;
}
if (amp != 0) {
// Get sample with interpolation
uint ptr = threadData[th].samplePtr;
if (ptr >= sampleLen << 16)
ptr = 0;
uint p1 = ptr >> 16;
uint p2 = (ptr >> 4) & 0x0FFF;
int s1 = channelData[ch].sample[p1] * (0x1000 - p2);
++p1;
if (p1 >= sampleLen)
p1 = 0;
s1 += channelData[ch].sample[p1] * p2;
s1 = (s1 + (1 << 11)) >> 12;
sample += (((amp >> 16) * volume) >> 20) * s1;
ptr += frq;
if (ptr >= sampleLen << 16)
ptr -= sampleLen << 16;
threadData[th].samplePtr = ptr;
gate = true;
}
}
sample = (sample + (1 << 11)) >> 12;
if (sample > 32767)
sample = 32767;
else if (sample < -32767)
sample = -32767;
*reinterpret_cast<qint16 *>(&data[dataOffset]) = static_cast<qint16>(sample);
dataOffset += 2;
}
if (gate != gateState) {
gateState = gate;
emit gateStateChanged(gateState, deviceId);
}
}
return dataOffset;
}
qint64 Synthesizer::writeData(const char *data, qint64 maxSize)
{
Q_UNUSED(data)
Q_UNUSED(maxSize)
return 0;
}
uint Synthesizer::calcTuning(uint channel, uint key)
{
Channel &ch = channelData[channel];
uint z = (tuneConst * keytuning[key] + (1 << 12)) >> 13;
z = (z * ch.sampleLen / ch.samplePeriods + (1 << 6)) >> 7;
return z;
}
void Synthesizer::assignKeyToThread(uint channel, uint thread, uint key, uint velocity)
{
Channel &ch = channelData[channel];
Thread &thr = threadData[thread];
if (thr.assignedKey != 0 && thr.currAmp != 0 && thr.assignedKey != key && thr.targetAmp != 0) {
emit keyStateChanged(channel, thr.assignedKey, false);
}
thr.targetAmp = velocity * 33818640U;
thr.ampRate = ch.attackRate;
thr.assignedKey = key;
thr.assignedVelocity = velocity;
thr.targetFreq = calcTuning(channel, key);
uint step = thr.targetFreq > thr.currFreq ?
thr.targetFreq - thr.currFreq :
thr.currFreq - thr.targetFreq;
step = (step + (1 << 5)) >> 6;
if (ch.decayRate == 0)
step = (step * ch.legatoRate + (1 << 11)) >> 11;
if (step == 0)
step = 1;
thr.legatoStep = step;
emit keyStateChanged(channel, key, true);
//qDebug() << channel << thread << thr.targetFreq << step;
}
bool Synthesizer::releaseKey(int channel, uint key)
{
if (channel < 0 || channel >= channelData.count() || key >= 128)
return false;
bool ret = false;
Channel &ch = channelData[channel];
uint firstThread = ch.firstThread;
uint threadsCount = ch.threadsCount;
int i = ch.unassigned.indexOf(key);
if (i >= 0) {
ch.unassigned.remove(i);
ret = true;
}
for(uint ti = 0; ti < threadsCount; ++ti) {
uint th = firstThread + ti;
Thread &thr = threadData[th];
if (thr.assignedKey == key) {
if (thr.currAmp != 0)
emit keyStateChanged(channel, key, false);
if (thr.targetAmp != 0) {
thr.targetAmp = 0;
thr.ampRate = ch.releaseRate;
if (!ch.unassigned.empty()) {
assignKeyToThread(channel, th, ch.unassigned.pop(), thr.assignedVelocity); // FIXME: velocity lost
}
ret = true;
}
}
}
return ret;
}