| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #include "common_audio/vad/include/vad.h" |
| 12 |
| 13 | #include <memory> |
| 14 |
| 15 | #include "common_audio/vad/include/webrtc_vad.h" |
| 16 | #include "rtc_base/checks.h" |
| 17 |
| 18 | namespace webrtc { |
| 19 |
| 20 | namespace { |
| 21 |
| 22 | class VadImpl final : public Vad { |
| 23 | public: |
| 24 | explicit VadImpl(Aggressiveness aggressiveness) |
| 25 | : handle_(nullptr), aggressiveness_(aggressiveness) { |
| 26 | Reset(); |
| 27 | } |
| 28 |
| 29 | ~VadImpl() override { WebRtcVad_Free(handle_); } |
| 30 |
| 31 | Activity VoiceActivity(const int16_t* audio, |
| 32 | size_t num_samples, |
| 33 | int sample_rate_hz) override { |
| 34 | int ret = WebRtcVad_Process(handle_, sample_rate_hz, audio, num_samples); |
| 35 | switch (ret) { |
| 36 | case 0: |
| 37 | return kPassive; |
| 38 | case 1: |
| 39 | return kActive; |
| 40 | default: |
| 41 | RTC_DCHECK_NOTREACHED() << "WebRtcVad_Process returned an error."; |
| 42 | return kError; |
| 43 | } |
| 44 | } |
| 45 |
| 46 | void Reset() override { |
| 47 | if (handle_) |
| 48 | WebRtcVad_Free(handle_); |
| 49 | handle_ = WebRtcVad_Create(); |
| 50 | RTC_CHECK(handle_); |
| 51 | RTC_CHECK_EQ(WebRtcVad_Init(handle_), 0); |
| 52 | RTC_CHECK_EQ(WebRtcVad_set_mode(handle_, aggressiveness_), 0); |
| 53 | } |
| 54 |
| 55 | private: |
| 56 | VadInst* handle_; |
| 57 | Aggressiveness aggressiveness_; |
| 58 | }; |
| 59 |
| 60 | } // namespace |
| 61 |
| 62 | std::unique_ptr<Vad> CreateVad(Vad::Aggressiveness aggressiveness) { |
| 63 | return std::unique_ptr<Vad>(new VadImpl(aggressiveness)); |
| 64 | } |
| 65 |
| 66 | } // namespace webrtc |
| 67 |