⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codectest.c

📁 由HawK提供的语音压缩软件
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
  Codec test program for the HawkVoice cross platform voice library
  Copyright (C) 2000-2004 Phil Frisbie, Jr. (phil@hawksoft.com)
*/
/*
  To test UNICODE on Windows NT/2000/XP, uncomment both the defines below and compile
  this program.
*/
//#define _UNICODE
//#define UNICODE

#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "../hawklib.h"
#include "../ulaw/u-law.h"
#include "../adpcm/adpcm.h"
#include "../gsm/gsm.h"
#include "../lpc/lpc.h"
#include "../lpc10/lpc10.h"
#include "../openlpc/openlpc.h"
#include "../celp/celp.h"

#ifdef HL_WINDOWS_APP
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
#endif

#ifndef _INC_TCHAR
#ifdef _UNICODE
#define TEXT(x)    L##x
#define _TCHAR      wchar_t
#define _tmain      wmain
#define _tprintf    wprintf
#define _stprintf   swprintf
#define _tcslen     wcslen
#ifdef HL_WINDOWS_APP
#define _ttoi       _wtoi
#else /* !HL_WINDOWS_APP*/
#define _ttoi       wtoi
#endif /* !HL_WINDOWS_APP*/
#else /* !UNICODE */
#define TEXT(x)    x
#define _TCHAR      char
#define _tmain      main
#define _tprintf    printf
#define _stprintf   sprintf
#define _tcslen     strlen
#endif /* !UNICODE */
#endif /* _INC_TCHAR */

/* This was copied from nl.h so that it did not need to be included */

#if defined WIN32 || defined WIN64 || defined __i386__ || defined __alpha__
#define NL_LITTLE_ENDIAN
#endif

#ifdef NL_LITTLE_ENDIAN
#define nlSwaps(x) (unsigned short)(((((unsigned short)x) & 0x00ff) << 8) |\
                                ((((unsigned short)x) & 0xff00) >> 8))

#define nlSwapl(x) (unsigned long)(((((unsigned long)x) & 0x000000ff) << 24) | \
                                ((((unsigned long)x) & 0x0000ff00) << 8) | \
                                ((((unsigned long)x) & 0x00ff0000) >> 8) | \
                                ((((unsigned long)x) & 0xff000000) >> 24))

#else
/* no conversion needed for big endian */
#define nlSwaps(x) (x)
#define nlSwapl(x) (x)
#endif /* NL_LITTLE_ENDIAN */

#ifdef HL_WINDOWS_APP
#include <io.h>
#include <windows.h>
#ifdef _UNICODE
#define _topen	    _wopen
#else
#define _topen	    _open
#endif /* !_UNICODE */
#define close	    _close
#define read	    _read
#define write       _write
#define lseek       _lseek
#define O_RDONLY    _O_RDONLY
#define O_RDWR      _O_RDWR
#define O_BINARY    _O_BINARY
#define O_CREAT     _O_CREAT
#define O_TRUNC     _O_TRUNC
#define S_IREAD     _S_IREAD
#define S_IWRITE    _S_IWRITE

#else

#include <sys/io.h>
#define _topen	    open
#define O_BINARY 0

#endif /* !HL_WINDOWS_APP */

#if defined _MSC_VER && defined _M_IX86 

#define RDTSC __asm _emit 0x0f __asm _emit 0x31

unsigned long getcycles(void)
{
	unsigned long	cycles;

__asm{
		RDTSC				; get the count
		mov cycles, eax		; copy register
	}
	return cycles;
}

#elif defined __GNUC__ && defined __i386__

unsigned long getcycles(void)
{
	unsigned long	cycles;

    asm("rdtsc" : "=a" (cycles));
	return cycles;
}

#else

unsigned long getcycles(void)
{
    return 0;
}

#endif /* !_MSC_VER && _M_IX86*/

unsigned long countcycles(unsigned long a)
{
    unsigned long b = getcycles();

    if(a == 0 && b == 0)
        return 0;

    if(a < b)
    {
        return (b - a);
    }
    else
    {
        return ((unsigned long)0xffffffff - a + b);
    }
}


#define AU_U_LAW            1
#define AU_PCM_8_BIT        2
#define AU_PCM_16_BIT       3
#define AU_NOT_SUPPORTED    0

/* number of samples to process at a time, ALWAYS an even number */
#define NUM_SAMPLES     160
#define LPC10_SAMPLES   180

#define CELP_CODEBOOK   32
#define CELP_FAST       1

struct auheader {
long magic, hsize, dsize, emode, rate, nchan;
};

int filetype;

static void writeHeader(int f)
{
    struct auheader h;

    h.magic = nlSwapl(0x2e736e64);
    h.hsize = nlSwapl(sizeof(struct auheader));
    h.dsize = nlSwapl(0xffffffff);
    h.emode = nlSwapl(AU_U_LAW);
    h.rate = nlSwapl(8000);
    h.nchan = nlSwapl(1);

    write(f, &h, sizeof(h));
}

static int readHeader(int f)
{
    struct auheader h;

    read(f, &h, sizeof(h));

    h.rate = nlSwapl(h.rate);
    h.nchan = nlSwapl(h.nchan);
    if(h.rate > 9000 || h.nchan != 1)
    {
        return AU_NOT_SUPPORTED;
    }
    h.emode = nlSwapl(h.emode);
    if(h.emode == AU_U_LAW || h.emode == AU_PCM_16_BIT || h.emode == AU_PCM_8_BIT)
    {
        return h.emode;
    }
    return AU_NOT_SUPPORTED;
}

static void swapSamples(short *buffer, int len)
{
    int i;

    for(i=0;i<len;i++)
    {
        buffer[i] = nlSwaps(buffer[i]);
    }
}

static void expandSamples(unsigned char *in, short *buffer, int len)
{
    int i;

    for(i=0;i<len;i++)
    {
        buffer[i] = in[i]|(in[i]<<8);
    }
}

static int readSamples(int f, int type, short *buffer, int len)
{
    int count;

    if(type == AU_PCM_16_BIT)
    {
        /* no conversion needed, just swap */
        count =  read(f, buffer, sizeof(short) * len);
        count /= 2;
        swapSamples(buffer, count);

        return count;
    }
    else if(type == AU_PCM_8_BIT)
    {
        unsigned char temp[1000];

        count =  read(f, buffer, len);
        expandSamples(temp, buffer, count);

        return count;
    }
    else
    {
        unsigned char temp[1000];

        /* convert the u-law samples to 16 bit */
        count = read(f, temp, len);
        ulawDecode(temp, buffer, count);
        return count;
    }
}

static void writeSamples(int f, short *buffer, int len)
{
    unsigned char temp[1000];

    ulawEncode(buffer, temp, len);

    (void)write(f, temp, len);
}
#ifdef HL_WINDOWS_APP
int __cdecl _tmain(int argc, _TCHAR **argv)
#else
int _tmain(int argc, _TCHAR **argv)
#endif
{
    int                         infile, ulawfile, adpcmfile, gsmfile, lpcfile, lpc10file, type;
    int                         lpc18file, lpc14file, celp45file, celp30file, celp23file, vbrlpc10file;
    int                         len = NUM_SAMPLES;
    struct adpcm_state          adpcmencode = {0, 0};
    struct adpcm_state          adpcmdecode = {0, 0};
    struct gsm_state            *gsmencode;
    struct gsm_state            *gsmdecode;
    lpc_encoder_state           *lpcencode;
    lpc_decoder_state           *lpcdecode;
    lpc10_encoder_state         *lpc10encode;
    lpc10_decoder_state         *lpc10decode;
    openlpc_encoder_state       *lpc18encode;
    openlpc_decoder_state       *lpc18decode;
    openlpc_encoder_state       *lpc14encode;
    openlpc_decoder_state       *lpc14decode;
    celp_encoder_state          *celpencode;
    celp_decoder_state          *celpdecode;
    lpc10_encoder_state         *vbrlpc10encode;
    lpc10_decoder_state         *vbrlpc10decode;
    unsigned long               ulawe, adpcme, gsme, lpce, lpc10e, lpc18e, lpc14e, vbrlpc10e, count;
    unsigned long               ulawd, adpcmd, gsmd, lpcd, lpc10d, lpc18d, lpc14d, celpe, celpd, vbrlpc10d;
    int                         mytrue = 1;
#ifdef WIN32
	int tp, pp;
	HANDLE t, p;

	t = GetCurrentThread();
	tp = GetThreadPriority(t);
	SetThreadPriority(t, THREAD_PRIORITY_TIME_CRITICAL);
	p = GetCurrentProcess();
	pp = GetPriorityClass(p);
	SetPriorityClass(p, HIGH_PRIORITY_CLASS);
#endif

    /* open the input file */
    if(argc > 1)
    {
        infile = _topen(argv[1], O_BINARY|O_RDONLY);
    }
    else
    {
        infile = _topen(TEXT("16bit.au"), O_BINARY|O_RDONLY);
    }
    if(infile < 0)
    {
        _tprintf(TEXT("Could not open input file\n"));
        return (1);
    }
    type = readHeader(infile);

    /* open the output files */
    ulawfile = _topen(TEXT("ulaw.au"), O_BINARY|O_CREAT|O_TRUNC|O_RDWR, S_IREAD|S_IWRITE);
    writeHeader(ulawfile);

    adpcmfile = _topen(TEXT("adpcm.au"), O_BINARY|O_CREAT|O_TRUNC|O_RDWR, S_IREAD|S_IWRITE);
    writeHeader(adpcmfile);

    gsmfile = _topen(TEXT("gsm.au"), O_BINARY|O_CREAT|O_TRUNC|O_RDWR, S_IREAD|S_IWRITE);
    writeHeader(gsmfile);
    gsmencode = gsm_create();
    gsmdecode = gsm_create();
    (void)gsm_option(gsmencode, GSM_OPT_LTP_CUT, &mytrue);

    lpcfile = _topen(TEXT("lpc.au"), O_BINARY|O_CREAT|O_TRUNC|O_RDWR, S_IREAD|S_IWRITE);
    writeHeader(lpcfile);
    lpcencode = create_lpc_encoder_state();
    init_lpc_encoder_state(lpcencode);
    lpcdecode = create_lpc_decoder_state();
    init_lpc_decoder_state(lpcdecode);

    lpc10file = _topen(TEXT("lpc10.au"), O_BINARY|O_CREAT|O_TRUNC|O_RDWR, S_IREAD|S_IWRITE);
    writeHeader(lpc10file);
    lpc10encode = create_lpc10_encoder_state();
    init_lpc10_encoder_state(lpc10encode);
    lpc10decode = create_lpc10_decoder_state();
    init_lpc10_decoder_state(lpc10decode);

    lpc18file = _topen(TEXT("lpc18.au"), O_BINARY|O_CREAT|O_TRUNC|O_RDWR, S_IREAD|S_IWRITE);
    writeHeader(lpc18file);
    lpc18encode = create_openlpc_encoder_state();
    init_openlpc_encoder_state(lpc18encode, OPENLPC_FRAMESIZE_1_8);
    lpc18decode = create_openlpc_decoder_state();
    init_openlpc_decoder_state(lpc18decode, OPENLPC_FRAMESIZE_1_8);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -