Hi,
im trying to implement this simple object/class, that takes symbol in the first inlet and based on its value gates signal from nd inlet to outlet. The symbol comes from udp data stream, that is being parsed as OSC. Internally the incoming signal is multiplied by the symbol value (0 or 1). So in my understanding output should be silent or come through.
Buuut its not working as excpected... signal output is terribly noisy and "laggy" sounding. Sometimes the puredata even shows and "Audio error". Interestingly, when i stop the OSC transmission it goes to normal and sounds as expected.
Is my understanding of signal manipulation wrong? Or is the datastream lagging Pd?
Thanks much for advice!
simple_matrix~.c
//sudo ln /Applications/Pd-0.55-0.app/Contents/Resources/src/m_pd.h m_pd.h
//snad je to ok udelat takhle no
#include <m_pd.h>
#include <string.h>
#define MATRIX_SIZE 7
#define TRANSM_SIZE 3
static t_class *simple_matrix_tilde_class;
typedef struct _simple_matrix_tilde
{
t_object x_obj;
t_float m_state[MATRIX_SIZE][MATRIX_SIZE]; //stav peč desky
t_symbol transm_in;
t_inlet *sig_one_in, *sig_two_in, *sig_tri_in, *sig_qud_in, *sig_fiv_in, *sig_six_in, *sig_sev_in;
t_outlet *sig_one_out, *sig_two_out, *sig_tri_out, *sig_qud_out, *sig_fiv_out, *sig_six_out, *sig_sev_out;
} t_simple_matrix_tilde;
void simple_matrix_tilde_osc_recv(t_simple_matrix_tilde *x, t_symbol *s)
{
if(strlen(s->s_name) > TRANSM_SIZE)
{
//pd_error(&x->x_obj, ":(");
//sometimes the size is for some reason larger, maybe UDP? crazy guy
}
//post("lenght %d", strlen(s->s_name));
//post("value %s", s->s_name);
char recv[TRANSM_SIZE]; //snad to nevybouchne
memcpy(recv, s->s_name, TRANSM_SIZE);
int val = recv[0] - '0';
int posx = recv[1] - '0';
int posy = recv[2] - '0';
//post("val %d posx %d posy %d", val, posx, posy);
x->m_state[posx][posy] = (t_float)val;
}
t_int *simple_matrix_tilde_perform(t_int *w)
{
t_simple_matrix_tilde *x = (t_simple_matrix_tilde*)(w[1]);
t_sample *sig_in = (t_sample *)(w[2]);
t_sample *sig_out = (t_sample *)(w[3]);
int len = (int)(w[4]); //lenght of sample
for(int u = 0; u < len; u++)
{
t_sample timesby = x->m_state[0][0];
*sig_out++ = *(sig_in++) * timesby;
}
return (w+5);
}
void simple_matrix_tilde_dsp(t_simple_matrix_tilde *x, t_signal **sp)
{
dsp_add(simple_matrix_tilde_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
return;
}
void *simple_matrix_tilde_new(void)
{
t_simple_matrix_tilde *x = (t_simple_matrix_tilde *)pd_new(simple_matrix_tilde_class);
//wall,
//wall,
x->sig_one_in = inlet_new(&x->x_obj, &x->x_obj.ob_pd, &s_signal, &s_signal);
x->sig_one_out = outlet_new(&x->x_obj, &s_signal);
return (void *) x;
}
void simple_matrix_tilde_freedom(t_simple_matrix_tilde *x)
{
inlet_free(x->sig_one_in);
outlet_free(x->sig_one_out);
}
void simple_matrix_tilde_setup(void)
{
simple_matrix_tilde_class = class_new(
gensym("simple_matrix~"),
(t_newmethod)simple_matrix_tilde_new,
(t_method)simple_matrix_tilde_freedom,
sizeof(t_simple_matrix_tilde),
CLASS_DEFAULT,
0
);
class_addsymbol(simple_matrix_tilde_class, simple_matrix_tilde_osc_recv);
class_addmethod(simple_matrix_tilde_class, (t_method)simple_matrix_tilde_dsp, gensym("dsp"), A_CANT, 0);
}
The function simple_matrix_tilde_osc_recv just takes the input symbol and updates state of matrix connections, from which the gate of signal is later determined in perform function. The object should have 7 signal inlets and outlets in the future.
Also i understand, that this could be implemented as a subpatch, but it seemed like good first external, to learn the basics.
aand my Pd patch for testing:

(i just got into writing externls, so maybe my undertanding of basic Pd internal concepts is little off)