blob: 7fc4d65ed27c3ffacfa7744523302bb896d27a13 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
bjornv@webrtc.org152c34c2012-01-23 12:36:46 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 06:47:31 +020011#include "common_audio/vad/include/webrtc_vad.h"
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000012
niklase@google.com470e71d2011-07-07 08:21:25 +000013#include <stdlib.h>
14#include <string.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "common_audio/signal_processing/include/signal_processing_library.h"
17#include "common_audio/vad/vad_core.h"
Mirko Bonadei71207422017-09-15 13:58:09 +020018#include "typedefs.h" // NOLINT(build/include)
niklase@google.com470e71d2011-07-07 08:21:25 +000019
20static const int kInitCheck = 42;
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +000021static const int kValidRates[] = { 8000, 16000, 32000, 48000 };
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000022static const size_t kRatesSize = sizeof(kValidRates) / sizeof(*kValidRates);
23static const int kMaxFrameLengthMs = 30;
niklase@google.com470e71d2011-07-07 08:21:25 +000024
Bjorn Volckerde4703c2015-05-27 07:22:58 +020025VadInst* WebRtcVad_Create() {
26 VadInstT* self = (VadInstT*)malloc(sizeof(VadInstT));
niklase@google.com470e71d2011-07-07 08:21:25 +000027
kma@webrtc.orgac4d70d2012-10-05 00:19:01 +000028 WebRtcSpl_Init();
bjornv@webrtc.org26e8a582012-01-31 14:42:50 +000029 self->init_flag = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000030
Bjorn Volckerde4703c2015-05-27 07:22:58 +020031 return (VadInst*)self;
niklase@google.com470e71d2011-07-07 08:21:25 +000032}
33
bjornv@webrtc.org2a796722014-04-22 04:45:35 +000034void WebRtcVad_Free(VadInst* handle) {
bjornv@webrtc.org26e8a582012-01-31 14:42:50 +000035 free(handle);
niklase@google.com470e71d2011-07-07 08:21:25 +000036}
37
bjornv@webrtc.orged700db2012-03-12 12:17:26 +000038// TODO(bjornv): Move WebRtcVad_InitCore() code here.
bjornv@webrtc.org2a4dcd72012-01-25 12:18:12 +000039int WebRtcVad_Init(VadInst* handle) {
40 // Initialize the core VAD component.
41 return WebRtcVad_InitCore((VadInstT*) handle);
niklase@google.com470e71d2011-07-07 08:21:25 +000042}
43
bjornv@webrtc.org78f0cdc2012-03-27 11:06:29 +000044// TODO(bjornv): Move WebRtcVad_set_mode_core() code here.
45int WebRtcVad_set_mode(VadInst* handle, int mode) {
46 VadInstT* self = (VadInstT*) handle;
niklase@google.com470e71d2011-07-07 08:21:25 +000047
bjornv@webrtc.org78f0cdc2012-03-27 11:06:29 +000048 if (handle == NULL) {
49 return -1;
50 }
51 if (self->init_flag != kInitCheck) {
52 return -1;
53 }
niklase@google.com470e71d2011-07-07 08:21:25 +000054
bjornv@webrtc.org78f0cdc2012-03-27 11:06:29 +000055 return WebRtcVad_set_mode_core(self, mode);
niklase@google.com470e71d2011-07-07 08:21:25 +000056}
57
andrew@webrtc.org65f93382014-04-30 16:44:13 +000058int WebRtcVad_Process(VadInst* handle, int fs, const int16_t* audio_frame,
Peter Kastingdce40cf2015-08-24 14:52:23 -070059 size_t frame_length) {
bjornv@webrtc.orgb38fca12012-06-19 11:03:32 +000060 int vad = -1;
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000061 VadInstT* self = (VadInstT*) handle;
niklase@google.com470e71d2011-07-07 08:21:25 +000062
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000063 if (handle == NULL) {
64 return -1;
65 }
niklase@google.com470e71d2011-07-07 08:21:25 +000066
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000067 if (self->init_flag != kInitCheck) {
68 return -1;
69 }
70 if (audio_frame == NULL) {
71 return -1;
72 }
73 if (WebRtcVad_ValidRateAndFrameLength(fs, frame_length) != 0) {
74 return -1;
75 }
niklase@google.com470e71d2011-07-07 08:21:25 +000076
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +000077 if (fs == 48000) {
78 vad = WebRtcVad_CalcVad48khz(self, audio_frame, frame_length);
79 } else if (fs == 32000) {
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000080 vad = WebRtcVad_CalcVad32khz(self, audio_frame, frame_length);
81 } else if (fs == 16000) {
82 vad = WebRtcVad_CalcVad16khz(self, audio_frame, frame_length);
83 } else if (fs == 8000) {
84 vad = WebRtcVad_CalcVad8khz(self, audio_frame, frame_length);
85 }
niklase@google.com470e71d2011-07-07 08:21:25 +000086
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000087 if (vad > 0) {
88 vad = 1;
89 }
90 return vad;
91}
92
Peter Kastingdce40cf2015-08-24 14:52:23 -070093int WebRtcVad_ValidRateAndFrameLength(int rate, size_t frame_length) {
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000094 int return_value = -1;
95 size_t i;
96 int valid_length_ms;
Peter Kastingdce40cf2015-08-24 14:52:23 -070097 size_t valid_length;
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000098
99 // We only allow 10, 20 or 30 ms frames. Loop through valid frame rates and
100 // see if we have a matching pair.
101 for (i = 0; i < kRatesSize; i++) {
102 if (kValidRates[i] == rate) {
103 for (valid_length_ms = 10; valid_length_ms <= kMaxFrameLengthMs;
104 valid_length_ms += 10) {
Peter Kastingdce40cf2015-08-24 14:52:23 -0700105 valid_length = (size_t)(kValidRates[i] / 1000 * valid_length_ms);
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000106 if (frame_length == valid_length) {
107 return_value = 0;
108 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000109 }
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000110 }
111 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 }
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000113 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000115 return return_value;
niklase@google.com470e71d2011-07-07 08:21:25 +0000116}