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

首先界面如下:

Qt5 QAudioOutput+QIODevice 播放固定频率的声音_第1张图片

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 
#include 

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 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>8));
        m_data.append((char)temp);
        m_data.append((char)(temp>>8));
    }
    it+=SAMPLE_COUNT;
}


mainwidget.h

#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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 

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


你可能感兴趣的:(Qt)