1 /* 2 * Copyright (c) 2011 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 12 /* 13 * This file includes the VAD API calls. For a specific function call description, 14 * see webrtc_vad.h 15 */ 16 17 #include <stdlib.h> 18 #include <string.h> 19 20 #include "webrtc_vad.h" 21 #include "vad_core.h" 22 23 static const int kInitCheck = 42; 24 25 WebRtc_Word16 WebRtcVad_get_version(char *version, size_t size_bytes) 26 { 27 const char my_version[] = "VAD 1.2.0"; 28 29 if (version == NULL) 30 { 31 return -1; 32 } 33 34 if (size_bytes < sizeof(my_version)) 35 { 36 return -1; 37 } 38 39 memcpy(version, my_version, sizeof(my_version)); 40 return 0; 41 } 42 43 WebRtc_Word16 WebRtcVad_AssignSize(int *size_in_bytes) 44 { 45 *size_in_bytes = sizeof(VadInstT) * 2 / sizeof(WebRtc_Word16); 46 return 0; 47 } 48 49 WebRtc_Word16 WebRtcVad_Assign(VadInst **vad_inst, void *vad_inst_addr) 50 { 51 52 if (vad_inst == NULL) 53 { 54 return -1; 55 } 56 57 if (vad_inst_addr != NULL) 58 { 59 *vad_inst = (VadInst*)vad_inst_addr; 60 return 0; 61 } else 62 { 63 return -1; 64 } 65 } 66 67 WebRtc_Word16 WebRtcVad_Create(VadInst **vad_inst) 68 { 69 70 VadInstT *vad_ptr = NULL; 71 72 if (vad_inst == NULL) 73 { 74 return -1; 75 } 76 77 *vad_inst = NULL; 78 79 vad_ptr = (VadInstT *)malloc(sizeof(VadInstT)); 80 *vad_inst = (VadInst *)vad_ptr; 81 82 if (vad_ptr == NULL) 83 { 84 return -1; 85 } 86 87 vad_ptr->init_flag = 0; 88 89 return 0; 90 } 91 92 WebRtc_Word16 WebRtcVad_Free(VadInst *vad_inst) 93 { 94 95 if (vad_inst == NULL) 96 { 97 return -1; 98 } 99 100 free(vad_inst); 101 return 0; 102 } 103 104 WebRtc_Word16 WebRtcVad_Init(VadInst *vad_inst) 105 { 106 short mode = 0; // Default high quality 107 108 if (vad_inst == NULL) 109 { 110 return -1; 111 } 112 113 return WebRtcVad_InitCore((VadInstT*)vad_inst, mode); 114 } 115 116 WebRtc_Word16 WebRtcVad_set_mode(VadInst *vad_inst, WebRtc_Word16 mode) 117 { 118 VadInstT* vad_ptr; 119 120 if (vad_inst == NULL) 121 { 122 return -1; 123 } 124 125 vad_ptr = (VadInstT*)vad_inst; 126 if (vad_ptr->init_flag != kInitCheck) 127 { 128 return -1; 129 } 130 131 return WebRtcVad_set_mode_core((VadInstT*)vad_inst, mode); 132 } 133 134 WebRtc_Word16 WebRtcVad_Process(VadInst *vad_inst, 135 WebRtc_Word16 fs, 136 WebRtc_Word16 *speech_frame, 137 WebRtc_Word16 frame_length) 138 { 139 WebRtc_Word16 vad; 140 VadInstT* vad_ptr; 141 142 if (vad_inst == NULL) 143 { 144 return -1; 145 } 146 147 vad_ptr = (VadInstT*)vad_inst; 148 if (vad_ptr->init_flag != kInitCheck) 149 { 150 return -1; 151 } 152 153 if (speech_frame == NULL) 154 { 155 return -1; 156 } 157 158 if (fs == 32000) 159 { 160 if ((frame_length != 320) && (frame_length != 640) && (frame_length != 960)) 161 { 162 return -1; 163 } 164 vad = WebRtcVad_CalcVad32khz((VadInstT*)vad_inst, speech_frame, frame_length); 165 166 } else if (fs == 16000) 167 { 168 if ((frame_length != 160) && (frame_length != 320) && (frame_length != 480)) 169 { 170 return -1; 171 } 172 vad = WebRtcVad_CalcVad16khz((VadInstT*)vad_inst, speech_frame, frame_length); 173 174 } else if (fs == 8000) 175 { 176 if ((frame_length != 80) && (frame_length != 160) && (frame_length != 240)) 177 { 178 return -1; 179 } 180 vad = WebRtcVad_CalcVad8khz((VadInstT*)vad_inst, speech_frame, frame_length); 181 182 } else 183 { 184 return -1; // Not a supported sampling frequency 185 } 186 187 if (vad > 0) 188 { 189 return 1; 190 } else if (vad == 0) 191 { 192 return 0; 193 } else 194 { 195 return -1; 196 } 197 } 198