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

« back to all changes in this revision

Viewing changes to vthreshold.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
/* --------------------- vthreshold ----------------------------- */
 
2
 
 
3
#include "defines.h"
 
4
#include "memory.h"
 
5
 
 
6
static t_class *vthreshold_class;
 
7
 
 
8
typedef struct _vthreshold
 
9
{
 
10
    t_object x_obj;
 
11
    t_outlet *x_outlet1;        /* bang out for high thresh */
 
12
    t_outlet *x_outlet2;        /* bang out for low thresh */
 
13
    int *x_state;                       /* 1 = high, 0 = low */
 
14
        int x_n;
 
15
    float x_hithresh;           /* value of high vthreshold */
 
16
    float x_lothresh;           /* value of low vthreshold */
 
17
    float x_hideadtime;         /* hi dead */
 
18
    float x_lodeadtime;         /* lo dead */
 
19
} t_vthreshold;
 
20
 
 
21
    /* "set" message to specify vthresholds and dead times */
 
22
static void vthreshold_set(t_vthreshold *x,
 
23
    t_floatarg hithresh, t_floatarg hideadtime,
 
24
    t_floatarg lothresh, t_floatarg lodeadtime,
 
25
        t_floatarg nf)
 
26
{
 
27
    if (lothresh > hithresh)
 
28
        lothresh = hithresh;
 
29
    x->x_hithresh = hithresh;
 
30
    x->x_hideadtime = hideadtime;
 
31
    x->x_lothresh = lothresh;
 
32
    x->x_lodeadtime = lodeadtime;
 
33
    freebytes(x->x_state,x->x_n);
 
34
        x->x_n=(int)nf;
 
35
    x->x_state = (int *)getbytes(sizeof(int)*x->x_n);
 
36
        memset(x->x_state , 0, x->x_n);
 
37
}
 
38
 
 
39
static t_vthreshold *vthreshold_new(t_floatarg hithresh,
 
40
    t_floatarg hideadtime, t_floatarg lothresh, t_floatarg lodeadtime, t_floatarg n)
 
41
{
 
42
    t_vthreshold *x = (t_vthreshold *)
 
43
        pd_new(vthreshold_class);
 
44
    x->x_state = 0;             /* low state */
 
45
    x->x_outlet1 = outlet_new(&x->x_obj, gensym("float"));
 
46
    x->x_outlet2 = outlet_new(&x->x_obj, gensym("float"));
 
47
    vthreshold_set(x, hithresh, hideadtime, lothresh, lodeadtime, n);
 
48
    return (x);
 
49
}
 
50
 
 
51
static void vthreshold_free(t_vthreshold *x)
 
52
{
 
53
        freebytes(x->x_state,x->x_n);
 
54
}
 
55
 
 
56
static void vthreshold_perform(t_vthreshold *x, t_symbol *s, int argc, t_atom *argv)
 
57
{
 
58
        int i;
 
59
        int *state;
 
60
 
 
61
        state=x->x_state;
 
62
        if (argc>x->x_n) argc=x->x_n;
 
63
        for (i=0;i<argc;i++)
 
64
        {
 
65
                float f;
 
66
                f=atom_getfloat(argv++);
 
67
 
 
68
                if (*state<0)
 
69
                {
 
70
                        if (f>x->x_hithresh)
 
71
                        {
 
72
                                outlet_float(x->x_outlet1, (float)i); // on
 
73
                                *state=1;
 
74
                        }
 
75
                }
 
76
                else 
 
77
                {
 
78
                        if (f<x->x_lothresh)
 
79
                        {
 
80
                                outlet_float(x->x_outlet2, (float)i); // off
 
81
                                *state=-1;
 
82
                        }
 
83
                }
 
84
                state++;
 
85
        }
 
86
}
 
87
 
 
88
 
 
89
static void vthreshold_ff(t_vthreshold *x)
 
90
{
 
91
    freebytes(x->x_state,x->x_n);
 
92
}
 
93
 
 
94
void vthreshold_setup( void)
 
95
{
 
96
    vthreshold_class = class_new(gensym("vthreshold"),
 
97
        (t_newmethod)vthreshold_new, (t_method)vthreshold_free,
 
98
        sizeof(t_vthreshold), 0,
 
99
            A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, A_DEFFLOAT, 0);
 
100
 
 
101
    class_addlist(vthreshold_class, (t_method)vthreshold_perform);   
 
102
        class_addmethod(vthreshold_class, (t_method)vthreshold_set,
 
103
        gensym("set"), A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0);
 
104
//    class_addmethod(vthreshold_class, (t_method)vthreshold_ft1,
 
105
//      gensym("ft1"), A_FLOAT, 0);
 
106
}