aboutsummaryrefslogtreecommitdiff
path: root/control/constant.c
blob: 0584344d3052a9b9a7ddc58ce7f8e7e015abd984 (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
/* (C) Guenter Geiger <geiger@epy.co.at> */


#include <m_pd.h>
#include <math.h>
#include <string.h>
#ifdef _MSC_VER
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

/* ------------------------ constant ----------------------------- */
#ifndef M_PI
#define M_PI 3.141593f
#endif

static t_class *constant_class;


typedef struct _constant
{
     t_object x_obj;
     t_float  x_constant;
} t_constant;


void constant_bang(t_constant *x)
{
     outlet_float(x->x_obj.ob_outlet, x->x_constant);
}

static void *constant_new(t_symbol* s)
{
    t_constant *x = (t_constant *)pd_new(constant_class);
    
    if (s == &s_)
        x->x_constant = M_PI;
    else if (!strcmp(s->s_name,"pi"))
        x->x_constant = M_PI;
    else if (!strcmp(s->s_name,"2pi"))
        x->x_constant = 2*M_PI;
    else if (!strcmp(s->s_name,"e"))
        x->x_constant = exp(1.0);
    else if (!strcmp(s->s_name,"M_PI"))
        x->x_constant = M_PI;
    else if (!strcmp(s->s_name,"PI"))
        x->x_constant = M_PI;
    else if (!strcmp(s->s_name,"TWOPI"))
        x->x_constant = 2*M_PI;
    else
        pd_error(x, "Unsupported constant '%s'", s->s_name);

    outlet_new(&x->x_obj, &s_float);
    return (x);
}

void constant_setup(void)
{
    constant_class = class_new(gensym("constant"),
                               (t_newmethod)constant_new,
                               0,
                               sizeof(t_constant),
                               0,
                               A_DEFSYMBOL, 
                               0);
    class_addbang(constant_class,constant_bang);
}