📄 gsm_hr_bak.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0079)http://ece.ut.ac.ir/classpages/VLSI/GSM%20Vocoder/Half-Rate/C-Source/GSM_HR.BAK -->
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2600.0" name=GENERATOR></HEAD>
<BODY><PRE>/**************************************************************************
*
* File Name: gsm_hr.c
*
* Purpose:
*
* This file contains the main routine for the GSM Half Rate speech
* codec. The code for the speech coder, including the VAD, DTX,
* Comfort Noise, bad frame handling (error concealment), and codec
* homing functions is in the files listed below:
*
* gsm_hr.c globdefs.c homing.c host.c
* mathdp31.c mathhalf.c sp_dec.c sp_enc.c
* sp_frm.c sp_rom.c sp_sfrm.c vad.c
* dtx.c err_conc.c
*
* This particular file only has the level 0, main(), and all level
* 1, main()'s daughter functions, in it.
*
* Details on how to run gsm_hr are in the readme.txt file.
*
* Below is a listing of all the functions appearing in the file.
* The ordering is hierarchical.
*
* main() "gsm_hr", main program.
* encode() - encodes a speech file, outputs an encoded parameter file
* decode() - decodes a parameter file, outputs a decoded speech file
*
**************************************************************************/
/*_________________________________________________________________________
| |
| Include Files |
|_________________________________________________________________________|
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
#include "gsm_hr.h"
#include "homing.h"
#include "host.h"
#include "sp_dec.h"
#include "sp_enc.h"
#include "reid.h"
#include "typedefs.h"
/* #include "comp.h" */
#define NUM_SAMPLES 160
#define NUM_HEADER 58
/****************************************************************************
*
* PROGRAM NAME: gsm_hr
*
* PURPOSE:
* gsm_hr - main program for the GSM Half-Rate speech coder.
* Allows running one of the following operational modes:
* 0. Encoder only with speech input / encoded parameter data output.
* 1. Decoder only with speech parameter data input / speech output.
*
* INPUT:
* inputFileName 0. speech file in 8 kHz, pcm format.
* 1. speech parameter file in decoder input format.
* <number of frames> - number of frames to be processed (optional).
* <dtx|nodtx> - switch to enable/disable the DTX functions.
*
* OUTPUT:
* outputFileName 0. encoded paramter output file.
* 1. synthesized speech file in 8 kHz, pcm format.
*
* RETURN:
* none
*
* REFERENCES: Sub-clause 4.0 of GSM Recomendation 06.20
*
* KEYWORDS: main, gsm_hr, speech codec, top level
*
***************************************************************************/
int main(void) /* OK */
{
/*_________________________________________________________________________
| |
| Local Constants |
|_________________________________________________________________________|
*/
/*_________________________________________________________________________
| |
| Automatic Variables |
|_________________________________________________________________________|
*/
int iDoneFrm,
iMaxNumFrms,
option,
i;
FILE *pfileInFile1,
*pfileInFile2,
*pfileOutFile;
/*_________________________________________________________________________
| |
| Executable Code |
|_________________________________________________________________________|
*/
/* This section changes the *.wav file so that a natural number of frames
become available(n*160 sample 16 bits) */
int iNumRead1,iNumRead2,cntr_fr;
Shortword pswSamplesRead1[NUM_SAMPLES],pswSamplesRead2[NUM_SAMPLES];
char header[NUM_HEADER],ch;
char inwavpath[50],outwavpath[50],inppath[50],codpaths[50],codpathr[50],
decpath[50],outpath[50],cmppath[50];
/* Shortword pswSamplesRead[NUM_PARA],pswSpeechParas[NUM_PARA];*/
char lastCompParas;
/* Shortword pswCompParas[NUM_COMPARA];*/
clrscr();
printf("\nInput file(*.wav)?");
scanf("%s",inwavpath);
printf("\nOutput file(*.wav)?");
scanf("%s",outwavpath);
strcpy(inppath,"tmp.inp");
pfileInFile1 = fopen(inwavpath, "rb");
if (pfileInFile1==NULL)
{
printf("Cannot open %s as input wave file.\n",inwavpath);
exit(0);
}
pfileInFile2 = fopen(inppath, "wb");
if (pfileInFile2==NULL)
{
printf("Cannot open %s as input file for vocoder.\n",inppath);
exit(0);
}
i=fseek(pfileInFile1,0L,0);
if (i!=0)
{
printf("Cannot fseek %s",inwavpath);
exit(0);
}
/*******************Reads the header of the *.wav file**********************/
i=fread((char *) header, 1,NUM_HEADER, pfileInFile1);
if (i!=NUM_HEADER)
{
printf("Can not read the header of %s",inwavpath);
exit(0);
}
/******************Change the *.wav file to the *.inp file******************/
/*Remove the header of the *.wav file and write n*160 samples to *.inp file*/
iNumRead1=NUM_SAMPLES;
cntr_fr=0;
while (iNumRead1==NUM_SAMPLES)
{
iNumRead1 = fread((char *) pswSamplesRead1, sizeof (Shortword),
NUM_SAMPLES, pfileInFile1);
if(iNumRead1==NUM_SAMPLES)
{
fwrite((char *) pswSamplesRead1, sizeof (Shortword),
NUM_SAMPLES, pfileInFile2);
cntr_fr++;
}
}
printf("File %s has %d frames.",inppath,cntr_fr);
fclose(pfileInFile1);
fclose(pfileInFile2);
/******This section encodes the "tmp.inp" and make the "tmp.cod" file"******/
/* iMaxNumFrms = cntr_fr;*/
iMaxNumFrms = 100;
/* giDTXon = 1;*/ /* enable */
giDTXon = 0; /* disable */
/* option = 0;*/ /* enc*/
/* option = 1;*/ /* dec */
pfileInFile1 = fopen(inppath, "rb");
strcpy(codpaths,"tmps.cod");
pfileInFile2 = fopen(codpaths, "wb");
if (pfileInFile1 == NULL)
{
printf("error: can not open file %s\n", inppath);
exit(1);
}
if (pfileInFile2 == NULL)
{
printf("error: can not open file %s\n", codpaths);
exit(1);
}
/***************************Here encoding starts****************************/
puts("\n\n");
puts(" ****************************************");
puts(" * *");
puts(" * GSM Half-Rate Speech Coder *");
puts(" * *");
puts(" * *");
printf(" * %s *\n", VERSION);
printf(" * %s *\n", DATE);
puts(" * *");
puts(" ****************************************");
puts("\n");
printf("option: ");
puts("enc (speech encoder)");
if (giDTXon)
printf("DTX mode: enabled\n");
else
printf("DTX mode: disabled\n");
printf("input file: %s\n", inppath);
printf("output file: %s\n\n", codpaths);
resetEnc();
for (giFrmCnt = 1, iDoneFrm = 0;
!iDoneFrm && giFrmCnt <= iMaxNumFrms;
giFrmCnt++)
{
#ifndef SILENT
printf("encoding frame %d \r", giFrmCnt);
#endif
if (encode(pfileInFile1, pfileInFile2))
iDoneFrm = 1;
}
if (iDoneFrm)
giFrmCnt--;
printf(" %d speech frames encoded\n", giFrmCnt - 1);
fclose(pfileInFile1);
fclose(pfileInFile2);
/**************************Here encoding ends*******************************/
/************************The encoded file is compressed*********************/
/* pfileInFile1 = fopen(codpaths, "rb");
strcpy(cmppath,"tmp.cmp");
pfileInFile2 = fopen(cmppath, "wb");
if (pfileInFile1 == NULL)
{
printf("error: can not open file %s\n", codpaths);
exit(1);
}
if (pfileInFile2 == NULL)
{
printf("error: can not open file %s\n", cmppath);
exit(1);
}
iNumRead1=NUM_PARA;
while (iNumRead1==NUM_PARA)
{
iNumRead1=fread((char *) pswSamplesRead, sizeof (Shortword),
NUM_PARA, pfileInFile1);
if (iNumRead1==NUM_PARA)
{
Compress(&pswSamplesRead[0],pswCompParas,&lastCompParas);
i = fwrite((char *) pswCompParas, sizeof (Shortword),
7,pfileInFile2);
i = fwrite((char *) &lastCompParas, sizeof (char),
1,pfileInFile2);
}
}
fclose(pfileInFile1);
fclose(pfileInFile2);
printf("\nThe tmps.cod file is compressed to the tmp.cmp file.\n");*/
/****************The compressed file is sent to the reciever****************/
/*********************Reciever gets the compressed file*********************/
/***********************From here decoding begins***************************/
/*********The compressed file is decompressed and becomes a *.cod file******/
/* pfileInFile1 = fopen(cmppath, "rb");
strcpy(codpathr,"tmpr.cod");
pfileInFile2 = fopen(codpathr, "wb");
if (pfileInFile1 == NULL)
{
printf("error: can not open file %s\n", cmppath);
exit(1);
}
if (pfileInFile2 == NULL)
{
printf("error: can not open file %s\n", codpathr);
exit(1);
}
iNumRead1=NUM_COMPARA+1;
i=0;
while ((iNumRead1+i)==NUM_COMPARA+1)
{
iNumRead1=fread((char *) pswCompParas, sizeof (Shortword),
NUM_COMPARA, pfileInFile1);
i=fread((char *) lastCompParas, sizeof (char),
1, pfileInFile1);
if ((iNumRead1+i)==NUM_COMPARA+1)
{
Decompress(pswCompParas,lastCompParas,pswSpeechParas);
fwrite((char *) pswSpeechParas, sizeof (Shortword), 20,pfileInFile2);
}
}
fclose(pfileInFile1);
fclose(pfileInFile2);
printf("\nThe tmp.cmp file is decompressed to the tmpr.cod file.\n");*/
/****In this section the reid function must be done to change the *.cod file
to the *.dec file ******/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -