niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
bjornv@webrtc.org | 152c34c | 2012-01-23 12:36:46 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
pbos@webrtc.org | aa30bb7 | 2013-05-27 09:49:58 +0000 | [diff] [blame^] | 11 | #include "webrtc/common_audio/vad/include/webrtc_vad.h" |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 12 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | |
pbos@webrtc.org | aa30bb7 | 2013-05-27 09:49:58 +0000 | [diff] [blame^] | 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" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 19 | |
| 20 | static const int kInitCheck = 42; |
tina.legrand@webrtc.org | ef43357 | 2012-10-15 17:46:19 +0000 | [diff] [blame] | 21 | static const int kValidRates[] = { 8000, 16000, 32000, 48000 }; |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 22 | static const size_t kRatesSize = sizeof(kValidRates) / sizeof(*kValidRates); |
| 23 | static const int kMaxFrameLengthMs = 30; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
bjornv@webrtc.org | 26e8a58 | 2012-01-31 14:42:50 +0000 | [diff] [blame] | 25 | int WebRtcVad_Create(VadInst** handle) { |
| 26 | VadInstT* self = NULL; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
bjornv@webrtc.org | 26e8a58 | 2012-01-31 14:42:50 +0000 | [diff] [blame] | 28 | if (handle == NULL) { |
| 29 | return -1; |
| 30 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 31 | |
bjornv@webrtc.org | 26e8a58 | 2012-01-31 14:42:50 +0000 | [diff] [blame] | 32 | *handle = NULL; |
| 33 | self = (VadInstT*) malloc(sizeof(VadInstT)); |
| 34 | *handle = (VadInst*) self; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 35 | |
bjornv@webrtc.org | 26e8a58 | 2012-01-31 14:42:50 +0000 | [diff] [blame] | 36 | if (self == NULL) { |
| 37 | return -1; |
| 38 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | |
kma@webrtc.org | ac4d70d | 2012-10-05 00:19:01 +0000 | [diff] [blame] | 40 | WebRtcSpl_Init(); |
| 41 | |
bjornv@webrtc.org | 26e8a58 | 2012-01-31 14:42:50 +0000 | [diff] [blame] | 42 | self->init_flag = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | |
bjornv@webrtc.org | 26e8a58 | 2012-01-31 14:42:50 +0000 | [diff] [blame] | 44 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 45 | } |
| 46 | |
bjornv@webrtc.org | 26e8a58 | 2012-01-31 14:42:50 +0000 | [diff] [blame] | 47 | int WebRtcVad_Free(VadInst* handle) { |
| 48 | if (handle == NULL) { |
| 49 | return -1; |
| 50 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | |
bjornv@webrtc.org | 26e8a58 | 2012-01-31 14:42:50 +0000 | [diff] [blame] | 52 | free(handle); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 53 | |
bjornv@webrtc.org | 26e8a58 | 2012-01-31 14:42:50 +0000 | [diff] [blame] | 54 | return 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 55 | } |
| 56 | |
bjornv@webrtc.org | ed700db | 2012-03-12 12:17:26 +0000 | [diff] [blame] | 57 | // TODO(bjornv): Move WebRtcVad_InitCore() code here. |
bjornv@webrtc.org | 2a4dcd7 | 2012-01-25 12:18:12 +0000 | [diff] [blame] | 58 | int WebRtcVad_Init(VadInst* handle) { |
| 59 | // Initialize the core VAD component. |
| 60 | return WebRtcVad_InitCore((VadInstT*) handle); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 61 | } |
| 62 | |
bjornv@webrtc.org | 78f0cdc | 2012-03-27 11:06:29 +0000 | [diff] [blame] | 63 | // TODO(bjornv): Move WebRtcVad_set_mode_core() code here. |
| 64 | int WebRtcVad_set_mode(VadInst* handle, int mode) { |
| 65 | VadInstT* self = (VadInstT*) handle; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | |
bjornv@webrtc.org | 78f0cdc | 2012-03-27 11:06:29 +0000 | [diff] [blame] | 67 | if (handle == NULL) { |
| 68 | return -1; |
| 69 | } |
| 70 | if (self->init_flag != kInitCheck) { |
| 71 | return -1; |
| 72 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 73 | |
bjornv@webrtc.org | 78f0cdc | 2012-03-27 11:06:29 +0000 | [diff] [blame] | 74 | return WebRtcVad_set_mode_core(self, mode); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 75 | } |
| 76 | |
bjornv@webrtc.org | b38fca1 | 2012-06-19 11:03:32 +0000 | [diff] [blame] | 77 | int WebRtcVad_Process(VadInst* handle, int fs, int16_t* audio_frame, |
| 78 | int frame_length) { |
| 79 | int vad = -1; |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 80 | VadInstT* self = (VadInstT*) handle; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 81 | |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 82 | if (handle == NULL) { |
| 83 | return -1; |
| 84 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 85 | |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 86 | if (self->init_flag != kInitCheck) { |
| 87 | return -1; |
| 88 | } |
| 89 | if (audio_frame == NULL) { |
| 90 | return -1; |
| 91 | } |
| 92 | if (WebRtcVad_ValidRateAndFrameLength(fs, frame_length) != 0) { |
| 93 | return -1; |
| 94 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 95 | |
tina.legrand@webrtc.org | ef43357 | 2012-10-15 17:46:19 +0000 | [diff] [blame] | 96 | if (fs == 48000) { |
| 97 | vad = WebRtcVad_CalcVad48khz(self, audio_frame, frame_length); |
| 98 | } else if (fs == 32000) { |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 99 | vad = WebRtcVad_CalcVad32khz(self, audio_frame, frame_length); |
| 100 | } else if (fs == 16000) { |
| 101 | vad = WebRtcVad_CalcVad16khz(self, audio_frame, frame_length); |
| 102 | } else if (fs == 8000) { |
| 103 | vad = WebRtcVad_CalcVad8khz(self, audio_frame, frame_length); |
| 104 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 106 | if (vad > 0) { |
| 107 | vad = 1; |
| 108 | } |
| 109 | return vad; |
| 110 | } |
| 111 | |
| 112 | int WebRtcVad_ValidRateAndFrameLength(int rate, int frame_length) { |
| 113 | int return_value = -1; |
| 114 | size_t i; |
| 115 | int valid_length_ms; |
| 116 | int valid_length; |
| 117 | |
| 118 | // We only allow 10, 20 or 30 ms frames. Loop through valid frame rates and |
| 119 | // see if we have a matching pair. |
| 120 | for (i = 0; i < kRatesSize; i++) { |
| 121 | if (kValidRates[i] == rate) { |
| 122 | for (valid_length_ms = 10; valid_length_ms <= kMaxFrameLengthMs; |
| 123 | valid_length_ms += 10) { |
| 124 | valid_length = (kValidRates[i] / 1000 * valid_length_ms); |
| 125 | if (frame_length == valid_length) { |
| 126 | return_value = 0; |
| 127 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | } |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 129 | } |
| 130 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 131 | } |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 132 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 133 | |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 134 | return return_value; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 135 | } |