aboutsummaryrefslogtreecommitdiff
path: root/stksitar~/stksitar~.cc
blob: bfa7a5417b712a5639738a94a354e37716d466f0 (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
/* stksitar~ -- STK-based sitar synthesis
 * requires STK library
 * Copyleft 2001 Yves Degoyon.
 * Permission is granted to use this software for any purpose provided you
 * keep this copyright notice intact.
 *
 * THE AUTHOR AND HIS EXPLOITERS MAKE NO WARRANTY, EXPRESS OR IMPLIED,
 * IN CONNECTION WITH THIS SOFTWARE.
 *
*/

#include "m_pd.h"
#include "sitar.h"

#define DEFAULT_FREQ 250.0
#define DEFAULT_PLUCK 0.25

typedef struct _stksitar
{
    t_object x_obj;
    sitar *x_stksitar;
    t_int x_on;
    t_float x_freq;
    t_float x_pluck;
} t_stksitar;

static t_class *stksitar_class;

static void *stksitar_new(void)
{
    t_stksitar *x = (t_stksitar *)pd_new(stksitar_class);
    x->x_freq = DEFAULT_FREQ;
    x->x_pluck = DEFAULT_PLUCK;
    outlet_new(&x->x_obj, &s_signal);
    inlet_new( &x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("freq") );
    inlet_new( &x->x_obj, &x->x_obj.ob_pd, &s_float, gensym("pluck") );
    if( (x->x_stksitar = new sitar( 50.0 )) == NULL ) {
       post( "stksitar~: cannot build sitar instrument from STK" );
       return NULL;
    } else {
       return (x);
    }
}

static void stksitar_freq(t_stksitar* x, t_float ffreq)
{
    if ( ffreq < 50.0 )
    {
       post("stksitar~ : wrong frequency" );
       return;
    }
    x->x_stksitar->setFreq( (StkFloat) ffreq );
    x->x_freq = ffreq;
}

static void stksitar_pluck(t_stksitar* x, t_float fpluck)
{
    if ( fpluck < 0.05 || fpluck > 0.35 )
    {
       post("stksitar~ : wrong pluck argument" );
       return;
    }
    x->x_stksitar->pluck( (StkFloat) fpluck );
    x->x_pluck = fpluck;
}

static void stksitar_play(t_stksitar* x)
{
    x->x_on = 1; // activate sound
    x->x_stksitar->noteOn( x->x_freq, x->x_pluck ); // start sound
}

static void stksitar_stop(t_stksitar* x)
{
    x->x_on = 0; // deactivate sound
}

static t_int *stksitar_perform(t_int *w)
{
    t_float *out = (t_float *)(w[1]);
    int n = (int)(w[2]);
    t_stksitar* x = (t_stksitar*)(w[3]);

    while ( n-- ) 
    {
       if ( x->x_on ) 
       {
         double dare;

         dare = (float)x->x_stksitar->tick();
         // post( "synthesis : %f", dare );
         *out=dare;
       }
       else
       {
         *(out) = 0.0;
       }
       out++;
    }

    return (w+4);
}

static void stksitar_dsp(t_stksitar *x, t_signal **sp)
{
    dsp_add(stksitar_perform, 3, sp[0]->s_vec, sp[0]->s_n, x);
}

extern "C" void stksitar_tilde_setup(void)
{
    stksitar_class = class_new(gensym("stksitar~"), (t_newmethod)stksitar_new, 0,
    	sizeof(t_stksitar), 0, A_NULL);
    class_sethelpsymbol(stksitar_class, gensym("help-stksitar~.pd"));
    class_addmethod(stksitar_class, (t_method)stksitar_dsp, gensym("dsp"), A_NULL);
    class_addmethod(stksitar_class, (t_method)stksitar_play, gensym("play") , A_NULL);
    class_addmethod(stksitar_class, (t_method)stksitar_stop, gensym("stop") , A_NULL);
    class_addmethod(stksitar_class, (t_method)stksitar_freq, gensym("freq") , A_DEFFLOAT, A_NULL);
    class_addmethod(stksitar_class, (t_method)stksitar_pluck, gensym("pluck") , A_DEFFLOAT, A_NULL);
}