aboutsummaryrefslogtreecommitdiff
path: root/cyclone/sickle/peakamp.c
blob: 67d10932d27717011c44eefb958042339069655e (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
/* Copyright (c) 2002-2003 krzYszcz and others.
 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.  */

#include "m_pd.h"
#include "sickle/sic.h"

typedef struct _peakamp
{
    t_sic     x_sic;
    t_float   x_value;
    int       x_nwait;
    int       x_nleft;
    int       x_precount;
    float     x_waittime;
    float     x_ksr;
    t_clock  *x_clock;
} t_peakamp;

static t_class *peakamp_class;

static void peakamp_tick(t_peakamp *x)
{
    outlet_float(((t_object *)x)->ob_outlet, x->x_value);
    x->x_value = 0;
    if ((x->x_nleft = x->x_nwait - x->x_precount) < 0)  /* CHECKME */
	x->x_nleft = 0;
}

static void peakamp_bang(t_peakamp *x)
{
    peakamp_tick(x);  /* CHECKME */
}

static void peakamp_ft1(t_peakamp *x, t_floatarg f)
{
    if ((x->x_waittime = f) < 0.)
	x->x_waittime = 0.;
    if ((x->x_nwait = (int)(x->x_waittime * x->x_ksr)) < 0)
	x->x_nwait = 0;
}

static t_int *peakamp_perform(t_int *w)
{
    t_peakamp *x = (t_peakamp *)(w[1]); 
    int nblock = (int)(w[2]);
    t_float *in = (t_float *)(w[3]);
    t_float value = x->x_value;
    if (x->x_nwait)
    {
	if (x->x_nleft < nblock)
	{
	    clock_delay(x->x_clock, 0);
	    x->x_precount = nblock - x->x_nleft;
	    x->x_nleft = 0;  /* LATER rethink */
	}
	else x->x_nleft -= nblock;
    }
    while (nblock--)
    {
	t_float f = *in++;
	if (f > value)
	    value = f;
	else if (f < -value)
	    value = -f;
    }
    x->x_value = value;
    return (w + 4);
}

static void peakamp_dsp(t_peakamp *x, t_signal **sp)
{
    x->x_ksr = sp[0]->s_sr * 0.001;
    x->x_nwait = (int)(x->x_waittime * x->x_ksr);
    dsp_add(peakamp_perform, 3, x, sp[0]->s_n, sp[0]->s_vec);
}

static void peakamp_free(t_peakamp *x)
{
    if (x->x_clock) clock_free(x->x_clock);
}

static void *peakamp_new(t_floatarg f)
{
    t_peakamp *x = (t_peakamp *)pd_new(peakamp_class);
    x->x_value = 0.;
    x->x_nleft = 0;
    x->x_ksr = sys_getsr() * 0.001;
    peakamp_ft1(x, f);
    inlet_new((t_object *)x, (t_pd *)x, &s_float, gensym("ft1"));
    outlet_new((t_object *)x, &s_float);
    x->x_clock = clock_new(x, (t_method)peakamp_tick);
    return (x);
}

void peakamp_tilde_setup(void)
{
    peakamp_class = class_new(gensym("peakamp~"),
			      (t_newmethod)peakamp_new,
			      (t_method)peakamp_free,
			      sizeof(t_peakamp), 0,
			      A_DEFFLOAT, 0);
    sic_setup(peakamp_class, peakamp_dsp, SIC_FLOATTOSIGNAL);
    class_addbang(peakamp_class, peakamp_bang);
    class_addmethod(peakamp_class, (t_method)peakamp_ft1,
		    gensym("ft1"), A_FLOAT, 0);
}