audiounit - Reading chunks of audio with ringbuffer -
i analyze chunks of audio data of 1 second. purpose implemented audio unit fills ringbuffer (tpcircularbuffer michael tyson). in file try read chunks of 1 second using nstimer. unfortunately, receive errors consuming these data.
the buffer filled in kaudiooutputunitproperty_setinputcallback , works fine
device * = (__bridge device *)inrefcon; // render audio buffer audiobufferlist bufferlist; bufferlist.mnumberbuffers = 1; bufferlist.mbuffers[0].mnumberchannels = 2; bufferlist.mbuffers[0].mdata = null; bufferlist.mbuffers[0].mdatabytesize = innumberframes * sizeof(sint16) * 2; checkerror(audiounitrender(this -> riounit, ioactionflags, intimestamp, 1, innumberframes, &bufferlist), "audiounitrender"); // put audio circular buffer tpcircularbufferproducebytes(&circbuffer, bufferlist.mbuffers[0].mdata, innumberframes * 2 * sizeof(sint16));
to read 1 second of samples implemented te following code:
- (void)initializetimer { timer = [nstimer scheduledtimerwithtimeinterval:1 target:self selector:@selector(timerfired:) userinfo:nil repeats:yes]; } - (void) timerfired:(nstimer *)thetimer { nslog(@"reading %i second(s) ring",1); int32_t availablebytes; sint16 *tail = tpcircularbuffertail(&circbuffer, &availablebytes); int availablesamples = availablebytes / sizeof(sint16); nslog(@"available samples %i", availablesamples); (int = 0; < availablesamples; i++) { printf("%i\n",tail[i]); } tpcircularbufferconsume(&circbuffer, sizeof(sint16) * availablebytes); }
however, when run code number of samples printed receive following error: assertion failed: (buffer->fillcount >= 0), function tpcircularbufferconsume, file …/tpcircularbuffer.h, line 142.
unfortunately, don’t know going wrong consuming data. buffer length set samplerate * 2 long enough.
i happy if knows going wrong here.
your circular buffer isn't long enough. don't check available size positive before emptying, , time print statements take let buffer over-filled. make buffer @ least 2x larger timer, check before emptying, empty buffer before printing, , use far fewer print statements.
Comments
Post a Comment