diff --git a/audiohandler.cpp b/audiohandler.cpp index 6968410..3f68bd8 100644 --- a/audiohandler.cpp +++ b/audiohandler.cpp @@ -271,17 +271,10 @@ void audioHandler::convertedOutput(audioPacket packet) { */ lastSentSeq = packet.seq; amplitude = packet.amplitudePeak; - computeLevels(); - emit haveLevels(getAmplitude(), setup.latency, currentLatency, isUnderrun, isOverrun); + emit haveLevels(getAmplitude(), static_cast(packet.amplitudeRMS * 255.0), setup.latency, currentLatency, isUnderrun, isOverrun); } } -void audioHandler::computeLevels() -{ - if(levelMean) - levelMean[(levelPosition++)%levelSize] = amplitude * 255; -} - void audioHandler::getNextAudioChunk() { if (audioDevice) { @@ -319,7 +312,7 @@ void audioHandler::convertedInput(audioPacket audio) } lastReceived = QTime::currentTime(); amplitude = audio.amplitudePeak; - emit haveLevels(getAmplitude(), setup.latency, currentLatency, isUnderrun, isOverrun); + emit haveLevels(getAmplitude(), audio.amplitudeRMS, setup.latency, currentLatency, isUnderrun, isOverrun); } } diff --git a/audiohandler.h b/audiohandler.h index 7c8412b..760b215 100644 --- a/audiohandler.h +++ b/audiohandler.h @@ -70,7 +70,7 @@ signals: void audioMessage(QString message); void sendLatency(quint16 newSize); void haveAudioData(const audioPacket& data); - void haveLevels(quint16 amplitude,quint16 latency,quint16 current,bool under,bool over); + void haveLevels(quint16 amplitudePeak, quint16 amplitudeRMS, quint16 latency,quint16 current,bool under,bool over); void setupConverter(QAudioFormat in, QAudioFormat out, quint8 opus, quint8 resamp); void sendToConverter(audioPacket audio); @@ -116,12 +116,6 @@ private: float amplitude=0.0; qreal volume = 1.0; - unsigned char *levelMean = Q_NULLPTR; - unsigned char *levelPeak = Q_NULLPTR; - unsigned char levelSize = 50; - unsigned char levelPosition = 0; - void computeLevels(); - audioSetup setup; OpusEncoder* encoder = Q_NULLPTR; diff --git a/pahandler.cpp b/pahandler.cpp index f437e0c..d27886c 100644 --- a/pahandler.cpp +++ b/pahandler.cpp @@ -279,7 +279,7 @@ void paHandler::convertedOutput(audioPacket packet) { } amplitude = packet.amplitudePeak; - emit haveLevels(getAmplitude(), setup.latency, currentLatency, isUnderrun, isOverrun); + emit haveLevels(getAmplitude(), static_cast(packet.amplitudeRMS * 255.0), setup.latency, currentLatency, isUnderrun, isOverrun); } } @@ -292,7 +292,7 @@ void paHandler::convertedInput(audioPacket packet) amplitude = packet.amplitudePeak; const PaStreamInfo* info = Pa_GetStreamInfo(audio); currentLatency = packet.time.msecsTo(QTime::currentTime()) + (info->inputLatency * 1000); - emit haveLevels(getAmplitude(), setup.latency, currentLatency, isUnderrun, isOverrun); + emit haveLevels(getAmplitude(), packet.amplitudeRMS, setup.latency, currentLatency, isUnderrun, isOverrun); } } diff --git a/pahandler.h b/pahandler.h index a8fac05..c9bd474 100644 --- a/pahandler.h +++ b/pahandler.h @@ -55,7 +55,7 @@ signals: void audioMessage(QString message); void sendLatency(quint16 newSize); void haveAudioData(const audioPacket& data); - void haveLevels(quint16 amplitude, quint16 latency, quint16 current, bool under, bool over); + void haveLevels(quint16 amplitudePeak, quint16 amplitudeRMS, quint16 latency, quint16 current, bool under, bool over); void setupConverter(QAudioFormat in, QAudioFormat out, quint8 opus, quint8 resamp); void sendToConverter(audioPacket audio); diff --git a/rthandler.cpp b/rthandler.cpp index 73b8edb..b203402 100644 --- a/rthandler.cpp +++ b/rthandler.cpp @@ -333,7 +333,7 @@ void rtHandler::convertedOutput(audioPacket packet) audioMutex.unlock(); amplitude = packet.amplitudePeak; currentLatency = packet.time.msecsTo(QTime::currentTime()) + (outFormat.durationForBytes(audio->getStreamLatency() * (outFormat.sampleSize() / 8) * outFormat.channelCount())/1000); - emit haveLevels(getAmplitude(), setup.latency, currentLatency, isUnderrun, isOverrun); + emit haveLevels(getAmplitude(), packet.amplitudeRMS, setup.latency, currentLatency, isUnderrun, isOverrun); } @@ -344,7 +344,7 @@ void rtHandler::convertedInput(audioPacket packet) emit haveAudioData(packet); amplitude = packet.amplitudePeak; currentLatency = packet.time.msecsTo(QTime::currentTime()) + (outFormat.durationForBytes(audio->getStreamLatency() * (outFormat.sampleSize() / 8) * outFormat.channelCount())/1000); - emit haveLevels(getAmplitude(), setup.latency, currentLatency, isUnderrun, isOverrun); + emit haveLevels(getAmplitude(), static_cast(packet.amplitudeRMS * 255.0), setup.latency, currentLatency, isUnderrun, isOverrun); } } diff --git a/rthandler.h b/rthandler.h index c48a2b4..2b73b12 100644 --- a/rthandler.h +++ b/rthandler.h @@ -61,7 +61,7 @@ signals: void audioMessage(QString message); void sendLatency(quint16 newSize); void haveAudioData(const audioPacket& data); - void haveLevels(quint16 amplitude, quint16 latency, quint16 current, bool under, bool over); + void haveLevels(quint16 amplitudePeak, quint16 amplitudeRMS, quint16 latency, quint16 current, bool under, bool over); void setupConverter(QAudioFormat in, QAudioFormat out, quint8 opus, quint8 resamp); void sendToConverter(audioPacket audio); diff --git a/udpaudio.cpp b/udpaudio.cpp index dc1e5c5..1326a67 100644 --- a/udpaudio.cpp +++ b/udpaudio.cpp @@ -159,13 +159,13 @@ void udpAudio::setVolume(unsigned char value) emit haveSetVolume(value); } -void udpAudio::getRxLevels(quint16 amplitude, quint16 latency, quint16 current, bool under, bool over) { +void udpAudio::getRxLevels(quint16 amplitudePeak, quint16 amplitudeRMS, quint16 latency, quint16 current, bool under, bool over) { - emit haveRxLevels(amplitude, latency, current, under, over); + emit haveRxLevels(amplitudePeak, amplitudeRMS, latency, current, under, over); } -void udpAudio::getTxLevels(quint16 amplitude, quint16 latency, quint16 current, bool under, bool over) { - emit haveTxLevels(amplitude, latency, current, under, over); +void udpAudio::getTxLevels(quint16 amplitudePeak, quint16 amplitudeRMS, quint16 latency, quint16 current, bool under, bool over) { + emit haveTxLevels(amplitudePeak, amplitudeRMS, latency, current, under, over); } void udpAudio::dataReceived() @@ -256,7 +256,7 @@ void udpAudio::startAudio() { connect(this, SIGNAL(haveAudioData(audioPacket)), rxaudio, SLOT(incomingAudio(audioPacket))); connect(this, SIGNAL(haveChangeLatency(quint16)), rxaudio, SLOT(changeLatency(quint16))); connect(this, SIGNAL(haveSetVolume(unsigned char)), rxaudio, SLOT(setVolume(unsigned char))); - connect(rxaudio, SIGNAL(haveLevels(quint16, quint16, quint16, bool, bool)), this, SLOT(getRxLevels(quint16, quint16, quint16, bool, bool))); + connect(rxaudio, SIGNAL(haveLevels(quint16, quint16, quint16, quint16, bool, bool)), this, SLOT(getRxLevels(quint16, quint16, quint16, quint16, bool, bool))); connect(rxAudioThread, SIGNAL(finished()), rxaudio, SLOT(deleteLater())); @@ -290,7 +290,7 @@ void udpAudio::startAudio() { connect(this, SIGNAL(setupTxAudio(audioSetup)), txaudio, SLOT(init(audioSetup))); connect(txaudio, SIGNAL(haveAudioData(audioPacket)), this, SLOT(receiveAudioData(audioPacket))); - connect(txaudio, SIGNAL(haveLevels(quint16, quint16, quint16, bool, bool)), this, SLOT(getTxLevels(quint16, quint16, quint16, bool, bool))); + connect(txaudio, SIGNAL(haveLevels(quint16, quint16, quint16, quint16, bool, bool)), this, SLOT(getTxLevels(quint16, quint16, quint16, quint16, bool, bool))); connect(txAudioThread, SIGNAL(finished()), txaudio, SLOT(deleteLater())); emit setupTxAudio(txSetup); diff --git a/udpaudio.h b/udpaudio.h index 884edef..3ab6d42 100644 --- a/udpaudio.h +++ b/udpaudio.h @@ -51,14 +51,14 @@ signals: void haveChangeLatency(quint16 value); void haveSetVolume(unsigned char value); - void haveRxLevels(quint16 amplitude, quint16 latency, quint16 current, bool under, bool over); - void haveTxLevels(quint16 amplitude, quint16 latency, quint16 current, bool under, bool over); + void haveRxLevels(quint16 amplitudePeak, quint16 amplitudeRMS, quint16 latency, quint16 current, bool under, bool over); + void haveTxLevels(quint16 amplitudePeak, quint16 amplitudeRMS, quint16 latency, quint16 current, bool under, bool over); public slots: void changeLatency(quint16 value); void setVolume(unsigned char value); - void getRxLevels(quint16 amplitude, quint16 latency, quint16 current, bool under, bool over); - void getTxLevels(quint16 amplitude, quint16 latency, quint16 current, bool under, bool over); + void getRxLevels(quint16 amplitude, quint16 amplitudeRMS, quint16 latency, quint16 current, bool under, bool over); + void getTxLevels(quint16 amplitude, quint16 amplitudeRMS, quint16 latency, quint16 current, bool under, bool over); void receiveAudioData(audioPacket audio); private: @@ -85,4 +85,4 @@ private: }; -#endif \ No newline at end of file +#endif diff --git a/udphandler.cpp b/udphandler.cpp index fd32779..f075d6b 100644 --- a/udphandler.cpp +++ b/udphandler.cpp @@ -143,41 +143,50 @@ void udpHandler::receiveDataFromUserToRig(QByteArray data) } } -void udpHandler::getRxLevels(quint16 amplitude,quint16 latency,quint16 current, bool under, bool over) { - status.rxAudioLevel = amplitude; +void udpHandler::getRxLevels(quint16 amplitudePeak, quint16 amplitudeRMS,quint16 latency,quint16 current, bool under, bool over) { + status.rxAudioLevel = amplitudePeak; status.rxLatency = latency; status.rxCurrentLatency = current; status.rxUnderrun = under; status.rxOverrun = over; - audioLevelsRxPeak[(audioLevelsRxPeakPosition++)%audioLevelBufferSize] = amplitude; - if((audioLevelsRxPeakPosition++)%3 == 0) + audioLevelsRxPeak[(audioLevelsRxPosition)%audioLevelBufferSize] = amplitudePeak; + audioLevelsRxRMS[(audioLevelsRxPosition)%audioLevelBufferSize] = amplitudeRMS; + + if((audioLevelsRxPosition)%3 == 0) { // calculate mean and emit signal - unsigned char mean = findMean(audioLevelsRxPeak); + unsigned char meanPeak = findMax(audioLevelsRxPeak); + unsigned char meanRMS = findMean(audioLevelsRxRMS); networkAudioLevels l; l.haveRxLevels = true; - l.rxAudioPeak = mean; - //qDebug(logSystem()) << "audio level meter being emitted from udpHandler"; + l.rxAudioPeak = meanPeak; + l.rxAudioRMS = meanRMS; emit haveNetworkAudioLevels(l); } + audioLevelsRxPosition++; } -void udpHandler::getTxLevels(quint16 amplitude,quint16 latency, quint16 current, bool under, bool over) { - status.txAudioLevel = amplitude; +void udpHandler::getTxLevels(quint16 amplitudePeak, quint16 amplitudeRMS ,quint16 latency, quint16 current, bool under, bool over) { + status.txAudioLevel = amplitudePeak; status.txLatency = latency; status.txCurrentLatency = current; status.txUnderrun = under; status.txOverrun = over; - audioLevelsTxPeak[(audioLevelsTxPeakPosition++)%audioLevelBufferSize] = amplitude; - if((audioLevelsTxPeakPosition++)%3 == 0) + audioLevelsTxPeak[(audioLevelsTxPosition)%audioLevelBufferSize] = amplitudePeak; + audioLevelsTxRMS[(audioLevelsTxPosition)%audioLevelBufferSize] = amplitudeRMS; + + if((audioLevelsTxPosition)%3 == 0) { // calculate mean and emit signal - unsigned char mean = findMean(audioLevelsTxPeak); + unsigned char meanPeak = findMax(audioLevelsTxPeak); + unsigned char meanRMS = findMean(audioLevelsTxRMS); networkAudioLevels l; l.haveTxLevels = true; - l.txAudioPeak = mean; + l.txAudioPeak = meanPeak; + l.txAudioRMS = meanRMS; emit haveNetworkAudioLevels(l); } + audioLevelsTxPosition++; } unsigned char udpHandler::findMean(unsigned char *data) @@ -190,6 +199,17 @@ unsigned char udpHandler::findMean(unsigned char *data) return sum / audioLevelBufferSize; } +unsigned char udpHandler::findMax(unsigned char *data) +{ + unsigned int max=0; + for(int p=0; p < audioLevelBufferSize; p++) + { + if(data[p] > max) + max = data[p]; + } + return max; +} + void udpHandler::dataReceived() { while (udp->hasPendingDatagrams()) { @@ -348,8 +368,8 @@ void udpHandler::dataReceived() QObject::connect(audio, SIGNAL(haveAudioData(audioPacket)), this, SLOT(receiveAudioData(audioPacket))); QObject::connect(this, SIGNAL(haveChangeLatency(quint16)), audio, SLOT(changeLatency(quint16))); QObject::connect(this, SIGNAL(haveSetVolume(unsigned char)), audio, SLOT(setVolume(unsigned char))); - QObject::connect(audio, SIGNAL(haveRxLevels(quint16, quint16, quint16, bool, bool)), this, SLOT(getRxLevels(quint16, quint16, quint16, bool, bool))); - QObject::connect(audio, SIGNAL(haveTxLevels(quint16, quint16, quint16, bool, bool)), this, SLOT(getTxLevels(quint16, quint16, quint16, bool, bool))); + QObject::connect(audio, SIGNAL(haveRxLevels(quint16, quint16, quint16, quint16, bool, bool)), this, SLOT(getRxLevels(quint16, quint16, quint16, quint16, bool, bool))); + QObject::connect(audio, SIGNAL(haveTxLevels(quint16, quint16, quint16, quint16, bool, bool)), this, SLOT(getTxLevels(quint16, quint16, quint16, quint16, bool, bool))); } qInfo(logUdp()) << this->metaObject()->className() << "Got serial and audio request success, device name: " << devName; diff --git a/udphandler.h b/udphandler.h index 231577c..74cca92 100644 --- a/udphandler.h +++ b/udphandler.h @@ -56,10 +56,8 @@ public slots: void setVolume(unsigned char value); void init(); void setCurrentRadio(quint8 radio); - void getRxLevels(quint16 amplitude, quint16 latency, quint16 current, bool under, bool over); - void getTxLevels(quint16 amplitude, quint16 latency, quint16 current, bool under, bool over); - //void handleRxLevels(networkAudioLevels); - //void handleTxLevels(networkAudioLevels); + void getRxLevels(quint16 amplitudePeak, quint16 amplitudeRMS, quint16 latency, quint16 current, bool under, bool over); + void getTxLevels(quint16 amplitudePeak, quint16 amplitudeRMS, quint16 latency, quint16 current, bool under, bool over); signals: void haveDataFromPort(QByteArray data); // emit this when we have data, connect to rigcommander @@ -127,9 +125,14 @@ private: unsigned char audioLevelsTxPeak[audioLevelBufferSize]; unsigned char audioLevelsRxPeak[audioLevelBufferSize]; - unsigned char audioLevelsTxPeakPosition = 0; - unsigned char audioLevelsRxPeakPosition = 0; + + unsigned char audioLevelsTxRMS[audioLevelBufferSize]; + unsigned char audioLevelsRxRMS[audioLevelBufferSize]; + + unsigned char audioLevelsTxPosition = 0; + unsigned char audioLevelsRxPosition = 0; unsigned char findMean(unsigned char *d); + unsigned char findMax(unsigned char *d); };