Qt 实时获取麦克风数据绘制波形

效果图:

 使用


#include <QChart>
#include "QmyDisplayDevice.h"
void mySpeechSample::initChart()
{

	//创建显示图表
	QChart *chart = new QChart;

	chart->setTitle(QString::fromLocal8Bit("音频输入原始信号"));
	chart->setTitleBrush(QBrush(Qt::blue));
	ui.chartView->setChart(chart);
	chart->setBackgroundVisible(false);
	lineSeries = new QLineSeries(); //序列
	chart->addSeries(lineSeries);

	QValueAxis *axisX = new QValueAxis;  //坐标轴
	axisX->setRange(0, displayPointsCount); //chart显示4000个采样点数据
	//axisX->setLabelFormat("%g");
	//axisX->setTitleText("Samples");

	QValueAxis *axisY = new QValueAxis;  //坐标轴
	axisY->setRange(0, 256); // UnSingedInt采样,数据范围0-255
						 //    axisY->setRange(-1, 1);
	axisY->setLabelsColor(Qt::blue);
	axisX->setLabelsColor(Qt::blue);
	//axisY->setTitleText("Audio level");

	chart->setAxisX(axisX, lineSeries);
	chart->setAxisY(axisY, lineSeries);
	chart->legend()->hide();
	chart->layout()->setContentsMargins(0, 0, 0, 0);//设置外边界全部为0
	chart->setMargins(QMargins(0, 0, 0, 0));//设置内边界全部为0
	chart->setBackgroundRoundness(0);//设置背景区域无圆角
	deviceList = QAudioDeviceInfo::availableDevices(QAudio::AudioInput);//输入设备列表
	for (int i = 0; i < deviceList.count(); i++)
	{
		QAudioDeviceInfo device = deviceList.at(i);
	}

	if (deviceList.size() > 0)
	{
		curDevice = deviceList.at(0);//
	}
	else
	{
	}
	on_actStart();
}

void mySpeechSample::on_actStart()
{//开始音频输入
	QAudioFormat defaultAudioFormat; //缺省格式
	defaultAudioFormat.setSampleRate(8000);
	defaultAudioFormat.setChannelCount(1);
	defaultAudioFormat.setSampleSize(8);
	defaultAudioFormat.setCodec("audio/pcm");
	defaultAudioFormat.setByteOrder(QAudioFormat::LittleEndian);
	defaultAudioFormat.setSampleType(QAudioFormat::UnSignedInt);

	//    curDevice = QAudioDeviceInfo::defaultInputDevice(); // 选择缺省设备
	if (!curDevice.isFormatSupported(defaultAudioFormat))
	{
		QMessageBox::critical(this, "音频输入设置测试", "测试失败,输入设备不支持此设置");
		return;
	}

	audioInput = new QAudioInput(curDevice, defaultAudioFormat, this);
	audioInput->setBufferSize(displayPointsCount);

	
	// 接收音频输入数据的流设备
	displayDevice = new QmyDisplayDevice(lineSeries, displayPointsCount, this);

	connect(displayDevice, SIGNAL(updateBlockSize(qint64)),
		this, SLOT(on_IODevice_UpdateBlockSize(qint64)));

	connect(displayDevice, SIGNAL(updateProbar(qint64)),
		this, SLOT(slotupdateProcessBar(qint64)));

	displayDevice->open(QIODevice::WriteOnly); //必须以写方式打开

	audioInput->start(displayDevice); //以流设备作为参数,开始录入音频输入数据

									 
}
void mySpeechSample::slotupdateProcessBar(qint64 va)
{
	float value = va *1.0 * 100 / 128;
	qDebug() << value;
	ui->horizontalSlider->setValue(abs(value));
}

 QmyDisplayDevice.h

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef QMYDISPLAYDEVICE_H
#define QMYDISPLAYDEVICE_H

//#include <QtCore/QIODevice>
//#include <QtCharts/QChartGlobal>

#include  <QtCharts>
#include  <QIODevice>

//QT_CHARTS_BEGIN_NAMESPACE
//class QXYSeries;
//QT_CHARTS_END_NAMESPACE

//QT_CHARTS_USE_NAMESPACE

class QmyDisplayDevice : public QIODevice
{
    Q_OBJECT
public:
    explicit QmyDisplayDevice(QXYSeries * series, qint64 pointsCount,QObject *parent = 0);

protected:
    qint64 readData(char * data, qint64 maxSize);
    qint64 writeData(const char * data, qint64 maxSize);

private:
    QXYSeries *m_series;
    qint64  range=4000;

signals:
    void  updateBlockSize(qint64 blockSize);
	void updateProbar(qint64);
};

#endif // QMYDISPLAYDEVICE_H

  QmyDisplayDevice.cpp

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Charts module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qmydisplaydevice.h"
#include <QtCharts/QXYSeries>

QmyDisplayDevice::QmyDisplayDevice(QXYSeries * series, qint64 pointsCount, QObject *parent) :
    QIODevice(parent)
//    m_series(series)
{
   m_series= series;
   range=pointsCount;
}

qint64 QmyDisplayDevice::readData(char * data, qint64 maxSize)
{// 流的读 操作不处理
    Q_UNUSED(data)
    Q_UNUSED(maxSize)
    return -1;
}

qint64 QmyDisplayDevice::writeData(const char * data, qint64 maxSize)
{ //读取数据块内的数据,更新到序列
    QVector<QPointF> oldPoints = m_series->pointsVector();
    QVector<QPointF> points;
//    int resolution = 4;//只是每4个取1个数
//    int resolution = 1;//应改为sampleSize=1 byte

    if (oldPoints.count() < range)
    { //m_series序列的数据未满4000点,
       points = m_series->pointsVector();
    }
    else
    {//将原来maxSize至4000的数据点前移,
       for (int i = maxSize; i < oldPoints.count(); i++)
          points.append(QPointF(i - maxSize, oldPoints.at(i).y()));
    }

    qint64 size = points.count();
	for (int k = 0; k < maxSize; k++) //数据块内的数据填充序列的尾部
	{
		points.append(QPointF(k + size, (quint8)data[k]));
		//emit updateProbar((quint8)data[k]);
	}
/*绘制QSlider
       int pingjunzhi = 0;
	for (int k = 0; k < maxSize; k++) //数据块内的数据填充序列的尾部
	{
		points.append(QPointF(k + size, (quint8)data[k]));
		pingjunzhi += abs((quint8)data[k] -128);
		if (k % 50 == 0)
		{
			emit updateProbar(1.0*pingjunzhi/10);
			pingjunzhi = 0;
		}
	}
*/
    m_series->replace(points); //最快的方式

    emit updateBlockSize(maxSize);
    return maxSize;
}

 

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值