当前位置:网站首页>QT document reading notes audio example analysis
QT document reading notes audio example analysis
2022-07-23 14:45:00 【IT1995】
This example is very interesting , Today I analyzed , Take notes here , Convenient for next reference .
This example shows how to draw dynamic data ( Data input by microphone )
The interface is as follows :

The key documents are as follows :

The official explanation here is gone , I'll add later .
First, we need to learn two classes , Namely :QAudioInput and QAudioFormat
You can check my blog for details -Qt Document reading notes -QAudioInput&QAudioFormat Analysis and examples .
First, in the main.cpp Key code in :
const QAudioDeviceInfo inputDevice = QAudioDeviceInfo::defaultInputDevice();
if (inputDevice.isNull()) {
QMessageBox::warning(nullptr, "audio",
"There is no audio input device available.");
return -1;
}Here you can check whether the microphone is set in the current system , without , Just quit .
stay Widget.cpp Configuration of the QAudioFormat Related properties , as follows :
QAudioFormat formatAudio;
formatAudio.setSampleRate(8000);
formatAudio.setChannelCount(1);
formatAudio.setSampleSize(8);
formatAudio.setCodec("audio/pcm");
formatAudio.setByteOrder(QAudioFormat::LittleEndian);
formatAudio.setSampleType(QAudioFormat::UnSignedInt);
| Parameters | describe |
| Sample Rate | Audio per second Hertz |
| Number of channels | Mono or Bi channel |
| Sample size | Data press 8 Bitstore or 16 Bitstore |
| Sample type | The type of storage is int still unsigned int still float |
| Byte order | Save as big end or small end |
Finally, let's talk about the most interesting class XYSeriesIODevice. Corresponding
xyseriesiodevice.h:
#ifndef XYSERIESIODEVICE_H
#define XYSERIESIODEVICE_H
#include <QtCore/QIODevice>
#include <QtCore/QPointF>
#include <QtCore/QVector>
#include <QtCharts/QChartGlobal>
QT_CHARTS_BEGIN_NAMESPACE
class QXYSeries;
QT_CHARTS_END_NAMESPACE
QT_CHARTS_USE_NAMESPACE
class XYSeriesIODevice : public QIODevice
{
Q_OBJECT
public:
explicit XYSeriesIODevice(QXYSeries *series, QObject *parent = nullptr);
static const int sampleCount = 2000;
protected:
qint64 readData(char *data, qint64 maxSize) override;
qint64 writeData(const char *data, qint64 maxSize) override;
private:
QXYSeries *m_series;
QVector<QPointF> m_buffer;
};
#endif // XYSERIESIODEVICE_Hxyseriesiodevice.cpp:
#include "xyseriesiodevice.h"
#include <QtCharts/QXYSeries>
#include <QDebug>
XYSeriesIODevice::XYSeriesIODevice(QXYSeries *series, QObject *parent) :
QIODevice(parent),
m_series(series)
{
}
qint64 XYSeriesIODevice::readData(char *data, qint64 maxSize)
{
Q_UNUSED(data)
Q_UNUSED(maxSize)
return -1;
}
qint64 XYSeriesIODevice::writeData(const char *data, qint64 maxSize)
{
static const int resolution = 4;
if (m_buffer.isEmpty()) {
m_buffer.reserve(sampleCount);
for (int i = 0; i < sampleCount; ++i)
m_buffer.append(QPointF(i, 0));
}
int start = 0;
const int availableSamples = int(maxSize) / resolution;
if (availableSamples < sampleCount) {
start = sampleCount - availableSamples;
for (int s = 0; s < start; ++s)
m_buffer[s].setY(m_buffer.at(s + availableSamples).y());
}
for (int s = start; s < sampleCount; ++s, data += resolution)
m_buffer[s].setY(qreal(uchar(*data) - 128) / qreal(128));
m_series->replace(m_buffer);
return (sampleCount - start) * resolution;
}The key code here is this :
qint64 XYSeriesIODevice::writeData(const char *data, qint64 maxSize)
{
static const int resolution = 4;
if (m_buffer.isEmpty()) {
m_buffer.reserve(sampleCount);
for (int i = 0; i < sampleCount; ++i)
m_buffer.append(QPointF(i, 0));
}
int start = 0;
const int availableSamples = int(maxSize) / resolution;
if (availableSamples < sampleCount) {
start = sampleCount - availableSamples;
for (int s = 0; s < start; ++s)
m_buffer[s].setY(m_buffer.at(s + availableSamples).y());
}
for (int s = start; s < sampleCount; ++s, data += resolution)
m_buffer[s].setY(qreal(uchar(*data) - 128) / qreal(128));
m_series->replace(m_buffer);
return (sampleCount - start) * resolution;
}The whole curve x The shaft length is :
static const int sampleCount = 2000;y Shaft length :
axisY->setRange(-1, 1);The audio size is :
formatAudio.setSampleType(QAudioFormat::UnSignedInt);This function data Every time I pass 320 One byte, come here , Switch to UnsignedInt Namely 80 individual UnsignedInt.
The following paragraph is right x The axis data is initialized :
if (m_buffer.isEmpty()) {
m_buffer.reserve(sampleCount);
for (int i = 0; i < sampleCount; ++i)
m_buffer.append(QPointF(i, 0));
}The following is the data network x The axis moves to the left 1920 A coordinate :
if (availableSamples < sampleCount) {
start = sampleCount - availableSamples;
for (int s = 0; s < start; ++s)
m_buffer[s].setY(m_buffer.at(s + availableSamples).y());
}Put the audio just collected to the end 80 In the coordinates (1920~2000)
for (int s = start; s < sampleCount; ++s, data += resolution)
m_buffer[s].setY(qreal(uchar(*data) - 128) / qreal(128));here -128 It's to make y Axis has negative number , Divide 128 Because .
axisY->setRange(-1, 1);Y Axis coordinates are -1, To 1 Between .
边栏推荐
- Quick introduction to PKI system
- Towhee weekly model
- What is per title encoding?
- Generate order number
- 对象使用过程中背后调用了哪些方法
- Question 142 of Li Kou: circular linked list 2
- Is it risky and safe to open a mobile stock account?
- Flat style feedback form page
- Okrk3399 Development Board Reserved i2c4 Mounting EEPROM
- [WinForm] desktop program implementation scheme for screenshot recognition and calculation
猜你喜欢

对象使用过程中背后调用了哪些方法

中望CAD专业版 2022软件安装包下载及安装教程

C language implementation of classroom random roll call system

First acquaintance and search set

Okrk3399 Development Board Reserved i2c4 Mounting EEPROM

JS calendar style pie chart statistics plug-in

【论文笔记】基于分层深度强化学习的移动机器人导航方法

Offline data interoperability
![[test platform development] 23. interface assertion function - save interface assertion and edit echo](/img/36/aed4feb6a4e40b6b5c06206a8ed440.png)
[test platform development] 23. interface assertion function - save interface assertion and edit echo

Towhee weekly model
随机推荐
Pychart reads excel file with error: raise xlrderror (file_format_descriptions[file_format]+; not supported)
因为资源限制,导致namenode启动失败,报错unable to create new native thread
Towhee 每周模型
104 maximum depth of binary tree and 543 diameter of binary tree and 124 maximum path sum of binary tree
優化華為雲服務器采用Key登陸
webstrom ERROR in [eslint] ESLint is not a constructor
Due to resource constraints, the namenode fails to start with an error unable to create new native thread
手工测试如何转向自动化测试?字节5年自动化经验浅谈一下...
Vk36n5d anti power interference / mobile phone interference 5-key 5-channel touch detection chip anti freeze function ponding in the touch area can still be operated
基本51单片机点阵汉字显示程序设计
FFmpeg 2 - ffplay、ffprobe、ffmpeg 命令使用
Can bus quick understanding
Cool code rain dynamic background registration page
炫酷代码雨动态背景注册页面
C语言项目实战:24点游戏计算器(基于结构体、指针、函数、数组、循环等知识点)
(重链剖分)魔法树
spotlight灯箱js插件全屏放大图片
优化华为云服务器采用Key登陆
Is it risky and safe to open a mobile stock account?
转自玉溪信息公开:mRNA新冠疫苗、九洲马破伤风免疫球蛋白等产品有望年内上市。