Hello, I have been trying to solve this problem for a couple of days by now. I am pretty sure that this is a simple pointer arithmetics error on my part, but still, this is driving me crazy and I hope you don't mind asking me this question here.
I am writing a simple external which adds all the inlet signals. I expect to allow any number of inlets, so I am using the dsp_addv method. Here, for debugging purposes I am only using two inlets. When I access the inlets like in the example above everything works as expected.
Code:
t_int *ak_addthese_tilde_perform(t_int *w) { int i, j; t_ak_addthese_tilde *x = (t_ak_addthese_tilde *)(w[1]); int n = (int)(w[2]); t_sample * in1 = (t_sample *)(w[3]); t_sample * in2 = (t_sample *)(w[4]); t_sample * out = (t_sample *)(w[5]);
while(n--) { *out++ = *in1++ + *in2++; }
return (w + 6); }
When I access all input signals with an array of pointers, I cannot hear the rightmost signal in the output. So in this code above I only hear the signal from the first inlet. Like so,
Code:
t_int *ak_addthese_tilde_perform(t_int *w) { int i, j; t_ak_addthese_tilde *x = (t_ak_addthese_tilde *)(w[1]); t_int n = (t_int)(w[2]);
t_sample ** ins = getbytes(sizeof(t_sample *) * 2); t_sample * out = (t_sample *)(w[5]);
for(i = 0; i < 2; i++) { ins[i] = (t_sample *)(w[3 + i]); }
while(n--) { *out = 0.0; for(j = 0; j < 2; j++) {
*out += *ins[j]++;
}
out++;
}
return (w + 6); }
I would be grateful if you could point out where I am mistaken. Best
-- ahmet kizilay http://www.ahmetkizilay.com
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On 2011-05-12 10:34, Ahmet Kizilay wrote:
while(n--) { *out = 0.0;
signals can share memory, e.g. the pointer to the/an input vector might be the same as the pointer to the/an output vector. therefore the above _might_ overwrite the data in one of the input signals. you should do something like: while(n--) { t_sample tmp=0; for(j = 0; j < 2; j++) tmp += *ins[j]++; *out++=tmp; }
fgmasdr IOhannes
Hi,
Unrelated to your issue, perhaps, but I thought this work mentioning:
On 12/05/11 09:34, Ahmet Kizilay wrote:
t_int *ak_addthese_tilde_perform(t_int *w) {
[snip]
t_sample ** ins = getbytes(sizeof(t_sample *) * 2);
[snip]
There's a memory leak here (no corresponding call to freebytes() or whatever it is called)! And technically it's not "realtime-safe" to allocate memory in perform method (but this doesn't matter so much with the current Pd implementation, afaik).
I suggest allocating this memory in the constructor (new method) (and freeing it in the destructor), because you (probably) already know the number of inlets in the constructor (otherwise allocate in the dsp add method).
IOHannes, With your suggestion the object is finally working properly. Thanks a lot.
Claude, Thanks for pointing out. I changed my code accordingly.
Best, ahmet kizilay
On Thu, May 12, 2011 at 12:09 PM, Claude Heiland-Allen claude@goto10.org wrote:
Hi,
Unrelated to your issue, perhaps, but I thought this work mentioning:
On 12/05/11 09:34, Ahmet Kizilay wrote:
t_int *ak_addthese_tilde_perform(t_int *w) {
[snip]
t_sample ** ins = getbytes(sizeof(t_sample *) * 2);
[snip]
There's a memory leak here (no corresponding call to freebytes() or whatever it is called)! And technically it's not "realtime-safe" to allocate memory in perform method (but this doesn't matter so much with the current Pd implementation, afaik).
I suggest allocating this memory in the constructor (new method) (and freeing it in the destructor), because you (probably) already know the number of inlets in the constructor (otherwise allocate in the dsp add method).
Claude
http://claudiusmaximus.goto10.org
Pd-list@iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list