jeevan_reddy
jeevan_reddy

Reputation: 43

Unable to record audio from microphone to QFile in Qt

I am trying to record audio from the default microphone present in my desktop and store it in QFile. Finally I want to play the recorded file using windows media player. The issue is that QAudioInput has a method bytesReady() which always returns 0 in my case. Even if the audio state is QAudio::ActiveState the bytesReady() shows 0 bytes implying that there are no audio bytes to read from the microphone. The system microphone works just fine. My code gets perfectly compiled, I also get a file in the specifed location with 80KB size, but the file doesn't play with windows media player even if saved as a .wav, it returns an error. The output window shows bytesReady as 0 in my case everytime. I suspect the QAudioInput is not able to read audio data from microphone.

Could you please help me find mistakes in my code?

 `class mikeDemoClass : public QWidget
    {
        Q_OBJECT

    public:
        mikeDemoClass(QWidget *parent = 0, Qt::WFlags flags = 0);
        ~mikeDemoClass();

    public slots:

        void startRecording();
        void browseFiles();
        void stopRecording();
        void handleAudioInputState(QAudio::State);
        void notified();

    private:
        Ui::mikeDemoWidget ui;
        QAudioInput *audioInput;
        QFile *recordFile;
        QTimer *testTimer;
        int audio_state;

    };

// cpp file starts here

    #include <QIODevice>
    #include "mic_demo.h"

    mikeDemoClass::mikeDemoClass(QWidget *parent, Qt::WFlags flags)
        : QWidget(parent, flags)
    {
        ui.setupUi(this);
        audioInput = NULL;
        recordFile = NULL;
        audio_state = -1;

        connect(ui.browseButton,SIGNAL(clicked()),this,SLOT(browseFiles()));
        connect(ui.recordingButton,SIGNAL(clicked()), this,SLOT(startRecording()));
    }

    mikeDemoClass::~mikeDemoClass()
    {
        if(recordFile)
        {
            delete recordFile;
            recordFile = NULL;
        }
    }

    void mikeDemoClass::browseFiles()
    {
        QString FileName = QFileDialog::getSaveFileName(this, tr("Browse Files"), "D:/", tr("Media Files (*.raw)"));
        if(!FileName.isEmpty())
        {
            recordFile = new QFile(FileName);
            QTextDocument *textDoc = new QTextDocument(FileName);
            ui.textEdit->setDocument(textDoc);
        }

    }

    void mikeDemoClass::startRecording()
    {
        bool status = recordFile->open(QIODevice::WriteOnly);
        if(!status)
        {
            qDebug() <<"Error opening the file";
        }


        QString default_deviceName = "";
        QAudioFormat preferred_format;

        QList<QAudioDeviceInfo> device_list = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);
        int count = device_list.count();

        if(device_list.empty())
        {
            qDebug() <<"The Audio Input Devices is empty";
        }
        else
        {

        foreach(QAudioDeviceInfo device_info, device_list)
        {
            QString device_name = device_info.deviceName();
            qDebug() << "device_name:" << device_name.toLatin1();
        }

        QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
        default_deviceName = info.deviceName();

        if(!default_deviceName.isEmpty())
        {
            preferred_format = info.preferredFormat();
            QString codec = preferred_format.codec();
            int sampleRate = preferred_format.sampleRate();
            int sampleSize = preferred_format.sampleSize();
            int channelCount = preferred_format.channelCount();
            int sampleType = preferred_format.sampleType();
            int byteOrder =  preferred_format.byteOrder();
            qDebug() << "codec:" << codec.toLatin1() << "sampleRate :" << sampleRate << "sampleSize:" << sampleSize << "channel Count:" << channelCount << "sample type:" <<sampleType
                << "byteOrder:" << byteOrder;
        }

    }

    QAudioFormat format;
    format.setSampleRate(8000);
    format.setChannels(1);
    format.setSampleSize(8);
    format.setCodec("audio/PCM");
    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setSampleType(QAudioFormat::UnSignedInt);

    QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
    if(!info.isFormatSupported(format))
    {
        qDebug() <<"Default format not supported, try to use nearest format";
        format = info.nearestFormat(format);
    }

    audioInput = new QAudioInput(info, format, this);

     connect(audioInput, SIGNAL(notify()), this, SLOT(notified()));
    connect(audioInput,SIGNAL(stateChanged(QAudio::State)),this, SLOT(handleAudioInputState(QAudio::State)));
    QTimer::singleShot(10000, this, SLOT(stopRecording()));

    //audioInput->setBufferSize(4096);
    qDebug() << "platform buffer size:" << audioInput->bufferSize();
    audioInput->start(recordFile);
}

void mikeDemoClass::stopRecording()
{
    testTimer->stop();
    audioInput->stop();
    recordFile->close();
    delete audioInput;
}

void  mikeDemoClass::handleAudioInputState(QAudio::State state)
{
    qDebug() << "Audio State:" << state;

    audio_state = state;

    if(state == QAudio::StoppedState)
    {
        qDebug() << "Error State:" << audioInput->error();

        if(audioInput->error() != QAudio::NoError)
        {
            qDebug() << "QAudioInput error:" << audioInput->error();
        }
    }
}

void  mikeDemoClass::notified()
{
    if(audio_state ==  QAudio::ActiveState)
    {
        qDebug() << "Error State:" << audioInput->error();

        qDebug() << "platform buffer size after calling QAudioInput start():" << audioInput->bufferSize();

        qDebug() << "bytesReady = " << audioInput->bytesReady()
        << ", " << "elapsedUSecs = " <<audioInput->elapsedUSecs()
        << ", " << "processedUSecs = "<<audioInput->processedUSecs();
    }

}`

The output displays as follows:

   device_name: "Microphone (High Definition Aud" 
device_name: "default" 
codec: "audio/pcm" sampleRate : 11025 sampleSize: 8 channel Count: 1 sample type: 1 byteOrder: 1 
Default format not supported, try to use nearest format 
platform buffer size: 0 
Audio State: 0 
Error State: 0 
platform buffer size after called QAudioInput start(): 1600 
bytesReady =  0 ,  elapsedUSecs =  1003000 ,  processedUSecs =  1000000 
Error State: 0 
platform buffer size after called QAudioInput start(): 1600 
bytesReady =  0 ,  elapsedUSecs =  1998000 ,  processedUSecs =  2000000 
Error State: 0 
platform buffer size after called QAudioInput start(): 1600 
bytesReady =  0 ,  elapsedUSecs =  3003000 ,  processedUSecs =  3000000 
Error State: 0 
platform buffer size after called QAudioInput start(): 1600 
bytesReady =  0 ,  elapsedUSecs =  4000000 ,  processedUSecs =  4000000 
Error State: 0 
platform buffer size after called QAudioInput start(): 1600 
bytesReady =  0 ,  elapsedUSecs =  5006000 ,  processedUSecs =  5000000 
Error State: 0 
platform buffer size after called QAudioInput start(): 1600 
bytesReady =  0 ,  elapsedUSecs =  6001000 ,  processedUSecs =  6000000 
Error State: 0 
platform buffer size after called QAudioInput start(): 1600 
bytesReady =  0 ,  elapsedUSecs =  7005000 ,  processedUSecs =  7000000 
Error State: 0 
platform buffer size after called QAudioInput start(): 1600 
bytesReady =  0 ,  elapsedUSecs =  8002000 ,  processedUSecs =  8000000 
Error State: 0 
platform buffer size after called QAudioInput start(): 1600 
bytesReady =  0 ,  elapsedUSecs =  8997000 ,  processedUSecs =  9000000 
Audio State: 2 
Error State: 0 

Upvotes: 1

Views: 3872

Answers (3)

mike510a
mike510a

Reputation: 2168

The answer to your question can be found in the output.

There is a line that says

Default format not supported, try to use nearest format

Which is the problem. The format you're using isn't supported by your platform. Try a different one or install the codecs/drivers to support that format.

Upvotes: 0

Thomas Ayoub
Thomas Ayoub

Reputation: 29451

Consider PHDEBUG or PHDBG as qDebug() and see the following (I think your missing point is the QIODevice which stand as a buffer.):

Reader.cpp :

#include "PhLtcReader.h"

PhLtcReader::PhLtcReader(QObject *parent) :
    QObject(parent),
    _input(NULL),
    _position(0),
    _buffer(NULL)
{
    _decoder = ltc_decoder_create(1920, 3840);
    PHDEBUG << "LTC Reader created";
}

bool PhLtcReader::init(QString input)
{
    QList<QAudioDeviceInfo> list = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);

    if(list.isEmpty())
    {
        PHDEBUG << "No audio input device";
        return false;
    }

    QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();

    foreach(QAudioDeviceInfo device, list)
    {
        if(device.deviceName() == input)
            info = device;
    }

    PHDEBUG << "LTC input device :" << info.deviceName();

    QAudioFormat format;
    format.setCodec("audio/pcm");
    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setSampleRate(48000);
    format.setChannelCount(1);
    format.setSampleSize(8);
    format.setSampleType(QAudioFormat::SignedInt);

    if(!info.isFormatSupported(format))
    {
        PHDEBUG << "Unsupported audio format";
        return false;
    }

    _position = 0;
    _input = new QAudioInput(info, format);

    connect(_input, SIGNAL(notify()), this, SLOT(onNotify()));
    _buffer = _input->start();
    _input->setNotifyInterval(10);

    _pauseDetector.start();

    return true;
}

void PhLtcReader::close()
{
    if(_input)
    {
        _input->stop();
        delete _buffer;
        _buffer = NULL;
        delete _input;
        _input = NULL;
    }
}

QList<QString> PhLtcReader::inputList()
{
    QList<QString> names;
    QList<QAudioDeviceInfo> list = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);

    foreach(QAudioDeviceInfo device, list)
        names.append(device.deviceName());

    return names;
}

void PhLtcReader::onNotify()
{

    QByteArray array = _buffer->readAll();
    char max = 0;
    for(int i = 0; i < array.count(); i++)
    {
        if(array.at(i) > max)
            max = array.at(i);
    }

    ltc_decoder_write(_decoder, (ltcsnd_sample_t*)array.data(), array.count(), _position);

    LTCFrameExt frame;
    unsigned int *hhmmssff = new unsigned int[4];
    while(ltc_decoder_read(_decoder, &frame))
    {
        PhFrame oldFrame = _clock.frame();
        hhmmssff[0] = frame.ltc.hours_tens * 10 + frame.ltc.hours_units;
        hhmmssff[1] = frame.ltc.mins_tens * 10 + frame.ltc.mins_units;
        hhmmssff[2] = frame.ltc.secs_tens * 10 + frame.ltc.secs_units;
        hhmmssff[3] = frame.ltc.frame_tens * 10 + frame.ltc.frame_units;
    }
    delete hhmmssff;

    _position += array.count();
}

Reader.h

class PhLtcReader : public QObject
{
    Q_OBJECT

public:
    explicit PhLtcReader(QObject *parent = 0);

    bool init(QString _input="");
    void close();

    static QList<QString> inputList();


private slots:
    void onNotify();

private:
    QAudioInput *_input;
    qint64 _position;
    QIODevice * _buffer;
};

#endif // PHLTCREADER_H

Upvotes: 0

TheDarkKnight
TheDarkKnight

Reputation: 27621

Having compared your code to the example in the Qt documentation, you're using the notified signal to handle the QAudio::ActiveState, whereas the example code connects a slot to the stateChanged signal: -

void AudioInputExample::handleStateChanged(QAudio::State newState)
{
    switch (newState) {
        case QAudio::StoppedState:
            if (audio->error() != QAudio::NoError) {
                // Error handling
            } else {
                // Finished recording
            }
            break;

        case QAudio::ActiveState:
            // Started recording - read from IO device
            break;

        default:
            // ... other cases as appropriate
            break;
    }
}

As notify() states: -

This signal is emitted when x ms of audio data has been processed the interval set by setNotifyInterval

Could the interval be zero?

However, I recommend following the example code as shown in the documentation and use the stateChanged signal, rather than notify.

Upvotes: 0

Related Questions