Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[c9fef0]: src / xsynth_voice_blosc.h Maximize Restore History

Download this file

xsynth_voice_blosc.h    622 lines (544 with data), 20.2 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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/* Xsynth DSSI software synthesizer plugin
*
* Copyright (C) 2004, 2009 Sean Bolton.
*
* 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.
*/
/* Ah, the gentle tedium of moving all possible conditionals outside
* the inner loops!
*
* This file gets included four times from xsynth_voice_render.c, to
* define each of the four types of oscillator functions:
* - single1 (VCO 1 without hard sync)
* - master (VCO 1 as hard sync master)
* - single2 (VCO 2 without hard sync)
* - slave (VCO 2 as hard sync slave)
*
* As a example of how the BLOSC_THIS macro works, if BLOSC_SINGLE1 is
* defined when this file is included, then:
* BLOSC_THIS(sine, ...)
* gets replaced with:
* blosc_single1sine(...)
*/
#ifdef BLOSC_SINGLE1
/* #define BLOSC_THIS(x, ...) blosc_single1##x(__VA_ARGS__) */
#define BLOSC_THIS(x, args...) blosc_single1##x(args)
#define BLOSC_W_TABLE 0
#endif
#ifdef BLOSC_MASTER
#define BLOSC_THIS(x, args...) blosc_master##x(args)
#define BLOSC_W_TABLE 0
#endif
#ifdef BLOSC_SINGLE2
#define BLOSC_THIS(x, args...) blosc_single2##x(args)
#define BLOSC_W_TABLE 1
#endif
#ifdef BLOSC_SLAVE
#define BLOSC_THIS(x, args...) blosc_slave##x(args)
#define BLOSC_W_TABLE 1
#endif
/* ==== blosc_*sine functions ==== */
/* static inline */ void
#if BLOSC_W_TABLE
BLOSC_THIS(sine, unsigned long sample_count, xsynth_voice_t *voice,
struct blosc *osc, int index, float gain, float *wp)
#else
BLOSC_THIS(sine, unsigned long sample_count, xsynth_voice_t *voice,
struct blosc *osc, int index, float gain, float w)
#endif
{
unsigned long sample;
float pos = osc->pos;
#if BLOSC_W_TABLE
float w;
#endif
float frac;
int i;
if (osc->last_waveform != osc->waveform) {
pos = 0.0f;
/* if we valued alias-free startup over low startup time, we could do:
* pos -= w;
* blosc_place_slope_dd(voice->osc_audio, index, 0.0f, w, gain * 0.5f * M_2PI_F); */
osc->last_waveform = osc->waveform;
}
for (sample = 0; sample < sample_count; sample++) {
#if BLOSC_W_TABLE
w = wp[sample];
#endif
pos += w;
#ifdef BLOSC_SLAVE
if (voice->osc_sync[sample] >= 0.0f) { /* sync to master */
float eof_offset = voice->osc_sync[sample] * w;
float pos_at_reset = pos - eof_offset;
float out, slope;
pos = eof_offset;
/* calculate amplitude and slope approaching reset point */
if (pos_at_reset >= 1.0f) {
pos_at_reset -= 1.0f;
}
frac = pos_at_reset * WAVE_POINTS;
i = lrintf(frac - 0.5f);
frac -= (float)i;
i &= (WAVE_POINTS - 1);
out = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * frac;
i = (i + WAVE_POINTS / 4) & (WAVE_POINTS - 1);
slope = sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * frac;
/* now place reset DDs */
blosc_place_slope_dd(voice->osc_audio, index, pos, w, gain * M_2PI_F * (0.5f - slope));
blosc_place_step_dd(voice->osc_audio, index, pos, w, gain * (/* 0.0f */ - out));
} else
#endif /* slave */
if (pos >= 1.0f) {
pos -= 1.0f;
#ifdef BLOSC_MASTER
voice->osc_sync[sample] = pos / w;
} else {
voice->osc_sync[sample] = -1.0f;
#endif /* master */
}
frac = pos * WAVE_POINTS;
i = lrintf(frac - 0.5f);
frac -= (float)i;
voice->osc_audio[index + DD_SAMPLE_DELAY] += gain *
(sine_wave[i + 4] + (sine_wave[i + 5] - sine_wave[i + 4]) * frac);
index++;
}
osc->pos = pos;
}
/* ==== blosc_*tri functions ==== */
/* static inline */ void
#if BLOSC_W_TABLE
BLOSC_THIS(tri, unsigned long sample_count, xsynth_voice_t *voice,
struct blosc *osc, int index, float gain, float *wp)
#else
BLOSC_THIS(tri, unsigned long sample_count, xsynth_voice_t *voice,
struct blosc *osc, int index, float gain, float w)
#endif
{
unsigned long sample;
#if BLOSC_W_TABLE
float w;
#endif
int bp_high = osc->bp_high;
float pos = osc->pos,
pw, out, slope_delta;
if (osc->waveform == 1) { /* triangle */
pw = 0.5f;
slope_delta = gain * 4.0f;
} else { /* variable-slope triangle */
pw = osc->pw;
#if BLOSC_W_TABLE
w = *wp;
#endif
if (pw < w) pw = w; /* w is sample phase width */
else if (pw > 1.0f - w) pw = 1.0f - w;
slope_delta = gain * (1.0f / pw + 1.0f / (1.0f - pw));
}
if (osc->last_waveform != osc->waveform) {
if (osc->waveform == 1) { /* triangle */
pos = 0.25f;
} else { /* variable-slope triangle */
pos = 0.5f * pw;
}
/* if we valued alias-free startup over low startup time, we could do:
* pos -= w;
* blosc_place_slope_dd(voice->osc_audio, index, 0.0f, w, gain * 1.0f / *pw); */
bp_high = 1;
osc->last_waveform = osc->waveform;
}
for (sample = 0; sample < sample_count; sample++) {
#if BLOSC_W_TABLE
w = wp[sample];
#endif
pos += w;
#ifdef BLOSC_SLAVE
if (voice->osc_sync[sample] >= 0.0f) { /* sync to master */
float eof_offset = voice->osc_sync[sample] * w;
float pos_at_reset = pos - eof_offset;
pos = eof_offset;
/* place any DDs that may have occurred in subsample before reset */
if (bp_high) {
out = -0.5f + pos_at_reset / pw;
if (pos_at_reset >= pw) {
out = 0.5f - (pos_at_reset - pw) / (1.0f - pw);
blosc_place_slope_dd(voice->osc_audio, index, pos_at_reset - pw + eof_offset, w, -slope_delta);
bp_high = 0;
}
if (pos_at_reset >= 1.0f) {
pos_at_reset -= 1.0f;
out = -0.5f + pos_at_reset / pw;
blosc_place_slope_dd(voice->osc_audio, index, pos_at_reset + eof_offset, w, slope_delta);
bp_high = 1;
}
} else {
out = 0.5f - (pos_at_reset - pw) / (1.0f - pw);
if (pos_at_reset >= 1.0f) {
pos_at_reset -= 1.0f;
out = -0.5f + pos_at_reset / pw;
blosc_place_slope_dd(voice->osc_audio, index, pos_at_reset + eof_offset, w, slope_delta);
bp_high = 1;
}
if (bp_high && pos_at_reset >= pw) {
out = 0.5f - (pos_at_reset - pw) / (1.0f - pw);
blosc_place_slope_dd(voice->osc_audio, index, pos_at_reset - pw + eof_offset, w, -slope_delta);
bp_high = 0;
}
}
/* now place reset DDs */
if (!bp_high)
blosc_place_slope_dd(voice->osc_audio, index, pos, w, slope_delta);
blosc_place_step_dd(voice->osc_audio, index, pos, w, gain * (-0.5f - out));
out = -0.5f + pos / pw;
bp_high = 1;
if (pos >= pw) {
out = 0.5f - (pos - pw) / (1.0f - pw);
blosc_place_slope_dd(voice->osc_audio, index, pos - pw, w, -slope_delta);
bp_high = 0;
}
} else
#endif /* slave */
if (bp_high) {
out = -0.5f + pos / pw;
if (pos >= pw) {
out = 0.5f - (pos - pw) / (1.0f - pw);
blosc_place_slope_dd(voice->osc_audio, index, pos - pw, w, -slope_delta);
bp_high = 0;
}
if (pos >= 1.0f) {
pos -= 1.0f;
#ifdef BLOSC_MASTER
voice->osc_sync[sample] = pos / w;
#endif /* master */
out = -0.5f + pos / pw;
blosc_place_slope_dd(voice->osc_audio, index, pos, w, slope_delta);
bp_high = 1;
#ifdef BLOSC_MASTER
} else {
voice->osc_sync[sample] = -1.0f;
#endif /* master */
}
} else {
out = 0.5f - (pos - pw) / (1.0f - pw);
if (pos >= 1.0f) {
pos -= 1.0f;
#ifdef BLOSC_MASTER
voice->osc_sync[sample] = pos / w;
#endif /* master */
out = -0.5f + pos / pw;
blosc_place_slope_dd(voice->osc_audio, index, pos, w, slope_delta);
bp_high = 1;
#ifdef BLOSC_MASTER
} else {
voice->osc_sync[sample] = -1.0f;
#endif /* master */
}
if (bp_high && pos >= pw) {
out = 0.5f - (pos - pw) / (1.0f - pw);
blosc_place_slope_dd(voice->osc_audio, index, pos - pw, w, -slope_delta);
bp_high = 0;
}
}
voice->osc_audio[index + DD_SAMPLE_DELAY] += gain * out;
index++;
}
osc->pos = pos;
osc->bp_high = bp_high;
}
/* ==== blosc_*sawup functions ==== */
/* static inline */ void
#if BLOSC_W_TABLE
BLOSC_THIS(sawup, unsigned long sample_count, xsynth_voice_t *voice,
struct blosc *osc, int index, float gain, float *wp)
#else
BLOSC_THIS(sawup, unsigned long sample_count, xsynth_voice_t *voice,
struct blosc *osc, int index, float gain, float w)
#endif
{
unsigned long sample;
#if BLOSC_W_TABLE
float w;
#endif
float pos = osc->pos;
if (osc->last_waveform != osc->waveform) {
/* this would be the cleanest startup:
* pos = 0.5f - w;
* blosc_place_slope_dd(voice->osc_audio, index, 0.0f, w, 1.0f);
* but we have to match the phase of the original Xsynth code: */
pos = 0.0f;
osc->last_waveform = osc->waveform;
}
for (sample = 0; sample < sample_count; sample++) {
#if BLOSC_W_TABLE
w = wp[sample];
#endif
pos += w;
#ifdef BLOSC_SLAVE
if (voice->osc_sync[sample] >= 0.0f) { /* sync to master */
float eof_offset = voice->osc_sync[sample] * w;
float pos_at_reset = pos - eof_offset;
pos = eof_offset;
/* place any DD that may have occurred in subsample before reset */
if (pos_at_reset >= 1.0f) {
pos_at_reset -= 1.0f;
blosc_place_step_dd(voice->osc_audio, index, pos_at_reset + eof_offset, w, -gain);
}
/* now place reset DD */
blosc_place_step_dd(voice->osc_audio, index, pos, w, -gain * pos_at_reset);
} else
#endif /* slave */
if (pos >= 1.0f) {
pos -= 1.0f;
#ifdef BLOSC_MASTER
voice->osc_sync[sample] = pos / w;
#endif /* master */
blosc_place_step_dd(voice->osc_audio, index, pos, w, -gain);
#ifdef BLOSC_MASTER
} else {
voice->osc_sync[sample] = -1.0f;
#endif /* master */
}
voice->osc_audio[index + DD_SAMPLE_DELAY] += gain * (-0.5f + pos);
index++;
}
osc->pos = pos;
}
/* ==== blosc_*sawdown functions ==== */
/* static inline */ void
#if BLOSC_W_TABLE
BLOSC_THIS(sawdown, unsigned long sample_count, xsynth_voice_t *voice,
struct blosc *osc, int index, float gain, float *wp)
#else
BLOSC_THIS(sawdown, unsigned long sample_count, xsynth_voice_t *voice,
struct blosc *osc, int index, float gain, float w)
#endif
{
unsigned long sample;
#if BLOSC_W_TABLE
float w;
#endif
float pos = osc->pos;
if (osc->last_waveform != osc->waveform) {
/* this would be the cleanest startup:
* pos = 0.5f - w;
* blosc_place_slope_dd(voice->osc_audio, index, 0.0f, w, -1.0f);
* but we have to match the phase of the original Xsynth code: */
pos = 0.0f;
osc->last_waveform = osc->waveform;
}
for (sample = 0; sample < sample_count; sample++) {
#if BLOSC_W_TABLE
w = wp[sample];
#endif
pos += w;
#ifdef BLOSC_SLAVE
if (voice->osc_sync[sample] >= 0.0f) { /* sync to master */
float eof_offset = voice->osc_sync[sample] * w;
float pos_at_reset = pos - eof_offset;
pos = eof_offset;
/* place any DD that may have occurred in subsample before reset */
if (pos_at_reset >= 1.0f) {
pos_at_reset -= 1.0f;
blosc_place_step_dd(voice->osc_audio, index, pos_at_reset + eof_offset, w, gain);
}
/* now place reset DD */
blosc_place_step_dd(voice->osc_audio, index, pos, w, gain * pos_at_reset);
} else
#endif /* slave */
if (pos >= 1.0f) {
pos -= 1.0f;
#ifdef BLOSC_MASTER
voice->osc_sync[sample] = pos / w;
#endif /* master */
blosc_place_step_dd(voice->osc_audio, index, pos, w, gain);
#ifdef BLOSC_MASTER
} else {
voice->osc_sync[sample] = -1.0f;
#endif /* master */
}
voice->osc_audio[index + DD_SAMPLE_DELAY] += gain * (0.5f - pos);
index++;
}
osc->pos = pos;
}
/* ==== blosc_*rect functions ==== */
/* static inline */ void
#if BLOSC_W_TABLE
BLOSC_THIS(rect, unsigned long sample_count, xsynth_voice_t *voice,
struct blosc *osc, int index, float gain, float *wp)
#else
BLOSC_THIS(rect, unsigned long sample_count, xsynth_voice_t *voice,
struct blosc *osc, int index, float gain, float w)
#endif
{
unsigned long sample;
#if BLOSC_W_TABLE
float w;
#endif
int bp_high = osc->bp_high;
float pos = osc->pos,
pw,
halfgain = gain * 0.5f,
out = (bp_high ? halfgain : -halfgain);
if (osc->waveform == 4) { /* square wave */
pw = 0.5f;
} else { /* variable-width pulse */
pw = osc->pw;
#if BLOSC_W_TABLE
w = *wp;
#endif
if (pw < w) pw = w; /* w is sample phase width */
else if (pw > 1.0f - w) pw = 1.0f - w;
}
if (osc->last_waveform != osc->waveform) {
pos = 0.0f;
/* for waveform 5, variable-width pulse, we could do DC compensation with:
* out = halfgain * (1.0f - pw);
* but that doesn't work well with highly modulated hard sync. Instead,
* we keep things in the range [-0.5f, 0.5f]. */
out = halfgain;
bp_high = 1;
/* if we valued alias-free startup over low startup time, we could do:
* pos -= w;
* blosc_place_step_dd(voice->osc_audio, index, 0.0f, w, halfgain); */
osc->last_waveform = osc->waveform;
}
for (sample = 0; sample < sample_count; sample++) {
#if BLOSC_W_TABLE
w = wp[sample];
#endif
pos += w;
#ifdef BLOSC_SLAVE
if (voice->osc_sync[sample] >= 0.0f) { /* sync to master */
float eof_offset = voice->osc_sync[sample] * w;
float pos_at_reset = pos - eof_offset;
pos = eof_offset;
/* place any DDs that may have occurred in subsample before reset */
if (bp_high) {
if (pos_at_reset >= pw) {
blosc_place_step_dd(voice->osc_audio, index, pos_at_reset - pw + eof_offset, w, -gain);
bp_high = 0;
out = -halfgain;
}
if (pos_at_reset >= 1.0f) {
pos_at_reset -= 1.0f;
blosc_place_step_dd(voice->osc_audio, index, pos_at_reset + eof_offset, w, gain);
bp_high = 1;
out = halfgain;
}
} else {
if (pos_at_reset >= 1.0f) {
pos_at_reset -= 1.0f;
blosc_place_step_dd(voice->osc_audio, index, pos_at_reset + eof_offset, w, gain);
bp_high = 1;
out = halfgain;
}
if (bp_high && pos_at_reset >= pw) {
blosc_place_step_dd(voice->osc_audio, index, pos_at_reset - pw + eof_offset, w, -gain);
bp_high = 0;
out = -halfgain;
}
}
/* now place reset DD */
if (!bp_high) {
blosc_place_step_dd(voice->osc_audio, index, pos, w, gain);
bp_high = 1;
out = halfgain;
}
if (pos >= pw) {
blosc_place_step_dd(voice->osc_audio, index, pos - pw, w, -gain);
bp_high = 0;
out = -halfgain;
}
} else
#endif /* slave */
if (bp_high) {
if (pos >= pw) {
blosc_place_step_dd(voice->osc_audio, index, pos - pw, w, -gain);
bp_high = 0;
out = -halfgain;
}
if (pos >= 1.0f) {
pos -= 1.0f;
#ifdef BLOSC_MASTER
voice->osc_sync[sample] = pos / w;
#endif /* master */
blosc_place_step_dd(voice->osc_audio, index, pos, w, gain);
bp_high = 1;
out = halfgain;
#ifdef BLOSC_MASTER
} else {
voice->osc_sync[sample] = -1.0f;
#endif /* master */
}
} else {
if (pos >= 1.0f) {
pos -= 1.0f;
#ifdef BLOSC_MASTER
voice->osc_sync[sample] = pos / w;
#endif /* master */
blosc_place_step_dd(voice->osc_audio, index, pos, w, gain);
bp_high = 1;
out = halfgain;
#ifdef BLOSC_MASTER
} else {
voice->osc_sync[sample] = -1.0f;
#endif /* master */
}
if (bp_high && pos >= pw) {
blosc_place_step_dd(voice->osc_audio, index, pos - pw, w, -gain);
bp_high = 0;
out = -halfgain;
}
}
voice->osc_audio[index + DD_SAMPLE_DELAY] += out;
index++;
}
osc->pos = pos;
osc->bp_high = bp_high;
}
/* ==== blosc_* dispatch function ==== */
static inline void
#if BLOSC_W_TABLE
BLOSC_THIS(, unsigned long sample_count, xsynth_voice_t *voice,
struct blosc *osc, int index, float gain, float *w)
#else
BLOSC_THIS(, unsigned long sample_count, xsynth_voice_t *voice,
struct blosc *osc, int index, float gain, float w)
#endif
{
switch (osc->waveform) {
default:
case 0: /* sine wave */
BLOSC_THIS(sine, sample_count, voice, osc, index, gain, w);
break;
case 1: /* triangle wave */
case 6: /* variable-slope triangle wave */
BLOSC_THIS(tri, sample_count, voice, osc, index, gain, w);
break;
case 2: /* up sawtooth wave */
BLOSC_THIS(sawup, sample_count, voice, osc, index, gain, w);
break;
case 3: /* down sawtooth wave */
BLOSC_THIS(sawdown, sample_count, voice, osc, index, gain, w);
break;
case 4: /* square wave */
case 5: /* pulse wave */
BLOSC_THIS(rect, sample_count, voice, osc, index, gain, w);
break;
}
}
#undef BLOSC_THIS
#undef BLOSC_W_TABLE