oops, hit send before I was ready. Calling malloc in the callback shouldn't cause a crash on most platforms. The problem you have is a bit more subtle. If I understand what you're doing, there are a few problems:
- userData isn't big enough. You want something like this (not in you callback): userData=malloc(sizeof(float)*NUM_SAMPS_IN_BUFFER); - then in your callback, you want something like this: float *inp = (float*) inputBuffer, *fftbuffer = (float *) userData; for (i=0;i<framesPerBuffer;i++) *fftbuffer++ = *inp++; you could also use a call like memcpy, which is more efficient, but depends on your system. bjorn On Thu, Jun 18, 2015 at 2:31 PM, Bjorn Roche <bj...@shimmeo.com> wrote: > Calling malloc in the callback shouldn't cause a crash on most platforms. > The problem you have is a bit more subtle and has to do with how pointers > are interpreted. If I understand what you're doing, there are a few > problems: > > - in/userData isn't big enough. > > > userData=malloc(sizeof(float)*NUM_SAMPS_IN_BUFFER); > > float *inp = (float*) inputBuffer, *fftbuffer = (float *) userData; > for (i=0;i<framesPerBuffer;i++) *fftbuffer++ = *inp++; > > > On Thu, Jun 18, 2015 at 1:15 PM, Connor Gettel <connorget...@me.com> > wrote: > >> Hello Everyone, >> >> Ross, Bjorn, Danny, Richard and Bogac, Thank you for your insightful >> feedback and advice with my project. I haven’t had time to look over all >> the material just yet, but i surely will over the next couple days. I’ve >> hit a bit wall with one specific part of the code, this mainly comes down >> to syntax I think and lack of experience with C. If anyone could tell me >> what i’m doing wrong in this instance, please let me know if i’m on the >> right track. >> >> inside the memory allocations in the main function I’ve got this line: >> >> in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*N); >> >> This is my input array, i need to fill it with data from portaudio’s >> inputBuffer (I think). >> >> As far as I know the way to do this is to use the *userData Parameter of >> the callbaack which is type void*. >> >> So now I want to make ‘in’ the userData Parameter… i need to cast inside >> the callback from double to void*, and then to float to match the >> inputBuffer… I’ve done this with: >> >> int i; >> double *in; >> in = malloc(sizeof(double)); >> userData=(void*)in; >> userData=in; >> float *inp = (float*) inputBuffer, *fftbuffer = (float *) userData; >> for (i=0;i<framesPerBuffer;i++) *fftbuffer++ = *inp++; >> >> So my thought process now is that inputBuffer should be feeding the >> fftbuffer (which is the input array of the fft) with data… which means I >> can now execute my plan… >> >> Compiling fine! Unfortunately i’m crashing. I’m pretty sure it’s because >> i’m calling malloc in the callback. Which isn’t meant to be done >> (obviously). >> >> Question are: >> 1) Am I on the right track at all? >> 2) Is there a malloc free way to cast from double to void* ? >> 3) Am I right in thinking the ‘in’ is the FFT Buffer? >> >> Cheers, >> >> Connor. >> -- >> dupswapdrop -- the music-dsp mailing list and website: >> subscription info, FAQ, source code archive, list archive, book reviews, >> dsp links >> http://music.columbia.edu/cmc/music-dsp >> http://music.columbia.edu/mailman/listinfo/music-dsp > > > > > -- > Bjorn Roche > @shimmeoapp > -- Bjorn Roche @shimmeoapp -- dupswapdrop -- the music-dsp mailing list and website: subscription info, FAQ, source code archive, list archive, book reviews, dsp links http://music.columbia.edu/cmc/music-dsp http://music.columbia.edu/mailman/listinfo/music-dsp