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

📄 encode.c

📁 G726标准的24kbps和40kbps两种比特率的音频编解码算法源代码
💻 C
字号:
/*
 * encode.c
 *
 * CCITT ADPCM encoder
 *
 * Usage : encode [-3|4|5] [-a|u|l] < infile > outfile
 */
#include <stdio.h>
#include <stdlib.h>
#include "g72x.h"
#include "pcm.h"

FILE *pInFile = NULL;
FILE *pOutFile = NULL;
int nByes = 0;


typedef struct riffHdr_t
{
    char riffID[4];         /* R','I','F','F'*/
    int  riffSize;          /* whole file size - 8, riffID and riffSize not included */
    char riffFormat[4];     /*'W','A','V','E'*/
}RIFF_HDR;

typedef struct wavFmtTrunk_t
{
    char    fmtId[4];       /* 'f','m','t' */
    int     size;           /* total header size - 8, fmtId and size not included */
    unsigned short fmtTag;  /* coding format, 0x0001 normally */
    unsigned short channels;/* number of voice channels */
    int     samplePerSec;   /* sample per second - sample rate */
    int     avgBytesPerSec; /* bytes per second */
    short   blockAlign;     /* bytes per sample */
    short   bitsPerSample;  /* bits per sample */
}WAV_FMT_TRUNK_HDR;

typedef struct wavDataTrunk_t
{
    char    dataId[4];      /* 'd','a','t','a'*/
    int     size;           /* size of the pure data in bytes */
}WAV_DATA_TRUNK_HDR;

#define RIFF_HDR_SIZE (sizeof(RIFF_HDR))
#define FMT_HDR_SIZE  (sizeof(WAV_FMT_TRUNK_HDR))
#define DATA_HDR_SIZE (sizeof(WAV_DATA_TRUNK_HDR))
#define WAV_FILE_HDR_SIZE (RIFF_HDR_SIZE + FMT_HDR_SIZE + DATA_HDR_SIZE)


void WriteWAVEFileHeader(FILE *fpwave, int nBytesToWrite)
{
    unsigned char tmpBuf[256];
    RIFF_HDR *pRiffHdr;
    WAV_FMT_TRUNK_HDR *pFmtHdr;
    WAV_DATA_TRUNK_HDR *pDataHdr;

    pRiffHdr = (RIFF_HDR *)tmpBuf;
    pFmtHdr = (WAV_FMT_TRUNK_HDR *)(tmpBuf + RIFF_HDR_SIZE);
    pDataHdr = (WAV_DATA_TRUNK_HDR *)(tmpBuf + RIFF_HDR_SIZE + FMT_HDR_SIZE);
    pRiffHdr->riffID[0] = 'R';
    pRiffHdr->riffID[1] = 'I';
    pRiffHdr->riffID[2] = 'F';
    pRiffHdr->riffID[3] = 'F';
    pRiffHdr->riffSize = nBytesToWrite + WAV_FILE_HDR_SIZE - 8;
    pRiffHdr->riffFormat[0] = 'W';
    pRiffHdr->riffFormat[1] = 'A';
    pRiffHdr->riffFormat[2] = 'V';
    pRiffHdr->riffFormat[3] = 'E';

    pFmtHdr->fmtId[0] = 'f';
    pFmtHdr->fmtId[1] = 'm';
    pFmtHdr->fmtId[2] = 't';
    pFmtHdr->fmtId[3] = ' ';
    pFmtHdr->size = 16;
    pFmtHdr->fmtTag = 0x0014; /* G.723 */
    pFmtHdr->channels = 1;
    pFmtHdr->samplePerSec = 8000;
    pFmtHdr->avgBytesPerSec = 8000;
    pFmtHdr->blockAlign = 1;
    pFmtHdr->bitsPerSample = 8;

    pDataHdr->dataId[0] = 'd';
    pDataHdr->dataId[1] = 'a';
    pDataHdr->dataId[2] = 't';
    pDataHdr->dataId[3] = 'a';
    pDataHdr->size = nBytesToWrite;
    fwrite(tmpBuf,1,WAV_FILE_HDR_SIZE,fpwave);
}


/*
 * Pack output codes into bytes and write them to stdout.
 * Returns 1 if there is residual output, else returns 0.
 */
int
pack_output(
	unsigned		code,
	int			bits)
{
	static unsigned int	out_buffer = 0;
	static int		out_bits = 0;
	unsigned char		out_byte;

	out_buffer |= (code << out_bits);
	out_bits += bits;
	if (out_bits >= 8) {
		out_byte = out_buffer & 0xff;
		out_bits -= 8;
		out_buffer >>= 8;
		fwrite(&out_byte, sizeof (char), 1, pOutFile);
		nByes += 1;
	}
	return (out_bits > 0);
}

int main()
{
	struct g72x_state	state;
	int		    code;
	int			resid;
	int			in_coding;
	char     	in_buf[256];
	int		    (*enc_routine)();
	int			enc_bits;
	int         choice,i,result;

	g72x_init_state(&state);

	/* Set defaults parameters */
	in_coding = AUDIO_ENCODING_ALAW;

    printf("please input todo:(1,g723_24_encoder)\r\n");
    printf("..................(2,g723_40_encoder)\r\n");
    printf("your choice:");
    scanf("%d",&choice);
    
	/* Process encoding argument, if any */
	if(choice == 1) 
	{
		enc_routine = g723_24_encoder;
		enc_bits = 3;
    }
	else if(choice == 2)
	{
		enc_routine = g723_40_encoder;
		enc_bits = 5;
    }

	pInFile = fopen("yes.pcm","rb");
	if(pInFile == NULL)
	{
	    printf("open source file error!\r\n");
	    return -1;
	}
	pOutFile = fopen("yes723.wav","wb");
	if(pInFile == NULL)
	{
	    printf("open destination file error!\r\n");
	    return -1;
	}
	/* there we should write file header */
	WriteWAVEFileHeader(pOutFile, 0);
	/* Read input file and process ,first should skip file header */
	if(fread((void *)in_buf, 1, WAV_FILE_HDR_SIZE, pInFile) == WAV_FILE_HDR_SIZE)
	{
	    printf("p = %ld\r\n",ftell(pInFile));
	}
	while (1)
	{
	    result = fread((void *)in_buf, 1, READ_SAMPLES, pInFile);
	    if(result == READ_SAMPLES)
	    {
    	    for(i = 0;i < READ_SAMPLES;i++)
    	    {
        	    code = (*enc_routine)(in_buf[i],in_coding, &state);
        	    resid = pack_output(code, enc_bits);
    	    }
	    }
	    if(result < READ_SAMPLES)
	    {
	        for(i = 0;i < result;i++)
    	    {
        	    code = (*enc_routine)(in_buf[i],in_coding, &state);
        	    resid = pack_output(code, enc_bits);
    	    }
    	    break;
	    }
	}

	/* Write zero codes until all residual codes are written out */
	while (resid)
	{
		resid = pack_output(0, enc_bits);
	}
	
	/* write file header again */
	pOutFile = fopen("yes.wav","r+");
	WriteWAVEFileHeader(pOutFile, nByes);
	
	printf("convert over, size = %d\r\n",nByes);
	fclose(pInFile);
	fclose(pInFile);
	return 0;
}

⌨️ 快捷键说明

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