📄 hvdi.c
字号:
/*
HawkVoice Direct Interface (HVDI) cross platform network voice library
Copyright (C) 2001-2004 Phil Frisbie, Jr. (phil@hawksoft.com)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Or go to http://www.gnu.org/copyleft/lgpl.html
*/
#ifdef _MSC_VER
#pragma warning (disable:4001) /* to disable warning in <math.h> */
#endif
#ifndef MACOSX
#include <malloc.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <math.h>
#include "hvdint.h"
#ifndef min
#define min(a,b) ((a) <= (b) ? (a) : (b))
#define max(a,b) ((a) >= (b) ? (a) : (b))
#endif
volatile hvdi_state_t hvdi_state = {0, 256, 0, 1, 0, 1448, HVDI_VOX_FAST, 0, 304};
/*
This is similar to HV_VOICE_DATA, but it is a little more compact
because it does not include the target.
HVDI_VOICE_DATA- This is a voice data packet.
NLubyte type of codec OR'd with encryption and sequence bits
NLushort packet sequence number (optional)
NLvoid *data
*/
/* internal functions */
static void hvdiFreeEncoder(hvdi_enc_state *st)
{
if(st->state == NULL)
{
return;
}
switch(st->codec){
case HV_2_4K_CODEC:
case HV_VBR_2_4K_CODEC:
destroy_lpc10_encoder_state((lpc10_encoder_state *)st->state);
break;
case HV_4_8K_CODEC:
destroy_lpc_encoder_state((lpc_encoder_state *)st->state);
break;
case HV_13_2K_CODEC:
gsm_destroy((struct gsm_state *)st->state);
break;
case HV_32K_CODEC:
free(st->state);
break;
case HV_1_4K_CODEC:
case HV_1_8K_CODEC:
destroy_openlpc_encoder_state((openlpc_encoder_state *)st->state);
break;
case HV_4_5K_CODEC:
case HV_3_0K_CODEC:
case HV_2_3K_CODEC:
destroy_celp_encoder_state((celp_encoder_state *)st->state);
break;
default:
break;
}
st->state = NULL;
}
static void hvdiFreeDecoder(hvdi_dec_state *st)
{
if(st->state == NULL)
{
return;
}
switch(st->codec){
case HV_2_4K_CODEC:
case HV_VBR_2_4K_CODEC:
destroy_lpc10_decoder_state((lpc10_decoder_state *)st->state);
break;
case HV_4_8K_CODEC:
destroy_lpc_decoder_state((lpc_decoder_state *)st->state);
break;
case HV_13_2K_CODEC:
gsm_destroy((struct gsm_state *)st->state);
break;
case HV_1_4K_CODEC:
case HV_1_8K_CODEC:
destroy_openlpc_decoder_state((openlpc_decoder_state *)st->state);
break;
case HV_4_5K_CODEC:
case HV_3_0K_CODEC:
case HV_2_3K_CODEC:
destroy_celp_decoder_state((celp_decoder_state *)st->state);
break;
default:
break;
}
st->state = NULL;
}
int hvdiSetDecoderCodec(unsigned char codec, hvdi_dec_state *st)
{
/* free the old state */
hvdiFreeDecoder(st);
/* create the new state */
switch(codec){
case HV_2_4K_CODEC:
case HV_VBR_2_4K_CODEC:
st->state = create_lpc10_decoder_state();
if(st->state == NULL)
{
return NL_FALSE;
}
init_lpc10_decoder_state((lpc10_decoder_state *)st->state);
break;
case HV_4_8K_CODEC:
st->state = create_lpc_decoder_state();
if(st->state == NULL)
{
return NL_FALSE;
}
init_lpc_decoder_state((lpc_decoder_state *)st->state);
break;
case HV_13_2K_CODEC:
st->state = gsm_create();
if(st->state == NULL)
{
return NL_FALSE;
}
break;
case HV_1_4K_CODEC:
st->state = create_openlpc_decoder_state();
if(st->state == NULL)
{
return NL_FALSE;
}
init_openlpc_decoder_state((openlpc_decoder_state *)st->state, OPENLPC_FRAMESIZE_1_4);
break;
case HV_1_8K_CODEC:
st->state = create_openlpc_decoder_state();
if(st->state == NULL)
{
return NL_FALSE;
}
init_openlpc_decoder_state((openlpc_decoder_state *)st->state, OPENLPC_FRAMESIZE_1_8);
break;
case HV_4_5K_CODEC:
st->state = create_celp_decoder_state();
init_celp_decoder_state(st->state, CELP_4_5_FRAMESIZE);
if(st->state == NULL)
{
return NL_FALSE;
}
break;
case HV_3_0K_CODEC:
st->state = create_celp_decoder_state();
init_celp_decoder_state(st->state, CELP_3_0_FRAMESIZE);
if(st->state == NULL)
{
return NL_FALSE;
}
break;
case HV_2_3K_CODEC:
st->state = create_celp_decoder_state();
init_celp_decoder_state(st->state, CELP_2_3_FRAMESIZE);
if(st->state == NULL)
{
return NL_FALSE;
}
break;
default:
break;
}
st->codec = codec;
return NL_TRUE;
}
void hvdiResetDecoderCodec(hvdi_dec_state *st)
{
/* reset the state */
switch(st->codec){
case HV_2_4K_CODEC:
case HV_VBR_2_4K_CODEC:
init_lpc10_decoder_state((lpc10_decoder_state *)st->state);
break;
case HV_4_8K_CODEC:
init_lpc_decoder_state((lpc_decoder_state *)st->state);
break;
case HV_13_2K_CODEC:
gsm_destroy((struct gsm_state *)st->state);
st->state = gsm_create();
break;
case HV_1_4K_CODEC:
init_openlpc_decoder_state((openlpc_decoder_state *)st->state, OPENLPC_FRAMESIZE_1_4);
break;
case HV_1_8K_CODEC:
init_openlpc_decoder_state((openlpc_decoder_state *)st->state, OPENLPC_FRAMESIZE_1_8);
break;
case HV_4_5K_CODEC:
init_celp_decoder_state((celp_decoder_state *)st->state, CELP_4_5_FRAMESIZE);
break;
case HV_3_0K_CODEC:
init_celp_decoder_state((celp_decoder_state *)st->state, CELP_3_0_FRAMESIZE);
break;
case HV_2_3K_CODEC:
init_celp_decoder_state((celp_decoder_state *)st->state, CELP_2_3_FRAMESIZE);
break;
default:
break;
}
}
/* HawkVoiceDI API */
HL_EXP hvdi_enc_state* HL_APIENTRY hvdiNewEncState(void)
{
hvdi_enc_state *state;
state = calloc(1, sizeof(hvdi_enc_state));
return state;
}
HL_EXP hvdi_dec_state* HL_APIENTRY hvdiNewDecState(void)
{
hvdi_dec_state *state;
state = calloc(1, sizeof(hvdi_dec_state));
return state;
}
HL_EXP void HL_APIENTRY hvdiDeleteEncState(hvdi_enc_state *st)
{
if(st != NULL)
{
/* free the codec state */
hvdiFreeEncoder(st);
/* free VOX if used */
if(st->vox != NULL)
{
free(st->vox);
st->vox = NULL;
}
/* free the state structure */
free(st);
}
}
HL_EXP void HL_APIENTRY hvdiDeleteDecState(hvdi_dec_state *st)
{
if(st != NULL)
{
/* free the codec state */
hvdiFreeDecoder(st);
/* free the state structure */
free(st);
}
}
HL_EXP int HL_APIENTRY hvdiEncStateSetCodec(hvdi_enc_state *st, unsigned char codec)
{
int frame = 0;
/* free the old state */
hvdiFreeEncoder(st);
/* create the new state */
switch(codec){
case HV_2_4K_CODEC:
case HV_VBR_2_4K_CODEC:
st->state = create_lpc10_encoder_state();
if(st->state == NULL)
{
return NL_INVALID;
}
init_lpc10_encoder_state((lpc10_encoder_state *)st->state);
frame = LPC10_SAMPLES_PER_FRAME;
break;
case HV_4_8K_CODEC:
st->state = create_lpc_encoder_state();
if(st->state == NULL)
{
return NL_INVALID;
}
init_lpc_encoder_state((lpc_encoder_state *)st->state);
frame = LPC_SAMPLES_PER_FRAME;
break;
case HV_13_2K_CODEC:
st->state = gsm_create();
if(st->state == NULL)
{
return NL_INVALID;
}
frame = GSM_SAMPLES_PER_FRAME;
break;
case HV_32K_CODEC:
st->state = calloc(1, sizeof(struct adpcm_state));
if(st->state == NULL)
{
return NL_INVALID;
}
break;
case HV_1_4K_CODEC:
st->state = create_openlpc_encoder_state();
if(st->state == NULL)
{
return NL_INVALID;
}
init_openlpc_encoder_state((openlpc_encoder_state *)st->state, OPENLPC_FRAMESIZE_1_4);
frame = OPENLPC_FRAMESIZE_1_4;
break;
case HV_1_8K_CODEC:
st->state = create_openlpc_encoder_state();
if(st->state == NULL)
{
return NL_INVALID;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -