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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "common_audio/signal_processing/include/signal_processing_library.h" |
| 17 | #include "common_audio/vad/vad_core.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame^] | 18 | #include "typedefs.h" // NOLINT(build/include) |
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 | |
Bjorn Volcker | de4703c | 2015-05-27 07:22:58 +0200 | [diff] [blame] | 25 | VadInst* WebRtcVad_Create() { |
| 26 | VadInstT* self = (VadInstT*)malloc(sizeof(VadInstT)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
kma@webrtc.org | ac4d70d | 2012-10-05 00:19:01 +0000 | [diff] [blame] | 28 | WebRtcSpl_Init(); |
bjornv@webrtc.org | 26e8a58 | 2012-01-31 14:42:50 +0000 | [diff] [blame] | 29 | self->init_flag = 0; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 30 | |
Bjorn Volcker | de4703c | 2015-05-27 07:22:58 +0200 | [diff] [blame] | 31 | return (VadInst*)self; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 32 | } |
| 33 | |
bjornv@webrtc.org | 2a79672 | 2014-04-22 04:45:35 +0000 | [diff] [blame] | 34 | void WebRtcVad_Free(VadInst* handle) { |
bjornv@webrtc.org | 26e8a58 | 2012-01-31 14:42:50 +0000 | [diff] [blame] | 35 | free(handle); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 36 | } |
| 37 | |
bjornv@webrtc.org | ed700db | 2012-03-12 12:17:26 +0000 | [diff] [blame] | 38 | // TODO(bjornv): Move WebRtcVad_InitCore() code here. |
bjornv@webrtc.org | 2a4dcd7 | 2012-01-25 12:18:12 +0000 | [diff] [blame] | 39 | int WebRtcVad_Init(VadInst* handle) { |
| 40 | // Initialize the core VAD component. |
| 41 | return WebRtcVad_InitCore((VadInstT*) handle); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 42 | } |
| 43 | |
bjornv@webrtc.org | 78f0cdc | 2012-03-27 11:06:29 +0000 | [diff] [blame] | 44 | // TODO(bjornv): Move WebRtcVad_set_mode_core() code here. |
| 45 | int WebRtcVad_set_mode(VadInst* handle, int mode) { |
| 46 | VadInstT* self = (VadInstT*) handle; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 47 | |
bjornv@webrtc.org | 78f0cdc | 2012-03-27 11:06:29 +0000 | [diff] [blame] | 48 | if (handle == NULL) { |
| 49 | return -1; |
| 50 | } |
| 51 | if (self->init_flag != kInitCheck) { |
| 52 | return -1; |
| 53 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
bjornv@webrtc.org | 78f0cdc | 2012-03-27 11:06:29 +0000 | [diff] [blame] | 55 | return WebRtcVad_set_mode_core(self, mode); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | } |
| 57 | |
andrew@webrtc.org | 65f9338 | 2014-04-30 16:44:13 +0000 | [diff] [blame] | 58 | int WebRtcVad_Process(VadInst* handle, int fs, const int16_t* audio_frame, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 59 | size_t frame_length) { |
bjornv@webrtc.org | b38fca1 | 2012-06-19 11:03:32 +0000 | [diff] [blame] | 60 | int vad = -1; |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 61 | VadInstT* self = (VadInstT*) handle; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 63 | if (handle == NULL) { |
| 64 | return -1; |
| 65 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 66 | |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 67 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 76 | |
tina.legrand@webrtc.org | ef43357 | 2012-10-15 17:46:19 +0000 | [diff] [blame] | 77 | if (fs == 48000) { |
| 78 | vad = WebRtcVad_CalcVad48khz(self, audio_frame, frame_length); |
| 79 | } else if (fs == 32000) { |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 80 | 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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 86 | |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 87 | if (vad > 0) { |
| 88 | vad = 1; |
| 89 | } |
| 90 | return vad; |
| 91 | } |
| 92 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 93 | int WebRtcVad_ValidRateAndFrameLength(int rate, size_t frame_length) { |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 94 | int return_value = -1; |
| 95 | size_t i; |
| 96 | int valid_length_ms; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 97 | size_t valid_length; |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 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 | 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 Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 105 | valid_length = (size_t)(kValidRates[i] / 1000 * valid_length_ms); |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 106 | if (frame_length == valid_length) { |
| 107 | return_value = 0; |
| 108 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 109 | } |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 110 | } |
| 111 | break; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 112 | } |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 113 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | |
bjornv@webrtc.org | b1c3276 | 2012-06-12 08:19:24 +0000 | [diff] [blame] | 115 | return return_value; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 116 | } |