Skip to content

Commit

Permalink
[UI] render recent max volume
Browse files Browse the repository at this point in the history
  • Loading branch information
zhang-ray committed Nov 2, 2018
1 parent 7cbb78c commit 3655f49
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 18 deletions.
47 changes: 42 additions & 5 deletions client_essential/Worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "evc/Factory.hpp"
#include "evc/TcpClient.hpp"
#include "evc/AudioVolume.hpp"
#include <mutex> // for std::once_flag

//// TODO
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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});
}


Expand Down
9 changes: 6 additions & 3 deletions client_qt5/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)");
Expand Down Expand Up @@ -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_? vertical_bar_half_full: vertical_bar_empty);
}
if (aivl.recentMaxLevel_> aivl.level_){
label_img_[(uint8_t)aivl.io_][aivl.recentMaxLevel_]->setPixmap(vertical_bar_full);
}
}
else{
QCoreApplication::postEvent(this, new AudioVolumeEvent(aivl));
Expand Down Expand Up @@ -340,7 +343,7 @@ bool MainWindow::event(QEvent *event)
return true;
} else if (event->type() == AudioVolumeEvent::sType) {
AudioVolumeEvent *myEvent = static_cast<AudioVolumeEvent *>(event);
onVolumeChanged({myEvent->io_, myEvent->level_});
onVolumeChanged({myEvent->io_, myEvent->level_, myEvent->recentMaxLevel_});
return true;
}
else if (event->type() == VadEvent::sType){
Expand Down
9 changes: 0 additions & 9 deletions client_qt5/mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
};


Expand Down Expand Up @@ -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){}
Expand Down
7 changes: 6 additions & 1 deletion include/evc/Worker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <thread>
#include <string>

#include "evc/AudioVolume.hpp"

class AudioDecoder;
class AudioEncoder;
class AudioDevice;
Expand All @@ -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)
{

}
Expand Down Expand Up @@ -59,6 +63,7 @@ class Worker {

uint8_t vadCounter_ = 0; // nbActivated
bool needSend_ = true;
SuckAudioVolume sav;
public:
Worker(bool needAec);
~Worker();
Expand Down

0 comments on commit 3655f49

Please sign in to comment.