~ubuntu-branches/ubuntu/saucy/pd-smlib/saucy

« back to all changes in this revision

Viewing changes to vlmax.c

  • Committer: Bazaar Package Importer
  • Author(s): Hans-Christoph Steiner
  • Date: 2010-11-10 15:17:58 UTC
  • Revision ID: james.westby@ubuntu.com-20101110151758-3acjf69kiudo3gh4
Tags: upstream-0.12.1
ImportĀ upstreamĀ versionĀ 0.12.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "defines.h"
 
2
 
 
3
/*--------------- vlmax -----------------------*/
 
4
/* leaky maximum of each element in a list     */
 
5
/* arguments: [halfdecay] */
 
6
 
 
7
static t_class *vlmax_class;
 
8
 
 
9
typedef struct _vlmax
 
10
{
 
11
    t_object x_obj;
 
12
        float m_c_leak;
 
13
        float m_leak;
 
14
        float *m_max;
 
15
        int m_n;
 
16
} t_vlmax;
 
17
 
 
18
 
 
19
static void vlmax_perform(t_vlmax *x, t_symbol *s, int argc, t_atom *argv)
 
20
{
 
21
        int i;
 
22
        t_atom *ap,*app;
 
23
        float *fp;
 
24
        float m_leak;
 
25
        float m_c_leak;
 
26
        m_leak=x->m_leak;
 
27
        m_c_leak=x->m_c_leak;
 
28
 
 
29
        if (argc!=x->m_n)
 
30
        {
 
31
                int i;
 
32
                if (x->m_max)
 
33
                        freebytes(x->m_max,x->m_n);
 
34
                x->m_max=(float*)getbytes(argc*sizeof(float));
 
35
                for(i=0;i<argc;i++)
 
36
                        x->m_max[i]=0.0f;
 
37
                x->m_n=argc;
 
38
        }
 
39
 
 
40
        fp=x->m_max;
 
41
    ap = (t_atom *)getbytes(sizeof(t_atom)*argc);
 
42
        app=ap;
 
43
        for (i = 0; i < argc; i++)
 
44
        {
 
45
                float f=atom_getfloat(argv++);
 
46
                *fp =(f > *fp ) ? f : *fp *m_c_leak + f*m_leak;
 
47
                SETFLOAT(app, *fp);
 
48
                app++;
 
49
                fp++;
 
50
        }
 
51
        outlet_list(x->x_obj.ob_outlet,gensym("list"),argc,ap);
 
52
    freebytes(ap,argc);
 
53
}
 
54
 
 
55
static void vlmax_setHalfDecay(t_vlmax *x, t_floatarg halfDecayTime)
 
56
{
 
57
        x->m_c_leak=(float)powf(.5,(1.0f/halfDecayTime));
 
58
        x->m_leak=1.0f-x->m_c_leak;
 
59
}
 
60
 
 
61
static void *vlmax_new(t_float halfDecayTime)
 
62
{
 
63
        t_vlmax *x=(t_vlmax *)pd_new(vlmax_class);
 
64
        outlet_new(&x->x_obj, gensym("list"));
 
65
        vlmax_setHalfDecay(x, halfDecayTime);
 
66
        x->m_n=0;
 
67
        x->m_max=0;
 
68
        return (void *)x;
 
69
}
 
70
 
 
71
static void vlmax_free(t_vlmax *x)
 
72
{
 
73
        freebytes(x->m_max,x->m_n);
 
74
}
 
75
 
 
76
void vlmax_setup(void)
 
77
{
 
78
    vlmax_class = class_new(gensym("vlmax"),
 
79
        (t_newmethod)vlmax_new, (t_method)vlmax_free,
 
80
                sizeof(t_vlmax), 
 
81
                CLASS_DEFAULT,
 
82
            A_DEFFLOAT, 0);
 
83
        class_addmethod(vlmax_class, (t_method)vlmax_setHalfDecay,
 
84
        gensym("decay"), A_DEFFLOAT, NULL);
 
85
    class_addlist(vlmax_class, (t_method)vlmax_perform);
 
86
}