1788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org/* 2788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org * 4788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org * Use of this source code is governed by a BSD-style license 5788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org * that can be found in the LICENSE file in the root of the source 6788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org * tree. An additional intellectual property rights grant can be found 7788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org * in the file PATENTS. All contributing project authors may 8788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org * be found in the AUTHORS file in the root of the source tree. 9788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org */ 10788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 11ecf6b81644af9823dbff5c24a3d5b9bb596c0d5baluebs#include "webrtc/modules/audio_processing/vad/standalone_vad.h" 12788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 13788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org#include <assert.h> 14788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 15ff761fba8274d93bd73e76c8b8a1f2d0776dd840Henrik Kjellander#include "webrtc/modules/include/module_common_types.h" 16ff761fba8274d93bd73e76c8b8a1f2d0776dd840Henrik Kjellander#include "webrtc/modules/utility/include/audio_frame_operations.h" 17788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org#include "webrtc/typedefs.h" 18788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 19788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.orgnamespace webrtc { 20788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 21788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.orgstatic const int kDefaultStandaloneVadMode = 3; 22788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 23788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.orgStandaloneVad::StandaloneVad(VadInst* vad) 24ecf6b81644af9823dbff5c24a3d5b9bb596c0d5baluebs : vad_(vad), buffer_(), index_(0), mode_(kDefaultStandaloneVadMode) { 25ecf6b81644af9823dbff5c24a3d5b9bb596c0d5baluebs} 26788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 27788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.orgStandaloneVad::~StandaloneVad() { 28788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org WebRtcVad_Free(vad_); 29788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org} 30788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 31788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.orgStandaloneVad* StandaloneVad::Create() { 32de4703c5d1290da22feeb708fe915179884e210fBjorn Volcker VadInst* vad = WebRtcVad_Create(); 33de4703c5d1290da22feeb708fe915179884e210fBjorn Volcker if (!vad) 34de4703c5d1290da22feeb708fe915179884e210fBjorn Volcker return nullptr; 35788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 36788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org int err = WebRtcVad_Init(vad); 37788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org err |= WebRtcVad_set_mode(vad, kDefaultStandaloneVadMode); 38788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org if (err != 0) { 39788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org WebRtcVad_Free(vad); 40de4703c5d1290da22feeb708fe915179884e210fBjorn Volcker return nullptr; 41788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org } 42788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org return new StandaloneVad(vad); 43788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org} 44788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 45dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kastingint StandaloneVad::AddAudio(const int16_t* data, size_t length) { 46788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org if (length != kLength10Ms) 47788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org return -1; 48788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 49788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org if (index_ + length > kLength10Ms * kMaxNum10msFrames) 50788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org // Reset the buffer if it's full. 51788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org // TODO(ajm): Instead, consider just processing every 10 ms frame. Then we 52788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org // can forgo the buffering. 53788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org index_ = 0; 54788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 55788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org memcpy(&buffer_[index_], data, sizeof(int16_t) * length); 56788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org index_ += length; 57788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org return 0; 58788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org} 59788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 60dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kastingint StandaloneVad::GetActivity(double* p, size_t length_p) { 61788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org if (index_ == 0) 62788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org return -1; 63788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 64dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting const size_t num_frames = index_ / kLength10Ms; 65788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org if (num_frames > length_p) 66788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org return -1; 67788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org assert(WebRtcVad_ValidRateAndFrameLength(kSampleRateHz, index_) == 0); 68788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 69788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org int activity = WebRtcVad_Process(vad_, kSampleRateHz, buffer_, index_); 70788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org if (activity < 0) 71788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org return -1; 72788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org else if (activity == 0) 73788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org p[0] = 0.01; // Arbitrary but small and non-zero. 74788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org else 75788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org p[0] = 0.5; // 0.5 is neutral values when combinned by other probabilities. 76dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting for (size_t n = 1; n < num_frames; n++) 77788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org p[n] = p[0]; 78788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org // Reset the buffer to start from the beginning. 79788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org index_ = 0; 80788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org return activity; 81788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org} 82788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 83788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.orgint StandaloneVad::set_mode(int mode) { 84788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org if (mode < 0 || mode > 3) 85788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org return -1; 86788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org if (WebRtcVad_set_mode(vad_, mode) != 0) 87788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org return -1; 88788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 89788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org mode_ = mode; 90788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org return 0; 91788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org} 92788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org 93788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org} // namespace webrtc 94