Skip to content

Commit

Permalink
+ toggleButton_micMute
Browse files Browse the repository at this point in the history
  • Loading branch information
zhang-ray committed Nov 2, 2018
1 parent ef6f312 commit 73182ad
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 83 deletions.
159 changes: 80 additions & 79 deletions client_essential/Worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,117 +247,118 @@ void Worker::syncStart(const std::string &host,const std::string &port,
/// TODO:
/// in practice, device_->read would be unblock in first several blocks
/// so let's clear microphone's buffer on first time???
{
std::vector<short> micBuffer(blockSize);
auto ret = device_->read(micBuffer);
if (!ret){
break;
if (!mute_){
{
std::vector<short> micBuffer(blockSize);
auto ret = device_->read(micBuffer);
if (!ret){
break;
}
auto temp = (short*)micBuffer.data();
auto outTemp = denoisedBuffer.data();
WebRtcNsx_Process(ns_, &temp, 1, &outTemp);
}
auto temp = (short*)micBuffer.data();
auto outTemp = denoisedBuffer.data();
WebRtcNsx_Process(ns_, &temp, 1, &outTemp);
}



if (needAec_){
std::vector<float> floatNearend(blockSize);
for (int i=0;i<blockSize; i++){
floatNearend[i] = (float)denoisedBuffer[i]/(1<<15);
}
for (int i = 0; i < blockSize; i+=160){
auto temp = &(floatNearend[i]);
auto temp2 = &(out[i]);

auto ret =
WebRtcAec_Process(aec,
&temp,
sampleRate / 16000,
&temp2,
blockSize,
blockSize,
0 );
if (ret){
throw;
if (needAec_){
std::vector<float> floatNearend(blockSize);
for (int i=0;i<blockSize; i++){
floatNearend[i] = (float)denoisedBuffer[i]/(1<<15);
}
for (int i = 0; i < blockSize; i+=160){
auto temp = &(floatNearend[i]);
auto temp2 = &(out[i]);

auto ret =
WebRtcAec_Process(aec,
&temp,
sampleRate / 16000,
&temp2,
blockSize,
blockSize,
0 );
if (ret){
throw;
}

for (int j = i; j< i+blockSize; j++){
tobeSend[j]=out[j]*(1<<15);
for (int j = i; j< i+blockSize; j++){
tobeSend[j]=out[j]*(1<<15);
}
}
}
}



if (volumeReporter_){
auto currentLevel = sav.calculate(denoisedBuffer, AudioIoVolume::MAX_VOLUME_LEVEL);
static auto recentMaxLevel = currentLevel;
if (volumeReporter_){
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;
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();
}
// 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();
if (currentLevel>recentMaxLevel){
recentMaxLevel=currentLevel;
// re calculate hold-on time
lastTimeStamp = std::chrono::system_clock::now();
}

volumeReporter_({AudioInOut::In, currentLevel, recentMaxLevel});
}

volumeReporter_({AudioInOut::In, currentLevel, recentMaxLevel});
}

auto haveVoice = (1==WebRtcVad_Process(vad, sampleRate, denoisedBuffer.data(), sampleRate/100));
if (vadReporter_){
vadReporter_(haveVoice);
}

auto haveVoice = (1==WebRtcVad_Process(vad, sampleRate, denoisedBuffer.data(), sampleRate/100));
if (vadReporter_){
vadReporter_(haveVoice);
}

if (haveVoice){
vadCounter_++;
needSend_=true;
}

if (haveVoice){
vadCounter_++;
needSend_=true;
}

{
static auto lastTimeStamp = std::chrono::system_clock::now();
auto now = std::chrono::system_clock::now();
auto elapsed = now - lastTimeStamp;

{
static auto lastTimeStamp = std::chrono::system_clock::now();
auto now = std::chrono::system_clock::now();
auto elapsed = now - lastTimeStamp;
if (elapsed > std::chrono::seconds(1)){
if (vadCounter_==0){
/// 100 section per one second
/// in last one second, no voice found
/// so that we predict there's no voice in future
needSend_ = false;
}
lastTimeStamp = std::chrono::system_clock::now();

if (elapsed > std::chrono::seconds(1)){
if (vadCounter_==0){
/// 100 section per one second
/// in last one second, no voice found
/// so that we predict there's no voice in future
needSend_ = false;
vadCounter_=0;
}
lastTimeStamp = std::chrono::system_clock::now();

vadCounter_=0;
}
}


if (needSend_){
std::vector<char> outData;
auto retEncode = encoder->encode(needAec_? tobeSend: denoisedBuffer, outData);
if (!retEncode){
std::cout << retEncode.message() << std::endl;
break;
}
if (needSend_){
std::vector<char> outData;
auto retEncode = encoder->encode(needAec_? tobeSend: denoisedBuffer, outData);
if (!retEncode){
std::cout << retEncode.message() << std::endl;
break;
}

client.send(NetPacket(NetPacket::PayloadType::AudioMessage, outData));
client.send(NetPacket(NetPacket::PayloadType::AudioMessage, outData));
}
}



// send heartbeat
{
static auto lastTimeStamp = std::chrono::system_clock::now();
Expand Down
16 changes: 16 additions & 0 deletions client_qt5/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ MainWindow::MainWindow(QWidget *parent)
/// tool tip
{
ui->lineEdit_serverHost->setToolTip("example: your.server.com\n or: 12.45.67.89");
ui->toggleButton_micMute->setToolTip("Mute Microphone!");
}


Expand Down Expand Up @@ -349,6 +350,10 @@ bool MainWindow::event(QEvent *event)
return true;
} else if (event->type() == AudioVolumeEvent::sType) {
AudioVolumeEvent *myEvent = static_cast<AudioVolumeEvent *>(event);
if (myEvent->io_==AudioInOut::In && ui->toggleButton_micMute->isChecked()){
// we got an AudioVolumeEvent here after toggleButton_micMute checked sometimes;
return true;
}
onVolumeChanged({myEvent->io_, myEvent->level_, myEvent->recentMaxLevel_});
return true;
}
Expand All @@ -366,3 +371,14 @@ bool MainWindow::event(QEvent *event)
return QWidget::event(event);
}


void MainWindow::on_toggleButton_micMute_clicked(bool checked){
if (worker_){
worker_->setMute(checked);
}

/// clean UI
if (checked){
onVolumeChanged({AudioInOut::In, 0u, 0u});
}
}
2 changes: 2 additions & 0 deletions client_qt5/mainwindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ private slots:
void toggleAdvancedMode(){toggleAdvancedMode(!advancedMode_);}
void toggleAdvancedMode(bool newMode);
void gotoWork();
void on_toggleButton_micMute_clicked(bool checked);

private:
Ui::MainWindow *ui = nullptr;

Expand Down
20 changes: 16 additions & 4 deletions client_qt5/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,22 @@
<string/>
</property>
</widget>
<widget class="QPushButton" name="toggleButton_micMute">
<property name="geometry">
<rect>
<x>210</x>
<y>30</y>
<width>31</width>
<height>25</height>
</rect>
</property>
<property name="text">
<string>M</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</widget>
<widget class="QGroupBox" name="groupBox_aout">
<property name="geometry">
Expand Down Expand Up @@ -423,10 +439,6 @@
<string>RUDP</string>
</property>
</widget>
<zorder>radioButton_tcp</zorder>
<zorder>radioButton_rtp</zorder>
<zorder>radioButton_rudp</zorder>
<zorder>verticalSpacer</zorder>
</widget>
</widget>
<widget class="QPushButton" name="pushButton_connecting">
Expand Down
2 changes: 2 additions & 0 deletions include/evc/Worker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Worker {
bool needAec_ = false;

uint8_t vadCounter_ = 0; // nbActivated
bool mute_ = false;
bool needSend_ = true;
SuckAudioVolume sav;
private:
Expand All @@ -71,6 +72,7 @@ class Worker {
Worker(bool needAec);
~Worker();

void setMute(bool mute){mute_ = mute;}
bool initCodec();
bool initDevice(std::function<void(const std::string &,const std::string &)> reportInfo,
std::function<void(const AudioIoVolume)> reportVolume,
Expand Down

0 comments on commit 73182ad

Please sign in to comment.