c++qtaudioqaudioinput

On Fedora using Qt 5.9.4, I'm unable to simultaneously record and play audio at the same time


I'm trying to write a program in Qt that simultaneously records audio from a microphone and plays it back at the same time. I'm using Qt 5.9.4 and I'm on Fedora 29 (can't update to latest version as our production environment is Fedora 29 -- can't update it, have already asked boss).

I have some barebones code written, as you can see below. But everytime I run the program, I get the following error message:

using null output device, none available
using null input device, none available

I've installed every qt5* package. I have alsa-utils and pulse audio installed as well.

I have also looked at these which more or less helped me but did not solve my problem:

I don't know if this is a fedora related issue or a Qt related issue. Please help!

myaudiorecorder.h:

 #ifndef MYAUDIORECORDER_H
 #define MYAUDIORECORDER_H

#include <QAudioFormat>
#include <QAudioDeviceInfo>
#include <QTextStream>
#include <QAudioInput>
#include <QAudioOutput>
#include <QObject>

class MyAudioRecorder : public QObject
{
    Q_OBJECT

public:
    MyAudioRecorder();

    QAudioFormat formatIn;
    QAudioFormat formatOut;

    QAudioInput *m_audioInput;
    QAudioOutput *m_audioOutput;

    QAudioDeviceInfo m_InputDevice;
    QAudioDeviceInfo m_OutputDevice;

    QIODevice *m_input;
    QIODevice *m_output;

    QAudioDeviceInfo deviceIn;
    QAudioDeviceInfo deviceOut;

    void getFormat();
    void createAudioInput();
    void createAudioOutput();
    void beginAudio();

};

#endif // MYAUDIORECORDER_H

myaudiorecorder.cpp:

#include "myaudiorecorder.h"

MyAudioRecorder::MyAudioRecorder() {
    getFormat();
    createAudioInput();
    createAudioOutput();
}


void MyAudioRecorder::getFormat(){
    formatIn.setSampleSize(8);
    formatIn.setCodec("audio/pcm");
    formatIn.setByteOrder(QAudioFormat::LittleEndian);
    formatIn.setSampleType(QAudioFormat::UnSignedInt);

    deviceIn = QAudioDeviceInfo::availableDevices(QAudio::AudioInput).at(1);
    if(!deviceIn.isFormatSupported(formatIn)){
    QTextStream(stdout) << " default formatIn not supported " << endl;
    formatIn = deviceIn.nearestFormat(formatIn);
    } else {
    QTextStream(stdout) << " default formatIn supported " << endl;
    }

    deviceOut = QAudioDeviceInfo::availableDevices(QAudio::AudioOutput).at(0);
    if(!deviceOut.isFormatSupported(formatOut)) {
    QTextStream(stdout) << "1. default formatOut not supported " << endl;
    formatOut = deviceOut.nearestFormat(formatOut);
    }

}


void MyAudioRecorder::createAudioInput(){
    m_audioInput = new QAudioInput(m_InputDevice, formatIn, 0);
}

void MyAudioRecorder::createAudioOutput(){
    m_audioOutput = new QAudioOutput(m_OutputDevice, formatOut, 0);
}

void MyAudioRecorder::beginAudio(){
    m_output = m_audioOutput->start();
    m_input = m_audioInput->start();
}

Solution

  • void MyAudioRecorder::beginAudio(){
        m_output = m_audioOutput->start();
        m_audioInput->start(m_output);
    
    
       //Above should do the trick but do check the volume, state and error if any:
       qDebug() << "m_audioInput: volume=" <<  m_audioInput->volume()
                << ", state=" << m_audioInput->state()
                << ", error=" << m_audioInput->error();
       qDebug() << "m_audioOutput: volume=" <<  m_audioOutput->volume()
                << ", state=" << m_audioOutput->state()
                << ", error=" << m_audioOutput->error();
    
    }