当前位置:网站首页>【音视频开发系列】QT 采集麦克风PCM并播放
【音视频开发系列】QT 采集麦克风PCM并播放
2022-08-04 05:34:00 【_杜竞宁_】
[注] 调试前请先戴耳机,作为测试使用,没有项目的严谨,需要自调,适合做回声消除类似的测试用例
项目地址:https://github.com/dujingning/PcmCaptureAndPlay
1.创建 QT 项目
2.pro文件添加
QT += multimedia
3添加类 .PcmCaptureAndPlay
①pcmcaptureandplay.h
/* * auth: dujingning * date: 2022.05.03 * licenst: MIT */
#ifndef PCMCAPTUREANDPLAY_H
#define PCMCAPTUREANDPLAY_H
#include <QThread>
#include <QDebug>
#include <QTimer>
#include <QFile>
#include <QAudio>
#include <QAudioFormat>
#include <QAudioInput>
#include <QAudioOutput>
#include <QIODevice>
class PcmCaptureAndPlay: public QThread
{
Q_OBJECT
public:
PcmCaptureAndPlay();
private: /* QT audio play */
QAudioFormat qAudioFormat;
QAudioOutput *out;
QIODevice *audioIO;
QTimer *audioPlayTimer;
QThread *timerTHread;
bool initQtAudioForPlay();
private: /* QT audio capture */
QAudioFormat format;
QAudioInput* audioInput;
QIODevice *qIODevice;
QFile file; // 可以输出到文件,咱不用,直接播
bool initQtAudioForCapture();
private: /* QT audio capture */
void audioCaptureAndPlay();
public slots:
void onReadyRead(); /*采集并填充*/
public:
void run();
};
#endif // PCMCAPTUREANDPLAY_H
② pcmcaptureandplay.cpp
#include "PcmCaptureAndPlay.h"
PcmCaptureAndPlay::PcmCaptureAndPlay()
{
}
bool PcmCaptureAndPlay::initQtAudioForPlay()
{
qAudioFormat.setSampleRate(16000);
qAudioFormat.setChannelCount(1);
qAudioFormat.setSampleSize(16);
qAudioFormat.setCodec("audio/pcm");
qAudioFormat.setByteOrder(QAudioFormat::LittleEndian);
qAudioFormat.setSampleType(QAudioFormat::SignedInt);
out = new QAudioOutput(qAudioFormat);
if (!out) {
return false;
}
audioIO = out->start();
return true;
}
bool PcmCaptureAndPlay::initQtAudioForCapture()
{
qIODevice = nullptr;
format.setSampleRate(16000);
format.setChannelCount(1); // 设定声道数目,mono(平声道)的声道数目是1;stero(立体声)的声道数目是2
format.setSampleSize(16); // 采样位深
format.setCodec("audio/pcm"); // 设置唯一支持的codec
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(format))
{
format = info.nearestFormat(format);
}
file.setFileName("test.raw");
file.open( QIODevice::WriteOnly | QIODevice::Truncate );
audioInput = new QAudioInput(format, nullptr);
qIODevice = audioInput->start(); // 这里可以直接写入到文件,咱不用直接就是播放 audioInput->start(file);
if (qIODevice) {
qDebug() << "device available";
return true;
}
return false;
}
void PcmCaptureAndPlay::onReadyRead()
{
qint64 len = 0, size = 4096;
char buffer[4096]{
0};
len = qIODevice->read(buffer, size); //读取音频
if (len <= 0) {
return;
}
qDebug() << "read pcm size" << len;
qint64 bufferPlay = out->periodSize();
if (out->bytesFree() < bufferPlay) {
//
qDebug() << "play buffer not enough";
return;
}
audioIO->write((const char*)(buffer),len);
}
void PcmCaptureAndPlay::audioCaptureAndPlay()
{
initQtAudioForPlay(); // for play
initQtAudioForCapture(); // for capture
connect(qIODevice, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
}
void PcmCaptureAndPlay::run()
{
audioCaptureAndPlay();
exec();
}
3.使用
PcmCaptureAndPlay *PCAP = new PcmCaptureAndPlay;
PCAP->start();
边栏推荐
猜你喜欢
随机推荐
用PPAPI插件技术在Web上显示会议视频、桌面、PPT等
MySQL存储过程学习笔记(基于8.0)
Visualization and Animation Technology (3D Visualization)
分布式cache项目
【HIT-SC-MEMO2】哈工大2022软件构造 复习笔记2
最全的最详细的指针讲解(C语言)
ZYNQ之FPGA LED 灯闪烁实验
LeetCode刷题
Uos统信系统 chrony配置
MySQL基础
华为鲲鹏arm服务器下使用webrtc和boost踩坑记--编译篇
2020-03-27
Uos统信系统控制台欢迎登陆后消息及所处区域配置
线程池原理
Prematurely reached end of stream
沉浸式体验参加网络安全培训班,学习过程详细到底!
键盘扫描码
调用时序错误导致webrtc无法建立链接
网络通信与Socket编程概述
Uos统信系统 Postfix-smtps & Dovecot-imaps




![虚幻引擎 5 完整指南[2022六月最新课程学习内容]](/img/b3/fe90bca8166108e2e577d5a5e2ea6a.png)




