Turn webrtc::Vad into a pure virtual interface
Review URL: https://codereview.webrtc.org/1317243005 Cr-Commit-Position: refs/heads/master@{#9899}
This commit is contained in:
@ -12,12 +12,12 @@
|
|||||||
#define WEBRTC_COMMON_AUDIO_VAD_INCLUDE_VAD_H_
|
#define WEBRTC_COMMON_AUDIO_VAD_INCLUDE_VAD_H_
|
||||||
|
|
||||||
#include "webrtc/base/checks.h"
|
#include "webrtc/base/checks.h"
|
||||||
|
#include "webrtc/base/scoped_ptr.h"
|
||||||
#include "webrtc/common_audio/vad/include/webrtc_vad.h"
|
#include "webrtc/common_audio/vad/include/webrtc_vad.h"
|
||||||
#include "webrtc/typedefs.h"
|
#include "webrtc/typedefs.h"
|
||||||
|
|
||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
|
|
||||||
// This is a C++ wrapper class for WebRtcVad.
|
|
||||||
class Vad {
|
class Vad {
|
||||||
public:
|
public:
|
||||||
enum Aggressiveness {
|
enum Aggressiveness {
|
||||||
@ -29,21 +29,22 @@ class Vad {
|
|||||||
|
|
||||||
enum Activity { kPassive = 0, kActive = 1, kError = -1 };
|
enum Activity { kPassive = 0, kActive = 1, kError = -1 };
|
||||||
|
|
||||||
explicit Vad(enum Aggressiveness mode);
|
virtual ~Vad() = default;
|
||||||
|
|
||||||
virtual ~Vad();
|
|
||||||
|
|
||||||
|
// Calculates a VAD decision for the given audio frame. Valid sample rates
|
||||||
|
// are 8000, 16000, and 32000 Hz; the number of samples must be such that the
|
||||||
|
// frame is 10, 20, or 30 ms long.
|
||||||
virtual Activity VoiceActivity(const int16_t* audio,
|
virtual Activity VoiceActivity(const int16_t* audio,
|
||||||
size_t num_samples,
|
size_t num_samples,
|
||||||
int sample_rate_hz);
|
int sample_rate_hz) = 0;
|
||||||
|
|
||||||
// Reset VAD state.
|
// Resets VAD state.
|
||||||
virtual void Reset();
|
virtual void Reset() = 0;
|
||||||
|
|
||||||
private:
|
|
||||||
VadInst* handle_;
|
|
||||||
Aggressiveness aggressiveness_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Returns a Vad instance that's implemented on top of WebRtcVad.
|
||||||
|
rtc::scoped_ptr<Vad> CreateVad(Vad::Aggressiveness aggressiveness);
|
||||||
|
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
|
||||||
#endif // WEBRTC_COMMON_AUDIO_VAD_INCLUDE_VAD_H_
|
#endif // WEBRTC_COMMON_AUDIO_VAD_INCLUDE_VAD_H_
|
||||||
|
@ -19,7 +19,6 @@ namespace webrtc {
|
|||||||
|
|
||||||
class MockVad : public Vad {
|
class MockVad : public Vad {
|
||||||
public:
|
public:
|
||||||
explicit MockVad(enum Aggressiveness mode) : Vad(mode) {}
|
|
||||||
virtual ~MockVad() { Die(); }
|
virtual ~MockVad() { Die(); }
|
||||||
MOCK_METHOD0(Die, void());
|
MOCK_METHOD0(Die, void());
|
||||||
|
|
||||||
|
@ -14,17 +14,20 @@
|
|||||||
|
|
||||||
namespace webrtc {
|
namespace webrtc {
|
||||||
|
|
||||||
Vad::Vad(enum Aggressiveness mode) : handle_(nullptr), aggressiveness_(mode) {
|
namespace {
|
||||||
|
|
||||||
|
class VadImpl final : public Vad {
|
||||||
|
public:
|
||||||
|
explicit VadImpl(Aggressiveness aggressiveness)
|
||||||
|
: handle_(nullptr), aggressiveness_(aggressiveness) {
|
||||||
Reset();
|
Reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
Vad::~Vad() {
|
~VadImpl() override { WebRtcVad_Free(handle_); }
|
||||||
WebRtcVad_Free(handle_);
|
|
||||||
}
|
|
||||||
|
|
||||||
enum Vad::Activity Vad::VoiceActivity(const int16_t* audio,
|
Activity VoiceActivity(const int16_t* audio,
|
||||||
size_t num_samples,
|
size_t num_samples,
|
||||||
int sample_rate_hz) {
|
int sample_rate_hz) override {
|
||||||
int ret = WebRtcVad_Process(handle_, sample_rate_hz, audio, num_samples);
|
int ret = WebRtcVad_Process(handle_, sample_rate_hz, audio, num_samples);
|
||||||
switch (ret) {
|
switch (ret) {
|
||||||
case 0:
|
case 0:
|
||||||
@ -35,15 +38,26 @@ enum Vad::Activity Vad::VoiceActivity(const int16_t* audio,
|
|||||||
DCHECK(false) << "WebRtcVad_Process returned an error.";
|
DCHECK(false) << "WebRtcVad_Process returned an error.";
|
||||||
return kError;
|
return kError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Vad::Reset() {
|
void Reset() override {
|
||||||
if (handle_)
|
if (handle_)
|
||||||
WebRtcVad_Free(handle_);
|
WebRtcVad_Free(handle_);
|
||||||
handle_ = WebRtcVad_Create();
|
handle_ = WebRtcVad_Create();
|
||||||
CHECK(handle_);
|
CHECK(handle_);
|
||||||
CHECK_EQ(WebRtcVad_Init(handle_), 0);
|
CHECK_EQ(WebRtcVad_Init(handle_), 0);
|
||||||
CHECK_EQ(WebRtcVad_set_mode(handle_, aggressiveness_), 0);
|
CHECK_EQ(WebRtcVad_set_mode(handle_, aggressiveness_), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
VadInst* handle_;
|
||||||
|
Aggressiveness aggressiveness_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
rtc::scoped_ptr<Vad> CreateVad(Vad::Aggressiveness aggressiveness) {
|
||||||
|
return rtc::scoped_ptr<Vad>(new VadImpl(aggressiveness));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
@ -54,7 +54,8 @@ AudioEncoderCng::AudioEncoderCng(const Config& config)
|
|||||||
num_cng_coefficients_(config.num_cng_coefficients),
|
num_cng_coefficients_(config.num_cng_coefficients),
|
||||||
sid_frame_interval_ms_(config.sid_frame_interval_ms),
|
sid_frame_interval_ms_(config.sid_frame_interval_ms),
|
||||||
last_frame_active_(true),
|
last_frame_active_(true),
|
||||||
vad_(config.vad ? config.vad : new Vad(config.vad_mode)) {
|
vad_(config.vad ? rtc_make_scoped_ptr(config.vad)
|
||||||
|
: CreateVad(config.vad_mode)) {
|
||||||
CHECK(config.IsOk()) << "Invalid configuration.";
|
CHECK(config.IsOk()) << "Invalid configuration.";
|
||||||
cng_inst_ = CreateCngInst(SampleRateHz(), sid_frame_interval_ms_,
|
cng_inst_ = CreateCngInst(SampleRateHz(), sid_frame_interval_ms_,
|
||||||
num_cng_coefficients_);
|
num_cng_coefficients_);
|
||||||
|
@ -34,7 +34,7 @@ static const int kCngPayloadType = 18;
|
|||||||
class AudioEncoderCngTest : public ::testing::Test {
|
class AudioEncoderCngTest : public ::testing::Test {
|
||||||
protected:
|
protected:
|
||||||
AudioEncoderCngTest()
|
AudioEncoderCngTest()
|
||||||
: mock_vad_(new MockVad(Vad::kVadNormal)),
|
: mock_vad_(new MockVad),
|
||||||
timestamp_(4711),
|
timestamp_(4711),
|
||||||
num_audio_samples_10ms_(0),
|
num_audio_samples_10ms_(0),
|
||||||
sample_rate_hz_(8000) {
|
sample_rate_hz_(8000) {
|
||||||
|
Reference in New Issue
Block a user