Solved How to write using QAudioOutput
-
I Have a QByteArray containing sound samples and i want to play them using a QAudioOutput.
I initiialize QAudioOutput like this:QAudioFormat format; format.setSampleRate(44100); format.setChannelCount(2); format.setSampleSize(16); format.setCodec("audio/pcm"); format.setByteOrder(QAudioFormat::LittleEndian); format.setSampleType(QAudioFormat::SignedInt); audioOutput = new QAudioOutput(device, format); output = audioOutput->start(); bytesPlayed = 0; pushTimer = new QTimer(); connect(pushTimer, SIGNAL(timeout()), SLOT(pushTimerExpired()), Qt::DirectConnection); pushTimer->start(20);
and when the timer expires i try to write samples like this:
qint64 written = output->write(decodedAudio.mid(bytesPlayed, audioOutput->bytesFree())); bytesPlayed = bytesPlayed + written;
The audio played sounds just awkward, it is like slowed down and full of noise. I can't get what i'm doing wrong. I tried different timer intervals, or to write bigger or smaller amounts of data but it is always just worse. How do I properly write samples using QAudioOutput?
-
It seems that the code should work, although the
write
part could have some performance-optimization.
So I think the problem is either fromformat
ordecodedAudio
.
How do you get thedecodedAudio
then?
You can verify them by saving thedecodedAudio
to a raw pcm file, then using a software which supports playing raw audio to play it, with setting the playing format asformat
.
For example:
ffplay file_name.pcm -f s16le -ar 44100 -ac 2 -
It seems that the code should work, although the
write
part could have some performance-optimization.
So I think the problem is either fromformat
ordecodedAudio
.
How do you get thedecodedAudio
then?
You can verify them by saving thedecodedAudio
to a raw pcm file, then using a software which supports playing raw audio to play it, with setting the playing format asformat
.
For example:
ffplay file_name.pcm -f s16le -ar 44100 -ac 2 -
Oh yes. I saved it to pcm, opened in audacity and it sounded absolutely the same. I did a mistake while decoding audio. I used QAudioDecoder for it, but I've saved every decoded chunk twice in the ByteArray, so every second chunk was just filled with some random nonsense. I was so sure that i decoded it right so I didn't even check it :)