为语音识别录制pcm音频文件

在PC上使用科大讯飞语音识别或听写的sdk时,需要提前录制好pcm文件,并且文件格式要求为16K、16bit、单声道、无压缩,在我们测试的时候,我们可以用音频软件去转化成这一格式,但集成编程时,我们必须要编程来进行录音。在这里,我简单说一下在linux环境下,如何用qt进行录音:

算了,废话不多说,直接上代码,用了可以记得好评啊!:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include 
#include 
#include 
#include
#include 
#include 
namespace Ui {
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();private slots:void on_pushButton_clicked();void on_pushButton_2_clicked();private:Ui::MainWindow *ui;QAudioInput *audioInput;QFile inputFile;
};#endif // MAINWINDOW_H

继续!

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"QDebug"
#include 
#include 
#include 
#include #include
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::on_pushButton_clicked()
{inputFile.setFileName("/home/ghy1234567oice/test.pcm");inputFile.open(QIODevice::WriteOnly | QIODevice::Truncate);QAudioFormat format;format.setSampleRate(16000);//format.setChannels(1);format.setChannelCount(1);format.setSampleSize(16);format.setCodec("audio/pcm");format.setByteOrder(QAudioFormat::LittleEndian);format.setSampleType(QAudioFormat::SignedInt);QAudioDeviceInfo info(QAudioDeviceInfo::defaultInputDevice());if (!info.isFormatSupported(format)){qWarning() << "default format not supported try to use nearest";format = info.nearestFormat(format);}audioInput = new QAudioInput(format, this);//QTimer::singleShot(10000, this, SLOT(stopRecording()));audioInput->start(&inputFile);qDebug() << "record begin!";
//       for(int i=0;i<100000;i++)
//           for(int j=0;j<10000;j++);//    sleep(4);//       audioInput->stop();
//       inputFile.close();
//       delete audioInput;
//       qDebug() << "record end!";
}void MainWindow::on_pushButton_2_clicked()//
{audioInput->stop();inputFile.close();delete audioInput;qDebug() << "record end!";
}
在.pro文件中可以加入QT  +=multimedia

点击一个按钮,录音开始,点击另一个按钮,录音结束,可以将录制好的pcm文件利用Col edit pro软件进行分析,查看数据格式是否符合要求

想去去掉按钮的话,可以在开始录音和结束录音之间设置定时器,定时器时间一般以4S为宜。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部