-
Notifications
You must be signed in to change notification settings - Fork 17
/
VoiceActiveCheck.cpp
92 lines (83 loc) · 2.38 KB
/
VoiceActiveCheck.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// VoiceActiveCheck.cpp
// VADTEST
//
// Created by zhangyu on 2017/10/11.
// Copyright © 2017年 Michong. All rights reserved.
//
#include "VoiceActiveCheck.h"
#include "webrtc_vad.h"
namespace webrtc {
class VoiceActiveCheck::Vad {
public:
Vad() {
reset();
}
~Vad() {
if (state_)
WebRtcVad_Free(state_);
}
void reset() {
if (state_)
WebRtcVad_Free(state_);
state_ = WebRtcVad_Create();
WebRtcVad_Init(state_);
}
VadInst* state() { return state_; }
private:
VadInst* state_ = nullptr;
};
VoiceActiveCheck::VoiceActiveCheck(int sample_rate_hz) {
sample_rate_hz_ = sample_rate_hz;
vad_ = new VoiceActiveCheck::Vad();
frame_size_samples_ =
static_cast<size_t>(frame_size_ms_ * sample_rate_hz_) / 1000;
//preset & reset
reset();
}
VoiceActiveCheck::~VoiceActiveCheck() {
if (vad_) {
delete vad_;
vad_ = nullptr;
}
}
bool VoiceActiveCheck:: isActiveVoice(int16_t *buf) {
int vad_ret = WebRtcVad_Process(vad_->state(), sample_rate_hz_,
buf,
frame_size_samples_);
return vad_ret != 0;
}
void VoiceActiveCheck::set_likelihood(VoiceActiveCheck::Likelihood likelihood) {
if (likelihood_ == likelihood) {
return;
}
likelihood_ = likelihood;
int mode = 2;
switch (likelihood) {
case VoiceActiveCheck::kLowestLikelihood:
mode = 4;
break;
case VoiceActiveCheck::kVeryLowLikelihood:
mode = 3;
break;
case VoiceActiveCheck::kLowLikelihood:
mode = 2;
break;
case VoiceActiveCheck::kModerateLikelihood:
mode = 1;
break;
case VoiceActiveCheck::kHighLikelihood:
mode = 0;
break;
default:
break;
}
WebRtcVad_set_mode(vad_->state(), mode);
}
void VoiceActiveCheck::reset() {
vad_->reset();
Likelihood hood = likelihood_;
likelihood_ = (Likelihood)1001;
set_likelihood(hood);
}
}