1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00

update QQt audio manager

This commit is contained in:
tianduanrui 2018-02-10 16:12:47 +08:00
parent bcee91d195
commit 533326c80f
6 changed files with 126 additions and 20 deletions

View File

@ -2,11 +2,11 @@
QQtAudioManager::QQtAudioManager ( QObject* parent ) : QObject ( parent )
{
mInputAudioFormat = QAudioDeviceInfo::defaultInputDevice().preferredFormat();
mOutputAudioFormat = QAudioDeviceInfo::defaultOutputDevice().preferredFormat();
mInputAudioFormat = defaultInputDevice().preferredFormat();
mOutputAudioFormat = defaultOutputDevice().preferredFormat();
mInputDeviceInfo = QAudioDeviceInfo::defaultInputDevice();
mOutputDeviceInfo = QAudioDeviceInfo::defaultOutputDevice();
mInputDeviceInfo = defaultInputDevice();
mOutputDeviceInfo = defaultOutputDevice();
mInputDevice = NULL;
mOutputDevice = NULL;
@ -47,8 +47,10 @@ void QQtAudioManager::startInput()
void QQtAudioManager::stopInput()
{
//输入设备还开着那么输入流设备一定开着。这是QQtAudioManager的功能设定。
if ( mInputDevice )
{
//关闭QAudioInput等于关闭了拾音器。
mInputManager->stop();
mInputManager->deleteLater();
mInputManager = NULL;
@ -96,3 +98,34 @@ void QQtAudioManager::writeBytes ( QByteArray& bytes )
QAudioOutput* QQtAudioManager::outputManager() { return mOutputManager; }
QIODevice* QQtAudioManager::outputDevice() { return mOutputDevice; }
/*用户只需要处理音频输入、输出设备和每个的格式QAudioInput、QAudioOutput、QIODevice(in + out)都被QQtAudioManager处理了。*/
void QQtAudioManager::startDefaultInput()
{
/*使用默认输入设备*/
mInputDeviceInfo = QQtAudioManager::defaultInputDevice();
// pline() << "in prefer" << mInputDeviceInfo.preferredFormat().channelCount() <<
// mInputDeviceInfo.preferredFormat().sampleRate() <<
// mInputDeviceInfo.preferredFormat().sampleSize();
// pline() << "in" << mInputAudioFormat.channelCount() << mInputAudioFormat.sampleRate() <<
// mInputAudioFormat.sampleSize();
startInput();
}
void QQtAudioManager::startDefaultOutput()
{
/*使用默认输出设备*/
mOutputDeviceInfo = QQtAudioManager::defaultOutputDevice();
// pline() << "out prefer" << mOutputDeviceInfo.preferredFormat().channelCount() <<
// mOutputDeviceInfo.preferredFormat().sampleRate() <<
// mOutputDeviceInfo.preferredFormat().sampleSize();
// pline() << "out" << mOutputAudioFormat.channelCount() << mOutputAudioFormat.sampleRate() <<
// mOutputAudioFormat.sampleSize();
startOutput();
}

View File

@ -31,6 +31,8 @@
*
* manager把读写声音设备当做读写一个设备处理hdmi接口上的声卡
*
* *******使QQtAudioManager*******
*
*
*
* 1 0
@ -58,6 +60,10 @@ public:
static QAudioDeviceInfo defaultOutputDevice();
/*一般建议设置一个AudioFormat然后这个format和设置的相等。default为preffered格式*/
//这里保证输入、输出使用格式相等 或者 不同
//如果格式不同在mac电脑上本地输入输出设备是可以使用的但是对于连接的语音蓝牙话筒却是不可以使用的原因未知。
//格式相同的时候,实在是太好用啦。
//这个建议默认就相同但是在QQtAudioManager当中并没有直接将其相等处理如果用户在readyRead槽函数里可以更改采样率进行某些特殊处理。一般不需要差异处理的相等就行了。
QAudioFormat& inputAudioFormat ( void );
QAudioFormat& outputAudioFormat ( void );
@ -79,6 +85,14 @@ public:
QAudioOutput* outputManager();
QIODevice* outputDevice();
//这是个方便
//如果使用这个函数建议设置公共的AudioFormat比如输出的format或者输入、输出都支持的Format。
//这个Format不会跟随默认设备的改变而改变有初始值但是用户在使用过程中有必要关注和更改。
//prefer和nearest并不是default所以还是需要用户设置。
QAudioFormat& defaultAudioFormat();
void startDefaultInput();
void startDefaultOutput();
signals:
/*输入音频数据准备就绪readAll即可读取。*/
void readyRead();
@ -96,10 +110,10 @@ private:
/*操作输入、输出设备的工具*/
QAudioInput* mInputManager;
QAudioOutput* mOutputManager;
/*读写输入、输出设备的流控制器QIODevice*/
QIODevice* mInputDevice;
QIODevice* mOutputDevice;
};
#endif // QQTAUDIOMANAGER_H

View File

@ -12,10 +12,8 @@
#if you succeed with LibQQt, please thumb up.
#2017年11月10日18:53:56
#-------------------------------------------------
SOURCES = \
$$PWD/multimedia/qqtaudiomanager.cpp
HEADERS = \
$$PWD/multimedia/qqtaudiomanager.h
SOURCES =
HEADERS =
#root dir
HEADERS += $$PWD/qqt.h \
@ -146,13 +144,21 @@ contains(QKIT_PRIVATE, iOS||iOSSimulator) {
#multimedia
#arm mips
#TODO: +wince +android +ios +macOS +win +linux
#audio success.
#video arm mips
#TODO: video +wince +android +ios +macOS +win +linux
contains (DEFINES, __MULTIMEDIA__) {
#mplayer
contains (DEFINES, __PROCESSMODULE__){
SOURCES += $$PWD/multimedia/qqtmplayer.cpp
HEADERS += $$PWD/multimedia/qqtmplayer.h
}
#audio
SOURCES += \
$$PWD/multimedia/qqtaudiomanager.cpp
HEADERS += \
$$PWD/multimedia/qqtaudiomanager.h
}

View File

@ -228,9 +228,13 @@ void MainWindow::currentOutputRowChanged ( QModelIndex cur, QModelIndex )
void MainWindow::on_pushButton_clicked()
{
/*这里是自定义输入、输出设备*/
QString name = ui->inputListWidget->currentIndex().data().toString();
QString name = QQtAudioManager::defaultInputDevice().deviceName();
if ( ui->inputListWidget->currentIndex().isValid() )
name = ui->inputListWidget->currentIndex().data().toString();
QAudioDeviceInfo dev = findInputAudioDeviceInfoByName ( name );
name = QQtAudioManager::defaultOutputDevice().deviceName();
if ( ui->outputListWidget->currentIndex().isValid() )
name = ui->outputListWidget->currentIndex().data().toString();
QAudioDeviceInfo devOut = findOutputAudioDeviceInfoByName ( name );
@ -249,18 +253,43 @@ void MainWindow::on_pushButton_clicked()
//如果格式不同在mac电脑上本地输入输出设备是可以使用的但是对于连接的语音蓝牙话筒却是不可以使用的原因未知。
//格式相同的时候,实在是太好用啦。
//这个建议默认就相同但是在QQtAudioManager当中并没有直接将其相等处理如果用户在readyRead槽函数里可以更改采样率进行某些特殊处理。一般不需要差异处理的相等就行了。
manager.inputAudioFormat() = manager.outputDeviceInfo().preferredFormat();
manager.outputAudioFormat() = manager.outputDeviceInfo().preferredFormat();
// int inBit = ui->inBit->currentIndex().data().toInt();
// int inChn = ui->inChn->currentIndex().data().toInt();
// int inRate = ui->intRate->currentIndex().data().toInt();
// QAudioFormat inFmt;
// inFmt.setChannelCount ( inChn );
// inFmt.setSampleSize ( inBit );
// inFmt.setSampleRate ( inRate );
// inFmt.setCodec ( "audio/pcm" );
// manager.inputAudioFormat() = inFmt;
QAudioFormat outFmt = manager.outputDeviceInfo().preferredFormat();
int outBit = outFmt.sampleSize(), outChn = outFmt.channelCount(), outRate = outFmt.sampleRate();
if ( ui->outBit->currentIndex().isValid() )
outBit = ui->outBit->currentIndex().data().toInt();
if ( ui->outChn->currentIndex().isValid() )
outChn = ui->outChn->currentIndex().data().toInt();
if ( ui->outRate->currentIndex().isValid() )
outRate = ui->outRate->currentIndex().data().toInt();
outFmt.setChannelCount ( outChn );
outFmt.setSampleSize ( outBit );
outFmt.setSampleRate ( outRate );
outFmt.setCodec ( "audio/pcm" );
manager.inputAudioFormat() = outFmt;
manager.outputAudioFormat() = outFmt;
pline() << "in prefer" << dev.preferredFormat().channelCount() << dev.preferredFormat().sampleRate() <<
dev.preferredFormat().sampleSize();
pline() << "out prefer" << devOut.preferredFormat().channelCount() << devOut.preferredFormat().sampleRate() <<
devOut.preferredFormat().sampleSize();
pline() << "in" << manager.inputAudioFormat().channelCount() << manager.inputAudioFormat().sampleRate() <<
manager.inputAudioFormat().sampleSize();
pline() << "out prefer" << devOut.preferredFormat().channelCount() << devOut.preferredFormat().sampleRate() <<
devOut.preferredFormat().sampleSize();
pline() << "out" << manager.outputAudioFormat().channelCount() << manager.outputAudioFormat().sampleRate() <<
manager.outputAudioFormat().sampleSize();
@ -316,3 +345,12 @@ void MainWindow::on_outHS_valueChanged ( int value )
pline() << "输出音量" << vol ;
manager.outputManager()->setVolume ( vol );
}
void MainWindow::on_pushButton_4_clicked()
{
manager.inputAudioFormat() = QQtAudioManager::defaultOutputDevice().preferredFormat();
manager.outputAudioFormat() = QQtAudioManager::defaultOutputDevice().preferredFormat();
manager.startDefaultInput();
manager.startDefaultOutput();
}

View File

@ -32,6 +32,8 @@ private slots:
void on_outHS_valueChanged ( int value );
void on_pushButton_4_clicked();
private:
Ui::MainWindow* ui;
QQtAudioManager manager;

View File

@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>917</width>
<height>587</height>
<width>1198</width>
<height>591</height>
</rect>
</property>
<property name="windowTitle">
@ -91,6 +91,19 @@
<string>stop (optional)</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_4">
<property name="geometry">
<rect>
<x>820</x>
<y>40</y>
<width>291</width>
<height>32</height>
</rect>
</property>
<property name="text">
<string>start take voice and play (default device)</string>
</property>
</widget>
<widget class="QWidget" name="">
<property name="geometry">
<rect>