PDA

View Full Version : Save QAudioRecorder to QByteArray



vanillac
27th March 2015, 04:48
I am trying to record from a microphone using QAudioRecorder. I do not want to save the file locally and then read it into a QByteArray. How can I save from the audio recorder to a QByteArray? I tried using QAudioProbe with the audioBufferProbed(QAudioBuffer) signal, but I don't know if it is possible to save the QAudioBuffer as a QByteArray.

anda_skoa
27th March 2015, 07:44
QAudioBuffer has accessors for data and byte count.
Looks sufficient to either directly create a QByteArray for the buffer's data or for writing into a QBuffer.

Cheers,
_

vanillac
27th March 2015, 17:32
Thanks for the reply. Is the QAudioBuffer.data() the actual microphone data or only formatting data (channels, bit rate, etc)? If not, how do I convert the const void* from data() to a QByteArray?

anda_skoa
27th March 2015, 18:53
Well, the QAudioBuffer has a QAudioFormat so the data must be the actual audio data.

Without trying the API would suggest that this should be possible:


QByteArray byteData(buffer.constData<char>(), buffer.byteCount());


Cheers,
_

vanillac
27th March 2015, 19:49
I tried using the code you posted to save the QByteArray to a file. I get an error, "Windows Media Player cannot play this file."
edit: I did some testing and it saves the microphone data, but it didn't use the codec from the QAudioRecorder.

anda_skoa
28th March 2015, 08:59
If you want to write to a file, then converting into a byte array is just an unnecessary step, you can directly write the "const char*" data into the file.

Cheers,
_