| 1 |
#include <stdlib.h> |
| 2 |
#include <m_pd.h> |
| 3 |
#include "Python.h" |
| 4 |
#include "m_pyo.h" |
| 5 |
|
| 6 |
static t_class *pyo_tilde_class; |
| 7 |
|
| 8 |
typedef struct _pyo_tilde { |
| 9 |
t_object obj; |
| 10 |
t_sample f; |
| 11 |
int debug; |
| 12 |
int bs; |
| 13 |
int add; |
| 14 |
int chnls; |
| 15 |
float sr; |
| 16 |
const char *file; |
| 17 |
t_sample **in; |
| 18 |
t_sample **out; |
| 19 |
int id; /* pyo server id */ |
| 20 |
float *inbuf; /* pyo input buffer */ |
| 21 |
float *outbuf; /* pyo output buffer */ |
| 22 |
char *msg; /* preallocated string to construct message for pyo */ |
| 23 |
void (*callback)(int); /* pointer to pyo embedded server callback */ |
| 24 |
PyThreadState *interp; /* Python thread state linked to this sub interpreter */ |
| 25 |
} t_pyo_tilde; |
| 26 |
|
| 27 |
t_int *pyo_tilde_perform(t_int *w) { |
| 28 |
int i, j, n; |
| 29 |
t_pyo_tilde *x = (t_pyo_tilde *)(w[1]); /* pointer to instance struct */ |
| 30 |
n = (int)(w[2]); /* vector size */ |
| 31 |
t_sample **in = x->in; |
| 32 |
t_sample **out = x->out; |
| 33 |
for (i=0; i<n; i++) { |
| 34 |
for (j=0; j<x->chnls; j++) { |
| 35 |
x->inbuf[i*x->chnls+j] = in[j][i]; |
| 36 |
} |
| 37 |
} |
| 38 |
(*x->callback)(x->id); |
| 39 |
for (i=0; i<n; i++) { |
| 40 |
for (j=0; j<x->chnls; j++) { |
| 41 |
out[j][i] = x->outbuf[i*x->chnls+j]; |
| 42 |
} |
| 43 |
} |
| 44 |
return (w+3); |
| 45 |
} |
| 46 |
|
| 47 |
void pyo_tilde_dsp(t_pyo_tilde *x, t_signal **sp) { |
| 48 |
int i, err; |
| 49 |
t_sample **dummy = x->in; |
| 50 |
for (i=0; i<x->chnls; i++) |
| 51 |
*dummy++ = sp[i]->s_vec; |
| 52 |
dummy = x->out; |
| 53 |
for (i=x->chnls; i<x->chnls*2; i++) |
| 54 |
*dummy++ = sp[i]->s_vec; |
| 55 |
/* reset pyo only if sampling rate or buffer size have changed */ |
| 56 |
if ((float)sp[0]->s_sr != x->sr || (int)sp[0]->s_n != x->bs) { |
| 57 |
x->sr = (float)sp[0]->s_sr; |
| 58 |
x->bs = (int)sp[0]->s_n; |
| 59 |
pyo_set_server_params(x->interp, x->sr, x->bs); |
| 60 |
if (x->file != NULL) { |
| 61 |
err = pyo_exec_file(x->interp, x->file, x->msg, x->add); |
| 62 |
if (err == 1) { |
| 63 |
post("Unable to open file < %s >", x->file); |
| 64 |
x->file = NULL; |
| 65 |
} else if (err == 2) { |
| 66 |
post("Bad code in file < %s >", x->file); |
| 67 |
x->file = NULL; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
dsp_add(pyo_tilde_perform, 2, x, sp[0]->s_n); |
| 72 |
} |
| 73 |
|
| 74 |
void *pyo_tilde_new(t_floatarg f) { |
| 75 |
int i; |
| 76 |
t_pyo_tilde *x = (t_pyo_tilde *)pd_new(pyo_tilde_class); |
| 77 |
|
| 78 |
x->chnls = (f) ? f : 2; |
| 79 |
x->bs = 64; |
| 80 |
x->sr = 44100.0; |
| 81 |
x->file = NULL; |
| 82 |
x->add = 0; |
| 83 |
x->debug = 0; |
| 84 |
|
| 85 |
/* create signal inlets (first is done in pyo_tilde_setup) */ |
| 86 |
for (i=1; i<x->chnls; i++) |
| 87 |
inlet_new(&x->obj, &x->obj.ob_pd, &s_signal, &s_signal); |
| 88 |
/* create signal outlets */ |
| 89 |
for (i=0; i<x->chnls; i++) |
| 90 |
outlet_new(&x->obj, &s_signal); |
| 91 |
|
| 92 |
x->in = (t_sample **)getbytes(x->chnls * sizeof(t_sample **)); |
| 93 |
x->out = (t_sample **)getbytes(x->chnls * sizeof(t_sample **)); |
| 94 |
x->msg = (char *)getbytes(262144 * sizeof(char *)); |
| 95 |
|
| 96 |
for (i=0; i<x->chnls; i++) |
| 97 |
x->in[i] = x->out[i] = 0; |
| 98 |
|
| 99 |
x->interp = pyo_new_interpreter(x->sr, x->bs, x->chnls); |
| 100 |
|
| 101 |
x->inbuf = (float *)pyo_get_input_buffer_address(x->interp); |
| 102 |
x->outbuf = (float *)pyo_get_output_buffer_address(x->interp); |
| 103 |
x->callback = (void *)pyo_get_embedded_callback_address(x->interp); |
| 104 |
x->id = pyo_get_server_id(x->interp); |
| 105 |
|
| 106 |
return (void *)x; |
| 107 |
} |
| 108 |
|
| 109 |
void pyo_tilde_free(t_pyo_tilde *x) { |
| 110 |
freebytes(x->in, sizeof(x->in)); |
| 111 |
freebytes(x->out, sizeof(x->out)); |
| 112 |
freebytes(x->msg, sizeof(x->msg)); |
| 113 |
pyo_end_interpreter(x->interp); |
| 114 |
} |
| 115 |
|
| 116 |
void pyo_tilde_set_value(t_pyo_tilde *x, char *att, int argc, t_atom *argv) { |
| 117 |
int err, bracket = 0; |
| 118 |
char fchar[32]; |
| 119 |
t_symbol *c = atom_getsymbol(argv); |
| 120 |
argc--; argv++; |
| 121 |
sprintf(x->msg, "%s%s=", c->s_name, att); |
| 122 |
if (argc > 1) { |
| 123 |
strcat(x->msg, "["); |
| 124 |
bracket = 1; |
| 125 |
} |
| 126 |
while (argc-- > 0) { |
| 127 |
if (argv->a_type == A_SYMBOL) { |
| 128 |
strcat(x->msg, atom_getsymbol(argv)->s_name); |
| 129 |
} |
| 130 |
else if (argv->a_type == A_FLOAT) { |
| 131 |
sprintf(fchar, "%.6f", atom_getfloat(argv)); |
| 132 |
strcat(x->msg, fchar); |
| 133 |
} |
| 134 |
if (argc > 0) |
| 135 |
strcat(x->msg, ","); |
| 136 |
argv++; |
| 137 |
} |
| 138 |
if (bracket) |
| 139 |
strcat(x->msg, "]"); |
| 140 |
err = pyo_exec_statement(x->interp, x->msg, x->debug); |
| 141 |
if (err) |
| 142 |
post("pyo~: %s", x->msg); |
| 143 |
} |
| 144 |
|
| 145 |
void pyo_tilde_value(t_pyo_tilde *x, t_symbol *s, int argc, t_atom *argv) { |
| 146 |
char *att = ".value"; |
| 147 |
pyo_tilde_set_value(x, att, argc, argv); |
| 148 |
} |
| 149 |
void pyo_tilde_set(t_pyo_tilde *x, t_symbol *s, int argc, t_atom *argv) { |
| 150 |
char *att = ""; |
| 151 |
pyo_tilde_set_value(x, att, argc, argv); |
| 152 |
} |
| 153 |
|
| 154 |
void pyo_tilde_create(t_pyo_tilde *x, t_symbol *s, int argc, t_atom *argv) { |
| 155 |
int err; |
| 156 |
const char *varname, *object; |
| 157 |
char fchar[32]; |
| 158 |
t_symbol *c = atom_getsymbol(argv); |
| 159 |
varname = c->s_name; |
| 160 |
argc--; argv++; |
| 161 |
c = atom_getsymbol(argv); |
| 162 |
object = c->s_name; |
| 163 |
argc--; argv++; |
| 164 |
sprintf(x->msg, "%s=%s(", varname, object); |
| 165 |
while (argc-- > 0) { |
| 166 |
if (argv->a_type == A_SYMBOL) { |
| 167 |
strcat(x->msg, atom_getsymbol(argv)->s_name); |
| 168 |
} |
| 169 |
else if (argv->a_type == A_FLOAT) { |
| 170 |
sprintf(fchar, "%f", atom_getfloat(argv)); |
| 171 |
strcat(x->msg, fchar); |
| 172 |
} |
| 173 |
if (argc > 0) |
| 174 |
strcat(x->msg, ","); |
| 175 |
argv++; |
| 176 |
} |
| 177 |
strcat(x->msg, ")"); |
| 178 |
err = pyo_exec_statement(x->interp, x->msg, x->debug); |
| 179 |
if (err) |
| 180 |
post("pyo~: %s", x->msg); |
| 181 |
} |
| 182 |
|
| 183 |
void pyo_tilde_midi_event(t_pyo_tilde *x, t_symbol *s, int argc, t_atom *argv) { |
| 184 |
int status = 0, data1 = 0, data2 = 0; |
| 185 |
if (argc > 0) |
| 186 |
status = (int)atom_getfloat(argv); |
| 187 |
if (argc > 1) |
| 188 |
data1 = (int)atom_getfloat(++argv); |
| 189 |
if (argc > 2) |
| 190 |
data2 = (int)atom_getfloat(++argv); |
| 191 |
pyo_add_midi_event(x->interp, status, data1, data2); |
| 192 |
} |
| 193 |
|
| 194 |
void pyo_tilde_clear(t_pyo_tilde *x) { |
| 195 |
pyo_server_reboot(x->interp); |
| 196 |
} |
| 197 |
|
| 198 |
void pyo_tilde_debug(t_pyo_tilde *x, t_float debug) { |
| 199 |
x->debug = debug <= 0 ? 0 : 1; |
| 200 |
} |
| 201 |
|
| 202 |
void pyo_tilde_read(t_pyo_tilde *x, t_symbol *s, int argc, t_atom *argv) { |
| 203 |
int err; |
| 204 |
switch (argc) { |
| 205 |
case 1: |
| 206 |
x->add = 0; |
| 207 |
x->file = atom_getsymbol(argv)->s_name; |
| 208 |
break; |
| 209 |
case 2: |
| 210 |
x->add = strcmp(atom_getsymbol(argv++)->s_name, "-a") == 0 ? 1 : 0; |
| 211 |
x->file = atom_getsymbol(argv)->s_name; |
| 212 |
break; |
| 213 |
} |
| 214 |
if (pyo_is_server_started(x->interp)) { |
| 215 |
err = pyo_exec_file(x->interp, x->file, x->msg, x->add); |
| 216 |
if (err == 1) { |
| 217 |
post("Unable to open file < %s >", x->file); |
| 218 |
x->file = NULL; |
| 219 |
} else if (err == 2) { |
| 220 |
post("Bad code in file < %s >", x->file); |
| 221 |
x->file = NULL; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
void pyo_tilde_call(t_pyo_tilde *x, t_symbol *s, int argc, t_atom *argv) { |
| 227 |
int err; |
| 228 |
char fchar[32]; |
| 229 |
sprintf(x->msg, "%s(", atom_getsymbol(argv)->s_name); |
| 230 |
argc--; argv++; |
| 231 |
while (argc-- > 0) { |
| 232 |
if (argv->a_type == A_SYMBOL) { |
| 233 |
strcat(x->msg, atom_getsymbol(argv)->s_name); |
| 234 |
} |
| 235 |
else if (argv->a_type == A_FLOAT) { |
| 236 |
sprintf(fchar, "%f", atom_getfloat(argv)); |
| 237 |
strcat(x->msg, fchar); |
| 238 |
} |
| 239 |
if (argc > 0) |
| 240 |
strcat(x->msg, ", "); |
| 241 |
argv++; |
| 242 |
} |
| 243 |
strcat(x->msg, ")"); |
| 244 |
err = pyo_exec_statement(x->interp, x->msg, x->debug); |
| 245 |
if (err) |
| 246 |
post("pyo~: %s", x->msg); |
| 247 |
} |
| 248 |
|
| 249 |
void pyo_tilde_setup(void) { |
| 250 |
pyo_tilde_class = class_new(gensym("pyo~"), (t_newmethod)pyo_tilde_new, |
| 251 |
(t_method)pyo_tilde_free, sizeof(t_pyo_tilde), CLASS_DEFAULT, A_DEFFLOAT, 0); |
| 252 |
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_dsp, gensym("dsp"), 0); |
| 253 |
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_clear, gensym("clear"), 0); |
| 254 |
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_value, gensym("value"), |
| 255 |
A_GIMME, 0); /* send value(s) to a Sig or SigTo object */ |
| 256 |
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_set, gensym("set"), |
| 257 |
A_GIMME, 0); /* send value(s) to any object's attribute */ |
| 258 |
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_read, gensym("read"), |
| 259 |
A_GIMME, 0); /* read a script file */ |
| 260 |
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_call, gensym("call"), |
| 261 |
A_GIMME, 0); /* call a function or a method */ |
| 262 |
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_create, gensym("create"), |
| 263 |
A_GIMME, 0); /* create a python object */ |
| 264 |
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_midi_event, gensym("midi"), |
| 265 |
A_GIMME, 0); /* create a python object */ |
| 266 |
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_debug, gensym("debug"), |
| 267 |
A_DEFFLOAT, 0); /* set the debug (verbose) mode */ |
| 268 |
CLASS_MAINSIGNALIN(pyo_tilde_class, t_pyo_tilde, f); |
| 269 |
} |