michaeltyson / TPCircularBuffer

A simple, fast circular buffer implementation
http://atastypixel.com/blog/a-simple-fast-circular-buffer-implementation-for-audio-processing/
839 stars 141 forks source link

How to change buffer size? #4

Closed blnked closed 9 years ago

blnked commented 9 years ago

Any way to change buffer size (existing buffer)

thanks

blnked commented 9 years ago

found one way but I'm not sure that optimal way

bufferSize = 'new buffer size';

int32_t availableToReadBytes;
uint8_t *tempBuffer = TPCircularBufferTail(&buffer, &availableToReadBytes);
printf("+ availableToReadBytes %d \n", availableToReadBytes);

SInt16 *newBuffer = (SInt16*)malloc(sizeof(SInt16)*availableToReadBytes);
memcpy(newBuffer, tempBuffer, availableToReadBytes);

TPCircularBufferCleanup(&buffer);

TPCircularBufferInit(&buffer, bufferSize);

TPCircularBufferProduceBytes(&buffer, newBuffer, availableToReadBytes);
printf("buffer size was changed on %d \n", bufferSize);

// check how many bytes is available
int32_t bytesToRead;
TPCircularBufferTail(&buffer, &bytesToRead);
printf("+ bytesToRead %d \n", availableToReadBytes);
michaeltyson commented 9 years ago

One warning - if you're doing this on the fly, manipulating the buffer like that kinda breaks it's thread-safeness. Unless this isn't on the fly and you're not using it at the time you're resizing - then that's okay.

On 15 Feb 2015, at 8:37 am, Eduard Beleninik notifications@github.com wrote:

found one way but I'm not sure that optimal way

bufferSize = 'new buffer size';

int32_t availableToReadBytes; uint8_t *tempBuffer = TPCircularBufferTail(&buffer, &availableToReadBytes); printf("+ availableToReadBytes %d \n", availableToReadBytes);

SInt16 newBuffer = (SInt16)malloc(sizeof(SInt16)*availableToReadBytes); memcpy(newBuffer, tempBuffer, availableToReadBytes);

TPCircularBufferCleanup(&buffer);

TPCircularBufferInit(&buffer, bufferSize);

TPCircularBufferProduceBytes(&buffer, newBuffer, availableToReadBytes); printf("change buffer %d on size: %d \n", buffer_size_default, bufferSize);

// check how many bytes is available int32_t bytesToRead; TPCircularBufferTail(&buffer, &bytesToRead); printf("+ bytesToRead %d \n", availableToReadBytes); — Reply to this email directly or view it on GitHub.

blnked commented 9 years ago

quick note for who are trying to do something similar

please review your app architecture again. probably you won't to think in future that buffer size changing is a good idea. I hope that's help you keep time.