2011-07-07 08:21:25 +00:00
|
|
|
/*
|
2012-01-23 12:36:46 +00:00
|
|
|
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
2011-07-07 08:21:25 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
2013-05-27 09:49:58 +00:00
|
|
|
#include "webrtc/common_audio/vad/include/webrtc_vad.h"
|
2012-06-12 08:19:24 +00:00
|
|
|
|
2011-07-07 08:21:25 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2013-05-27 09:49:58 +00:00
|
|
|
#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
|
|
|
|
|
#include "webrtc/common_audio/vad/vad_core.h"
|
|
|
|
|
#include "webrtc/typedefs.h"
|
2011-07-07 08:21:25 +00:00
|
|
|
|
|
|
|
|
static const int kInitCheck = 42;
|
2012-10-15 17:46:19 +00:00
|
|
|
static const int kValidRates[] = { 8000, 16000, 32000, 48000 };
|
2012-06-12 08:19:24 +00:00
|
|
|
static const size_t kRatesSize = sizeof(kValidRates) / sizeof(*kValidRates);
|
|
|
|
|
static const int kMaxFrameLengthMs = 30;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2015-05-27 07:22:58 +02:00
|
|
|
VadInst* WebRtcVad_Create() {
|
|
|
|
|
VadInstT* self = (VadInstT*)malloc(sizeof(VadInstT));
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-10-05 00:19:01 +00:00
|
|
|
WebRtcSpl_Init();
|
2012-01-31 14:42:50 +00:00
|
|
|
self->init_flag = 0;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2015-05-27 07:22:58 +02:00
|
|
|
return (VadInst*)self;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-22 04:45:35 +00:00
|
|
|
void WebRtcVad_Free(VadInst* handle) {
|
2012-01-31 14:42:50 +00:00
|
|
|
free(handle);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-12 12:17:26 +00:00
|
|
|
// TODO(bjornv): Move WebRtcVad_InitCore() code here.
|
2012-01-25 12:18:12 +00:00
|
|
|
int WebRtcVad_Init(VadInst* handle) {
|
|
|
|
|
// Initialize the core VAD component.
|
|
|
|
|
return WebRtcVad_InitCore((VadInstT*) handle);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2012-03-27 11:06:29 +00:00
|
|
|
// TODO(bjornv): Move WebRtcVad_set_mode_core() code here.
|
|
|
|
|
int WebRtcVad_set_mode(VadInst* handle, int mode) {
|
|
|
|
|
VadInstT* self = (VadInstT*) handle;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-03-27 11:06:29 +00:00
|
|
|
if (handle == NULL) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (self->init_flag != kInitCheck) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-03-27 11:06:29 +00:00
|
|
|
return WebRtcVad_set_mode_core(self, mode);
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
2014-04-30 16:44:13 +00:00
|
|
|
int WebRtcVad_Process(VadInst* handle, int fs, const int16_t* audio_frame,
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
size_t frame_length) {
|
2012-06-19 11:03:32 +00:00
|
|
|
int vad = -1;
|
2012-06-12 08:19:24 +00:00
|
|
|
VadInstT* self = (VadInstT*) handle;
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-06-12 08:19:24 +00:00
|
|
|
if (handle == NULL) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-06-12 08:19:24 +00:00
|
|
|
if (self->init_flag != kInitCheck) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (audio_frame == NULL) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (WebRtcVad_ValidRateAndFrameLength(fs, frame_length) != 0) {
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-10-15 17:46:19 +00:00
|
|
|
if (fs == 48000) {
|
|
|
|
|
vad = WebRtcVad_CalcVad48khz(self, audio_frame, frame_length);
|
|
|
|
|
} else if (fs == 32000) {
|
2012-06-12 08:19:24 +00:00
|
|
|
vad = WebRtcVad_CalcVad32khz(self, audio_frame, frame_length);
|
|
|
|
|
} else if (fs == 16000) {
|
|
|
|
|
vad = WebRtcVad_CalcVad16khz(self, audio_frame, frame_length);
|
|
|
|
|
} else if (fs == 8000) {
|
|
|
|
|
vad = WebRtcVad_CalcVad8khz(self, audio_frame, frame_length);
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-06-12 08:19:24 +00:00
|
|
|
if (vad > 0) {
|
|
|
|
|
vad = 1;
|
|
|
|
|
}
|
|
|
|
|
return vad;
|
|
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
int WebRtcVad_ValidRateAndFrameLength(int rate, size_t frame_length) {
|
2012-06-12 08:19:24 +00:00
|
|
|
int return_value = -1;
|
|
|
|
|
size_t i;
|
|
|
|
|
int valid_length_ms;
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
size_t valid_length;
|
2012-06-12 08:19:24 +00:00
|
|
|
|
|
|
|
|
// We only allow 10, 20 or 30 ms frames. Loop through valid frame rates and
|
|
|
|
|
// see if we have a matching pair.
|
|
|
|
|
for (i = 0; i < kRatesSize; i++) {
|
|
|
|
|
if (kValidRates[i] == rate) {
|
|
|
|
|
for (valid_length_ms = 10; valid_length_ms <= kMaxFrameLengthMs;
|
|
|
|
|
valid_length_ms += 10) {
|
Update a ton of audio code to use size_t more correctly and in general reduce
use of int16_t/uint16_t.
This is the upshot of a recommendation by henrik.lundin and kwiberg on an original small change ( https://webrtc-codereview.appspot.com/42569004/#ps1 ) to stop using int16_t just because values could fit in it, and is similar in nature to a previous "mass change to use size_t more" ( https://webrtc-codereview.appspot.com/23129004/ ) which also needed to be split up for review but to land all at once, since, like adding "const", such changes tend to cause a lot of transitive effects.
This was be reviewed and approved in pieces:
https://codereview.webrtc.org/1224093003
https://codereview.webrtc.org/1224123002
https://codereview.webrtc.org/1224163002
https://codereview.webrtc.org/1225133003
https://codereview.webrtc.org/1225173002
https://codereview.webrtc.org/1227163003
https://codereview.webrtc.org/1227203003
https://codereview.webrtc.org/1227213002
https://codereview.webrtc.org/1227893002
https://codereview.webrtc.org/1228793004
https://codereview.webrtc.org/1228803003
https://codereview.webrtc.org/1228823002
https://codereview.webrtc.org/1228823003
https://codereview.webrtc.org/1228843002
https://codereview.webrtc.org/1230693002
https://codereview.webrtc.org/1231713002
The change is being landed as TBR to all the folks who reviewed the above.
BUG=chromium:81439
TEST=none
R=andrew@webrtc.org, pbos@webrtc.org
TBR=aluebs, andrew, asapersson, henrika, hlundin, jan.skoglund, kwiberg, minyue, pbos, pthatcher
Review URL: https://codereview.webrtc.org/1230503003 .
Cr-Commit-Position: refs/heads/master@{#9768}
2015-08-24 14:52:23 -07:00
|
|
|
valid_length = (size_t)(kValidRates[i] / 1000 * valid_length_ms);
|
2012-06-12 08:19:24 +00:00
|
|
|
if (frame_length == valid_length) {
|
|
|
|
|
return_value = 0;
|
|
|
|
|
break;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2012-06-12 08:19:24 +00:00
|
|
|
}
|
|
|
|
|
break;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|
2012-06-12 08:19:24 +00:00
|
|
|
}
|
2011-07-07 08:21:25 +00:00
|
|
|
|
2012-06-12 08:19:24 +00:00
|
|
|
return return_value;
|
2011-07-07 08:21:25 +00:00
|
|
|
}
|