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

语音压缩

开发平台:

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.     Name: melp_syn.c
  13.     Description: MELP synthesis
  14.       This program takes the new parameters for a speech
  15.       frame and synthesizes the output speech.  It keeps
  16.       an internal record of the previous frame parameters
  17.       to use for interpolation.
  18.     Inputs:
  19.       *par - MELP parameter structure
  20.     Outputs: 
  21.       speech[] - output speech signal
  22.     Returns: void
  23. */
  24. /* compiler include files */
  25.  
  26. #include <stdio.h>
  27. #include <math.h>
  28. #include "melp.h"
  29. #include "spbstd.h"
  30. #include "lpc.h"
  31. #include "mat.h"
  32. #include "vq.h"
  33. #include "fs.h"
  34. /* compiler constants */
  35.  
  36. #if (MIX_ORD > DISP_ORD)
  37. #define BEGIN MIX_ORD
  38. #else
  39. #define BEGIN DISP_ORD
  40. #endif
  41. #define TILT_ORD 1
  42. #define SYN_GAIN 1000.0
  43. #define SCALEOVER 10
  44. #define PDEL SCALEOVER
  45. /* external memory references */
  46.  
  47. extern float bp_cof[NUM_BANDS][MIX_ORD+1];
  48. extern float disp_cof[DISP_ORD+1];
  49. extern float msvq_cb[];
  50. extern float fsvq_cb[];
  51. extern int fsvq_weighted;
  52. /* temporary memory */
  53. static float sigbuf[BEGIN+PITCHMAX];
  54. static float sig2[BEGIN+PITCHMAX];
  55. static float fs_real[PITCHMAX];
  56. /* permanent memory */
  57.  
  58. static int firstcall = 1; /* Just used for noise gain init */
  59. static int prev_gain_err;
  60. static float sigsave[PITCHMAX];
  61. static struct melp_param prev_par;
  62. static int syn_begin;
  63. static float prev_scale;
  64. static float noise_gain = MIN_NOISE;
  65. static float pulse_del[MIX_ORD],noise_del[MIX_ORD];
  66. static float lpc_del[LPC_ORD],ase_del[LPC_ORD],tilt_del[TILT_ORD];
  67. static float disp_del[DISP_ORD];
  68. static struct msvq_param vq_par;  /* MSVQ parameters */
  69. static struct msvq_param fs_vq_par;  /* Fourier series VQ parameters */
  70. static float w_fs_inv[NUM_HARM];
  71. /* these can be saved or recomputed */
  72. static float prev_pcof[MIX_ORD+1],prev_ncof[MIX_ORD+1];
  73. static float prev_tilt;
  74. static float prev_gain;
  75. void melp_syn(struct melp_param *par,float sp_out[])
  76. {
  77.     int i, gaincnt;
  78.     int erase;
  79.     int length;
  80.     float intfact, ifact, ifact_gain;
  81.     float gain,pulse_gain,pitch,jitter;
  82.     float curr_tilt,tilt_cof[TILT_ORD+1];
  83.     float temp,sig_prob;
  84.     float lsf[LPC_ORD+1];
  85.     float lpc[LPC_ORD+1];
  86.     float ase_num[LPC_ORD+1],ase_den[LPC_ORD+1];
  87.     float curr_pcof[MIX_ORD+1],curr_ncof[MIX_ORD+1];
  88.     float pulse_cof[MIX_ORD+1],noise_cof[MIX_ORD+1];
  89.     
  90.     /* Copy previous period of processed speech to output array */
  91.     if (syn_begin > 0) {
  92. if (syn_begin > FRAME) {
  93.     v_equ(&sp_out[0],&sigsave[0],FRAME);
  94.     /* past end: save remainder in sigsave[0] */
  95.     v_equ(&sigsave[0],&sigsave[FRAME],syn_begin-FRAME);
  96. }
  97. else 
  98.   v_equ(&sp_out[0],&sigsave[0],syn_begin);
  99.     }
  100.     
  101.     erase = 0; /* no erasures yet */
  102.     
  103.     /* Update MSVQ information */
  104.     par->msvq_stages = vq_par.num_stages;
  105.     par->msvq_bits = vq_par.num_bits;
  106.     par->msvq_levels = vq_par.num_levels;
  107.     par->msvq_index = vq_par.indices;
  108.     par->fsvq_index = fs_vq_par.indices;
  109.     
  110.     /* Read and decode channel input buffer */
  111.     erase = melp_chn_read(par,&prev_par);
  112.     if (par->uv_flag != 1 && !erase) { 
  113. /* Un-weight Fourier magnitudes */
  114. window(&par->fs_mag[0],w_fs_inv,&par->fs_mag[0],NUM_HARM);
  115.     }
  116.     /* Update adaptive noise level estimate based on last gain */
  117.     if (firstcall) {
  118. firstcall = 0;
  119. noise_gain = par->gain[NUM_GAINFR-1];
  120.     }
  121.     
  122.     else if (!erase) {
  123. for (i = 0; i < NUM_GAINFR; i++) {
  124.     noise_est(par->gain[i],&noise_gain,UPCONST,DOWNCONST,
  125.       MIN_NOISE,MAX_NOISE);
  126.     /* Adjust gain based on noise level (noise suppression) */
  127.     noise_sup(&par->gain[i],noise_gain,MAX_NS_SUP,MAX_NS_ATT,NFACT);
  128. }
  129.     
  130.     }
  131.     
  132.     /* Clamp LSP bandwidths to avoid sharp LPC filters */
  133.     lpc_clamp(par->lsf,BWMIN,LPC_ORD);
  134.     
  135.     /* Calculate spectral tilt for current frame for spectral enhancement */
  136.     tilt_cof[0] = 1.0;
  137.     lpc_lsp2pred(par->lsf,lpc,LPC_ORD);
  138.     lpc_pred2refl(lpc,sig2,LPC_ORD);
  139.     if (sig2[1] < 0.0)
  140.       curr_tilt = 0.5*sig2[1];
  141.     else
  142.       curr_tilt = 0.0;
  143.     
  144.     /* Disable pitch interpolation for high-pitched onsets */
  145.     if (par->pitch < 0.5*prev_par.pitch && 
  146. par->gain[0] > 6.0 + prev_par.gain[NUM_GAINFR-1]) {
  147. /* copy current pitch into previous */
  148. prev_par.pitch = par->pitch;
  149.     }
  150.     
  151.     /* Set pulse and noise coefficients based on voicing strengths */
  152.     v_zap(curr_pcof,MIX_ORD+1);
  153.     v_zap(curr_ncof,MIX_ORD+1);
  154.     for (i = 0; i < NUM_BANDS; i++) {
  155. if (par->bpvc[i] > 0.5)
  156.     v_add(curr_pcof,&bp_cof[i][0],MIX_ORD+1);
  157. else
  158.     v_add(curr_ncof,&bp_cof[i][0],MIX_ORD+1);
  159.     }
  160.     /* Process each pitch period */
  161.     
  162.     while (syn_begin < FRAME) {
  163. /* interpolate previous and current parameters */
  164. ifact = ((float) syn_begin ) / FRAME;
  165. if (syn_begin >= GAINFR) {
  166.     gaincnt = 2;
  167.     ifact_gain = ((float) syn_begin-GAINFR) / GAINFR;
  168. }
  169. else {
  170.     gaincnt = 1;
  171.     ifact_gain = ((float) syn_begin) / GAINFR;
  172. }
  173. /* interpolate gain */
  174. if (gaincnt > 1) {
  175.     gain = ifact_gain*par->gain[gaincnt-1] + 
  176. (1.0-ifact_gain)*par->gain[gaincnt-2];
  177. }
  178. else {
  179.     gain = ifact_gain*par->gain[gaincnt-1] + 
  180. (1.0-ifact_gain)*prev_par.gain[NUM_GAINFR-1];
  181. }
  182. /* Set overall interpolation path based on gain change */
  183. temp = par->gain[NUM_GAINFR-1] - prev_par.gain[NUM_GAINFR-1];
  184. if (fabs(temp) > 6) {
  185.     /* Power surge: use gain adjusted interpolation */
  186.     intfact = (gain - prev_par.gain[NUM_GAINFR-1]) / temp;
  187.     if (intfact > 1.0)
  188. intfact = 1.0;
  189.     if (intfact < 0.0)
  190. intfact = 0.0;
  191. }
  192. else
  193.     /* Otherwise, linear interpolation */
  194.     intfact = ((float) syn_begin) / FRAME;
  195. /* interpolate LSF's and convert to LPC filter */
  196. interp_array(prev_par.lsf,par->lsf,lsf,intfact,LPC_ORD+1);
  197. lpc_lsp2pred(lsf,lpc,LPC_ORD);
  198. /* Check signal probability for adaptive spectral enhancement filter */
  199. sig_prob = lin_int_bnd(gain,noise_gain+12.0,noise_gain+30.0,
  200.        0.0,1.0);
  201. /* Calculate adaptive spectral enhancement filter coefficients */
  202. ase_num[0] = 1.0;
  203. lpc_bw_expand(lpc,ase_num,sig_prob*ASE_NUM_BW,LPC_ORD);
  204. lpc_bw_expand(lpc,ase_den,sig_prob*ASE_DEN_BW,LPC_ORD);
  205. tilt_cof[1] = sig_prob*(intfact*curr_tilt + 
  206. (1.0-intfact)*prev_tilt);
  207. /* interpolate pitch and pulse gain */
  208. pitch = intfact*par->pitch + (1.0-intfact)*prev_par.pitch;
  209. pulse_gain = SYN_GAIN*sqrt(pitch);
  210. /* interpolate pulse and noise coefficients */
  211. temp = sqrt(ifact);
  212. interp_array(prev_pcof,curr_pcof,pulse_cof,temp,MIX_ORD+1);
  213. interp_array(prev_ncof,curr_ncof,noise_cof,temp,MIX_ORD+1);
  214. /* interpolate jitter */
  215. jitter = ifact*par->jitter + 
  216.     (1.0-ifact)*prev_par.jitter;
  217. /* convert gain to linear */
  218. gain = pow(10.0,0.05*gain);
  219. /* Set period length based on pitch and jitter */
  220. rand_num(&temp,1.0,1);
  221. length = pitch * (1.0-jitter*temp) + 0.5;
  222. if (length < PITCHMIN)
  223.     length = PITCHMIN;
  224. if (length > PITCHMAX)
  225.     length = PITCHMAX;
  226. /* Use inverse DFT for pulse excitation */
  227. fill(fs_real,1.0,length);
  228. fs_real[0] = 0.0;
  229. interp_array(prev_par.fs_mag,par->fs_mag,&fs_real[1],intfact,
  230.      NUM_HARM);
  231. idft_real(fs_real,&sigbuf[BEGIN],length);
  232. /* Delay overall signal by PDEL samples (circular shift) */
  233. /* use fs_real as a scratch buffer */
  234. v_equ(fs_real,&sigbuf[BEGIN],length);
  235. v_equ(&sigbuf[BEGIN+PDEL],fs_real,length-PDEL);
  236. v_equ(&sigbuf[BEGIN],&fs_real[length-PDEL],PDEL);
  237. /* Scale by pulse gain */
  238. v_scale(&sigbuf[BEGIN],pulse_gain,length);
  239. /* Filter and scale pulse excitation */
  240. v_equ(&sigbuf[BEGIN-MIX_ORD],pulse_del,MIX_ORD);
  241. v_equ(pulse_del,&sigbuf[length+BEGIN-MIX_ORD],MIX_ORD);
  242. zerflt(&sigbuf[BEGIN],pulse_cof,&sigbuf[BEGIN],MIX_ORD,length);
  243. /* Get scaled noise excitation */
  244. rand_num(&sig2[BEGIN],(1.732*SYN_GAIN),length);
  245. /* Filter noise excitation */
  246. v_equ(&sig2[BEGIN-MIX_ORD],noise_del,MIX_ORD);
  247. v_equ(noise_del,&sig2[length+BEGIN-MIX_ORD],MIX_ORD);
  248. zerflt(&sig2[BEGIN],noise_cof,&sig2[BEGIN],MIX_ORD,length);
  249. /* Add two excitation signals (mixed excitation) */
  250. v_add(&sigbuf[BEGIN],&sig2[BEGIN],length);
  251. /* Adaptive spectral enhancement */
  252. v_equ(&sigbuf[BEGIN-LPC_ORD],ase_del,LPC_ORD);
  253. lpc_synthesis(&sigbuf[BEGIN],&sigbuf[BEGIN],ase_den,LPC_ORD,length);
  254. v_equ(ase_del,&sigbuf[BEGIN+length-LPC_ORD],LPC_ORD);
  255. zerflt(&sigbuf[BEGIN],ase_num,&sigbuf[BEGIN],LPC_ORD,length);
  256. v_equ(&sigbuf[BEGIN-TILT_ORD],tilt_del,TILT_ORD);
  257. v_equ(tilt_del,&sigbuf[length+BEGIN-TILT_ORD],TILT_ORD);
  258. zerflt(&sigbuf[BEGIN],tilt_cof,&sigbuf[BEGIN],TILT_ORD,length);
  259. /* Perform LPC synthesis filtering */
  260. v_equ(&sigbuf[BEGIN-LPC_ORD],lpc_del,LPC_ORD);
  261. lpc_synthesis(&sigbuf[BEGIN],&sigbuf[BEGIN],lpc,LPC_ORD,length);
  262. v_equ(lpc_del,&sigbuf[length+BEGIN-LPC_ORD],LPC_ORD);
  263. /* Adjust scaling of synthetic speech */
  264. scale_adj(&sigbuf[BEGIN],gain,&prev_scale,length,SCALEOVER);
  265. /* Implement pulse dispersion filter */
  266. v_equ(&sigbuf[BEGIN-DISP_ORD],disp_del,DISP_ORD);
  267. v_equ(disp_del,&sigbuf[length+BEGIN-DISP_ORD],DISP_ORD);
  268. zerflt(&sigbuf[BEGIN],disp_cof,&sigbuf[BEGIN],DISP_ORD,length);
  269. /* Copy processed speech to output array (not past frame end) */
  270. if (syn_begin+length > FRAME) {
  271.     v_equ(&sp_out[syn_begin],&sigbuf[BEGIN],FRAME-syn_begin);
  272.     /* past end: save remainder in sigsave[0] */
  273.     v_equ(&sigsave[0],&sigbuf[BEGIN+FRAME-syn_begin],
  274.   length-(FRAME-syn_begin));
  275. }
  276. else
  277.     v_equ(&sp_out[syn_begin],&sigbuf[BEGIN],length);
  278. /* Update syn_begin for next period */
  279. syn_begin += length;
  280.     }
  281.     /* Save previous pulse and noise filters for next frame */
  282.     v_equ(prev_pcof,curr_pcof,MIX_ORD+1);
  283.     v_equ(prev_ncof,curr_ncof,MIX_ORD+1);
  284.     
  285.     /* Copy current parameters to previous parameters for next time */
  286.     prev_par = *par;
  287.     prev_tilt = curr_tilt;
  288.     
  289.     /* Update syn_begin for next frame */
  290.     syn_begin -= FRAME;
  291.     
  292. }
  293. /* 
  294.  *
  295.  * Subroutine melp_syn_init(): perform initialization for melp 
  296.  * synthesis
  297.  *
  298.  */
  299. void melp_syn_init()
  300. {
  301.     int i;
  302.     float w_fs[NUM_HARM];
  303.     v_zap(prev_par.gain,NUM_GAINFR);
  304.     prev_par.pitch = UV_PITCH;
  305.     prev_par.lsf[0] = 0.0;
  306.     for (i = 1; i < LPC_ORD+1; i++)
  307.       prev_par.lsf[i] = prev_par.lsf[i-1] + (1.0/(LPC_ORD+1));
  308.     prev_par.jitter = 0.0;
  309.     v_zap(&prev_par.bpvc[0],NUM_BANDS);
  310.     prev_tilt=0.0;
  311.     prev_gain = 0.0;
  312.     prev_scale = 0.0;
  313.     syn_begin = 0;
  314.     noise_gain = MIN_NOISE;
  315.     firstcall = 1;
  316.     prev_gain_err = 0;
  317.     v_zap(pulse_del,MIX_ORD);
  318.     v_zap(noise_del,MIX_ORD);
  319.     v_zap(lpc_del,LPC_ORD);
  320.     v_zap(ase_del,LPC_ORD);
  321.     v_zap(tilt_del,TILT_ORD);
  322.     v_zap(disp_del,DISP_ORD);
  323.     v_zap(sig2,BEGIN+PITCHMAX);
  324.     v_zap(sigbuf,BEGIN+PITCHMAX);
  325.     v_zap(sigsave,PITCHMAX);
  326.     v_zap(prev_pcof,MIX_ORD+1);
  327.     v_zap(prev_ncof,MIX_ORD+1);
  328.     prev_ncof[MIX_ORD/2] = 1.0;
  329.     fill(prev_par.fs_mag,1.0,NUM_HARM);
  330.     /* 
  331.      * Initialize multi-stage vector quantization (read codebook) 
  332.      */
  333.  
  334.     vq_par.num_best = MSVQ_M;
  335.     vq_par.num_stages = 4;
  336.     vq_par.dimension = 10;
  337.     /* 
  338.      * Allocate memory for number of levels per stage and indices
  339.      * and for number of bits per stage 
  340.      */
  341.  
  342.     MEM_ALLOC(MALLOC,vq_par.num_levels,vq_par.num_stages,int);
  343.     MEM_ALLOC(MALLOC,vq_par.indices,vq_par.num_stages,int);
  344.     MEM_ALLOC(MALLOC,vq_par.num_bits,vq_par.num_stages,int);
  345.     vq_par.num_levels[0] = 128;
  346.     vq_par.num_levels[1] = 64;
  347.     vq_par.num_levels[2] = 64;
  348.     vq_par.num_levels[3] = 64;
  349.     vq_par.num_bits[0] = 7;
  350.     vq_par.num_bits[1] = 6;
  351.     vq_par.num_bits[2] = 6;
  352.     vq_par.num_bits[3] = 6;
  353.     vq_par.cb = msvq_cb;
  354.     /* Scale codebook to 0 to 1 */
  355.     if (fsvq_weighted == 0)
  356.       v_scale(vq_par.cb,(2.0/FSAMP),3200);
  357.     /* 
  358.      * Initialize Fourier magnitude vector quantization (read codebook) 
  359.      */
  360.  
  361.     fs_vq_par.num_best = 1;
  362.     fs_vq_par.num_stages = 1;
  363.     fs_vq_par.dimension = NUM_HARM;
  364.     /* 
  365.      * Allocate memory for number of levels per stage and indices
  366.      * and for number of bits per stage 
  367.      */
  368.  
  369.     MEM_ALLOC(MALLOC,fs_vq_par.num_levels,fs_vq_par.num_stages,int);
  370.     MEM_ALLOC(MALLOC,fs_vq_par.indices,fs_vq_par.num_stages,int);
  371.     MEM_ALLOC(MALLOC,fs_vq_par.num_bits,fs_vq_par.num_stages,int);
  372.     fs_vq_par.num_levels[0] = FS_LEVELS;
  373.     fs_vq_par.num_bits[0] = FS_BITS;
  374.     fs_vq_par.cb = fsvq_cb;
  375.     /* 
  376.      * Initialize fixed MSE weighting and inverse of weighting 
  377.      */
  378.     vq_fsw(w_fs, NUM_HARM, 60.0);
  379.     for (i = 0; i < NUM_HARM; i++)
  380.       w_fs_inv[i] = 1.0/w_fs[i];
  381.     /* 
  382.      * Pre-weight codebook (assume single stage only) 
  383.      */
  384.     if (fsvq_weighted == 0)
  385.       {
  386.   fsvq_weighted = 1;
  387.   for (i = 0; i < fs_vq_par.num_levels[0]; i++)
  388.     window(&fs_vq_par.cb[i*NUM_HARM],w_fs,&fs_vq_par.cb[i*NUM_HARM],
  389.    NUM_HARM);
  390.       }
  391. }