From ed700db014a5dc533a7dc7b70630863847956792 Mon Sep 17 00:00:00 2001 From: "bjornv@webrtc.org" Date: Mon, 12 Mar 2012 12:17:26 +0000 Subject: [PATCH] VAD refactor: Assign functions. Mostly style changes. Includes - parameter order - type changes - variable name changes - comment changes - indentations - test changes In addition made minor style changes. Review URL: https://webrtc-codereview.appspot.com/384001 git-svn-id: http://webrtc.googlecode.com/svn/trunk@1874 4adac7df-926f-26a2-2b94-8c16560cd09d --- src/common_audio/vad/include/webrtc_vad.h | 68 +++++++++-------------- src/common_audio/vad/vad_unittest.cc | 14 ++--- src/common_audio/vad/webrtc_vad.c | 34 +++--------- 3 files changed, 41 insertions(+), 75 deletions(-) diff --git a/src/common_audio/vad/include/webrtc_vad.h b/src/common_audio/vad/include/webrtc_vad.h index 3f9e40230a..1e860ae9a3 100644 --- a/src/common_audio/vad/include/webrtc_vad.h +++ b/src/common_audio/vad/include/webrtc_vad.h @@ -16,42 +16,32 @@ #ifndef WEBRTC_COMMON_AUDIO_VAD_INCLUDE_WEBRTC_VAD_H_ #define WEBRTC_COMMON_AUDIO_VAD_INCLUDE_WEBRTC_VAD_H_ +#include + #include "typedefs.h" typedef struct WebRtcVadInst VadInst; #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif -/**************************************************************************** - * WebRtcVad_AssignSize(...) - * - * This functions get the size needed for storing the instance for encoder - * and decoder, respectively - * - * Input/Output: - * - size_in_bytes : Pointer to integer where the size is returned - * - * Return value : 0 - */ -WebRtc_Word16 WebRtcVad_AssignSize(int *size_in_bytes); +// TODO(bjornv): Investigate if we need the Assign calls below at all. -/**************************************************************************** - * WebRtcVad_Assign(...) - * - * This functions Assigns memory for the instances. - * - * Input: - * - vad_inst_addr : Address to where to assign memory - * Output: - * - vad_inst : Pointer to the instance that should be created - * - * Return value : 0 - Ok - * -1 - Error - */ -WebRtc_Word16 WebRtcVad_Assign(VadInst **vad_inst, void *vad_inst_addr); +// Gets the size needed for storing the instance for the VAD. +// +// returns : The size in bytes needed to allocate memory for the VAD instance. +size_t WebRtcVad_AssignSize(); + +// Assigns memory for the instances at a given address. It is assumed that the +// memory for the VAD instance is allocated at |memory| in accordance with +// WebRtcVad_AssignSize(). +// +// - memory [i] : Address to where the memory is assigned. +// - handle [o] : Pointer to the instance that should be created. +// +// returns : 0 - (OK), -1 (NULL pointer in) +int WebRtcVad_Assign(void* memory, VadInst** handle); // Creates an instance to the VAD structure. // @@ -67,21 +57,13 @@ int WebRtcVad_Create(VadInst** handle); // returns : 0 - (OK), -1 - (NULL pointer in) int WebRtcVad_Free(VadInst* handle); -/**************************************************************************** - * WebRtcVad_Init(...) - * - * This function initializes a VAD instance - * - * Input: - * - vad_inst : Instance that should be initialized - * - * Output: - * - vad_inst : Initialized instance - * - * Return value : 0 - Ok - * -1 - Error - */ -int WebRtcVad_Init(VadInst *vad_inst); +// Initializes a VAD instance. +// +// - handle [i/o] : Instance that should be initialized. +// +// returns : 0 - (OK), +// -1 - (NULL pointer or Default mode could not be set) +int WebRtcVad_Init(VadInst* handle); /**************************************************************************** * WebRtcVad_set_mode(...) diff --git a/src/common_audio/vad/vad_unittest.cc b/src/common_audio/vad/vad_unittest.cc index 60ee208528..4b3c22483e 100644 --- a/src/common_audio/vad/vad_unittest.cc +++ b/src/common_audio/vad/vad_unittest.cc @@ -69,15 +69,15 @@ TEST_F(VadTest, ApiTest) { EXPECT_EQ(-1, WebRtcVad_set_mode(NULL, kModes[0])); EXPECT_EQ(-1, WebRtcVad_Process(NULL, kRates[0], speech, kFrameLengths[0])); - // WebRtcVad_AssignSize tests - int handle_size_bytes = 0; - EXPECT_EQ(0, WebRtcVad_AssignSize(&handle_size_bytes)); - EXPECT_EQ(576, handle_size_bytes); + // WebRtcVad_AssignSize() test. + size_t handle_size_bytes = WebRtcVad_AssignSize(); + EXPECT_EQ(576u, handle_size_bytes); - // WebRtcVad_Assign tests + // WebRtcVad_Assign() tests. void* tmp_handle = malloc(handle_size_bytes); - EXPECT_EQ(-1, WebRtcVad_Assign(&handle, NULL)); - EXPECT_EQ(0, WebRtcVad_Assign(&handle, tmp_handle)); + EXPECT_EQ(-1, WebRtcVad_Assign(NULL, &handle)); + EXPECT_EQ(-1, WebRtcVad_Assign(tmp_handle, NULL)); + EXPECT_EQ(0, WebRtcVad_Assign(tmp_handle, &handle)); EXPECT_EQ(handle, tmp_handle); free(tmp_handle); diff --git a/src/common_audio/vad/webrtc_vad.c b/src/common_audio/vad/webrtc_vad.c index 26941e6dcc..c00c962352 100644 --- a/src/common_audio/vad/webrtc_vad.c +++ b/src/common_audio/vad/webrtc_vad.c @@ -8,12 +8,6 @@ * be found in the AUTHORS file in the root of the source tree. */ - -/* - * This file includes the VAD API calls. For a specific function call description, - * see webrtc_vad.h - */ - #include #include @@ -22,28 +16,17 @@ static const int kInitCheck = 42; -WebRtc_Word16 WebRtcVad_AssignSize(int *size_in_bytes) -{ - *size_in_bytes = sizeof(VadInstT) * 2 / sizeof(WebRtc_Word16); - return 0; +size_t WebRtcVad_AssignSize() { + return sizeof(VadInstT); } -WebRtc_Word16 WebRtcVad_Assign(VadInst **vad_inst, void *vad_inst_addr) -{ +int WebRtcVad_Assign(void* memory, VadInst** handle) { + if (handle == NULL || memory == NULL) { + return -1; + } - if (vad_inst == NULL) - { - return -1; - } - - if (vad_inst_addr != NULL) - { - *vad_inst = (VadInst*)vad_inst_addr; - return 0; - } else - { - return -1; - } + *handle = (VadInst*) memory; + return 0; } int WebRtcVad_Create(VadInst** handle) { @@ -76,6 +59,7 @@ int WebRtcVad_Free(VadInst* handle) { return 0; } +// TODO(bjornv): Move WebRtcVad_InitCore() code here. int WebRtcVad_Init(VadInst* handle) { // Initialize the core VAD component. return WebRtcVad_InitCore((VadInstT*) handle);