3
votes

My goal is to save a QAudioRecorder recording into memory. From my research it seems the best way to store the recording is to use a QByteArray. My audio recorder is probed using a QAudioProbe.

From the audioBufferProbed signal I try to append the data to the byte array using this slot method.

QByteArray *byteArr;

void AudioRecorder::processBuffer(const QAudioBuffer &buffer)
{
    byteArr->append(buffer.constData<char>());
    qDebug() << buffer.byteCount();
    qDebug() << byteArr->size();
}

However that doesn't seem to work considering buffer.byteCount(); returns 4092 constantly which seems normal but byteArr->size(); returns weird and irregular increments starting off usually with 2, 4, 6, 7, 189.

The data also usually only ends up being around 18kb in size which also leads me to believe that the data is not being appended into the byte array correctly.

According to the QByteArray::size() docs size() should give how many bytes are in the array. Along with QAudioBuffer::byteCount() which also should give the amount of bytes in the current buffer, shouldn't the full 4092 from the buffer be copied to the array?

I am also open to another solution that doesn't use QByteArray if there is a better way to store the data.

2

2 Answers

3
votes

You have a QByteArray pointer, but have not set it to anything.

You need to set it to a QByteArray, in your case you can use QByteArray(const char * data, int size):

byteArr = new QByteArray(buffer.constData<char>(), buffer.byteCount());

But to be honest, I am not sure why you are using a pointer.

You could just do:

QByteArray byteArr; // Your global declaration 
   :
   :
byteArr.append(buffer.constData<char>(), buffer.byteCount());

You might want to try printing like this if you are using binary data...:

qDebug() << byteArr.toHex();
2
votes

You are using method QByteArray::append which does something else than you expect. This overload of append you are using appends bytes until zero is encountered. This API should be used for c-strings!

So fix code like this:

QByteArray byteArr; // there is no point of create this on heap
byteArr.reserve(8*1024*1024); // reserve 8 MB - it will improve performance

void AudioRecorder::processBuffer(const QAudioBuffer &buffer)
{
    byteArr.append(buffer.constData<char>(), buffer.size());
    qDebug() << buffer.byteCount();
    qDebug() << byteArr.size();
}