PDA

View Full Version : Left audio channel intermittently drops out (QAudioOutput, Linux)



QtDrivesMeCrazy
1st March 2016, 23:09
I have an annoying intermittent problem in my Qt Application where occasionally the left audio channel will stop playing, but the right channel will continue playing. If I stop and restart the audio, both channels will once again play correctly. I'm running on Ubuntu 14.04 with Qt5.5.

I'm writing left and right audio samples in PCM format to a buffer at the same time, so I know Qt is getting the data for both channels. It just chooses to not play the left channel sometimes for some reason.

I'm not sure if this is a problem with my code, Qt, or Ubuntu or how to rule anything out.

Here is my code:


// Set audio format
QAudioFormat format;
format.setSampleRate(44100);
format.setChannelCount(2);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(format)) {
emit error_string(QString("Raw audio (audio/pcm) format not supported on this computer. Cannot play audio."));
return;
}

// Create audio buffer
QByteArray *audio_buffer = new QByteArray;
buffer = new QBuffer;
buffer->setBuffer(audio_buffer);
buffer->open(QIODevice::ReadWrite);

// Audio player
audio_output = new QAudioOutput(format, parent());
audio_output->setNotifyInterval(Constants::audio_notify_interval );

// Temporary buffer for left and right audio values
qint16 *sample_buffer = new qint16[max_buffer_size * channel_count];

// ...some processing...

// Write samples to audio buffer
audio_buffer->append((const char *)sample_buffer, (qint64)(values_written * 2)); // 2 bytes per sample

// Start playing
audio_output->start(buffer);

// ...some more processing...

// Write samples in for loop until finished
audio_buffer->append((const char *)sample_buffer, (qint64)(values_written * 2));