From 3655f49b5a4ce43ba07f362a0a8f5b42d19524c1 Mon Sep 17 00:00:00 2001 From: Ray Zhang <41682958+zhang-ray@users.noreply.github.com> Date: Fri, 2 Nov 2018 12:37:45 +0800 Subject: [PATCH] [UI] render recent max volume --- client_essential/Worker.cpp | 47 +++++++++++++++++++++++++++++++++---- client_qt5/mainwindow.cpp | 9 ++++--- client_qt5/mainwindow.hpp | 9 ------- include/evc/Worker.hpp | 7 +++++- 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/client_essential/Worker.cpp b/client_essential/Worker.cpp index 6a88233..a8b0b10 100644 --- a/client_essential/Worker.cpp +++ b/client_essential/Worker.cpp @@ -3,7 +3,6 @@ #include "evc/Factory.hpp" #include "evc/TcpClient.hpp" -#include "evc/AudioVolume.hpp" #include // for std::once_flag //// TODO @@ -146,8 +145,27 @@ void Worker::syncStart(const std::string &host,const std::string &port, } auto ret = device_->write(decodedPcm); if (volumeReporter_){ - static SuckAudioVolume sav; - volumeReporter_({AudioInOut::Out, sav.calculate(decodedPcm, AudioIoVolume::MAX_VOLUME_LEVEL)}); + auto currentLevel = sav.calculate(decodedPcm, AudioIoVolume::MAX_VOLUME_LEVEL); + static auto recentMaxLevel = currentLevel; + + static auto lastTimeStamp = std::chrono::system_clock::now(); + auto now = std::chrono::system_clock::now(); + auto elapsed = now - lastTimeStamp; + + // hold on 1s + if (elapsed > std::chrono::seconds(1)){ + recentMaxLevel=0; + lastTimeStamp = std::chrono::system_clock::now(); + } + + + if (currentLevel>recentMaxLevel){ + recentMaxLevel=currentLevel; + // re calculate hold-on time + lastTimeStamp = std::chrono::system_clock::now(); + } + + volumeReporter_({AudioInOut::Out, currentLevel, recentMaxLevel}); } if (!ret) { std::cout << ret.message() << std::endl; @@ -250,8 +268,27 @@ void Worker::syncStart(const std::string &host,const std::string &port, if (volumeReporter_){ - static SuckAudioVolume sav; - volumeReporter_({AudioInOut::In, sav.calculate(denoisedBuffer, AudioIoVolume::MAX_VOLUME_LEVEL)}); + auto currentLevel = sav.calculate(denoisedBuffer, AudioIoVolume::MAX_VOLUME_LEVEL); + static auto recentMaxLevel = currentLevel; + + static auto lastTimeStamp = std::chrono::system_clock::now(); + auto now = std::chrono::system_clock::now(); + auto elapsed = now - lastTimeStamp; + + // hold on 1s + if (elapsed > std::chrono::seconds(1)){ + recentMaxLevel=0; + lastTimeStamp = std::chrono::system_clock::now(); + } + + + if (currentLevel>recentMaxLevel){ + recentMaxLevel=currentLevel; + // re calculate hold-on time + lastTimeStamp = std::chrono::system_clock::now(); + } + + volumeReporter_({AudioInOut::In, currentLevel, recentMaxLevel}); } diff --git a/client_qt5/mainwindow.cpp b/client_qt5/mainwindow.cpp index e94730b..ccd0b00 100644 --- a/client_qt5/mainwindow.cpp +++ b/client_qt5/mainwindow.cpp @@ -136,8 +136,8 @@ MainWindow::MainWindow(QWidget *parent) /// init state { - onVolumeChanged({AudioInOut::In, 0u}); - onVolumeChanged({AudioInOut::Out, 0u}); + onVolumeChanged({AudioInOut::In, 0u, 0u}); + onVolumeChanged({AudioInOut::Out, 0u, 0u}); onNetworkChanged(NetworkState::Disconnected); toggleAdvancedMode(true); showMessage("F1: help F2: toggle mode (advanced/easy mode)"); @@ -242,6 +242,9 @@ void MainWindow::onVolumeChanged(const AudioIoVolume aivl) { for (int i =0;i< AudioIoVolume::MAX_VOLUME_LEVEL;i++){ label_img_[(uint8_t)aivl.io_][i]->setPixmap(i aivl.level_){ + label_img_[(uint8_t)aivl.io_][aivl.recentMaxLevel_]->setPixmap(vertical_bar_full); + } } else{ QCoreApplication::postEvent(this, new AudioVolumeEvent(aivl)); @@ -340,7 +343,7 @@ bool MainWindow::event(QEvent *event) return true; } else if (event->type() == AudioVolumeEvent::sType) { AudioVolumeEvent *myEvent = static_cast(event); - onVolumeChanged({myEvent->io_, myEvent->level_}); + onVolumeChanged({myEvent->io_, myEvent->level_, myEvent->recentMaxLevel_}); return true; } else if (event->type() == VadEvent::sType){ diff --git a/client_qt5/mainwindow.hpp b/client_qt5/mainwindow.hpp index 6034271..87f7760 100644 --- a/client_qt5/mainwindow.hpp +++ b/client_qt5/mainwindow.hpp @@ -21,12 +21,6 @@ class AudioVolumeEvent : public QEvent, public AudioIoVolume { , AudioIoVolume(aiv) { } - - AudioVolumeEvent(const AudioInOut io, const uint8_t level) - : QEvent(sType) - , AudioIoVolume(io, level) - { - } }; @@ -57,9 +51,6 @@ class MainWindow : public QMainWindow private slots: void on_pushButton_connecting_clicked(); - - - /// TODO: render max volume bar void onNetworkChanged(const NetworkState networkState); void onVolumeChanged(const AudioIoVolume); void onDeviceNameChanged(const std::string &newMic, const std::string &newSpk){} diff --git a/include/evc/Worker.hpp b/include/evc/Worker.hpp index b425d9f..160ace2 100644 --- a/include/evc/Worker.hpp +++ b/include/evc/Worker.hpp @@ -4,6 +4,8 @@ #include #include +#include "evc/AudioVolume.hpp" + class AudioDecoder; class AudioEncoder; class AudioDevice; @@ -23,10 +25,12 @@ class AudioIoVolume { AudioInOut io_; Level level_; + Level recentMaxLevel_; public: - AudioIoVolume(const AudioInOut io, const Level level) + AudioIoVolume(const AudioInOut io, const Level level, const Level recentMaxLevel) :io_(io) ,level_(level) + , recentMaxLevel_(recentMaxLevel) { } @@ -59,6 +63,7 @@ class Worker { uint8_t vadCounter_ = 0; // nbActivated bool needSend_ = true; + SuckAudioVolume sav; public: Worker(bool needAec); ~Worker();