aboutsummaryrefslogtreecommitdiff
path: root/src/z_nop.c
blob: 6fa6ac025714260a4d6649f83935f5e71df2f1a9 (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
#include "zexy.h"
#ifdef NT
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

/* ------------------------ nop~ ----------------------------- */
/* this will pass trough the signal unchanged except for a delay of 1 block */

static t_class *nop_class;

typedef struct _nop
{
	t_object x_obj;
	t_float *buf;
	int n;
	int toggle;
} t_nop;

static t_int *nop_perform(t_int *w)
{
	t_float *in  = (t_float *)w[1];
	t_float *out = (t_float *)w[2];
	t_nop *x = (t_nop *)w[3];
	int n = x->n;
	t_float *rp = x->buf + n * x->toggle, *wp = x->buf + n * (x->toggle ^= 1);

	while (n--)	{
		*wp++ = *in++;
		*out++ = *rp++;
	}

	return (w+4);
}

static void nop_dsp(t_nop *x, t_signal **sp)
{
	if (x->n != sp[0]->s_n)
		{
			freebytes(x->buf, x->n * 2 * sizeof(t_float));

			x->buf = (t_float *)getbytes(sizeof(t_float) * 2 * (x->n = sp[0]->s_n));
		}
	dsp_add(nop_perform, 3, sp[0]->s_vec, sp[1]->s_vec, x);
}

static void helper(t_nop *x)
{
	post("%c nop~-object ::\tdo_nothing but delay a signal for 1 block\n"
		"\t\t this might be helpful for synchronising signals", HEARTSYMBOL);
}

static void nop_free(t_nop *x)
{
	freebytes(x->buf, x->n * sizeof(t_float));
}


static void *nop_new()
{
	t_nop *x = (t_nop *)pd_new(nop_class);
	outlet_new(&x->x_obj, gensym("signal"));
	x->toggle = 0;
	x->n = 0;

	return (x);
}

void z_nop_setup(void)
{
	nop_class = class_new(gensym("nop~"), (t_newmethod)nop_new, (t_method)nop_free,
					sizeof(t_nop), 0, A_DEFFLOAT, 0);
	class_addmethod(nop_class, nullfn, gensym("signal"), 0);
	class_addmethod(nop_class, (t_method)nop_dsp, gensym("dsp"), 0);

	class_addmethod(nop_class, (t_method)helper, gensym("help"), 0);
	class_sethelpsymbol(nop_class, gensym("zexy/nop~"));
}