无法在Qt中将麦克风的音频录制到QFile

时间:2014-01-29 11:32:58

标签: c++ qt audio

我正在尝试从桌面上的默认麦克风录制音频并将其存储在QFile中。 最后我想用windows media player播放录制的文件。问题是QAudioInput有一个方法bytesReady(),在我的情况下总是返回0。即使音频状态是QAudio :: ActiveState,bytesReady()也会显示0个字节,这意味着没有音频字节可以从麦克风中读取。系统麦克风工作得很好。我的代码得到了完美的编译,我也在80KB大小的指定位置获得了一个文件,但是即使保存为.wav,该文件也不能与Windows媒体播放器一起播放,它会返回错误。 每次输出窗口都将bytesReady显示为0。我怀疑QAudioInput无法从麦克风读取音频数据。

你能帮我找一些代码中的错误吗?

 `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();
    }

}`

输出显示如下:

   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 

3 个答案:

答案 0 :(得分:0)

将您的代码与Qt documentation中的示例进行比较后,您使用通知的信号来处理QAudio :: ActiveState,而示例代码将插槽连接到stateChanged信号: -

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()声明: -

  

当处理x ms的音频数据时,以setNotifyInterval

设置的间隔发出此信号

间隔可以为零吗?

但是,我建议按照documentation中所示的示例代码使用stateChanged信号,而不是通知。

答案 1 :(得分:0)

将PHDEBUG或PHDBG视为qDebug()并查看以下内容(我认为您的缺失点是QIODevice,它们是缓冲区。):

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

答案 2 :(得分:0)

您的问题的答案可以在输出中找到。

有一行写着

const daysOfWeek = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]; let currentDay = prompt("Enter day of week:"); while (!daysOfWeek.includes(currentDay)) { currentDay = prompt("Invalid day. Enter day of week:").toLowerCase(); } let currentDayPassed = false; const daysLeft = []; for (let i = 0; i < daysOfWeek.length; i++) { if (daysOfWeek[i] === currentDay) { currentDayPassed = true; } if (currentDayPassed) { daysLeft.push(daysOfWeek[i]); } } console.log(daysLeft);

这是问题所在。您的平台不支持您使用的格式。尝试使用其他格式或安装编解码器/驱动程序以支持该格式。