Qt - audio help

Hi all,

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 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 installed as well.

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);

    // QAudioDeviceInfo deviceIn = QAudioDeviceInfo::defaultInputDevice();
    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;
    }

    // QAudioDeviceInfo deviceOut = QAudioDeviceInfo::defaultOutputDevice();
    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);
}

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

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

Hi!
Not a dev myself, but consider to use a debugger. It should show you upon what statement it throws this error.

I’m not a FAN to redirect people to other track but in this case I think this is not the proper site to ask this kind of question, your one is more oriented to a Development Question in C++ and QT5

Sites like stackexchange you will get more audience to your Question, or Wait to a Community member with this skill answer your Question…

Regards., and sorry…

There are a lot of problems with this code. Here’s a short list:

  1. all class members are public
  2. raw pointers are used as public members
  3. there is no explicit destructor, and the default leaks memory
  4. the code uses at(1) with no assurance that that device even exists
  5. the formatIn variable is incompletely initialized and will never match a real device
  6. the formatOut variable is not initialized at all before use, guaranteeing failure

I would suggest using code more like this: https://doc.qt.io/qt-5/audiooverview.html#monitoring-audio-data-during-playback-or-recording