Forums > Dev

buffer~ questions

July 24, 2007 | 12:07 am

hi folks,

so I spent the last 4-5 hours reading every single thread in the
archives related to buffer~.

I have been keeping into consideration threads priority and buffer~’s
lightweight locking mechanism (b_valid and b_inuse flags).

I am writing a normalization routine to be used inside a more complex
object and here is what I came up with:

void myobj_normalize(t_myobj *x)
{
t_buffer *b = x->buf;

if (b && b->b_valid) {

float *table = b->b_samples;
long chan = x->chan;
long frames = b->b_frames;
long nc = b->b_nchans;
int i;

// don’t need to worry about threads priority
// when reading from a buffer~

float maxval = 0.;
for (i = 0; i < frames; ++i) {
float val = table[i * nc + chan];
float absval = fabsf(val);
if (absval > maxval) {
maxval = absval;
}
}

if (maxval > 0.) {

// now I need to write to buffer
// so threads priority become an issue

// defer to low priority

float scale = 1. / maxval;
defer_low(x, (method)myobj_doscale,0L,0,0L);
}
}
}

void myobj_doscale(t_myobj *x, double scale)
{
t_buffer *b = x->buf;
float *table = b->b_samples;
long chan = x->chan;
frames = b->b_frames;
long nc = b->b_nchans;
int i;

while (buf->b_inuse) ; // spin until buffer is no longer
// in use by the audio thread

buf->b_valid = 0; // set b_valid to false to prevent
// buffer~ access while editing

for (i = 0; i < frames; ++i) {
table[i * nc + chan] *= scale;
}

b->b_valid = 1; // restore buffer~ access

b->b_modtime = gettime(); // set new modification time
// so that any object referencing
// this buffer~ can update
}

So far the code seems to be working correctly but I am not sure it IS
correct.

1. Is it strictly necessary to defer methods that alter the buffer~
content in any way to low priority ?

2. Am I supposed to be using deferlow as above or is defer more
appropriate in this situation ?

3. Am I correctly understanding and using buffer~’s locking mechanism ?

Any comments highly appreciated.

Thank you.

- Luigi

————————————————————
THIS E-MAIL MESSAGE IS FOR THE SOLE USE OF THE INTENDED RECIPIENT AND MAY CONTAIN CONFIDENTIAL AND/OR PRIVILEGED INFORMATION. ANY UNAUTHORIZED REVIEW, USE, DISCLOSURE OR DISTRIBUTION IS PROHIBITED. IF YOU ARE NOT THE INTENDED RECIPIENT, CONTACT THE SENDER BY E-MAIL AT SUPERBIGIO@YAHOO.COM AND DESTROY ALL COPIES OF THE ORIGINAL MESSAGE. WITHOUT PREJUDICE UCC1-207.
————————————————————

Moody friends. Drama queens. Your life? Nope! – their life, your story. Play Sims Stories at Yahoo! Games.

http://sims.yahoo.com/


March 25, 2009 | 10:46 am

Dear all

I am reviving this thread because my hopes in the new SDK have been disappointed. I wish there was the code of poke~ so I could see Cycling74 approved buffer writing etiquette.

Can someone give me the real practice of the inuse, valid and dirty flags?

thanks

pa


March 26, 2009 | 4:20 pm

hi pa,

did you see this, posted the other day by tim place?

http://blog.74objects.com/2009/03/22/accessing-buffers/

oli


Viewing 3 posts - 1 through 3 (of 3 total)