PDA

View Full Version : Appending data to the buffer of a QAudioOutput



PLM
7th November 2011, 10:47
Hi,

I'm writing an application which plays audio using QAudioOutput.
The audio is decoded by libavcodec in a separate thread and passed to the main thread in pieces.
The main thread starts playing the audio that was already decoded and keeps playing while new audio data is appended to the buffer.

To do so I am using a QBuffer opened in ReadWrite-mode.



Buffer = new QBuffer;
Buffer->open(QIODevice::ReadWrite);

Output = new QAudioOutput(*Format);

Buffer->seek(0);
Buffer->write(Array->data(), Array->length());
Buffer->seek(0);

Output->start(Buffer);


When I append data to the buffer after having called start() I need to reset the position where data is read and written to it's initial value:



qint64 ActPos = Buffer->pos();
Buffer->seek(Buffer->size());
Buffer->write(Array->data(), Array->length());
Buffer->seek(ActPos);


This seems to work as far as I can't hear any disruptions in the played audio, but I doubt that this is the optimum method.

How does QAudioOutput manage this buffer?
Does it grab the bytes one by one or are they copied to the audio memory in chunks?
Does it matter if the buffer isn't available for a short moment e.g. while I am writing to it?

Thanks in advance for your answers!

PLM
7th November 2011, 15:12
I also noticed some strange behaviour of QAudioOuput's processedUSecs().
This variable seems to be updated in jumps of 40ms. Changing the notify intervall had no effect on it.

Why should one use a 64-bit integer to store multiples of 40ms?
Might that be a bug?

I also noticed some strange behaviour of QAudioOuput's processedUSecs().
This variable seems to be updated in jumps of 40ms. Changing the notify intervall had no effect on it.

Why should one use a 64-bit integer to store multiples of 40ms?
Might that be a bug?

moytrage
1st March 2013, 01:32
In order to have no disruptions due to writing into the buffer you may use external QByteArray:


QByteArray data();
QBuffer buffer(&data);
// use buffer ...
data.append("abc", 3); // this extra data is now available in the buffer, buffer's pos doesn't change
// use buffer further more...

rgbDonnie
9th August 2013, 09:13
Hello, I also writing an application which plays audio using QAudioOutput. Can you tell me how to achieve it ? Thanks!:o

ChrisW67
9th August 2013, 10:12
You could start by looking at the obvious documentation for QAudioOutput. Then in the Multimedia Examples you could read the Audio Output example. Once you have done that you might be able to ask targeted questions in a thread you have not hijacked.

rgbDonnie
9th August 2013, 11:05
You could start by looking at the obvious documentation for QAudioOutput. Then in the Multimedia Examples you could read the Audio Output example. Once you have done that you might be able to ask targeted questions in a thread you have not hijacked.

Thank you! :D
There is my main code

QAudioOutput *m_audioOutput;
RepletQIODevice *m_audioListenFile;
m_audioOutput->start(m_audioListenFile);

RepletQIODevice is Inherited from QIODevice, and I Reappeared "readData" function.


qint64 RepletQIODevice::readData(char *data, qint64 maxlen)
{
memset(data, 0, maxlen);

if (listenArray.size() < maxlen) {
maxlen = listenArray.size();
}

if (maxlen > 0) {
memcpy(data, listenArray.left(maxlen).data(), maxlen);
listenArray.remove(0, maxlen);
}

return maxlen;
}


If listenArray had no data to read by readData, my application will will not respond when I Execute "m_audioOutput->stop", I can only force quit.
I don't know why!
Can you help me?
By the way , My english level is very low, please forgive me. Haha......:o

rgbDonnie
19th August 2013, 04:01
Haha......This problem has been solved. The problem is not here. :p