Qt文档阅读笔记-QAudioInput&QAudioFormat解析与实例

x33g5p2x  于2022-07-22 转载在 其他  
字(3.2k)|赞(0)|评价(0)|浏览(276)

官方解析

QAudioInput

这个类提供了从外设接收音频数据的接口。

使用逻辑是这样的,首先创建QAudioInput并且指定好QAudioDeviceInfo,以及QAudioFormat。然后调用start函数,指定QIODevice完成自己想要的功能。

比如下面这个官方代码:

QFile destinationFile;   // Class member
 QAudioInput* audio; // Class member
 {
     destinationFile.setFileName("/tmp/test.raw");
     destinationFile.open( QIODevice::WriteOnly | QIODevice::Truncate );

     QAudioFormat format;
     // Set up the desired format, for example:
     format.setSampleRate(8000);
     format.setChannelCount(1);
     format.setSampleSize(8);
     format.setCodec("audio/pcm");
     format.setByteOrder(QAudioFormat::LittleEndian);
     format.setSampleType(QAudioFormat::UnSignedInt);

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

     audio = new QAudioInput(format, this);
     connect(audio, SIGNAL(stateChanged(QAudio::State)), this, SLOT(handleStateChanged(QAudio::State)));

     QTimer::singleShot(3000, this, SLOT(stopRecording()));
     audio->start(&destinationFile);
     // Records audio for 3000ms
 }

调用start后会以指定的形式进行输出到file中。需要停止的话调用如下代码:

void AudioInputExample::stopRecording()
 {
     audio->stop();
     destinationFile.close();
     delete audio;
 }

在任意时刻,QAduioInput有以下四种状态:

active:活动

suspended:暂停

stopped:结束

idle:空闲

这些状态可以在QAudio::State中获得,可以调用suspend()、resume()、stop()、reset()、start()这些函数去设置状态。调用state()可以返回当前QAudioInput的状态。,当状态改变时会触发stateChanged()信号。

当出现error时,可以调用error()函数,可以通过关联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;
     }
 }

QAudioFormat

这个东西也比较重要,录好的音频用CoolEditor打开时,配置和这个是一样的,所以在此说明下。

QAudioFormat类存储audio的各个参数,关键参数如下:

| 参数 | 描述 |
| Sample Rate | 每秒音频的Hertz |
| Number of channels | 单声道还是双声道 |
| Sample size | 数据按8位存还是16位存 |
| Sample type | 存储的类型是int还是unsigned int还是float |
| Byte order | 存成大端还是小端 |

博主例子

程序结构如下:

main.cpp

#include <QCoreApplication>
#include <QFile>
#include <QAudioInput>
#include <QAudioFormat>
#include <QEventLoop>
#include <QDebug>
#include <QTimer>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile destinationFile("audio.pcm");
    if(!destinationFile.open(QIODevice::WriteOnly | QIODevice::Truncate)){

        qDebug() << "open failed";
        return 0;
    }

    QAudioFormat format;
    // Set up the desired format, for example:
    format.setSampleRate(8000);
    format.setChannelCount(1);
    format.setSampleSize(8);
    format.setCodec("audio/pcm");
    format.setByteOrder(QAudioFormat::LittleEndian);
    format.setSampleType(QAudioFormat::UnSignedInt);

    QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
    if (!info.isFormatSupported(format)) {

        qWarning() << "Default format not supported, trying to use the nearest.";
        format = info.nearestFormat(format);
    }

    QAudioInput audio(format);
    audio.start(&destinationFile);

    QEventLoop loop;
    QTimer::singleShot(1000 * 5, &loop, &QEventLoop::quit);
    loop.exec();

    audio.stop();
    destinationFile.close();
    qDebug() << "over";
    return a.exec();
}

保存好的文件用CoolEditor打开:

注意这些配置和QAudioFormat要一致。

就完成了!

源码打包下载地址:

Qt/AudioDemo at master · fengfanchen/Qt · GitHub

相关文章

微信公众号

最新文章

更多