OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/common_audio/vad/include/vad.h" | 11 #include "webrtc/common_audio/vad/include/vad.h" |
12 | 12 |
13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
14 | 14 |
15 namespace webrtc { | 15 namespace webrtc { |
16 | 16 |
17 Vad::Vad(enum Aggressiveness mode) { | 17 Vad::Vad(enum Aggressiveness mode) : handle_(nullptr), aggressiveness_(mode) { |
18 handle_ = WebRtcVad_Create(); | 18 Reset(); |
19 CHECK(handle_); | |
20 CHECK_EQ(WebRtcVad_Init(handle_), 0); | |
21 CHECK_EQ(WebRtcVad_set_mode(handle_, mode), 0); | |
22 } | 19 } |
23 | 20 |
24 Vad::~Vad() { | 21 Vad::~Vad() { |
25 WebRtcVad_Free(handle_); | 22 WebRtcVad_Free(handle_); |
26 } | 23 } |
27 | 24 |
28 enum Vad::Activity Vad::VoiceActivity(const int16_t* audio, | 25 enum Vad::Activity Vad::VoiceActivity(const int16_t* audio, |
29 size_t num_samples, | 26 size_t num_samples, |
30 int sample_rate_hz) { | 27 int sample_rate_hz) { |
31 int ret = WebRtcVad_Process(handle_, sample_rate_hz, audio, num_samples); | 28 int ret = WebRtcVad_Process(handle_, sample_rate_hz, audio, num_samples); |
32 switch (ret) { | 29 switch (ret) { |
33 case 0: | 30 case 0: |
34 return kPassive; | 31 return kPassive; |
35 case 1: | 32 case 1: |
36 return kActive; | 33 return kActive; |
37 default: | 34 default: |
38 DCHECK(false) << "WebRtcVad_Process returned an error."; | 35 DCHECK(false) << "WebRtcVad_Process returned an error."; |
39 return kError; | 36 return kError; |
40 } | 37 } |
41 } | 38 } |
42 | 39 |
| 40 void Vad::Reset() { |
| 41 if (handle_) |
| 42 WebRtcVad_Free(handle_); |
| 43 handle_ = WebRtcVad_Create(); |
| 44 CHECK(handle_); |
| 45 CHECK_EQ(WebRtcVad_Init(handle_), 0); |
| 46 CHECK_EQ(WebRtcVad_set_mode(handle_, aggressiveness_), 0); |
| 47 } |
| 48 |
43 } // namespace webrtc | 49 } // namespace webrtc |
OLD | NEW |