Large incoming data to QByteArray and what to do with it?
-
Hello everyone.
I have an audio recording application that reads audio data from a microphone system. The user presses record and after an unknown duration he is done recording, he will click to save the file.
Each second streams about 8MB of data into a qbuffer - qbytearray.
On average, the user suggests about 15 mins worth of recording. However, it could be shorter/longer.
For 15 mins, size = 15m * 60s * 8 M = 7.03GB. (I only managed to do 3 minutes before my pc crashed)-
Any suggestions on what I can do to prevent having so much data lingering in my qbytearray?
-
Would writing directly to an open file be better?
-
Can I keep a file stream open while I collect data before writing it over?
-
What about temporary files? I have tried creating temporary files with the QTemporaryFile class and it gets saved to my .../temp/ folder. I don't know how how I can translate that temp file into a permanent file?
Thanks.
-
-
- Well, write it to a file
- Yes
- Sure
- Simply move the file to final location. See http://doc.qt.io/qt-5/qdir.html#rename
-
QBuffer is a QIODevice. QFile is one, too. So, you can combine using QByteArray as buffer and keep saving to QFile at the same time, if you want to. I think it would be easier to just write directly to a QFile, though.
My main suggestion is to use QDataStream to stream the data into the file. This way you won't be consuming much RAM - just enough to receive and save the audio as it arrives. This should solve both 1 & 2.
-
- yes.
-
- use QSaveFile instead of QTemporaryFile, if you want to. But using normal file and data stream is the way to go, I think.
Some pseudo code to get you started:
// init QDataStream stream(&file); // when audio comes in void readAudio(const QByteArray &audioPacket) { stream << audioPacket; }
-