-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtable.c
490 lines (377 loc) · 11.4 KB
/
gtable.c
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
/**
* Copyright (c) 2009 Richard Dobson
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
// gtable.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "gtable.h"
static void norm_gtable(GTABLE* gtable);
GTABLE* new_gtable(unsigned long length)
{
unsigned long i;
GTABLE* gtable = NULL;
if (length == 0) return NULL;
gtable = (GTABLE*) malloc(sizeof(GTABLE));
if (gtable == NULL) return NULL;
gtable->table = (double*) malloc((length+1) * sizeof(double));
if (gtable->table == NULL) {
free(gtable);
return NULL;
}
gtable->length = length;
for (i = 0; i <= length; i++) gtable->table[i] = 0.0;
return gtable;
}
void gtable_free(GTABLE** gtable)
{
if (gtable && *gtable && (*gtable)->table) {
free((*gtable)->table);
free(*gtable);
*gtable = NULL;
}
}
void norm_gtable(GTABLE* gtable)
{
unsigned long i;
double val, maxamp = 0.0;
double* table = gtable->table;
for (i = 0; i < gtable->length; i++) {
val = fabs(table[i]);
if (maxamp < val) maxamp = val;
}
maxamp = 1.0 / maxamp;
for (i = 0; i < gtable->length; i++) table[i] *= maxamp;
table[i] = table[0];
}
GTABLE* new_sine(unsigned long length)
{
unsigned long i;
double step;
GTABLE* gtable = NULL;
if (length == 0) return NULL;
gtable = new_gtable(length);
if (gtable == NULL) return NULL;
// generate sinewave
step = TWOPI / length;
for (i = 0; i < length; i++) {
gtable->table[i] = sin(step * i);
}
gtable->table[i] = gtable->table[0];
// guard point
gtable->length = length;
return gtable;
}
GTABLE* new_triangle(unsigned long length, unsigned long nharms)
{
unsigned long i, j;
double step, amp;
GTABLE* gtable;
int harmonic = 1;
if (length == 0 || nharms == 0 || nharms >= length / 2) return NULL;
gtable = new_gtable(length);
if (gtable == NULL) return NULL;
step = TWOPI / length;
for (i = 0; i < nharms; i++) {
amp = 1.0 / (harmonic * harmonic);
for (j = 0; j < length; j++) gtable->table[j] += amp * cos(step * harmonic * j);
harmonic += 2;
}
norm_gtable(gtable);
return gtable;
}
GTABLE* new_square(unsigned long length, unsigned long nharms)
{
unsigned long i, j;
double step, amp;
GTABLE* gtable;
int harmonic = 1;
if (length == 0 || nharms == 0 || nharms >= length / 2) return NULL;
gtable = new_gtable(length);
if (gtable == NULL) return NULL;
step = TWOPI / length;
for (i = 0; i < nharms; i++) {
amp = 1.0 / harmonic;
for (j = 0; j < gtable->length; j++) gtable->table[j] += amp * sin(step * harmonic * j);
harmonic+=2;
}
norm_gtable(gtable);
return gtable;
}
GTABLE* new_saw(unsigned long length,unsigned long nharms, int up)
{
unsigned long i, j;
double step, val, amp = 1.0;
GTABLE* gtable;
int harmonic = 1;
if (length == 0 || nharms == 0 || nharms >= length / 2) return NULL;
gtable = new_gtable(length);
if (gtable == NULL) return NULL;
step = TWOPI / length;
if (up) amp = -1;
for (i = 0; i < nharms; i++) {
val = amp / harmonic;
for (j = 0; j < gtable->length; j++) gtable->table[j] += val * sin(step * harmonic * j);
harmonic++;
}
norm_gtable(gtable);
return gtable;
}
GTABLE* new_pulse(unsigned long length, unsigned long nharms)
{
unsigned long i, j;
double step;
GTABLE* gtable;
int harmonic = 1;
if (length == 0 || nharms == 0 || nharms >= length / 2) return NULL;
gtable = new_gtable(length);
if (gtable == NULL) return NULL;
step = TWOPI / length;
for (i = 0; i < nharms; i++) {
for (j = 0; j < gtable->length; j++) gtable->table[j] += cos(step * harmonic * j);
harmonic++;
}
norm_gtable(gtable);
return gtable;
}
GTABLE* new_instr(unsigned long length, double amps[], unsigned long nharms)
{
unsigned long i, j;
double step;
GTABLE* gtable;
int harmonic = 1;
if (length == 0 || nharms == 0 || nharms >= length / 2) return NULL;
gtable = new_gtable(length);
if (gtable == NULL) return NULL;
step = TWOPI / length;
for (i = 0; i < nharms; i++) {
for (j = 0; j < gtable->length; j++) gtable->table[j] += amps[i] * sin(step * harmonic * j);
harmonic++;
}
norm_gtable(gtable);
return gtable;
}
void gen_pulse(GTABLE* gtable, unsigned long nharms)
{
unsigned long i, j, harmonic = 1;
double step;
step = TWOPI / gtable->length;
for (i = 0; i < nharms; i++) {
for (j = 0; j < gtable->length; j++) gtable->table[j] += cos(step * harmonic * j);
harmonic++;
}
}
OSCILT* new_oscilt(unsigned long srate, const GTABLE* gtable, double phase)
{
OSCILT* p_osc;
// do we have a good GTABLE?
if (gtable == NULL || gtable->table== NULL || gtable->length == 0) return NULL;
p_osc = (OSCILT *) malloc(sizeof(OSCILT));
if (p_osc== NULL) return NULL;
// init the osc...
p_osc->osc.curfreq = 0.0;
p_osc->osc.curphase = gtable->length * phase;
p_osc->osc.incr = 0.0;
// then the table-specific things
p_osc->gtable = gtable;
p_osc->dtablen = (double) gtable->length;
p_osc->sizeovrsr = p_osc->dtablen / (double) srate;
return p_osc;
}
double tabtick(OSCILT* p_osc, double freq)
{
int index = (int) (p_osc->osc.curphase);
double val;
double dtablen = p_osc->dtablen, curphase = p_osc->osc.curphase;
double* table = p_osc->gtable->table;
if (p_osc->osc.curfreq != freq) {
p_osc->osc.curfreq = freq;
p_osc->osc.incr = p_osc->sizeovrsr * p_osc->osc.curfreq;
}
curphase += p_osc->osc.incr;
while (curphase >= dtablen) curphase -= dtablen;
// allow negative frequencies!
while (curphase < 0.0) curphase += dtablen;
p_osc->osc.curphase = curphase;
val = table[index];
return val ;
}
double tabitick(OSCILT* p_osc, double freq)
{
int base_index = (int) p_osc->osc.curphase;
unsigned long next_index = base_index + 1;
double frac, slope, val;
double dtablen = p_osc->dtablen, curphase = p_osc->osc.curphase;
double* table = p_osc->gtable->table;
if (p_osc->osc.curfreq != freq) {
p_osc->osc.curfreq = freq;
p_osc->osc.incr = p_osc->sizeovrsr * p_osc->osc.curfreq;
}
frac = curphase - base_index;
val = table[base_index];
slope = table[next_index] - val;
val += (frac * slope);
curphase += p_osc->osc.incr;
// curphase = fmod(curphase,dtablen);
// the while loop permits ferocious aliasing!
while (curphase >= dtablen) curphase -= dtablen;
while (curphase < 0.0) curphase += dtablen;
p_osc->osc.curphase = curphase;
return val;
}
/*********** SINE **************/
double sinetick(OSCIL* p_osc, double freq)
{
double val;
if (p_osc->curfreq != freq) {
p_osc->curfreq = freq;
p_osc->incr = p_osc->twopiovrsr * freq;
}
val = sin(p_osc->curphase);
p_osc->curphase += p_osc->incr;
while (p_osc->curphase > TWOPI) p_osc->curphase -= TWOPI;
while (p_osc->curphase < 0.0) p_osc->curphase += TWOPI;
return val;
}
// Csound style vector processing - expects amp and freq trajectories for this block in amp, freq buffers */
void sinefbuf(OSCIL* p_osc, float* buffer, unsigned long nframes, unsigned long nchans,
const double* amp, const double* freq)
{
float val;
unsigned long i, j;
for (i = 0; i < nframes; i++) {
val = (float)(*amp++ * sin(p_osc->curphase));
if (p_osc->curfreq != *freq) {
p_osc->curfreq = *freq;
p_osc->incr = p_osc->twopiovrsr * *freq;
freq++;
}
for (j = 0; j < nchans; j++) *buffer++ = val;
p_osc->curphase += p_osc->incr;
while (p_osc->curphase > TWOPI) p_osc->curphase -= TWOPI;
while (p_osc->curphase < 0.0) p_osc->curphase += TWOPI;
}
}
void tabinbuf(OSCILT* p_oscs[], unsigned long noscs, float* buffer, unsigned long nframes,
unsigned long nchans, const double* amp, const double* freq)
{
double val, ampfac;
unsigned long i, j, k;
ampfac = 1.0 / noscs;
for (i = 0; i < nframes; amp++, freq++, i++) {
val = 0.0;
for (k = 0; k < noscs; k++) val += *amp * tabitick(p_oscs[k], *freq);
val *= ampfac;
for (j = 0; j < nchans; j++) *buffer++ = (float) val;
}
}
// Csound-style integer lookup
#define MAXLEN 0x1000000L
//= 16777216 = 65536 * 256
//#define FMAXLEN ((MYFLT)(MAXLEN))
//#define PHMASK 0x0FFFFFFL
//#define PFRAC(x) ((MYFLT)((x) & ftp->lomask) * ftp->lodiv)
//#define MAXPOS 0x7FFFFFFFL
// strictly cyclic osc: guardpoint must always be copy of first, set by caller
CS_OSCIL* new_csoscil(double srate, const GTABLE* gtable)
{
unsigned long len, flen, flenp1, ltest, lobits, lomod, lenmask;
CS_OSCIL* p_osc;
if (gtable == NULL || gtable->table == NULL || gtable->length == 0) return NULL;
len = gtable->length;
flen = len;
flen &= -2;
// flen now w/o guardpt
flenp1 = flen + 1;
// & flenp1 with guardpt
for (ltest=flen, lobits=0; (ltest & MAXLEN) == 0; lobits++,ltest<<=1)
;
// flen must be power-of-2
if (ltest != MAXLEN) return NULL;
p_osc = (CS_OSCIL*) malloc(sizeof(CS_OSCIL));
if (p_osc== NULL) return NULL;
p_osc->maxlen = MAXLEN;
p_osc->gtable = gtable;
lenmask = flen - 1;
p_osc->flen = flen;
p_osc->lenmask = lenmask; /* init hdr w powof2 data */
p_osc->lobits = lobits;
lomod = MAXLEN / flen;
p_osc->lomask = lomod - 1;
p_osc->lodiv = 1.0 / (double)lomod; /* & other useful vals */
p_osc->sicvt = (double)MAXLEN / srate;
p_osc->lphs = 0L;
p_osc->oneovrsr = 1.0 / srate;
return p_osc;
}
#define PHMASK 0x0FFFFFFL
double cs_itick(CS_OSCIL* p_osc, double freq)
{
const double* table = p_osc->gtable->table;
double frac, lodiv;
const double *ftab;
double val_1;
double outval;
//double tmp;
unsigned long phs, inc, lomask, lobits;
phs = p_osc->lphs;
inc = (unsigned long)(freq * p_osc->sicvt);
lomask = p_osc->lomask;
lodiv = p_osc->lodiv;
lobits = p_osc->lobits;
frac = (phs & lomask) * lodiv;
ftab = table + (phs >> lobits);
val_1 = *ftab++;
outval = val_1 + (*ftab - val_1) * frac;
phs += inc;
p_osc->lphs = phs & PHMASK ;
return outval;
}
// power-of-two trunc lookup osc used by adsyn
double cs_tick(CS_OSCIL* p_osc, double freq)
{
const double* table = p_osc->gtable->table;
unsigned long phs, inc, lomask, lobits;
double outval, frqscale;
frqscale = freq * p_osc->oneovrsr;
inc = (unsigned long) (freq * p_osc->gtable->length);
outval = *(table + p_osc->lphs);
p_osc->lphs += inc;
p_osc->lphs &= p_osc->lenmask;
}
// TODO: buffer version of above
void cs_tabinbuf(CS_OSCIL* p_oscs[], unsigned long noscs, float* buffer, unsigned long nframes,
unsigned long nchans, const double* amp, const double* freq)
{
double val;
double ampfac = 1.0 / noscs;
unsigned long i, j, k;
for (i = 0; i < nframes; i++, amp++, freq++) {
val = 0.0;
for (k = 0; k < noscs; k++)
val += *amp * cs_itick(p_oscs[k], *freq);
val *= ampfac;
for (j = 0; j < nchans; j++) *buffer++ = (float) val;
}
}