aboutsummaryrefslogtreecommitdiff
path: root/src/n_delay1p_line~.c
blob: 4493af835344cb7e2d6f44e57453e13cfcc24eb0 (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
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL
* WARRANTIES, see the file, "LICENSE.txt," in this distribution.

iem_delay written by Thomas Musil (c) IEM KUG Graz Austria 2002 - 2006 */


#include "m_pd.h"
#include "iemlib.h"
#include "iem_delay.h"

/* -------------------------- n_delay1p_line_tilde~ ------------------------------ */
static t_class *n_delay1p_line_tilde_class;

typedef struct _n_delay1p_line_tilde
{
	t_object	x_obj;
	int				x_mallocsize;
	t_float		x_max_delay_ms;
	t_float		*x_begmem0;
	t_float		*x_begmem1;
	int				x_writeindex;
	int				x_n_delays;
	int				*x_del_samp256_end;
	int				*x_del_samp256_cur;
	int				*x_inc256;
	int				*x_biginc256;
	int				x_blocksize;
	t_float		x_sr;
	t_float		x_ms2tick;
	t_float		x_ms2samples256;
	t_float		x_interpol_ms;
	int				x_interpol_ticks;
	int				x_ticksleft;
	int				x_old;
	int				x_retarget;
	t_float		**x_io;
	t_float		x_msi;
} t_n_delay1p_line_tilde;

static void n_delay1p_line_tilde_list(t_n_delay1p_line_tilde *x, t_symbol *s, int argc, t_atom *argv)
{
	if(argc == x->x_n_delays)
	{
		int i;
		t_float delay, max=x->x_max_delay_ms;

		if(x->x_interpol_ms <= 0.0f)
			x->x_ticksleft = x->x_retarget = 0;
		else
			x->x_retarget = 1;
		for(i=0; i<argc; i++)
		{
			delay = atom_getfloat(argv++);
			if(delay < 0.0f)
				delay = 0.0f;
			if(delay > max)
				delay = max;
			if(x->x_interpol_ms <= 0.0f)
				x->x_del_samp256_end[i] = x->x_del_samp256_cur[i] = (int)(x->x_ms2samples256 * delay + 0.5f) + 127;
			else
				x->x_del_samp256_end[i] = (int)(x->x_ms2samples256 * delay  + 0.5f) + 127;
		}
	}
}

static void n_delay1p_line_tilde_time(t_n_delay1p_line_tilde *x, t_floatarg interpol_ms)
{
	if(interpol_ms < 0.0f)
		interpol_ms = 0.0f;
	x->x_interpol_ms = interpol_ms;
	x->x_interpol_ticks = (int)(x->x_ms2tick * interpol_ms);
}

static void n_delay1p_line_tilde_stop(t_n_delay1p_line_tilde *x)
{
	int i, n=x->x_n_delays;

	for(i=0; i<n; i++)
		x->x_del_samp256_end[i] = x->x_del_samp256_cur[i];
	x->x_ticksleft = x->x_retarget = 0;
}

static t_int *n_delay1p_line_tilde_perform(t_int *w)
{
	t_n_delay1p_line_tilde *x = (t_n_delay1p_line_tilde *)(w[1]);
	int hn=(int)(w[2]);
	int nout=x->x_n_delays;
	t_float *in;
	t_float *out;
	int writeindex = x->x_writeindex;
	int i, j, n;
	int malloc_samples = x->x_mallocsize;
	t_float *begvec0 = x->x_begmem0;
	t_float *begvec1 = x->x_begmem1;
	t_float *writevec;
	t_float *readvec;
	int del256, inc256;

	writevec = begvec0 + writeindex;
	in=x->x_io[0];
	n = hn;
	while(n--)
	{
		*writevec++ = *in++;
	}
	writevec = begvec1 + writeindex;
	in=x->x_io[0];
	n = hn;
	while(n--)
	{
		*writevec++ = *in++;
	}

	if(x->x_retarget)
	{
		int nticks = x->x_interpol_ticks;

		if(!nticks)
			nticks = 1;
		x->x_ticksleft = nticks;
		for(j=0; j<nout; j++)
		{
			x->x_biginc256[j] = (x->x_del_samp256_end[j] - x->x_del_samp256_cur[j]) / nticks;
			x->x_inc256[j] = x->x_biginc256[j] / x->x_blocksize;
		}
		x->x_retarget = 0;
	}

	if(x->x_ticksleft)
	{
		for(j=0; j<nout; j++)
		{
			inc256 = x->x_inc256[j];
			del256 = x->x_del_samp256_cur[j];
			out = x->x_io[j+1];
			for(i=0; i<hn; i++)
			{
				readvec = begvec1 + writeindex - (del256 >> 8) + i;
				*out++ = *readvec;
				del256 += inc256;
			}
			x->x_del_samp256_cur[j] += x->x_biginc256[j];
		}
		x->x_ticksleft--;
	}
	else
	{
		for(j=0; j<nout; j++)
		{
			del256 = x->x_del_samp256_cur[j] = x->x_del_samp256_end[j];
			readvec = begvec1 + writeindex - (del256 >> 8);
			out = x->x_io[j+1];
			n = hn;
			while(n--)
			{
				*out++ = *readvec++;
			}
		}
	}
	writeindex += hn;
	if(writeindex >= malloc_samples)
	{
		writeindex -= malloc_samples;
	}
	x->x_writeindex = writeindex;
	return(w+3);
}

static t_int *n_delay1p_line_tilde_perf8(t_int *w)
{
	t_n_delay1p_line_tilde *x = (t_n_delay1p_line_tilde *)(w[1]);
	int hn=(int)(w[2]);
	int nout=x->x_n_delays;
	t_float *in;
	t_float *out;
	int writeindex = x->x_writeindex;
	int i, j, k, n;
	int malloc_samples = x->x_mallocsize;
	t_float *begvec0 = x->x_begmem0;
	t_float *begvec1 = x->x_begmem1;
	t_float *writevec;
	t_float *readvec;
	int del256, inc256;

//	post("writevec = %d",writeindex);
	writevec = begvec0 + writeindex;
	in=x->x_io[0];
	n = hn;
	while(n)
	{
		writevec[0] = in[0];
		writevec[1] = in[1];
		writevec[2] = in[2];
		writevec[3] = in[3];
		writevec[4] = in[4];
		writevec[5] = in[5];
		writevec[6] = in[6];
		writevec[7] = in[7];

		writevec += 8;
		n -= 8;
		in += 8;
	}
	writevec = begvec1 + writeindex;
	in=x->x_io[0];
	n = hn;
	while(n)
	{
		writevec[0] = in[0];
		writevec[1] = in[1];
		writevec[2] = in[2];
		writevec[3] = in[3];
		writevec[4] = in[4];
		writevec[5] = in[5];
		writevec[6] = in[6];
		writevec[7] = in[7];

		writevec += 8;
		n -= 8;
		in += 8;
	}

	if(x->x_retarget)
	{
		int nticks = x->x_interpol_ticks;

		if(!nticks)
			nticks = 1;
		x->x_ticksleft = nticks;
		for(j=0; j<nout; j++)
		{
			x->x_biginc256[j] = (x->x_del_samp256_end[j] - x->x_del_samp256_cur[j]) / nticks;
			x->x_inc256[j] = x->x_biginc256[j] / x->x_blocksize;
		}
		x->x_retarget = 0;
	}

	if(x->x_ticksleft)
	{
		for(j=0; j<nout; j++)
		{
			inc256 = x->x_inc256[j];
			del256 = x->x_del_samp256_cur[j];
			out = x->x_io[j+1];
			readvec = begvec1 + writeindex;
			for(i=0; i<hn; i+=8)
			{
				k = del256 >> 8;
				out[0] = readvec[0-k];
				del256 += inc256;

				k = del256 >> 8;
				out[1] = readvec[1-k];
				del256 += inc256;

				k = del256 >> 8;
				out[2] = readvec[2-k];
				del256 += inc256;

				k = del256 >> 8;
				out[3] = readvec[3-k];
				del256 += inc256;

				k = del256 >> 8;
				out[4] = readvec[4-k];
				del256 += inc256;

				k = del256 >> 8;
				out[5] = readvec[5-k];
				del256 += inc256;

				k = del256 >> 8;
				out[6] = readvec[6-k];
				del256 += inc256;

				k = del256 >> 8;
				out[7] = readvec[7-k];
				del256 += inc256;

				out += 8;
				readvec += 8;
			}
			x->x_del_samp256_cur[j] += x->x_biginc256[j];
		}
		x->x_ticksleft--;
	}
	else
	{
		for(j=0; j<nout; j++)
		{
			del256 = x->x_del_samp256_cur[j] = x->x_del_samp256_end[j];
			readvec = begvec1 + writeindex - (del256 >> 8);
			out = x->x_io[j+1];
			n = hn;
			while(n)
			{
				out[0] = readvec[0];
				out[1] = readvec[1];
				out[2] = readvec[2];
				out[3] = readvec[3];
				out[4] = readvec[4];
				out[5] = readvec[5];
				out[6] = readvec[6];
				out[7] = readvec[7];
				out += 8;
				readvec += 8;
				n -= 8;
			}
		}
	}

	writeindex += hn;
	if(writeindex >= malloc_samples)
	{
		writeindex -= malloc_samples;
	}
	x->x_writeindex = writeindex;

	return(w+3);
}

static void n_delay1p_line_tilde_dsp(t_n_delay1p_line_tilde *x, t_signal **sp)
{
	int n = sp[0]->s_n;
	int i, nd = x->x_n_delays + 1;

	if(!x->x_blocksize)/*first time*/
	{
		int nsamps = x->x_max_delay_ms * (t_float)sp[0]->s_sr * 0.001f;

		if(nsamps < 1)
			nsamps = 1;
		nsamps += ((- nsamps) & (n - 1));
		nsamps += n;
		x->x_mallocsize = nsamps;
		x->x_begmem0 = (t_float *)getbytes(2 * x->x_mallocsize * sizeof(t_float));
		x->x_begmem1 = x->x_begmem0 + x->x_mallocsize;
		x->x_writeindex = n;
	}
	else if((x->x_blocksize != n) || ((t_float)sp[0]->s_sr != x->x_sr))
	{
		int nsamps = x->x_max_delay_ms * (t_float)sp[0]->s_sr * 0.001f;

		if(nsamps < 1)
			nsamps = 1;
		nsamps += ((- nsamps) & (n - 1));
		nsamps += n;
		
		x->x_begmem0 = (t_float *)resizebytes(x->x_begmem0, 2*x->x_mallocsize*sizeof(t_float), 2*nsamps*sizeof(t_float));
		x->x_mallocsize = nsamps;
		x->x_begmem1 = x->x_begmem0 + x->x_mallocsize;
		if(x->x_writeindex >= nsamps)
			x->x_writeindex -= nsamps;
	}
	x->x_blocksize = n;
	x->x_ms2tick = 0.001f * (t_float)sp[0]->s_sr / (t_float)n;
	x->x_ms2samples256 = 0.256f * (t_float)sp[0]->s_sr;
	x->x_interpol_ticks = (int)(x->x_ms2tick * x->x_interpol_ms);
	for(i=0; i<nd; i++)
		x->x_io[i] = sp[i]->s_vec;
	if(n&7)
		dsp_add(n_delay1p_line_tilde_perform, 2, x, n);
	else
		dsp_add(n_delay1p_line_tilde_perf8, 2, x, n);
}

static void *n_delay1p_line_tilde_new(t_floatarg fout, t_floatarg delay_ms, t_floatarg interpol_ms)
{
	t_n_delay1p_line_tilde *x = (t_n_delay1p_line_tilde *)pd_new(n_delay1p_line_tilde_class);
	int i, n_out = (int)fout;
	int nsamps = delay_ms * sys_getsr() * 0.001f;

	if(n_out < 1)
		n_out = 1;
	x->x_n_delays = n_out;
	x->x_max_delay_ms = delay_ms;
	if(nsamps < 1)
		nsamps = 1;
	nsamps += ((- nsamps) & (DELLINE_DEF_VEC_SIZE - 1));
	nsamps += DELLINE_DEF_VEC_SIZE;
	x->x_mallocsize = nsamps;
	x->x_begmem0 = (t_float *)getbytes(2 * x->x_mallocsize * sizeof(t_float));
	x->x_begmem1 = x->x_begmem0 + x->x_mallocsize;
	x->x_writeindex = DELLINE_DEF_VEC_SIZE;
	x->x_blocksize = 0;
	x->x_sr = 0.0f;
	if(interpol_ms < 0.0f)
		interpol_ms = 0.0f;
	x->x_interpol_ms = interpol_ms;
	x->x_io = (t_float **)getbytes((x->x_n_delays + 1) * sizeof(t_float *));
	for(i=0; i<n_out; i++)
		outlet_new(&x->x_obj, &s_signal);
	x->x_del_samp256_end = (int *)getbytes(x->x_n_delays * sizeof(int));
	x->x_del_samp256_cur = (int *)getbytes(x->x_n_delays * sizeof(int));
	x->x_inc256 = (int *)getbytes(x->x_n_delays * sizeof(int));
	x->x_biginc256 = (int *)getbytes(x->x_n_delays * sizeof(int));
	x->x_ticksleft = x->x_retarget = 0;
	for(i=0; i<n_out; i++)
	{
		x->x_del_samp256_cur[i] = x->x_del_samp256_end[i] = 0;
		x->x_inc256[i] = x->x_biginc256[i] = 0;
	}
	x->x_interpol_ticks = 0;
	x->x_msi = 0.0f;
	return (x);
}

static void n_delay1p_line_tilde_free(t_n_delay1p_line_tilde *x)
{
	freebytes(x->x_del_samp256_end, x->x_n_delays * sizeof(int));
	freebytes(x->x_del_samp256_cur, x->x_n_delays * sizeof(int));
	freebytes(x->x_inc256, x->x_n_delays * sizeof(int));
	freebytes(x->x_biginc256, x->x_n_delays * sizeof(int));
	freebytes(x->x_io, (x->x_n_delays + 1) * sizeof(t_float *));
	freebytes(x->x_begmem0, 2 * x->x_mallocsize * sizeof(t_float));
}

void n_delay1p_line_tilde_setup(void)
{
	n_delay1p_line_tilde_class = class_new(gensym("n_delay1p_line~"), (t_newmethod)n_delay1p_line_tilde_new, (t_method)n_delay1p_line_tilde_free,
		sizeof(t_n_delay1p_line_tilde), 0, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);
	CLASS_MAINSIGNALIN(n_delay1p_line_tilde_class, t_n_delay1p_line_tilde, x_msi);
	class_addlist(n_delay1p_line_tilde_class, (t_method)n_delay1p_line_tilde_list);
	class_addmethod(n_delay1p_line_tilde_class, (t_method)n_delay1p_line_tilde_dsp, gensym("dsp"), 0);
	class_addmethod(n_delay1p_line_tilde_class, (t_method)n_delay1p_line_tilde_stop, gensym("stop"), 0);
	class_addmethod(n_delay1p_line_tilde_class, (t_method)n_delay1p_line_tilde_time, gensym("time"), A_FLOAT, 0);
	class_sethelpsymbol(n_delay1p_line_tilde_class, gensym("iemhelp2/n_delay1p_line~-help"));
}