aboutsummaryrefslogtreecommitdiff
path: root/src/iem16_array_tilde.c
blob: 5e120f1ffc83a2894a9d179ac913c3ec2b5a29ca (plain)
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
/* copyleft (c) 2003 forum::für::umläute -- IOhannes m zmölnig @ IEM
 * based on d_array.c from pd:
 * Copyright (c) 1997-1999 Miller Puckette and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

/* tab16write~, tab16play~, tab16read~, tab16read4~, tab16send~, tab16receive~ */

#include "iem16_table.h"

/* ------------------------- tab16write~ -------------------------- */

static t_class *tab16write_tilde_class;

typedef struct _tab16write_tilde {
  t_object x_obj;
  int x_phase;
  int x_nsampsintab;
  short *x_vec;
  t_symbol *x_arrayname;
  float x_f;
} t_tab16write_tilde;

static void *tab16write_tilde_new(t_symbol *s) {
  t_tab16write_tilde *x = (t_tab16write_tilde *)pd_new(tab16write_tilde_class);
  x->x_phase = 0x7fffffff;
  x->x_arrayname = s;
  x->x_f = 0;
  return (x);
}

static t_int *tab16write_tilde_perform(t_int *w) {
  t_tab16write_tilde *x = (t_tab16write_tilde *)(w[1]);
  t_float *in = (t_float *)(w[2]);
  int n = (int)(w[3]), phase = x->x_phase, endphase = x->x_nsampsintab;
  if (!x->x_vec) goto bad;
    
  if (endphase > phase)    {
    int nxfer = endphase - phase;
    t_iem16_16bit *fp = x->x_vec + phase;
    if (nxfer > n) nxfer = n;
    phase += nxfer;
    while (nxfer--)*fp++ = *in++*IEM16_SCALE_UP;
    x->x_phase = phase;
  }
 bad:
  return (w+4);
}

void tab16write_tilde_set(t_tab16write_tilde *x, t_symbol *s){
  t_table16 *a;

  x->x_arrayname = s;
  if (!(a = (t_table16 *)pd_findbyclass(x->x_arrayname, table16_class)))    {
    if (*s->s_name) pd_error(x, "tab16write~: %s: no such array",
			     x->x_arrayname->s_name);
    x->x_vec = 0;
  }
  else if (!table16_getarray16(a, &x->x_nsampsintab, &x->x_vec))    {
    error("%s: bad template for tab16write~", x->x_arrayname->s_name);
    x->x_vec = 0;
  }
  else table16_usedindsp(a);
}

static void tab16write_tilde_dsp(t_tab16write_tilde *x, t_signal **sp){
  tab16write_tilde_set(x, x->x_arrayname);
  dsp_add(tab16write_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
}

static void tab16write_tilde_bang(t_tab16write_tilde *x){
  x->x_phase = 0;
}

static void tab16write_tilde_stop(t_tab16write_tilde *x){}

static void tab16write_tilde_free(t_tab16write_tilde *x){}

void tab16write_tilde_setup(void){
  tab16write_tilde_class = class_new(gensym("tab16write~"),
				     (t_newmethod)tab16write_tilde_new, (t_method)tab16write_tilde_free,
				     sizeof(t_tab16write_tilde), 0, A_DEFSYM, 0);
  CLASS_MAINSIGNALIN(tab16write_tilde_class, t_tab16write_tilde, x_f);
  class_addmethod(tab16write_tilde_class, (t_method)tab16write_tilde_dsp,
		  gensym("dsp"), 0);
  class_addmethod(tab16write_tilde_class, (t_method)tab16write_tilde_set,
		  gensym("set"), A_SYMBOL, 0);
  class_addmethod(tab16write_tilde_class, (t_method)tab16write_tilde_stop,
		  gensym("stop"), 0);
  class_addbang(tab16write_tilde_class, tab16write_tilde_bang);
}

/* ------------ tab16play~ - non-transposing sample playback --------------- */

static t_class *tab16play_tilde_class;

typedef struct _tab16play_tilde{
  t_object x_obj;
  t_outlet *x_bangout;
  int x_phase;
  int x_nsampsintab;
  int x_limit;
  t_iem16_16bit *x_vec;
  t_symbol *x_arrayname;
} t_tab16play_tilde;

static void *tab16play_tilde_new(t_symbol *s){
  t_tab16play_tilde *x = (t_tab16play_tilde *)pd_new(tab16play_tilde_class);
  x->x_phase = 0x7fffffff;
  x->x_limit = 0;
  x->x_arrayname = s;
  outlet_new(&x->x_obj, &s_signal);
  x->x_bangout = outlet_new(&x->x_obj, gensym("bang"));
  return (x);
}

static t_int *tab16play_tilde_perform(t_int *w){
  t_tab16play_tilde *x = (t_tab16play_tilde *)(w[1]);
  t_float *out = (t_float *)(w[2]);
  t_iem16_16bit *fp;
  int n = (int)(w[3]), phase = x->x_phase,
    endphase = (x->x_nsampsintab < x->x_limit ?
		x->x_nsampsintab : x->x_limit), nxfer, n3;
  if (!x->x_vec || phase >= endphase)	goto zero;
    
  nxfer = endphase - phase;
  fp = x->x_vec + phase;
  if (nxfer > n)
    nxfer = n;
  n3 = n - nxfer;
  phase += nxfer;
  while (nxfer--) *out++ = *fp++*IEM16_SCALE_DOWN;
  if (phase >= endphase)  {
    x->x_phase = 0x7fffffff;
    while (n3--) *out++ = 0;
  }
  else x->x_phase = phase;
    
  return (w+4);
 zero:
  while (n--) *out++ = 0;
  return (w+4);
}

void tab16play_tilde_set(t_tab16play_tilde *x, t_symbol *s){
  t_table16 *a;

  x->x_arrayname = s;
  if (!(a = (t_table16 *)pd_findbyclass(x->x_arrayname, table16_class)))    {
    if (*s->s_name) pd_error(x, "tab16play~: %s: no such array",
			     x->x_arrayname->s_name);
    x->x_vec = 0;
  }
  else if (!table16_getarray16(a, &x->x_nsampsintab, &x->x_vec))    {
    error("%s: bad template for tab16play~", x->x_arrayname->s_name);
    x->x_vec = 0;
  }
  else table16_usedindsp(a);
}

static void tab16play_tilde_dsp(t_tab16play_tilde *x, t_signal **sp){
  tab16play_tilde_set(x, x->x_arrayname);
  dsp_add(tab16play_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
}

static void tab16play_tilde_list(t_tab16play_tilde *x, t_symbol *s,
				 int argc, t_atom *argv){
  long start = atom_getfloatarg(0, argc, argv);
  long length = atom_getfloatarg(1, argc, argv);
  if (start < 0) start = 0;
  if (length <= 0)x->x_limit = 0x7fffffff;
  else	    x->x_limit = start + length;
  x->x_phase = start;
}

static void tab16play_tilde_stop(t_tab16play_tilde *x){
  x->x_phase = 0x7fffffff;
}

static void tab16play_tilde_free(t_tab16play_tilde *x){}

void tab16play_tilde_setup(void){
  tab16play_tilde_class = class_new(gensym("tab16play~"),
				    (t_newmethod)tab16play_tilde_new, (t_method)tab16play_tilde_free,
				    sizeof(t_tab16play_tilde), 0, A_DEFSYM, 0);
  class_addmethod(tab16play_tilde_class, (t_method)tab16play_tilde_dsp,
		  gensym("dsp"), 0);
  class_addmethod(tab16play_tilde_class, (t_method)tab16play_tilde_stop,
		  gensym("stop"), 0);
  class_addmethod(tab16play_tilde_class, (t_method)tab16play_tilde_set,
		  gensym("set"), A_DEFSYM, 0);
  class_addlist(tab16play_tilde_class, tab16play_tilde_list);
}

/* ------------------------ tab16send~ ------------------------- */

static t_class *tab16send_class;

typedef struct _tab16send{
  t_object x_obj;
  t_iem16_16bit *x_vec;
  int x_graphperiod;
  int x_graphcount;
  t_symbol *x_arrayname;
  float x_f;
} t_tab16send;

static void *tab16send_new(t_symbol *s){
  t_tab16send *x = (t_tab16send *)pd_new(tab16send_class);
  x->x_graphcount = 0;
  x->x_arrayname = s;
  x->x_f = 0;
  return (x);
}

static t_int *tab16send_perform(t_int *w){
  t_tab16send *x = (t_tab16send *)(w[1]);
  t_float *in = (t_float *)(w[2]);
  int n = w[3];
  t_iem16_16bit *dest = x->x_vec;
  int i = x->x_graphcount;
  if (!x->x_vec) goto bad;

  while (n--)	*dest = *in++*IEM16_SCALE_UP;
  if (!i--)i = x->x_graphperiod;
  x->x_graphcount = i;
 bad:
  return (w+4);
}

static void tab16send_dsp(t_tab16send *x, t_signal **sp){
  int vecsize;
  t_table16 *a;

  if (!(a = (t_table16 *)pd_findbyclass(x->x_arrayname, table16_class)))    {
    if (*x->x_arrayname->s_name)
      error("tab16send~: %s: no such array", x->x_arrayname->s_name);
  }
  else if (!table16_getarray16(a, &vecsize, &x->x_vec))
    error("%s: bad template for tab16send~", x->x_arrayname->s_name);
  else    {
    int n = sp[0]->s_n;
    int ticksper = sp[0]->s_sr/n;
    if (ticksper < 1) ticksper = 1;
    x->x_graphperiod = ticksper;
    if (x->x_graphcount > ticksper) x->x_graphcount = ticksper;
    if (n < vecsize) vecsize = n;
    table16_usedindsp(a);
    dsp_add(tab16send_perform, 3, x, sp[0]->s_vec, vecsize);
  }
}

static void tab16send_free(t_tab16send *x){}

static void tab16send_setup(void){
  tab16send_class = class_new(gensym("tab16send~"), (t_newmethod)tab16send_new,
			      (t_method)tab16send_free, sizeof(t_tab16send), 0, A_DEFSYM, 0);
  CLASS_MAINSIGNALIN(tab16send_class, t_tab16send, x_f);
  class_addmethod(tab16send_class, (t_method)tab16send_dsp, gensym("dsp"), 0);
}

// G.Holzmann: for PD-extended build system
void tab16send_tilde_setup(void)
{
  tab16send_setup();
}

/* ------------------------ tab16receive~ ------------------------- */

static t_class *tab16receive_class;

typedef struct _tab16receive{
  t_object x_obj;
  t_iem16_16bit *x_vec;
  t_symbol *x_arrayname;
} t_tab16receive;

static t_int *tab16receive_perform(t_int *w){
  t_tab16receive *x = (t_tab16receive *)(w[1]);
  t_float *out = (t_float *)(w[2]);
  int n = w[3];
  t_iem16_16bit *from = x->x_vec;
  if (from) while (n--) *out++ = *from++*IEM16_SCALE_DOWN;
  else while (n--) *out++ = 0;
  return (w+4);
}

static void tab16receive_dsp(t_tab16receive *x, t_signal **sp){
  t_table16 *a;
  int vecsize;
    
  if (!(a = (t_table16 *)pd_findbyclass(x->x_arrayname, table16_class))) {
    if (*x->x_arrayname->s_name)
      error("tab16send~: %s: no such array", x->x_arrayname->s_name);
  }
  else if (!table16_getarray16(a, &vecsize, &x->x_vec))
    error("%s: bad template for tab16receive~", x->x_arrayname->s_name);
  else     {
    int n = sp[0]->s_n;
    if (n < vecsize) vecsize = n;
    table16_usedindsp(a);
    dsp_add(tab16receive_perform, 3, x, sp[0]->s_vec, vecsize);
  }
}

static void *tab16receive_new(t_symbol *s){
  t_tab16receive *x = (t_tab16receive *)pd_new(tab16receive_class);
  x->x_arrayname = s;
  outlet_new(&x->x_obj, gensym("signal"));
  return (x);
}

static void tab16receive_setup(void){
  tab16receive_class = class_new(gensym("tab16receive~"),
				 (t_newmethod)tab16receive_new, 0,
				 sizeof(t_tab16receive), 0, A_DEFSYM, 0);
  class_addmethod(tab16receive_class, (t_method)tab16receive_dsp,
		  gensym("dsp"), 0);
}

// G.Holzmann: for PD-extended build system
void tab16receive_tilde_setup(void)
{
  tab16receive_setup();
}

/******************** tab16read~ ***********************/

static t_class *tab16read_tilde_class;

typedef struct _tab16read_tilde{
  t_object x_obj;
  int x_npoints;
  t_iem16_16bit *x_vec;
  t_symbol *x_arrayname;
  float x_f;
} t_tab16read_tilde;

static void *tab16read_tilde_new(t_symbol *s){
  t_tab16read_tilde *x = (t_tab16read_tilde *)pd_new(tab16read_tilde_class);
  x->x_arrayname = s;
  x->x_vec = 0;
  outlet_new(&x->x_obj, gensym("signal"));
  x->x_f = 0;
  return (x);
}

static t_int *tab16read_tilde_perform(t_int *w){
  t_tab16read_tilde *x = (t_tab16read_tilde *)(w[1]);
  t_float *in = (t_float *)(w[2]);
  t_float *out = (t_float *)(w[3]);
  int n = (int)(w[4]);    
  int maxindex;
  t_iem16_16bit *buf = x->x_vec;
  int i;
    
  maxindex = x->x_npoints - 1;
  if (!buf) goto zero;

  for (i = 0; i < n; i++)    {
    int index = *in++;
    if (index < 0) index = 0;
    else if (index > maxindex) index = maxindex;
    *out++ = buf[index]*IEM16_SCALE_DOWN;
  }
  return (w+5);
 zero:
  while (n--) *out++ = 0;

  return (w+5);
}

void tab16read_tilde_set(t_tab16read_tilde *x, t_symbol *s){
  t_table16 *a;
    
  x->x_arrayname = s;
  if (!(a = (t_table16 *)pd_findbyclass(x->x_arrayname, table16_class)))  {
    if (*s->s_name)
      error("tab16read~: %s: no such array", x->x_arrayname->s_name);
    x->x_vec = 0;
  }
  else if (!table16_getarray16(a, &x->x_npoints, &x->x_vec)) {
    error("%s: bad template for tab16read~", x->x_arrayname->s_name);
    x->x_vec = 0;
  }
  else table16_usedindsp(a);
}

static void tab16read_tilde_dsp(t_tab16read_tilde *x, t_signal **sp){
  tab16read_tilde_set(x, x->x_arrayname);

  dsp_add(tab16read_tilde_perform, 4, x,
	  sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);

}

static void tab16read_tilde_free(t_tab16read_tilde *x){}

void tab16read_tilde_setup(void){
  tab16read_tilde_class = class_new(gensym("tab16read~"),
				    (t_newmethod)tab16read_tilde_new, (t_method)tab16read_tilde_free,
				    sizeof(t_tab16read_tilde), 0, A_DEFSYM, 0);
  CLASS_MAINSIGNALIN(tab16read_tilde_class, t_tab16read_tilde, x_f);
  class_addmethod(tab16read_tilde_class, (t_method)tab16read_tilde_dsp,
		  gensym("dsp"), 0);
  class_addmethod(tab16read_tilde_class, (t_method)tab16read_tilde_set,
		  gensym("set"), A_SYMBOL, 0);
}

/******************** tab16read4~ ***********************/

static t_class *tab16read4_tilde_class;

typedef struct _tab16read4_tilde{
  t_object x_obj;
  int x_npoints;
  t_iem16_16bit *x_vec;
  t_symbol *x_arrayname;
  float x_f;
} t_tab16read4_tilde;

static void *tab16read4_tilde_new(t_symbol *s){
  t_tab16read4_tilde *x = (t_tab16read4_tilde *)pd_new(tab16read4_tilde_class);
  x->x_arrayname = s;
  x->x_vec = 0;
  outlet_new(&x->x_obj, gensym("signal"));
  x->x_f = 0;
  return (x);
}

static t_int *tab16read4_tilde_perform(t_int *w){
  t_tab16read4_tilde *x = (t_tab16read4_tilde *)(w[1]);
  t_float *in = (t_float *)(w[2]);
  t_float *out = (t_float *)(w[3]);
  int n = (int)(w[4]);    
  int maxindex;
  t_iem16_16bit *buf = x->x_vec;
  t_iem16_16bit *fp;
  int i;
    
  maxindex = x->x_npoints - 3;

  if (!buf) goto zero;

  for (i = 0; i < n; i++)    {
    float findex = *in++;
    int index = findex;
    float frac,  a,  b,  c,  d, cminusb;
    if (index < 1)   index = 1, frac = 0;
    else if (index > maxindex)  index = maxindex, frac = 1;
    else frac = findex - index;
    fp = buf + index;
    a = fp[-1]*IEM16_SCALE_DOWN;
    b = fp[0]*IEM16_SCALE_DOWN;
    c = fp[1]*IEM16_SCALE_DOWN;
    d = fp[2]*IEM16_SCALE_DOWN;
    cminusb = c-b;
    *out++ = b + frac * (
			 cminusb - 0.5f * (frac-1.) * (
				(a - d + 3.0f * cminusb) * frac + (b - a - cminusb)
				)
			 );
  }
  return (w+5);
 zero:
  while (n--) *out++ = 0;
  
  return (w+5);
}

void tab16read4_tilde_set(t_tab16read4_tilde *x, t_symbol *s){
  t_table16 *a;
    
  x->x_arrayname = s;
  if (!(a = (t_table16 *)pd_findbyclass(x->x_arrayname, table16_class))) {
    if (*s->s_name)
      error("tab16read4~: %s: no such array", x->x_arrayname->s_name);
    x->x_vec = 0;
  }
  else if (!table16_getarray16(a, &x->x_npoints, &x->x_vec))  {
    error("%s: bad template for tab16read4~", x->x_arrayname->s_name);
    x->x_vec = 0;
  }
  else table16_usedindsp(a);
}

static void tab16read4_tilde_dsp(t_tab16read4_tilde *x, t_signal **sp){
  tab16read4_tilde_set(x, x->x_arrayname);

  dsp_add(tab16read4_tilde_perform, 4, x,
	  sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
}

void tab16read4_tilde_setup(void){
  tab16read4_tilde_class = class_new(gensym("tab16read4~"),
				     (t_newmethod)tab16read4_tilde_new, 0,
				     sizeof(t_tab16read4_tilde), 0, A_DEFSYM, 0);
  CLASS_MAINSIGNALIN(tab16read4_tilde_class, t_tab16read4_tilde, x_f);
  class_addmethod(tab16read4_tilde_class, (t_method)tab16read4_tilde_dsp,
		  gensym("dsp"), 0);
  class_addmethod(tab16read4_tilde_class, (t_method)tab16read4_tilde_set,
		  gensym("set"), A_SYMBOL, 0);
}
/* ------------------------ global setup routine ------------------------- */

void iem16_array_tilde_setup(void){
  tab16write_tilde_setup();
  tab16play_tilde_setup();
  tab16read_tilde_setup();
  tab16read4_tilde_setup();
  tab16send_setup();
  tab16receive_setup();
}