blob: 6dd14d8b55ca996f4d239ce3c4d0d786deadc8d6 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
bjornv@webrtc.org152c34c2012-01-23 12:36:462 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:253 *
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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "common_audio/vad/include/webrtc_vad.h"
bjornv@webrtc.orgb1c32762012-06-12 08:19:2412
niklase@google.com470e71d2011-07-07 08:21:2513#include <stdlib.h>
14#include <string.h>
15
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "common_audio/signal_processing/include/signal_processing_library.h"
17#include "common_audio/vad/vad_core.h"
niklase@google.com470e71d2011-07-07 08:21:2518
19static const int kInitCheck = 42;
tina.legrand@webrtc.orgef433572012-10-15 17:46:1920static const int kValidRates[] = { 8000, 16000, 32000, 48000 };
bjornv@webrtc.orgb1c32762012-06-12 08:19:2421static const size_t kRatesSize = sizeof(kValidRates) / sizeof(*kValidRates);
22static const int kMaxFrameLengthMs = 30;
niklase@google.com470e71d2011-07-07 08:21:2523
Artem Titova19f0c72022-04-13 11:44:3924VadInst* WebRtcVad_Create(void) {
Bjorn Volckerde4703c2015-05-27 05:22:5825 VadInstT* self = (VadInstT*)malloc(sizeof(VadInstT));
niklase@google.com470e71d2011-07-07 08:21:2526
bjornv@webrtc.org26e8a582012-01-31 14:42:5027 self->init_flag = 0;
niklase@google.com470e71d2011-07-07 08:21:2528
Bjorn Volckerde4703c2015-05-27 05:22:5829 return (VadInst*)self;
niklase@google.com470e71d2011-07-07 08:21:2530}
31
bjornv@webrtc.org2a796722014-04-22 04:45:3532void WebRtcVad_Free(VadInst* handle) {
bjornv@webrtc.org26e8a582012-01-31 14:42:5033 free(handle);
niklase@google.com470e71d2011-07-07 08:21:2534}
35
bjornv@webrtc.orged700db2012-03-12 12:17:2636// TODO(bjornv): Move WebRtcVad_InitCore() code here.
bjornv@webrtc.org2a4dcd72012-01-25 12:18:1237int WebRtcVad_Init(VadInst* handle) {
38 // Initialize the core VAD component.
39 return WebRtcVad_InitCore((VadInstT*) handle);
niklase@google.com470e71d2011-07-07 08:21:2540}
41
bjornv@webrtc.org78f0cdc2012-03-27 11:06:2942// TODO(bjornv): Move WebRtcVad_set_mode_core() code here.
43int WebRtcVad_set_mode(VadInst* handle, int mode) {
44 VadInstT* self = (VadInstT*) handle;
niklase@google.com470e71d2011-07-07 08:21:2545
bjornv@webrtc.org78f0cdc2012-03-27 11:06:2946 if (handle == NULL) {
47 return -1;
48 }
49 if (self->init_flag != kInitCheck) {
50 return -1;
51 }
niklase@google.com470e71d2011-07-07 08:21:2552
bjornv@webrtc.org78f0cdc2012-03-27 11:06:2953 return WebRtcVad_set_mode_core(self, mode);
niklase@google.com470e71d2011-07-07 08:21:2554}
55
andrew@webrtc.org65f93382014-04-30 16:44:1356int WebRtcVad_Process(VadInst* handle, int fs, const int16_t* audio_frame,
Peter Kastingdce40cf2015-08-24 21:52:2357 size_t frame_length) {
bjornv@webrtc.orgb38fca12012-06-19 11:03:3258 int vad = -1;
bjornv@webrtc.orgb1c32762012-06-12 08:19:2459 VadInstT* self = (VadInstT*) handle;
niklase@google.com470e71d2011-07-07 08:21:2560
bjornv@webrtc.orgb1c32762012-06-12 08:19:2461 if (handle == NULL) {
62 return -1;
63 }
niklase@google.com470e71d2011-07-07 08:21:2564
bjornv@webrtc.orgb1c32762012-06-12 08:19:2465 if (self->init_flag != kInitCheck) {
66 return -1;
67 }
68 if (audio_frame == NULL) {
69 return -1;
70 }
71 if (WebRtcVad_ValidRateAndFrameLength(fs, frame_length) != 0) {
72 return -1;
73 }
niklase@google.com470e71d2011-07-07 08:21:2574
tina.legrand@webrtc.orgef433572012-10-15 17:46:1975 if (fs == 48000) {
76 vad = WebRtcVad_CalcVad48khz(self, audio_frame, frame_length);
77 } else if (fs == 32000) {
bjornv@webrtc.orgb1c32762012-06-12 08:19:2478 vad = WebRtcVad_CalcVad32khz(self, audio_frame, frame_length);
79 } else if (fs == 16000) {
80 vad = WebRtcVad_CalcVad16khz(self, audio_frame, frame_length);
81 } else if (fs == 8000) {
82 vad = WebRtcVad_CalcVad8khz(self, audio_frame, frame_length);
83 }
niklase@google.com470e71d2011-07-07 08:21:2584
bjornv@webrtc.orgb1c32762012-06-12 08:19:2485 if (vad > 0) {
86 vad = 1;
87 }
88 return vad;
89}
90
Peter Kastingdce40cf2015-08-24 21:52:2391int WebRtcVad_ValidRateAndFrameLength(int rate, size_t frame_length) {
bjornv@webrtc.orgb1c32762012-06-12 08:19:2492 int return_value = -1;
93 size_t i;
94 int valid_length_ms;
Peter Kastingdce40cf2015-08-24 21:52:2395 size_t valid_length;
bjornv@webrtc.orgb1c32762012-06-12 08:19:2496
97 // We only allow 10, 20 or 30 ms frames. Loop through valid frame rates and
98 // see if we have a matching pair.
99 for (i = 0; i < kRatesSize; i++) {
100 if (kValidRates[i] == rate) {
101 for (valid_length_ms = 10; valid_length_ms <= kMaxFrameLengthMs;
102 valid_length_ms += 10) {
Peter Kastingdce40cf2015-08-24 21:52:23103 valid_length = (size_t)(kValidRates[i] / 1000 * valid_length_ms);
bjornv@webrtc.orgb1c32762012-06-12 08:19:24104 if (frame_length == valid_length) {
105 return_value = 0;
106 break;
niklase@google.com470e71d2011-07-07 08:21:25107 }
bjornv@webrtc.orgb1c32762012-06-12 08:19:24108 }
109 break;
niklase@google.com470e71d2011-07-07 08:21:25110 }
bjornv@webrtc.orgb1c32762012-06-12 08:19:24111 }
niklase@google.com470e71d2011-07-07 08:21:25112
bjornv@webrtc.orgb1c32762012-06-12 08:19:24113 return return_value;
niklase@google.com470e71d2011-07-07 08:21:25114}