melp.c
上传用户:csczyc
上传日期:2021-02-19
资源大小:1051k
文件大小:7k
- /*
- 2.4 kbps MELP Proposed Federal Standard speech coder
- Fixed-point C code, version 1.0
- Copyright (c) 1998, Texas Instruments, Inc.
- Texas Instruments has intellectual property rights on the MELP
- algorithm. The Texas Instruments contact for licensing issues for
- commercial and non-government use is William Gordon, Director,
- Government Contracts, Texas Instruments Incorporated, Semiconductor
- Group (phone 972 480 7442).
- The fixed-point version of the voice codec Mixed Excitation Linear
- Prediction (MELP) is based on specifications on the C-language software
- simulation contained in GSM 06.06 which is protected by copyright and
- is the property of the European Telecommunications Standards Institute
- (ETSI). This standard is available from the ETSI publication office
- tel. +33 (0)4 92 94 42 58. ETSI has granted a license to United States
- Department of Defense to use the C-language software simulation contained
- in GSM 06.06 for the purposes of the development of a fixed-point
- version of the voice codec Mixed Excitation Linear Prediction (MELP).
- Requests for authorization to make other use of the GSM 06.06 or
- otherwise distribute or modify them need to be addressed to the ETSI
- Secretariat fax: +33 493 65 47 16.
- */
- /* */
- /* melp.c: Mixed Excitation LPC speech coder */
- /* */
- /* compiler include files */
- #include <stdio.h>
- #include "melp.h"
- #include "spbstd.h"
- #include "mat.h"
- /* compiler constants */
- #define ANA_SYN 0
- #define ANALYSIS 1
- #define SYNTHESIS 2
- /* note: CHSIZE is shortest integer number of words in channel packet */
- #define CHSIZE 9
- #define NUM_CH_BITS 54
- /* global variables for fixed-point library */
- int saturation = 0;// complexity,mark del
- //int max_complexity = 0;
- /* external memory */
- Shortword melpmode = ANA_SYN;
- char in_name[80], out_name[80];
- void main(int argc, char **argv)
- {
- // void parse(int argc, char **argv);
- Shortword i;
- Longword length, frame;
- Shortword eof_reached;
- Longword num_frames = 0;
- Shortword speech_in[FRAME];
- Shortword speech_out[FRAME];
- static struct melp_param melp_par; /* melp parameters */
- UShortword chbuf[CHSIZE];
- unsigned int tempbuf[CHSIZE];
- FILE *fp_in, *fp_out;
- /* Print user message */
- printf("n2.4 kb/s Federal Standard MELP speech codern");
- printf(" Fixed-point C simulation, version 1.0nn");
- /* Get input parameters from command line */
- //parse(argc, argv);
- sprintf(in_name,"D:\mm\MELP\melp_429\test.pcm");
sprintf(out_name,"D:\mm\MELP\melp_429\test_out.pcm");
- /* Open input, output, and parameter files */
- if (( fp_in = fopen(in_name,"rb")) == NULL ) {
- printf(" ERROR: cannot read file %s.n",in_name);
- exit(1);
- }
- if (( fp_out = fopen(out_name,"wb")) == NULL ) {
- printf(" ERROR: cannot write file %s.n",out_name);
- exit(1);
- }
- /* Check length of channel input if needed */
- if (melpmode == SYNTHESIS) {
- fseek(fp_in,0L,2); /* go to end of input file */
- length = ftell(fp_in);
- rewind(fp_in);
- num_frames = (Longword)(0.5 + length * (8.0/NUM_CH_BITS) * (6.0/32));
-
- }
- /* Initialize MELP analysis and synthesis */
- if (melpmode != SYNTHESIS)
- melp_ana_init();
- if (melpmode != ANALYSIS)
- melp_syn_init();
- /* Run MELP coder on input signal */
- frame = 0;
- melp_par.chptr = chbuf;
- melp_par.chbit = 0;
- eof_reached = 0;
- while (eof_reached == 0) {
- #if (COMPLEXITY_COUNT)
- complexity_reset();
- #endif
- /* Perform MELP analysis */
- if (melpmode != SYNTHESIS) {
- /* read input speech */
- length = readbl(speech_in,fp_in,FRAME);
- if (length < FRAME) {
- v_zap(&speech_in[length],(Shortword)(FRAME-length));
- eof_reached = 1;
- }
- /* Run MELP analyzer */
- if (melpmode == ANA_SYN) {
- /* reset pointers to short channel buffer */
- melp_par.chptr = chbuf;
- melp_par.chbit = 0;
- }
- melp_ana(speech_in,&melp_par);
-
- /* Write channel output if needed */
- if (melpmode == ANALYSIS && melp_par.chbit == 0) {
- for (i = 0; i < CHSIZE; i++)
- tempbuf[i] = (int)chbuf[i];
- fwrite((void *) tempbuf,sizeof(int),
- melp_par.chptr-chbuf,fp_out);
- /* reset pointer to short channel buffer */
- melp_par.chptr = chbuf;
- }
- if (melp_par.chptr >= &chbuf[CHSIZE] && melp_par.chbit > 0) {
- printf("nERROR: Ran out of channel buffer memory.n");
- exit(1);
- }
- }
- /* Perform MELP synthesis (skip first frame) */
- if (melpmode != ANALYSIS) {
- if (melpmode == ANA_SYN) {
- /* reset pointers to short channel buffer */
- melp_par.chptr = chbuf;
- melp_par.chbit = 0;
- }
- /* Read channel input if needed */
- if (melpmode == SYNTHESIS && melp_par.chbit == 0) {
- fread((void *) tempbuf,sizeof(int),CHSIZE,fp_in);
- for (i = 0; i <CHSIZE; i++)
- chbuf[i] = (Shortword)tempbuf[i];
- /* reset pointer to short channel buffer */
- melp_par.chptr = chbuf;
- }
- melp_syn(&melp_par,speech_out);
- if (frame > 0)
- writebl(speech_out,fp_out,FRAME);
- }
- frame++;
- if (melpmode == SYNTHESIS) {
- if (frame >= num_frames)
- eof_reached = 1;
- }
- }
-
- /* Write channel output if needed */
- if (melpmode == ANALYSIS) {
- if (melp_par.chbit > 0) {
- for (i = 0; i < CHSIZE; i++)
- tempbuf[i] = (int)chbuf[i];
- fwrite((void *) tempbuf,sizeof(int),melp_par.chptr-chbuf+1,fp_out);
- }
- else {
- for (i = 0; i < CHSIZE; i++)
- tempbuf[i] = (int)chbuf[i];
- fwrite((void *) tempbuf,sizeof(int),melp_par.chptr-chbuf,fp_out);
- }
- }
- #if (COMPLEXITY_COUNT)
- complexity_print();
- #endif
- fclose(fp_in);
- fclose(fp_out);
- }
- void parse(int argc,char **argv){
- int error_flag;
- error_flag = 0;
- if (argc < 2)
- error_flag = 1;
- melpmode = ANA_SYN;
- while ((--argc>0) && ((*++argv)[0] == '-')){
- switch ((*argv)[1]){
- case 'a':
- melpmode=ANALYSIS; break;
- case 's':
- melpmode=SYNTHESIS; break;
- case 'i':
- sscanf(*++argv,"%s",in_name); --argc; break;
- case 'o':
- sscanf(*++argv,"%s",out_name); --argc; break;
- default:
- error_flag = 1;
- break;
- }
- }
- if (error_flag == 1) {
- fprintf(stderr,"Usage:nn");
- fprintf(stderr,"Analysis/synthesis: melp -i infile -o outfilen");
- fprintf(stderr,"Analysis only: melp -a -i infile -o bitfilen");
- fprintf(stderr,"Synthesis only: melp -s -i bitfile -o outfilen");
- exit(1);
- }
- if (melpmode == ANA_SYN)
- printf(" MELP analysis and synthesis n");
- else if (melpmode == ANALYSIS)
- printf(" MELP analysis n");
- else if (melpmode == SYNTHESIS)
- printf(" MELP synthesis n");
- printf(" input from %sn output to %s.n",in_name, out_name);
- }