blob: fcf03f805813ca4264579511d11220a662b3c453 [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
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000011#include "webrtc/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
pbos@webrtc.orgaa30bb72013-05-27 09:49:58 +000016#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
17#include "webrtc/common_audio/vad/vad_core.h"
18#include "webrtc/typedefs.h"
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));
bjornv@webrtc.org26e8a582012-01-31 14:42:50 +000027 if (self == NULL) {
Bjorn Volckerde4703c2015-05-27 07:22:58 +020028 return NULL;
bjornv@webrtc.org26e8a582012-01-31 14:42:50 +000029 }
niklase@google.com470e71d2011-07-07 08:21:25 +000030
kma@webrtc.orgac4d70d2012-10-05 00:19:01 +000031 WebRtcSpl_Init();
bjornv@webrtc.org26e8a582012-01-31 14:42:50 +000032 self->init_flag = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000033
Bjorn Volckerde4703c2015-05-27 07:22:58 +020034 return (VadInst*)self;
niklase@google.com470e71d2011-07-07 08:21:25 +000035}
36
bjornv@webrtc.org2a796722014-04-22 04:45:35 +000037void WebRtcVad_Free(VadInst* handle) {
bjornv@webrtc.org26e8a582012-01-31 14:42:50 +000038 free(handle);
niklase@google.com470e71d2011-07-07 08:21:25 +000039}
40
bjornv@webrtc.orged700db2012-03-12 12:17:26 +000041// TODO(bjornv): Move WebRtcVad_InitCore() code here.
bjornv@webrtc.org2a4dcd72012-01-25 12:18:12 +000042int WebRtcVad_Init(VadInst* handle) {
43 // Initialize the core VAD component.
44 return WebRtcVad_InitCore((VadInstT*) handle);
niklase@google.com470e71d2011-07-07 08:21:25 +000045}
46
bjornv@webrtc.org78f0cdc2012-03-27 11:06:29 +000047// TODO(bjornv): Move WebRtcVad_set_mode_core() code here.
48int WebRtcVad_set_mode(VadInst* handle, int mode) {
49 VadInstT* self = (VadInstT*) handle;
niklase@google.com470e71d2011-07-07 08:21:25 +000050
bjornv@webrtc.org78f0cdc2012-03-27 11:06:29 +000051 if (handle == NULL) {
52 return -1;
53 }
54 if (self->init_flag != kInitCheck) {
55 return -1;
56 }
niklase@google.com470e71d2011-07-07 08:21:25 +000057
bjornv@webrtc.org78f0cdc2012-03-27 11:06:29 +000058 return WebRtcVad_set_mode_core(self, mode);
niklase@google.com470e71d2011-07-07 08:21:25 +000059}
60
andrew@webrtc.org65f93382014-04-30 16:44:13 +000061int WebRtcVad_Process(VadInst* handle, int fs, const int16_t* audio_frame,
bjornv@webrtc.orgb38fca12012-06-19 11:03:32 +000062 int frame_length) {
63 int vad = -1;
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000064 VadInstT* self = (VadInstT*) handle;
niklase@google.com470e71d2011-07-07 08:21:25 +000065
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000066 if (handle == NULL) {
67 return -1;
68 }
niklase@google.com470e71d2011-07-07 08:21:25 +000069
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000070 if (self->init_flag != kInitCheck) {
71 return -1;
72 }
73 if (audio_frame == NULL) {
74 return -1;
75 }
76 if (WebRtcVad_ValidRateAndFrameLength(fs, frame_length) != 0) {
77 return -1;
78 }
niklase@google.com470e71d2011-07-07 08:21:25 +000079
tina.legrand@webrtc.orgef433572012-10-15 17:46:19 +000080 if (fs == 48000) {
81 vad = WebRtcVad_CalcVad48khz(self, audio_frame, frame_length);
82 } else if (fs == 32000) {
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000083 vad = WebRtcVad_CalcVad32khz(self, audio_frame, frame_length);
84 } else if (fs == 16000) {
85 vad = WebRtcVad_CalcVad16khz(self, audio_frame, frame_length);
86 } else if (fs == 8000) {
87 vad = WebRtcVad_CalcVad8khz(self, audio_frame, frame_length);
88 }
niklase@google.com470e71d2011-07-07 08:21:25 +000089
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +000090 if (vad > 0) {
91 vad = 1;
92 }
93 return vad;
94}
95
96int WebRtcVad_ValidRateAndFrameLength(int rate, int frame_length) {
97 int return_value = -1;
98 size_t i;
99 int valid_length_ms;
100 int valid_length;
101
102 // We only allow 10, 20 or 30 ms frames. Loop through valid frame rates and
103 // see if we have a matching pair.
104 for (i = 0; i < kRatesSize; i++) {
105 if (kValidRates[i] == rate) {
106 for (valid_length_ms = 10; valid_length_ms <= kMaxFrameLengthMs;
107 valid_length_ms += 10) {
108 valid_length = (kValidRates[i] / 1000 * valid_length_ms);
109 if (frame_length == valid_length) {
110 return_value = 0;
111 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000112 }
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000113 }
114 break;
niklase@google.com470e71d2011-07-07 08:21:25 +0000115 }
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000116 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
bjornv@webrtc.orgb1c32762012-06-12 08:19:24 +0000118 return return_value;
niklase@google.com470e71d2011-07-07 08:21:25 +0000119}