How to remove first Bytes of QByteArray safely while adding new data continuously end of it?

949 views Asked by At

I'm receiving audio data from mic with start() function of QAudioInput. This function is adding streaming data into a QBuffer(data goes into a QByteArray). And I want to remove the first Bytes of the buffer frequently if that part of data is not useful for me.

for example; I have a data including letters(saved in the QBuffer and stream stopped) :

a b c d e f g

When I use QByteArray::remove(0,1) function on this array I get the result I expected :

c d e f g

But if I use remove() function while data is streaming into the buffer, I get overwritten audio data at the end of the buffer, e.g. if I use QByteArray::remove(0,1) function just before the 'f' letter added (I have a b c d e in the array and i'll remove first two letters before adding f,g letters)

I expected to see;

[c d e f g] in the buffer but the result is like :

[c (d+f) (e+g)]

Does anyone know how to solve this problem?

1

There are 1 answers

1
szatmary On

Calling remove() is inefficient because all the data has to be moved in memory. It is better to have two buffers (arrays). one you write to and one you read from. When The read buffer is emptied, the buffers are swapped. I did something similar here audiocapdevice.h, audiocapdevice.cpp