QAudioInput "Unknown Error"



  • I'm trying to read audio data from a microphone using the QT API, and have been having some trouble with setting up a QIODevice that will let me read the data into a buffer.

    Below is my existing code:

    int main() {
    QAudioInput *audio;
    
    // Specifying format
    QAudioFormat format;
    format.setSampleRate(16000);
    format.setChannelCount(1);
    format.setSampleSize(8);
    format.setCodec("audio/pcm");
    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setSampleType(QAudioFormat::UnSignedInt);
    
    QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
    qWarning() << "Device being used:" << info.deviceName();
    
    if (!info.isFormatSupported(format)) {
        qWarning() << "Default format not supported, trying to use the nearest.";
        format = info.nearestFormat(format);
    }
    
    // Setting up the input
    audio = new QAudioInput(format);
    qWarning() << "\nAudio initialisation error:" << audio->error();
    qWarning() << "Audio initialisation state:" << audio->state() << "\n";
    
    
    QIODevice *inputStream = audio->start();
    qWarning() << "Audio stream start error:" << audio->error();
    qWarning() << "Audio stream start state:" << audio->state() << "\n";
    
    if (inputStream->waitForReadyRead(1000) == false) {
        qWarning() << inputStream->errorString();
        exit(1);
    }
    
    char input_data[512];
    while (true) {
        inputStream->read(input_data,512);
    }
    
    return 0;
    }
    

    My output when I run is:

    Device being used: "Built-in Microphone"
    
    Audio initialisation error: NoError
    Audio initialisation state: StoppedState 
    
    QObject::startTimer: Timers can only be used with threads started with QThread
    Audio stream start error: NoError
    Audio stream start state: IdleState 
    
    "Unknown error"
    

    I'm sure part of the problem is in the QObject::startTimer error, which clearly has to do with something that is happening inside of the QAudioInput object, but I have not been able to trace where this is coming from within the QAudioInput object by looking through the documentation.

    In summary, what I'd like to know:

    • What is happening inside of QAudioInput when I call start(), and why would it cause the warning shown above?
    • Where could I find the implementation of the start() function so that I can see what's happening under the hood?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    First thing, your not creating a QCoreApplication as the first step in your application, therefore Qt can't initialise it's internal state. Then you use a blocking loop so Qt wouldn't be able to work since you'd be strangulating the event loop. Finally, you don't start the event loop, so Qt can't do it's work.

    The QAudioInput documentation shows some basics to get you started.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.