aboutsummaryrefslogtreecommitdiff
path: root/externals/signal/lrshift~/lrshift~.c
blob: 87ac1029f1c2c0ffb0d642684c2e69b53403fa6b (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
#include "m_pd.h"

/* ------------------------ lrshift~ ----------------------------- */

static t_class *lrshift_tilde_class;

typedef struct _lrshift_tilde
{
    t_object x_obj;
    int x_n;
} t_lrshift_tilde;

#ifdef OBSOLETE
static t_int *leftshift_perform(t_int *w)
{
    t_float *in = (t_float *)(w[1]);
    t_float *out= (t_float *)(w[2]);
    int n = (int)(w[3]);
    int shift = (int)(w[4]);
    in += shift;
    n -= shift;
    while (n--)
    	*out++ = *in++;
    while (shift--)
    	*out++ = 0;
    return (w+5);
}

static t_int *rightshift_perform(t_int *w)
{
    t_float *in = (t_float *)(w[1]);
    t_float *out= (t_float *)(w[2]);
    int n = (int)(w[3]);
    int shift = (int)(w[4]);
    n -= shift;
    in -= shift;
    while (n--)
    	*--out = *--in;
    while (shift--)
    	*--out = 0;
    return (w+5);
}
#endif

static t_int *lrshift_perform(t_int *w)
{
    t_float *in = (t_float *)(w[1]);
    t_float *out= (t_float *)(w[2]);
    int n = (int)(w[3]);
	t_lrshift_tilde *x = (t_lrshift_tilde *)w[4];
	int shift = x->x_n;

    if (shift > n)
    	shift = n;
    if (shift < -n)
    	shift = -n;
    if (shift < 0)
	{
		shift = -shift;
		out += n;
		in += n;
		n -= shift;
	    in -= shift;
		while (n--)
	    	*--out = *--in;
	    while (shift--)
	    	*--out = 0;
	}
	else
	{
	    in += shift;
	    n -= shift;
	    while (n--)
	    	*out++ = *in++;
	    while (shift--)
	    	*out++ = 0;
	}
	return (w+5);
}

static void lrshift_tilde_dsp(t_lrshift_tilde *x, t_signal **sp)
{
    int n = sp[0]->s_n;
   	dsp_add(lrshift_perform, 4, sp[0]->s_vec, sp[1]->s_vec, n, x);

}

static void *lrshift_tilde_new(t_floatarg f)
{
    t_lrshift_tilde *x = (t_lrshift_tilde *)pd_new(lrshift_tilde_class);
    x->x_n = f;
    outlet_new(&x->x_obj, gensym("signal"));
    return (x);
}

static void lrshift_tilde_float(t_lrshift_tilde *x, t_floatarg f)
{
    x->x_n = f;
}

void lrshift_tilde_setup(void)
{
    lrshift_tilde_class = class_new(gensym("lrshift~"),
    	(t_newmethod)lrshift_tilde_new, 0, sizeof(t_lrshift_tilde), 0, 
	    A_DEFFLOAT, 0);
    class_addmethod(lrshift_tilde_class, nullfn, gensym("signal"), 0);
    class_addmethod(lrshift_tilde_class, (t_method)lrshift_tilde_dsp,
    	gensym("dsp"), 0);
	class_addfloat(lrshift_tilde_class, (t_method)lrshift_tilde_float);
}