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:

Qt Code:
  1. // Set audio format
  2. QAudioFormat format;
  3. format.setSampleRate(44100);
  4. format.setChannelCount(2);
  5. format.setSampleSize(16);
  6. format.setCodec("audio/pcm");
  7. format.setByteOrder(QAudioFormat::LittleEndian);
  8. format.setSampleType(QAudioFormat::SignedInt);
  9. QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
  10. if (!info.isFormatSupported(format)) {
  11. emit error_string(QString("Raw audio (audio/pcm) format not supported on this computer. Cannot play audio."));
  12. return;
  13. }
  14.  
  15. // Create audio buffer
  16. QByteArray *audio_buffer = new QByteArray;
  17. buffer = new QBuffer;
  18. buffer->setBuffer(audio_buffer);
  19. buffer->open(QIODevice::ReadWrite);
  20.  
  21. // Audio player
  22. audio_output = new QAudioOutput(format, parent());
  23. audio_output->setNotifyInterval(Constants::audio_notify_interval);
  24.  
  25. // Temporary buffer for left and right audio values
  26. qint16 *sample_buffer = new qint16[max_buffer_size * channel_count];
  27.  
  28. // ...some processing...
  29.  
  30. // Write samples to audio buffer
  31. audio_buffer->append((const char *)sample_buffer, (qint64)(values_written * 2)); // 2 bytes per sample
  32.  
  33. // Start playing
  34. audio_output->start(buffer);
  35.  
  36. // ...some more processing...
  37.  
  38. // Write samples in for loop until finished
  39. audio_buffer->append((const char *)sample_buffer, (qint64)(values_written * 2));
To copy to clipboard, switch view to plain text mode