pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 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 | |
aluebs | ecf6b81 | 2015-06-25 12:28:48 -0700 | [diff] [blame] | 11 | #include "webrtc/modules/audio_processing/vad/standalone_vad.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 12 | |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 13 | #include "webrtc/base/checks.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 14 | #include "webrtc/modules/include/module_common_types.h" |
| 15 | #include "webrtc/modules/utility/include/audio_frame_operations.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 16 | #include "webrtc/typedefs.h" |
| 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | static const int kDefaultStandaloneVadMode = 3; |
| 21 | |
| 22 | StandaloneVad::StandaloneVad(VadInst* vad) |
aluebs | ecf6b81 | 2015-06-25 12:28:48 -0700 | [diff] [blame] | 23 | : vad_(vad), buffer_(), index_(0), mode_(kDefaultStandaloneVadMode) { |
| 24 | } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 25 | |
| 26 | StandaloneVad::~StandaloneVad() { |
| 27 | WebRtcVad_Free(vad_); |
| 28 | } |
| 29 | |
| 30 | StandaloneVad* StandaloneVad::Create() { |
Bjorn Volcker | de4703c | 2015-05-27 07:22:58 +0200 | [diff] [blame] | 31 | VadInst* vad = WebRtcVad_Create(); |
| 32 | if (!vad) |
| 33 | return nullptr; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 34 | |
| 35 | int err = WebRtcVad_Init(vad); |
| 36 | err |= WebRtcVad_set_mode(vad, kDefaultStandaloneVadMode); |
| 37 | if (err != 0) { |
| 38 | WebRtcVad_Free(vad); |
Bjorn Volcker | de4703c | 2015-05-27 07:22:58 +0200 | [diff] [blame] | 39 | return nullptr; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 40 | } |
| 41 | return new StandaloneVad(vad); |
| 42 | } |
| 43 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 44 | int StandaloneVad::AddAudio(const int16_t* data, size_t length) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 45 | if (length != kLength10Ms) |
| 46 | return -1; |
| 47 | |
| 48 | if (index_ + length > kLength10Ms * kMaxNum10msFrames) |
| 49 | // Reset the buffer if it's full. |
| 50 | // TODO(ajm): Instead, consider just processing every 10 ms frame. Then we |
| 51 | // can forgo the buffering. |
| 52 | index_ = 0; |
| 53 | |
| 54 | memcpy(&buffer_[index_], data, sizeof(int16_t) * length); |
| 55 | index_ += length; |
| 56 | return 0; |
| 57 | } |
| 58 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 59 | int StandaloneVad::GetActivity(double* p, size_t length_p) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 60 | if (index_ == 0) |
| 61 | return -1; |
| 62 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 63 | const size_t num_frames = index_ / kLength10Ms; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 64 | if (num_frames > length_p) |
| 65 | return -1; |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 66 | RTC_DCHECK_EQ(0, WebRtcVad_ValidRateAndFrameLength(kSampleRateHz, index_)); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 67 | |
| 68 | int activity = WebRtcVad_Process(vad_, kSampleRateHz, buffer_, index_); |
| 69 | if (activity < 0) |
| 70 | return -1; |
| 71 | else if (activity == 0) |
| 72 | p[0] = 0.01; // Arbitrary but small and non-zero. |
| 73 | else |
| 74 | p[0] = 0.5; // 0.5 is neutral values when combinned by other probabilities. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 75 | for (size_t n = 1; n < num_frames; n++) |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 76 | p[n] = p[0]; |
| 77 | // Reset the buffer to start from the beginning. |
| 78 | index_ = 0; |
| 79 | return activity; |
| 80 | } |
| 81 | |
| 82 | int StandaloneVad::set_mode(int mode) { |
| 83 | if (mode < 0 || mode > 3) |
| 84 | return -1; |
| 85 | if (WebRtcVad_set_mode(vad_, mode) != 0) |
| 86 | return -1; |
| 87 | |
| 88 | mode_ = mode; |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | } // namespace webrtc |