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