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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
#include <stdlib.h>
#include <m_pd.h>
#include "Python.h"
#include "m_pyo.h"
static t_class *pyo_tilde_class;
typedef struct _pyo_tilde {
t_object obj;
t_sample f;
int debug;
int bs;
int add;
int chnls;
float sr;
const char *file;
t_sample **in;
t_sample **out;
int id; /* pyo server id */
float *inbuf; /* pyo input buffer */
float *outbuf; /* pyo output buffer */
char *msg; /* preallocated string to construct message for pyo */
void (*callback)(int); /* pointer to pyo embedded server callback */
PyThreadState *interp; /* Python thread state linked to this sub interpreter */
} t_pyo_tilde;
t_int *pyo_tilde_perform(t_int *w) {
int i, j, n;
t_pyo_tilde *x = (t_pyo_tilde *)(w[1]); /* pointer to instance struct */
n = (int)(w[2]); /* vector size */
t_sample **in = x->in;
t_sample **out = x->out;
for (i=0; i<n; i++) {
for (j=0; j<x->chnls; j++) {
x->inbuf[i*x->chnls+j] = in[j][i];
}
}
(*x->callback)(x->id);
for (i=0; i<n; i++) {
for (j=0; j<x->chnls; j++) {
out[j][i] = x->outbuf[i*x->chnls+j];
}
}
return (w+3);
}
void pyo_tilde_dsp(t_pyo_tilde *x, t_signal **sp) {
int i, err;
t_sample **dummy = x->in;
for (i=0; i<x->chnls; i++)
*dummy++ = sp[i]->s_vec;
dummy = x->out;
for (i=x->chnls; i<x->chnls*2; i++)
*dummy++ = sp[i]->s_vec;
/* reset pyo only if sampling rate or buffer size have changed */
if ((float)sp[0]->s_sr != x->sr || (int)sp[0]->s_n != x->bs) {
x->sr = (float)sp[0]->s_sr;
x->bs = (int)sp[0]->s_n;
pyo_set_server_params(x->interp, x->sr, x->bs);
if (x->file != NULL) {
err = pyo_exec_file(x->interp, x->file, x->msg, x->add);
if (err == 1) {
post("Unable to open file < %s >", x->file);
x->file = NULL;
} else if (err == 2) {
post("Bad code in file < %s >", x->file);
x->file = NULL;
}
}
}
dsp_add(pyo_tilde_perform, 2, x, sp[0]->s_n);
}
void *pyo_tilde_new(t_floatarg f) {
int i;
t_pyo_tilde *x = (t_pyo_tilde *)pd_new(pyo_tilde_class);
x->chnls = (f) ? f : 2;
x->bs = 64;
x->sr = 44100.0;
x->file = NULL;
x->add = 0;
x->debug = 0;
/* create signal inlets (first is done in pyo_tilde_setup) */
for (i=1; i<x->chnls; i++)
inlet_new(&x->obj, &x->obj.ob_pd, &s_signal, &s_signal);
/* create signal outlets */
for (i=0; i<x->chnls; i++)
outlet_new(&x->obj, &s_signal);
x->in = (t_sample **)getbytes(x->chnls * sizeof(t_sample **));
x->out = (t_sample **)getbytes(x->chnls * sizeof(t_sample **));
x->msg = (char *)getbytes(262144 * sizeof(char *));
for (i=0; i<x->chnls; i++)
x->in[i] = x->out[i] = 0;
x->interp = pyo_new_interpreter(x->sr, x->bs, x->chnls);
x->inbuf = (float *)pyo_get_input_buffer_address(x->interp);
x->outbuf = (float *)pyo_get_output_buffer_address(x->interp);
x->callback = (void *)pyo_get_embedded_callback_address(x->interp);
x->id = pyo_get_server_id(x->interp);
return (void *)x;
}
void pyo_tilde_free(t_pyo_tilde *x) {
freebytes(x->in, sizeof(x->in));
freebytes(x->out, sizeof(x->out));
freebytes(x->msg, sizeof(x->msg));
pyo_end_interpreter(x->interp);
}
void pyo_tilde_set_value(t_pyo_tilde *x, char *att, int argc, t_atom *argv) {
int err, bracket = 0;
char fchar[32];
t_symbol *c = atom_getsymbol(argv);
argc--; argv++;
sprintf(x->msg, "%s%s=", c->s_name, att);
if (argc > 1) {
strcat(x->msg, "[");
bracket = 1;
}
while (argc-- > 0) {
if (argv->a_type == A_SYMBOL) {
strcat(x->msg, atom_getsymbol(argv)->s_name);
}
else if (argv->a_type == A_FLOAT) {
sprintf(fchar, "%.6f", atom_getfloat(argv));
strcat(x->msg, fchar);
}
if (argc > 0)
strcat(x->msg, ",");
argv++;
}
if (bracket)
strcat(x->msg, "]");
err = pyo_exec_statement(x->interp, x->msg, x->debug);
if (err)
post("pyo~: %s", x->msg);
}
void pyo_tilde_value(t_pyo_tilde *x, t_symbol *s, int argc, t_atom *argv) {
char *att = ".value";
pyo_tilde_set_value(x, att, argc, argv);
}
void pyo_tilde_set(t_pyo_tilde *x, t_symbol *s, int argc, t_atom *argv) {
char *att = "";
pyo_tilde_set_value(x, att, argc, argv);
}
void pyo_tilde_create(t_pyo_tilde *x, t_symbol *s, int argc, t_atom *argv) {
int err;
const char *varname, *object;
char fchar[32];
t_symbol *c = atom_getsymbol(argv);
varname = c->s_name;
argc--; argv++;
c = atom_getsymbol(argv);
object = c->s_name;
argc--; argv++;
sprintf(x->msg, "%s=%s(", varname, object);
while (argc-- > 0) {
if (argv->a_type == A_SYMBOL) {
strcat(x->msg, atom_getsymbol(argv)->s_name);
}
else if (argv->a_type == A_FLOAT) {
sprintf(fchar, "%f", atom_getfloat(argv));
strcat(x->msg, fchar);
}
if (argc > 0)
strcat(x->msg, ",");
argv++;
}
strcat(x->msg, ")");
err = pyo_exec_statement(x->interp, x->msg, x->debug);
if (err)
post("pyo~: %s", x->msg);
}
void pyo_tilde_midi_event(t_pyo_tilde *x, t_symbol *s, int argc, t_atom *argv) {
int status = 0, data1 = 0, data2 = 0;
if (argc > 0)
status = (int)atom_getfloat(argv);
if (argc > 1)
data1 = (int)atom_getfloat(++argv);
if (argc > 2)
data2 = (int)atom_getfloat(++argv);
pyo_add_midi_event(x->interp, status, data1, data2);
}
void pyo_tilde_clear(t_pyo_tilde *x) {
pyo_server_reboot(x->interp);
}
void pyo_tilde_debug(t_pyo_tilde *x, t_float debug) {
x->debug = debug <= 0 ? 0 : 1;
}
void pyo_tilde_read(t_pyo_tilde *x, t_symbol *s, int argc, t_atom *argv) {
int err;
switch (argc) {
case 1:
x->add = 0;
x->file = atom_getsymbol(argv)->s_name;
break;
case 2:
x->add = strcmp(atom_getsymbol(argv++)->s_name, "-a") == 0 ? 1 : 0;
x->file = atom_getsymbol(argv)->s_name;
break;
}
if (pyo_is_server_started(x->interp)) {
err = pyo_exec_file(x->interp, x->file, x->msg, x->add);
if (err == 1) {
post("Unable to open file < %s >", x->file);
x->file = NULL;
} else if (err == 2) {
post("Bad code in file < %s >", x->file);
x->file = NULL;
}
}
}
void pyo_tilde_call(t_pyo_tilde *x, t_symbol *s, int argc, t_atom *argv) {
int err;
char fchar[32];
sprintf(x->msg, "%s(", atom_getsymbol(argv)->s_name);
argc--; argv++;
while (argc-- > 0) {
if (argv->a_type == A_SYMBOL) {
strcat(x->msg, atom_getsymbol(argv)->s_name);
}
else if (argv->a_type == A_FLOAT) {
sprintf(fchar, "%f", atom_getfloat(argv));
strcat(x->msg, fchar);
}
if (argc > 0)
strcat(x->msg, ", ");
argv++;
}
strcat(x->msg, ")");
err = pyo_exec_statement(x->interp, x->msg, x->debug);
if (err)
post("pyo~: %s", x->msg);
}
void pyo_tilde_setup(void) {
pyo_tilde_class = class_new(gensym("pyo~"), (t_newmethod)pyo_tilde_new,
(t_method)pyo_tilde_free, sizeof(t_pyo_tilde), CLASS_DEFAULT, A_DEFFLOAT, 0);
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_dsp, gensym("dsp"), 0);
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_clear, gensym("clear"), 0);
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_value, gensym("value"),
A_GIMME, 0); /* send value(s) to a Sig or SigTo object */
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_set, gensym("set"),
A_GIMME, 0); /* send value(s) to any object's attribute */
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_read, gensym("read"),
A_GIMME, 0); /* read a script file */
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_call, gensym("call"),
A_GIMME, 0); /* call a function or a method */
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_create, gensym("create"),
A_GIMME, 0); /* create a python object */
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_midi_event, gensym("midi"),
A_GIMME, 0); /* create a python object */
class_addmethod(pyo_tilde_class, (t_method)pyo_tilde_debug, gensym("debug"),
A_DEFFLOAT, 0); /* set the debug (verbose) mode */
CLASS_MAINSIGNALIN(pyo_tilde_class, t_pyo_tilde, f);
}
|