aboutsummaryrefslogtreecommitdiff
path: root/pd/extra/loop~/loop~.c
blob: 2f440030750209ae2e9f0df0b852da79258e0a5b (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
/* loop~ -- loop generator for sampling */

/*  Copyright 1997-1999 Miller Puckette.
Permission is granted to use this software for any purpose provided you
keep this copyright notice intact.

THE AUTHOR AND HIS EMPLOYERS MAKE NO WARRANTY, EXPRESS OR IMPLIED,
IN CONNECTION WITH THIS SOFTWARE.

This file is downloadable from http://www.crca.ucsd.edu/~msp .

*/

#ifdef PD
#include "m_pd.h"
#endif

typedef struct _loopctl
{
    double l_phase;
    float l_invwindow;
    float l_window;
    int l_resync;
} t_loopctl;

static void loopctl_run(t_loopctl *x, float *transposein,
        float *windowin, float *rawout, float *windowout, int n)
{
    float window, invwindow;
    double phase = x->l_phase;
    if (x->l_resync)
    {
        window = *windowin;
        if (window < 0)
        {
            if (window > -1)
                window = -1;
            invwindow = -1/window;
        }
        else
        {
            if (window < 1)
                window = 1;
            invwindow = 1/window;
        }
        x->l_resync = 0;
    }
    else
    {
        window = x->l_window;
        phase = x->l_phase;
        invwindow = x->l_invwindow;
    }
    while (n--)
    {
        double phaseinc = invwindow * *transposein++;
        double newphase;
        float nwind = *windowin++;
        if (phaseinc >= 1 || phaseinc < 0)
            phaseinc = 0;
        newphase = phase + phaseinc;
        if (newphase >= 1)
        {
            window = nwind;
            if (window < 0)
            {
                if (window > -1)
                    window = -1;
                invwindow = -1/window;
            }
            else
            {
                if (window < 1)
                    window = 1;
                invwindow = 1/window;
            }
            newphase -= 1.;
        }
        phase = newphase;
        *rawout++ = (float)phase;
        *windowout++ = window;
    }
    x->l_invwindow = invwindow;
    x->l_window = window;
    x->l_phase = phase;
}

static void loopctl_init(t_loopctl *x)
{
    x->l_window = 1;
    x->l_invwindow = 1;
    x->l_phase = 0;
}

static void loopctl_set(t_loopctl *x, float val)
{
    if (val < 0 || val > 1)
        val = 0;
    x->l_phase = val;
    x->l_resync = 1;
}

#ifdef PD

typedef struct _loop
{
    t_object x_obj;
    t_float x_f;
    t_loopctl x_loopctl;
} t_loop;

static t_class *loop_class;

static void *loop_new(void)
{
    t_loop *x = (t_loop *)pd_new(loop_class);
    loopctl_init(&x->x_loopctl);
    inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
    outlet_new(&x->x_obj, gensym("signal"));
    outlet_new(&x->x_obj, gensym("signal"));
    return (x);
}

static t_int *loop_perform(t_int *w)
{
    t_loopctl *ctl = (t_loopctl *)(w[1]);
    t_float *in1 = (t_float *)(w[2]);
    t_float *in2 = (t_float *)(w[3]);
    t_float *out1 = (t_float *)(w[4]);
    t_float *out2 = (t_float *)(w[5]);
    int n = (int)(w[6]);
    loopctl_run(ctl, in1, in2, out1, out2, n);
    return (w+7);
}

static void loop_dsp(t_loop *x, t_signal **sp)
{
    dsp_add(loop_perform, 6,
        &x->x_loopctl, sp[0]->s_vec, sp[1]->s_vec, sp[2]->s_vec, sp[3]->s_vec,
            sp[0]->s_n);
}

static void loop_set(t_loop *x, t_floatarg val)
{
    loopctl_set(&x->x_loopctl, val);
}

static void loop_bang(t_loop *x)
{
    loopctl_set(&x->x_loopctl, 0);
}

void loop_tilde_setup(void)
{
    loop_class = class_new(gensym("loop~"), (t_newmethod)loop_new, 0,
        sizeof(t_loop), 0, 0);
    class_addmethod(loop_class, (t_method)loop_dsp, gensym("dsp"), A_CANT, 0);
    CLASS_MAINSIGNALIN(loop_class, t_loop, x_f);
    class_addmethod(loop_class, (t_method)loop_set, gensym("set"),
        A_DEFFLOAT, 0);
    class_addbang(loop_class, loop_bang);
}

#endif /* PD */