3

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.

Alex
  • 361
  • 1
  • 4
  • 15

2 Answers2

3

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();
code_fodder
  • 15,263
  • 17
  • 90
  • 167
  • This worked, although I had to use `buffer.byteCount();`. And sorry, I should have mentioned that the byte array is initialized. The pointer was just out of testing. – Alex Jun 01 '16 at 21:16
  • @Alex oh yeah, sorry... `.size()` is QByteArray, my mistake :p – code_fodder Jun 01 '16 at 21:18
2

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();
}
Marek R
  • 32,568
  • 6
  • 55
  • 140
  • `appends bytes until zero is encountered` - Are you sure about that? I have not got my dev PC nearby, but I am always using QByteArray for binary data, I do not recall having any issue with appending binary data (i.e. non-ASCII strings)... – code_fodder Jun 01 '16 at 19:57
  • YES. You are using this overload: `QByteArray::append(const char * str)` and this is standard behavior inherited from ANSI C-strings. – Marek R Jun 01 '16 at 20:01
  • well... I just made a qt project test. I made an array of char[10] and set values to `'0', '1', '2', (char) 0x00, '0', '1', '2', (char) 0x00` and then used the append `ba.append(&arr[0], 8)` and it worked fine... QByteArray is not just for ANSI C-Strings... its for "bytes" – code_fodder Jun 01 '16 at 20:11
  • 1
    Another read of the Qt docs suggests that this overload does the count as the users specifies. If count is -1 it uses qstrlen() to determine the size - which I assume `QByteArray::append(const char * str)` does as well.... so looks like you can use this as it is – code_fodder Jun 01 '16 at 20:15