QAudioOutput always encounters UnderrunError

I am using Qt 4.8 with Qt Creator 2.4.1 on Windows 7 Ultimate x64.

I take audio input using the QAudioInput class and play it using QAudioOutput . There is a 2 second timeout after which I stop accepting input, and then I configure the output as follows:

 class MainWindow { // ... QByteArray output_data; QBuffer output_data_buffer; QAudioOutput *audio_out; // ... }; MainWindow::MainWindow(QWidget *parent) { // ... output_data_buffer.setBuffer(&output_data); // ... } void MainWindow::audioInputStopped(QByteArray data) { output_data = data; output_data_buffer.open(QIODevice::ReadOnly); audio_out = new QAudioOutput(audio_format, this); connect(audio_out, SIGNAL(stateChanged(QAudio::State)), SLOT(audioOutputStateChanged(QAudio::State))); audio_out->start(&output_data_buffer); } 

The audio format used is supported by both input devices and outputs. I checked them using QAudioDeviceInfo::isFormatSupported() . A sound of 2 seconds ( data in audioInputStopped() ) always plays normally.

In the audioOutputStateChanged slot audioOutputStateChanged I always encounter the QAudio::UnderrunError from audio_out->error() after the buffer has finished playing. After calling audio_out->start() state (passed as a parameter to audioOutputStateChanged() ) and the error occurs as follows:

  • Error. Active state.
  • Error. The state is stopped.
  • Overflow error. Idle state.

Note that I am stopping audio_out in the standby state according to this example . Why does the code encounter an underload error? This is normal?

+4
source share
1 answer

This may seem strange, but I saw where the built-in arrays in Qt work better when built on the heap, or at least when their elements are built on the heap (so they are just an array of pointers). Memory management is a little more complicated, but the elements pressed into them do not go beyond. The Qt Object Model also helps to place most things on the heap and properly educate them. This can help.

After reading a little bit about buffer overflows, it looks like something else is trying to read from the sound source, and something else is writing to it or vice versa. Check out some of the links below. You can try to disconnect the audio_in part from the buffer before reading the buffer. Most likely, this will fix the error.

I would also build your QAudioOutput pointer in the constructor for your main window (more like a style). Following some of how it is organized in the Qt examples, it seems like the best organization. Here is the cpp for the QAudioInput example .

If you had a more complete example, I could try more with it to recreate the error and debug it.

Here is someone else to sympathize with:

http://qt-project.org/forums/viewthread/16729

And the wiki article:

http://en.wikipedia.org/wiki/Buffer_underrun

And a list of multimedia examples in Qt:

http://doc.qt.nokia.com/4.7-snapshot/examples-multimedia.html

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1399645/


All Articles