Line data Source code
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 :
11 : #include "webrtc/common_audio/vad/include/webrtc_vad.h"
12 :
13 : #include <stdlib.h>
14 : #include <string.h>
15 :
16 : #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"
19 :
20 : static const int kInitCheck = 42;
21 : static const int kValidRates[] = { 8000, 16000, 32000, 48000 };
22 : static const size_t kRatesSize = sizeof(kValidRates) / sizeof(*kValidRates);
23 : static const int kMaxFrameLengthMs = 30;
24 :
25 0 : VadInst* WebRtcVad_Create() {
26 0 : VadInstT* self = (VadInstT*)malloc(sizeof(VadInstT));
27 :
28 0 : WebRtcSpl_Init();
29 0 : self->init_flag = 0;
30 :
31 0 : return (VadInst*)self;
32 : }
33 :
34 0 : void WebRtcVad_Free(VadInst* handle) {
35 0 : free(handle);
36 0 : }
37 :
38 : // TODO(bjornv): Move WebRtcVad_InitCore() code here.
39 0 : int WebRtcVad_Init(VadInst* handle) {
40 : // Initialize the core VAD component.
41 0 : return WebRtcVad_InitCore((VadInstT*) handle);
42 : }
43 :
44 : // TODO(bjornv): Move WebRtcVad_set_mode_core() code here.
45 0 : int WebRtcVad_set_mode(VadInst* handle, int mode) {
46 0 : VadInstT* self = (VadInstT*) handle;
47 :
48 0 : if (handle == NULL) {
49 0 : return -1;
50 : }
51 0 : if (self->init_flag != kInitCheck) {
52 0 : return -1;
53 : }
54 :
55 0 : return WebRtcVad_set_mode_core(self, mode);
56 : }
57 :
58 0 : int WebRtcVad_Process(VadInst* handle, int fs, const int16_t* audio_frame,
59 : size_t frame_length) {
60 0 : int vad = -1;
61 0 : VadInstT* self = (VadInstT*) handle;
62 :
63 0 : if (handle == NULL) {
64 0 : return -1;
65 : }
66 :
67 0 : if (self->init_flag != kInitCheck) {
68 0 : return -1;
69 : }
70 0 : if (audio_frame == NULL) {
71 0 : return -1;
72 : }
73 0 : if (WebRtcVad_ValidRateAndFrameLength(fs, frame_length) != 0) {
74 0 : return -1;
75 : }
76 :
77 0 : if (fs == 48000) {
78 0 : vad = WebRtcVad_CalcVad48khz(self, audio_frame, frame_length);
79 0 : } else if (fs == 32000) {
80 0 : vad = WebRtcVad_CalcVad32khz(self, audio_frame, frame_length);
81 0 : } else if (fs == 16000) {
82 0 : vad = WebRtcVad_CalcVad16khz(self, audio_frame, frame_length);
83 0 : } else if (fs == 8000) {
84 0 : vad = WebRtcVad_CalcVad8khz(self, audio_frame, frame_length);
85 : }
86 :
87 0 : if (vad > 0) {
88 0 : vad = 1;
89 : }
90 0 : return vad;
91 : }
92 :
93 0 : int WebRtcVad_ValidRateAndFrameLength(int rate, size_t frame_length) {
94 0 : int return_value = -1;
95 : size_t i;
96 : int valid_length_ms;
97 : size_t valid_length;
98 :
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 0 : for (i = 0; i < kRatesSize; i++) {
102 0 : if (kValidRates[i] == rate) {
103 0 : for (valid_length_ms = 10; valid_length_ms <= kMaxFrameLengthMs;
104 0 : valid_length_ms += 10) {
105 0 : valid_length = (size_t)(kValidRates[i] / 1000 * valid_length_ms);
106 0 : if (frame_length == valid_length) {
107 0 : return_value = 0;
108 0 : break;
109 : }
110 : }
111 0 : break;
112 : }
113 : }
114 :
115 0 : return return_value;
116 : }
|