Qt5 QAudioOutput+QIODevice 播放固定频率的声音

首先界面如下:

1、在.pro文件中加入 QT += multimediawidgets


本例采用QAudioOutput + QIODevice 实现音频播放 ,构造一个派生自 QIODevice 的类,派生 QIODevice 实现两个纯虚函数

    qint64 readData(char *data, qint64 maxlen);
    qint64 writeData(const char *data, qint64 len);


soundsource.h

#ifndef SOUNDSOURCE_H
#define SOUNDSOURCE_H

#include <QIODevice>
#include <QObject>

class SoundSource : public QIODevice
{
public:
    SoundSource();
    SoundSource(int _fq, int _vol);

    void setFreq(int fq);
    void setVolume(int vol);

protected:
    qint64 readData(char *data, qint64 maxlen);
    qint64 writeData(const char *data, qint64 len);
private:
    double frequency=1000;
    double volume=100;

    qint64 it=0;

    QList<char> m_data;
    void appendData();
};

#endif // SOUNDSOURCE_H


soundsource.cpp

#include "soundsource.h"

#define     SAMPLE_RATE         44100
#define     SAMPLE_COUNT        (SAMPLE_RATE * 0.5)   /* 0.5 seconds */
#define     AMPLITUDE           (1.0 * 0x7F00)
#define     ONE_HZ           (1.0 / SAMPLE_RATE)

SoundSource::SoundSource()
{

}

SoundSource::SoundSource(int _fq,int _vol)
{
    frequency=_fq;
    volume=_vol;
}

void SoundSource::setFreq(int _fq){
    frequency=_fq;
}

void SoundSource::setVolume(int _vol){
    volume=_vol;
}

qint64 SoundSource::readData(char *data, qint64 maxlen){
    if(m_data.count()==0)appendData();
    qint64 count=qMin(maxlen,(qint64)m_data.count());
    for(qint64 i=0;i<count;i++)data[i]=m_data[i];
    m_data.erase(m_data.begin(),m_data.begin()+count);
    return count;
}

qint64 SoundSource::writeData(const char *data, qint64 len){
    for(qint64 i=0;i<len;i++)m_data.push_back(data[i]);
    return len;
}

void SoundSource::appendData(){
    for(qint64 i=it;i<it+SAMPLE_COUNT;i++){
        short temp=(volume/100)*AMPLITUDE * sin (frequency* ONE_HZ * 2 * i * M_PI) ;
        m_data.append((char)temp);
        m_data.append((char)(temp>>8));
        m_data.append((char)temp);
        m_data.append((char)(temp>>8));
    }
    it+=SAMPLE_COUNT;
}


mainwidget.h

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <QWidget>
#include <QAudioFormat>
#include <QAudioOutput>
#include <QSlider>
#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include "soundsource.h"

class MainWidget : public QWidget
{
    Q_OBJECT
    void initialize();
    void setupUI();

    QSlider *fqSlider,*volSlider;
    QLabel *lb1,*lb2;
    QPushButton *playBtn,*stopBtn;

    QAudioOutput *audio;
    SoundSource *source;

    QHBoxLayout *lay1, *lay2, *lay3;
    QVBoxLayout *mainLay;

    QAudioFormat defaultFormat()
    {
        QAudioFormat audioFormat;
        audioFormat.setCodec("audio/pcm");
        audioFormat.setByteOrder(QAudioFormat::LittleEndian);
        audioFormat.setSampleRate(44100);
        audioFormat.setChannelCount(2);
        audioFormat.setSampleSize(16);
        audioFormat.setSampleType(QAudioFormat::SignedInt);
        return audioFormat;
    }
public:
    explicit MainWidget(QWidget *parent = nullptr);

signals:

public slots:
    void slotPlay();
    void slotStop();
    void slotFqChanged(int t);
    void slotVolChanged(int t);
};

#endif // MAINWIDGET_H


mainwidget.cpp

#include "mainwidget.h"

MainWidget::MainWidget(QWidget *parent) : QWidget(parent)
{
    initialize();
    setupUI();

    audio = new QAudioOutput(QAudioDeviceInfo::defaultOutputDevice(),defaultFormat());
    source = new SoundSource(fqSlider->value(),volSlider->value());

    source->open(QIODevice::ReadWrite);

    connect(playBtn,SIGNAL(clicked()),this,SLOT(slotPlay()));
    connect(stopBtn,SIGNAL(clicked()),this,SLOT(slotStop()));

    connect(fqSlider,SIGNAL(valueChanged(int)),this,SLOT(slotFqChanged(int)));
    connect(volSlider,SIGNAL(valueChanged(int)),this,SLOT(slotVolChanged(int)));
}

void MainWidget::slotFqChanged(int t){
    lb1->setText(QString("频率:%1").arg(t));
    source->setFreq(t);
}

void MainWidget::slotVolChanged(int t){
    lb2->setText(QString("音量:%1").arg(t));
    source->setVolume(t);
}

void MainWidget::slotPlay(){
    audio->start(source);
}

void MainWidget::slotStop(){
    audio->stop();
}

void MainWidget::initialize(){
    this->setFixedSize(400,240);
    fqSlider=new QSlider(Qt::Horizontal);
    fqSlider->setRange(0,15000);
    fqSlider->setValue(1000);

    volSlider=new QSlider(Qt::Horizontal);
    volSlider->setRange(0,100);
    volSlider->setValue(100);

    lb1=new QLabel("频率:1000");
    lb2=new QLabel("音量:100");

    lb1->setFixedWidth(120);
    lb2->setFixedWidth(120);

    playBtn=new QPushButton;
    stopBtn=new QPushButton;
    playBtn->setText("play");
    stopBtn->setText("stop");


    lay1=new QHBoxLayout;
    lay1->addWidget(lb1);
    lay1->addWidget(fqSlider);
    lay2=new QHBoxLayout;
    lay2->addWidget(lb2);
    lay2->addWidget(volSlider);


    lay3=new QHBoxLayout;
    lay3->addStretch();
    lay3->addWidget(playBtn);
    lay3->addStretch();
    lay3->addWidget(stopBtn);
    lay3->addStretch();


    mainLay=new QVBoxLayout;
    mainLay->addLayout(lay1);
    mainLay->addLayout(lay2);
    mainLay->addLayout(lay3);

    this->setLayout(mainLay);
}

void MainWidget::setupUI(){

}




main.cpp

#include "mainwidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWidget w;
    w.show();

    return a.exec();
}


资源地址:QAudioTest-CSDN下载  http://download.csdn.net/download/how0723/9958220


  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
Qt5中控制STM32F407步进电机的方法如下: 1. 首先,你需要确保你已经安装了Qt和STM32的开发环境,并且正确地连接了STM32F407开发板和电机。 2. 在Qt中,可以使用串口通信来控制STM32。你可以使用Qt的QSerialPort类来实现串口通信。首先,你需要设置串口的参数,包括波特率、数据位、校验位等。然后,你可以通过打开串口并发送特定的命令来控制步进电机的运动。 3. 在代码中,你可以使用QSerialPort类来打开串口,例如: ```cpp QSerialPort serialPort; serialPort.setPortName("COM1"); // 设置串口号 serialPort.setBaudRate(QSerialPort::Baud9600); // 设置波特率 serialPort.setDataBits(QSerialPort::Data8); // 设置数据位 serialPort.setParity(QSerialPort::NoParity); // 设置校验位 serialPort.setStopBits(QSerialPort::OneStop); // 设置停止位 serialPort.open(QIODevice::ReadWrite); // 打开串口 ``` 4. 接下来,你可以使用串口的write()函数来发送命令给STM32。根据你的需求,你可以发送不同的命令来控制步进电机的运动。例如,你可以发送命令来启动电机、改变运动方向、调整电机的速度等。 5. 在Qt中,你可以使用控件的setText()函数来显示文字。通过调用控件的setText()函数,你可以将需要显示的文字作为参数传递进去。在你的情况下,你可以将步进电机的相关信息作为文本显示在适当的控件上。 综上所述,你可以使用Qt的QSerialPort类来实现串口通信,并通过发送特定的命令来控制STM32F407步进电机的运动。在需要显示文字的控件上使用setText()函数来显示相关信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值