2018-11-04 14:38:39 -05:00
|
|
|
/*******************************************************************************
|
|
|
|
* plugins/VOIP/gui/AudioWizard.cpp *
|
|
|
|
* *
|
|
|
|
* Copyright (C) 2005-2010 Thorvald Natvig <thorvald@natvig.com> *
|
|
|
|
* Copyright (C) 2012 by Retroshare Team <retroshare.project@gmail.com> *
|
|
|
|
* *
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Affero General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU Affero General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Affero General Public License *
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
*******************************************************************************/
|
2013-10-19 20:56:34 -04:00
|
|
|
#include <QTimer>
|
|
|
|
|
2012-02-15 16:17:18 -05:00
|
|
|
#include "AudioWizard.h"
|
|
|
|
#include "audiodevicehelper.h"
|
2015-05-11 15:40:07 -04:00
|
|
|
#include "interface/rsVOIP.h"
|
2012-02-15 16:17:18 -05:00
|
|
|
|
|
|
|
#define iroundf(x) ( static_cast<int>(x) )
|
|
|
|
|
|
|
|
AudioWizard::~AudioWizard()
|
|
|
|
{
|
2012-09-25 19:08:46 -04:00
|
|
|
if (ticker) {
|
|
|
|
ticker->stop();
|
|
|
|
}
|
2012-02-15 16:17:18 -05:00
|
|
|
if (inputDevice) {
|
|
|
|
inputDevice->stop();
|
2012-09-25 19:08:46 -04:00
|
|
|
delete(inputDevice);
|
|
|
|
inputDevice = NULL;
|
|
|
|
}
|
|
|
|
if (inputProcessor) {
|
|
|
|
inputProcessor->close();
|
|
|
|
delete(inputProcessor);
|
|
|
|
inputProcessor = NULL;
|
2012-02-15 16:17:18 -05:00
|
|
|
}
|
|
|
|
if (outputDevice) {
|
|
|
|
outputDevice->stop();
|
2012-09-25 19:08:46 -04:00
|
|
|
delete(outputDevice);
|
|
|
|
outputDevice = NULL;
|
|
|
|
}
|
|
|
|
if (outputProcessor) {
|
|
|
|
outputProcessor->close();
|
|
|
|
delete(outputProcessor);
|
|
|
|
outputProcessor = NULL;
|
2012-02-15 16:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioWizard::AudioWizard(QWidget *p) : QWizard(p) {
|
|
|
|
bInit = true;
|
|
|
|
bLastActive = false;
|
|
|
|
//g.bInAudioWizard = true;
|
|
|
|
|
|
|
|
ticker = new QTimer(this);
|
|
|
|
ticker->setObjectName(QLatin1String("Ticker"));
|
|
|
|
|
|
|
|
setupUi(this);
|
|
|
|
inputProcessor = NULL;
|
|
|
|
inputDevice = NULL;
|
|
|
|
outputProcessor = NULL;
|
|
|
|
outputDevice = NULL;
|
|
|
|
|
|
|
|
abAmplify = new AudioBar(this);
|
|
|
|
abAmplify->qcBelow = Qt::green;
|
|
|
|
abAmplify->qcInside = QColor::fromRgb(255,128,0);
|
|
|
|
abAmplify->qcAbove = Qt::red;
|
|
|
|
|
|
|
|
verticalLayout_3->addWidget(abAmplify);
|
|
|
|
|
2015-05-11 15:40:07 -04:00
|
|
|
if (rsVOIP->getVoipATransmit() == RsVOIP::AudioTransmitPushToTalk)
|
2012-02-15 16:17:18 -05:00
|
|
|
qrPTT->setChecked(true);
|
2015-05-11 15:40:07 -04:00
|
|
|
else if (rsVOIP->getVoipATransmit() == RsVOIP::AudioTransmitVAD)
|
2012-02-15 16:17:18 -05:00
|
|
|
qrVAD->setChecked(true);
|
|
|
|
else
|
|
|
|
qrContinuous->setChecked(true);
|
|
|
|
|
|
|
|
abVAD = new AudioBar(this);
|
|
|
|
abVAD->qcBelow = Qt::red;
|
|
|
|
abVAD->qcInside = Qt::yellow;
|
|
|
|
abVAD->qcAbove = Qt::green;
|
|
|
|
|
2015-05-11 15:40:07 -04:00
|
|
|
qsTransmitMin->setValue(rsVOIP->getVoipfVADmin());
|
|
|
|
qsTransmitMax->setValue(rsVOIP->getVoipfVADmax());
|
2012-02-15 16:17:18 -05:00
|
|
|
|
|
|
|
verticalLayout_6->addWidget(abVAD);
|
|
|
|
|
|
|
|
// Volume
|
2015-05-11 15:40:07 -04:00
|
|
|
qsMaxAmp->setValue(rsVOIP->getVoipiMinLoudness());
|
2012-02-15 16:17:18 -05:00
|
|
|
|
|
|
|
setOption(QWizard::NoCancelButton, false);
|
|
|
|
resize(700, 500);
|
|
|
|
|
|
|
|
updateTriggerWidgets(qrVAD->isChecked());
|
|
|
|
|
|
|
|
bTransmitChanged = false;
|
|
|
|
|
|
|
|
iMaxPeak = 0;
|
|
|
|
iTicks = 0;
|
|
|
|
|
|
|
|
qpTalkingOn = QPixmap::fromImage(QImage(QLatin1String("skin:talking_on.svg")).scaled(64,64));
|
|
|
|
qpTalkingOff = QPixmap::fromImage(QImage(QLatin1String("skin:talking_off.svg")).scaled(64,64));
|
|
|
|
|
|
|
|
bInit = false;
|
|
|
|
|
|
|
|
connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(showPage(int)));
|
|
|
|
|
|
|
|
ticker->setSingleShot(false);
|
|
|
|
ticker->start(20);
|
|
|
|
connect( ticker, SIGNAL( timeout ( ) ), this, SLOT( on_Ticker_timeout() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioWizard::on_qsMaxAmp_valueChanged(int v) {
|
2015-05-11 15:40:07 -04:00
|
|
|
rsVOIP->setVoipiMinLoudness(qMin(v, 30000));
|
2012-02-15 16:17:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void AudioWizard::on_Ticker_timeout() {
|
|
|
|
if (!inputProcessor) {
|
|
|
|
inputProcessor = new QtSpeex::SpeexInputProcessor();
|
|
|
|
inputProcessor->open(QIODevice::WriteOnly | QIODevice::Unbuffered);
|
|
|
|
|
|
|
|
if (!inputDevice) {
|
|
|
|
inputDevice = AudioDeviceHelper::getPreferedInputDevice();
|
|
|
|
}
|
|
|
|
inputDevice->start(inputProcessor);
|
|
|
|
connect(inputProcessor, SIGNAL(networkPacketReady()), this, SLOT(loopAudio()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!outputProcessor) {
|
|
|
|
outputProcessor = new QtSpeex::SpeexOutputProcessor();
|
|
|
|
outputProcessor->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
|
|
|
|
|
|
|
|
if (!outputDevice) {
|
|
|
|
outputDevice = AudioDeviceHelper::getPreferedOutputDevice();
|
|
|
|
}
|
|
|
|
outputDevice->start(outputProcessor);
|
|
|
|
connect(outputProcessor, SIGNAL(playingFrame(QByteArray*)), inputProcessor, SLOT(addEchoFrame(QByteArray*)));
|
|
|
|
}
|
|
|
|
|
|
|
|
abVAD->iBelow = qsTransmitMin->value();
|
|
|
|
abVAD->iAbove = qsTransmitMax->value();
|
2015-05-11 15:40:07 -04:00
|
|
|
rsVOIP->setVoipfVADmin(qsTransmitMin->value());
|
|
|
|
rsVOIP->setVoipfVADmax(qsTransmitMax->value());
|
2012-02-15 16:17:18 -05:00
|
|
|
|
|
|
|
abVAD->iValue = iroundf(inputProcessor->dVoiceAcivityLevel * 32767.0f + 0.5f);
|
|
|
|
|
|
|
|
abVAD->update();
|
|
|
|
|
|
|
|
int iPeak = inputProcessor->dMaxMic;
|
|
|
|
|
|
|
|
if (iTicks++ >= 50) {
|
|
|
|
iMaxPeak = 0;
|
|
|
|
iTicks = 0;
|
|
|
|
}
|
|
|
|
if (iPeak > iMaxPeak)
|
|
|
|
iMaxPeak = iPeak;
|
|
|
|
|
|
|
|
abAmplify->iBelow = qsMaxAmp->value();
|
|
|
|
abAmplify->iValue = iPeak;
|
|
|
|
abAmplify->iPeak = iMaxPeak;
|
|
|
|
abAmplify->update();
|
|
|
|
|
|
|
|
bool active = inputProcessor->bPreviousVoice;
|
|
|
|
if (active != bLastActive) {
|
|
|
|
bLastActive = active;
|
|
|
|
qlTalkIcon->setPixmap(active ? qpTalkingOn : qpTalkingOff);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioWizard::loopAudio() {
|
|
|
|
while(inputProcessor && inputProcessor->hasPendingPackets()) {
|
|
|
|
packetQueue.enqueue(inputProcessor->getNetworkPacket());
|
|
|
|
QTimer* playEcho = new QTimer();
|
|
|
|
playEcho->setSingleShot(true);
|
|
|
|
connect( playEcho, SIGNAL( timeout ( ) ), this, SLOT( on_playEcho_timeout() ) );
|
|
|
|
playEcho->start(1500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioWizard::on_playEcho_timeout() {
|
|
|
|
if(!packetQueue.isEmpty()) {
|
|
|
|
if (!qcbStopEcho->isChecked()) {
|
|
|
|
if (outputDevice && outputDevice->error() != QAudio::NoError) {
|
|
|
|
std::cerr << "Stopping output device. Error " << outputDevice->error() << std::endl;
|
|
|
|
outputDevice->stop();
|
|
|
|
//TODO : find a way to restart output device, but there is a pulseaudio locks that prevents it here but it works in ChatWidget.cpp
|
|
|
|
//outputDevice->start(outputProcessor);
|
|
|
|
}
|
|
|
|
outputProcessor->putNetworkPacket("myself_loop",packetQueue.dequeue());
|
|
|
|
} else {
|
|
|
|
packetQueue.dequeue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioWizard::on_qsTransmitMax_valueChanged(int v) {
|
|
|
|
if (! bInit) {
|
2015-05-11 15:40:07 -04:00
|
|
|
rsVOIP->setVoipfVADmax(v);
|
2012-02-15 16:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioWizard::on_qsTransmitMin_valueChanged(int v) {
|
|
|
|
if (! bInit) {
|
2015-05-11 15:40:07 -04:00
|
|
|
rsVOIP->setVoipfVADmin(v);
|
2012-02-15 16:17:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioWizard::on_qrVAD_clicked(bool on) {
|
|
|
|
if (on) {
|
2015-05-11 15:40:07 -04:00
|
|
|
rsVOIP->setVoipATransmit(RsVOIP::AudioTransmitVAD);
|
2012-02-15 16:17:18 -05:00
|
|
|
updateTriggerWidgets(true);
|
|
|
|
bTransmitChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioWizard::on_qrPTT_clicked(bool on) {
|
|
|
|
if (on) {
|
2015-05-11 15:40:07 -04:00
|
|
|
rsVOIP->setVoipATransmit(RsVOIP::AudioTransmitPushToTalk);
|
2012-02-15 16:17:18 -05:00
|
|
|
updateTriggerWidgets(false);
|
|
|
|
bTransmitChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioWizard::on_qrContinuous_clicked(bool on) {
|
|
|
|
if (on) {
|
2015-05-11 15:40:07 -04:00
|
|
|
rsVOIP->setVoipATransmit(RsVOIP::AudioTransmitContinous);
|
2012-02-15 16:17:18 -05:00
|
|
|
updateTriggerWidgets(false);
|
|
|
|
bTransmitChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioWizard::updateTriggerWidgets(bool vad_on) {
|
|
|
|
if (!vad_on)
|
|
|
|
qwVAD->hide();
|
|
|
|
else
|
|
|
|
qwVAD->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AudioWizard::on_qcbHighContrast_clicked(bool on) {
|
|
|
|
abAmplify->highContrast = on;
|
|
|
|
abVAD->highContrast = on;
|
|
|
|
}
|