melp.c
上传用户:luckfish
上传日期:2021-12-16
资源大小:77k
文件大小:5k
源码类别:

语音压缩

开发平台:

Visual C++

  1. /*
  2. 2.4 kbps MELP Proposed Federal Standard speech coder
  3. version 1.2
  4. Copyright (c) 1996, Texas Instruments, Inc.  
  5. Texas Instruments has intellectual property rights on the MELP
  6. algorithm.  The Texas Instruments contact for licensing issues for
  7. commercial and non-government use is William Gordon, Director,
  8. Government Contracts, Texas Instruments Incorporated, Semiconductor
  9. Group (phone 972 480 7442).
  10. */
  11. /*                                                                  */
  12. /*  melp.c: Mixed Excitation LPC speech coder                       */
  13. /*                                                                  */
  14. /*  compiler include files  */
  15. #include <stdio.h>
  16. #include "melp.h"
  17. #include "spbstd.h"
  18. #include "mat.h"
  19. /*  compiler constants */
  20. #define ANA_SYN 0
  21. #define ANALYSIS 1
  22. #define SYNTHESIS 2
  23. /* note: CHSIZE is shortest integer number of words in channel packet */
  24. #define CHSIZE 9
  25. #define NUM_CH_BITS 54
  26. /*  external memory */
  27. int melpmode = ANA_SYN;
  28. char in_name[80], out_name[80];
  29.  
  30. void main(int argc, char **argv)
  31. {
  32.     void parse(int argc, char **argv);
  33.     int length, frame, eof_reached;
  34.     int num_frames = 0;
  35.     float speech_in[FRAME];
  36.     float speech_out[FRAME];
  37.     static struct melp_param melp_par;      /* melp parameters */
  38.     unsigned int chbuf[CHSIZE];
  39.     FILE *fp_in, *fp_out;
  40.     /* Print user message */
  41.     printf("n2.4 kb/s Proposed Federal Standard MELP speech codern");
  42.     printf("  C simulation, version 1.2nn");
  43.     /* Get input parameters from command line */
  44.     parse(argc, argv);
  45.     /* Open input, output, and parameter files */
  46.     if (( fp_in = fopen(in_name,"rb")) == NULL ) {
  47. printf("  ERROR: cannot read file %s.n",in_name);
  48. exit(1);
  49.     }
  50.     if (( fp_out = fopen(out_name,"wb")) == NULL ) {
  51. printf("  ERROR: cannot write file %s.n",out_name);
  52. exit(1);
  53.     }
  54.     /* Check length of channel input if needed */
  55.     if (melpmode == SYNTHESIS) {
  56. fseek(fp_in,0L,2);
  57. length = ftell(fp_in);
  58. rewind(fp_in);
  59. num_frames = 0.5 + length * (8.0 / NUM_CH_BITS) * (6.0/32);
  60.     }
  61.     /* Initialize MELP analysis and synthesis */
  62.     if (melpmode != SYNTHESIS)
  63.       melp_ana_init();
  64.     if (melpmode != ANALYSIS)
  65.       melp_syn_init();
  66.     /* Run MELP coder on input signal */
  67.     frame = 0;
  68.     melp_par.chptr = chbuf;
  69.     melp_par.chbit = 0;
  70.     eof_reached = 0;
  71.     while (eof_reached == 0) {
  72. /* Perform MELP analysis */
  73. if (melpmode != SYNTHESIS) {
  74.     /* read input speech */
  75.     length = readbl(speech_in,fp_in,FRAME);
  76.     if (length < FRAME) {
  77. v_zap(&speech_in[length],FRAME-length);
  78. eof_reached = 1;
  79.     }
  80.     /* Run MELP analyzer */
  81.     if (melpmode == ANA_SYN) {
  82. /* reset pointers to short channel buffer */
  83. melp_par.chptr = chbuf;
  84. melp_par.chbit = 0;
  85.     }
  86.     melp_ana(speech_in,&melp_par);
  87.  
  88.     /* Write channel output if needed */
  89.     if (melpmode == ANALYSIS && melp_par.chbit == 0) {
  90. fwrite((void *) chbuf,sizeof(int),melp_par.chptr-chbuf,fp_out);
  91. /* reset pointer to short channel buffer */
  92. melp_par.chptr = chbuf;
  93.     }
  94.     if (melp_par.chptr >= &chbuf[CHSIZE] && melp_par.chbit > 0) {
  95. printf("nERROR: Ran out of channel buffer memory.n");
  96. exit(1);
  97.     }
  98. }          
  99. /* Perform MELP synthesis (skip first frame) */
  100. if (melpmode != ANALYSIS) {
  101.     if (melpmode == ANA_SYN) {
  102. /* reset pointers to short channel buffer */
  103. melp_par.chptr = chbuf;
  104. melp_par.chbit = 0;
  105.     }
  106.     /* Read channel input if needed */
  107.     if (melpmode == SYNTHESIS && melp_par.chbit == 0) {
  108. fread((void *) chbuf,sizeof(int),CHSIZE,fp_in);
  109. /* reset pointer to short channel buffer */
  110. melp_par.chptr = chbuf;
  111.     }
  112.     melp_syn(&melp_par,speech_out);
  113.     if (frame > 0)
  114.       writebl(speech_out,fp_out,FRAME);
  115. }
  116. frame++; 
  117. if (melpmode == SYNTHESIS) {
  118.     if (frame >= num_frames)
  119.       eof_reached = 1;
  120. }
  121.     }
  122.  
  123.     /* Write channel output if needed */
  124.     if (melpmode == ANALYSIS) {
  125. if (melp_par.chbit > 0)
  126.   fwrite((void *) chbuf,sizeof(int),melp_par.chptr-chbuf+1,fp_out);
  127. else
  128.   fwrite((void *) chbuf,sizeof(int),melp_par.chptr-chbuf,fp_out);
  129.     }
  130.     fclose(fp_in);
  131.     fclose(fp_out);
  132. }
  133. void parse(int argc,char **argv){
  134.     int error_flag;
  135.     error_flag = 0;
  136.     if (argc < 2)
  137.       error_flag = 1;
  138.     melpmode = ANA_SYN;
  139.     while ((--argc>0) && ((*++argv)[0] == '-')){
  140. switch ((*argv)[1]){
  141.   case 'a':
  142.     melpmode=ANALYSIS; break;
  143.   case 's':
  144.     melpmode=SYNTHESIS; break;
  145.   case 'i':
  146.     sscanf(*++argv,"%s",in_name); --argc; break;
  147.   case 'o':
  148.     sscanf(*++argv,"%s",out_name); --argc; break;
  149.   default:
  150.     error_flag = 1;
  151.     break;
  152. }
  153.     }
  154.     if (error_flag == 1) {
  155. fprintf(stderr,"Usage:nn"); 
  156. fprintf(stderr,"Analysis/synthesis: melp -i infile -o outfilen"); 
  157. fprintf(stderr,"Analysis only:  melp -a -i infile  -o bitfilen"); 
  158. fprintf(stderr,"Synthesis only: melp -s -i bitfile -o outfilen"); 
  159.         exit(1);
  160.     }
  161.     if (melpmode == ANA_SYN)
  162.       printf(" MELP analysis and synthesis n");
  163.     else if (melpmode == ANALYSIS)
  164.       printf(" MELP analysis n");
  165.     else if (melpmode == SYNTHESIS)
  166.       printf(" MELP synthesis n");
  167.     printf("   input from %sn   output to %s.n",in_name, out_name);
  168. }