void *analyzer()
{
    clock_t t1, t2;
    //short ana_arr[540] = {0};
    float act_time,msec;
    unsigned char enc_speech[11];
    unsigned char array1[176] = {0x00};
    int b = 0, loc = 0, count_64blocks = 0;
    int i = 0;

    while(count != 0)
    {
        sem_wait(&id_analyzer);
        //sem_wait(&mutx);
        //if (count_64blocks == 0)

        t1 = clock();

        memcpy(ana_arr, buff, 1080);

        sem_post(&empty);

        //t1 = clock();
        num_frames = (int16_t) ((rate == RATE2400) ? 1 : NF);

        //for (i = 0; i < num_frames; i++)
        //{
        //	dc_rmv(&ana_arr[i * FRAME], &hpspeech[IN_BEG + i * FRAME],
        //			dcdelin, dcdelout_hi, dcdelout_lo, FRAME);
        //	//melp_ana(&hpspeech[i * FRAME], &melp_par[i], i);
        //}

        dc_rmv(&ana_arr[0], &hpspeech[IN_BEG + 0],
               dcdelin, dcdelout_hi, dcdelout_lo, FRAME);
        melp_ana(&hpspeech[0], &melp_par[0],0);

        dc_rmv(&ana_arr[FRAME], &hpspeech[IN_BEG + FRAME],
               dcdelin, dcdelout_hi, dcdelout_lo, FRAME);
        melp_ana(&hpspeech[FRAME], &melp_par[1], 1);

        dc_rmv(&ana_arr[2 * FRAME], &hpspeech[IN_BEG + 2 * FRAME],
               dcdelin, dcdelout_hi, dcdelout_lo, FRAME);
        melp_ana(&hpspeech[2 * FRAME], &melp_par[2], 2);

        /*sem_post(&start_melp_1ana);
        sem_post(&start_melp_2ana);
        sem_post(&start_melp_3ana);


        sem_wait(&melp_1ana);
        sem_wait(&melp_2ana);
        sem_wait(&melp_3ana);*/


        sc_ana(melp_par);

        /* ======== Quantization ======== */

        lpc[0] = ONE_Q12;

        lsf_vq(melp_par);

        pitch_vq(melp_par);

        gain_vq(melp_par);

        quant_u(&melp_par[0].jitter, &(quant_par.jit_index[0]), 0,
                MAX_JITTER_Q15, 2, ONE_Q15, 1, 7);
        quant_u(&melp_par[1].jitter, &(quant_par.jit_index[1]), 0,
                MAX_JITTER_Q15, 2, ONE_Q15, 1, 7);
        quant_u(&melp_par[2].jitter, &(quant_par.jit_index[2]), 0,
                MAX_JITTER_Q15, 2, ONE_Q15, 1, 7);

        //for (i = 0; i < NF; i++)
        //	quant_u(&melp_par[i].jitter, &(quant_par.jit_index[i]), 0,
        //		MAX_JITTER_Q15, 2, ONE_Q15, 1, 7);

        /* Quantize bandpass voicing */

        quant_bp(melp_par, num_frames);

        quant_jitter(melp_par);

        /* Calculate Fourier coeffs of residual from quantized LPC */
        //for (i = 0; i < num_frames; i++)
        //{
        //	/* The following fill() action is believed to be unnecessary. */
        //	//fill(melp_par[i].fs_mag, ONE_Q13, NUM_HARM);
        //
        //	if (!melp_par[i].uv_flag)
        //	{
        //		lpc_lsp2pred(melp_par[i].lsf, &(lpc[1]), LPC_ORD);
        //		zerflt(&hpspeech
        //			   [(i * FRAME + FRAME_END - (LPC_FRAME / 2))], lpc,
        //			   sigbuf, LPC_ORD, LPC_FRAME);
        //
        //		window(sigbuf, win_cof, sigbuf, LPC_FRAME);
        //
        //		find_harm(sigbuf, melp_par[i].fs_mag, melp_par[i].pitch, NUM_HARM,
        //			  LPC_FRAME);
        //	}
        //}
        i = 0;
        if (!melp_par[i].uv_flag)
        {
            lpc_lsp2pred(melp_par[i].lsf, &(lpc[1]), LPC_ORD);
            zerflt(&hpspeech
                   [(i * FRAME + FRAME_END - (LPC_FRAME / 2))], lpc,
                   sigbuf, LPC_ORD, LPC_FRAME);

            window(sigbuf, win_cof, sigbuf, LPC_FRAME);

            find_harm(sigbuf, melp_par[i].fs_mag, melp_par[i].pitch, NUM_HARM,
                      LPC_FRAME);
        }
        i = 1;
        if (!melp_par[i].uv_flag)
        {
            lpc_lsp2pred(melp_par[i].lsf, &(lpc[1]), LPC_ORD);
            zerflt(&hpspeech
                   [(i * FRAME + FRAME_END - (LPC_FRAME / 2))], lpc,
                   sigbuf, LPC_ORD, LPC_FRAME);

            window(sigbuf, win_cof, sigbuf, LPC_FRAME);

            find_harm(sigbuf, melp_par[i].fs_mag, melp_par[i].pitch, NUM_HARM,
                      LPC_FRAME);
        }
        i = 2;
        if (!melp_par[i].uv_flag)
        {
            lpc_lsp2pred(melp_par[i].lsf, &(lpc[1]), LPC_ORD);
            zerflt(&hpspeech
                   [(i * FRAME + FRAME_END - (LPC_FRAME / 2))], lpc,
                   sigbuf, LPC_ORD, LPC_FRAME);

            window(sigbuf, win_cof, sigbuf, LPC_FRAME);

            find_harm(sigbuf, melp_par[i].fs_mag, melp_par[i].pitch, NUM_HARM,
                      LPC_FRAME);
        }

        quant_fsmag(melp_par);

        //for (i = 0; i < num_frames; i++)
        //	quant_par.uv_flag[i] = melp_par[i].uv_flag;
        quant_par.uv_flag[0] = melp_par[0].uv_flag;
        quant_par.uv_flag[1] = melp_par[1].uv_flag;
        quant_par.uv_flag[2] = melp_par[2].uv_flag;

        /* Write channel bitstream */

#if !SKIP_CHANNEL
        low_rate_chn_write(&quant_par);
#endif

        /* Update delay buffers for next block */
        v_equ(hpspeech, &(hpspeech[num_frames * FRAME]), IN_BEG);

        memcpy(enc_speech,chbuf,11);



        /*======================old content of melp thread======================*/
        //melpe_a(enc_speech, buff);
        //fwrite(enc_speech,sizeof(char),11,fptr);
        //=======================================================================
        // ather_jawad work for 2400 baudrate
        //=======================================================================
        for (b=0; b<11; b++)
        {
            array1[loc] = enc_speech[b];
            loc++;
        }

        count_64blocks = count_64blocks + 1;
        if(count_64blocks == 16)
        {
            loc = 0;
            memcpy(array2,array1,176);
            sem_post(&last);

            count_64blocks = 0;
        }

        t2 = clock() - t1;
        //printf("Time %d = %f\n\n",count_64blocks,(float)t2/CLOCKS_PER_SEC);

        //sem_post(&full);
    }
    pthread_exit(NULL);
}
Example #2
0
void melp_ana(float sp_in[],struct melp_param *par)
{

    int i;
    int begin;
    float sub_pitch;
    float temp,pcorr,bpthresh;
    float r[LPC_ORD+1],refc[LPC_ORD+1],lpc[LPC_ORD+1];
    float weights[LPC_ORD];
        
    /* Remove DC from input speech */
    dc_rmv(sp_in,&speech[IN_BEG],dcdel,FRAME);
    
    /* Copy input speech to pitch window and lowpass filter */
    v_equ(&sigbuf[LPF_ORD],&speech[PITCH_BEG],PITCH_FR);
    v_equ(sigbuf,lpfsp_del,LPF_ORD);
    polflt(&sigbuf[LPF_ORD],lpf_den,&sigbuf[LPF_ORD],LPF_ORD,PITCH_FR);
    v_equ(lpfsp_del,&sigbuf[FRAME],LPF_ORD);
    zerflt(&sigbuf[LPF_ORD],lpf_num,&sigbuf[LPF_ORD],LPF_ORD,PITCH_FR);
    
    /* Perform global pitch search at frame end on lowpass speech signal */
    /* Note: avoid short pitches due to formant tracking */
    fpitch[END] = find_pitch(&sigbuf[LPF_ORD+(PITCH_FR/2)],&temp,
			     (2*PITCHMIN),PITCHMAX,PITCHMAX);
    
    /* Perform bandpass voicing analysis for end of frame */
    bpvc_ana(&speech[FRAME_END], fpitch, &par->bpvc[0], &sub_pitch);
    
    /* Force jitter if lowest band voicing strength is weak */    
    if (par->bpvc[0] < VJIT)
	par->jitter = MAX_JITTER;
    else
	par->jitter = 0.0;
    
    /* Calculate LPC for end of frame */
    window(&speech[(FRAME_END-(LPC_FRAME/2))],win_cof,sigbuf,LPC_FRAME);
    autocorr(sigbuf,r,LPC_ORD,LPC_FRAME);
    lpc[0] = 1.0;
    lpc_schur(r,lpc,refc,LPC_ORD);
    lpc_bw_expand(lpc,lpc,BWFACT,LPC_ORD);
    
    /* Calculate LPC residual */
    zerflt(&speech[PITCH_BEG],lpc,&sigbuf[LPF_ORD],LPC_ORD,PITCH_FR);
        
    /* Check peakiness of residual signal */
    begin = (LPF_ORD+(PITCHMAX/2));
    temp = peakiness(&sigbuf[begin],PITCHMAX);
    
    /* Peakiness: force lowest band to be voiced  */
    if (temp > PEAK_THRESH) {
	par->bpvc[0] = 1.0;
    }
    
    /* Extreme peakiness: force second and third bands to be voiced */
    if (temp > PEAK_THR2) {
	par->bpvc[1] = 1.0;
	par->bpvc[2] = 1.0;
    }
		
    /* Calculate overall frame pitch using lowpass filtered residual */
    par->pitch = pitch_ana(&speech[FRAME_END], &sigbuf[LPF_ORD+PITCHMAX], 
			   sub_pitch,pitch_avg,&pcorr);
    bpthresh = BPTHRESH;
    
    /* Calculate gain of input speech for each gain subframe */
    for (i = 0; i < NUM_GAINFR; i++) {
	if (par->bpvc[0] > bpthresh) {

	    /* voiced mode: pitch synchronous window length */
	    temp = sub_pitch;
	    par->gain[i] = gain_ana(&speech[FRAME_BEG+(i+1)*GAINFR],
				    temp,MIN_GAINFR,2*PITCHMAX);
	}
	else {
	    temp = 1.33*GAINFR - 0.5;
	    par->gain[i] = gain_ana(&speech[FRAME_BEG+(i+1)*GAINFR],
				    temp,0,2*PITCHMAX);
	}
    }
    
    /* Update average pitch value */
    if (par->gain[NUM_GAINFR-1] > SILENCE_DB)
      temp = pcorr;
    else
      temp = 0.0;
    pitch_avg = p_avg_update(par->pitch, temp, VMIN);
    
    /* Calculate Line Spectral Frequencies */
    lpc_pred2lsp(lpc,par->lsf,LPC_ORD);
    
    /* Force minimum LSF bandwidth (separation) */
    lpc_clamp(par->lsf,BWMIN,LPC_ORD);
    
    /* Quantize MELP parameters to 2400 bps and generate bitstream */
    
    /* Quantize LSF's with MSVQ */
    vq_lspw(weights, &par->lsf[1], lpc, LPC_ORD);
    msvq_enc(&par->lsf[1], weights, &par->lsf[1], vq_par);
    par->msvq_index = vq_par.indices;
    
    /* Force minimum LSF bandwidth (separation) */
    lpc_clamp(par->lsf,BWMIN,LPC_ORD);
    
    /* Quantize logarithmic pitch period */
    /* Reserve all zero code for completely unvoiced */
    par->pitch = log10(par->pitch);
    quant_u(&par->pitch,&par->pitch_index,PIT_QLO,PIT_QUP,PIT_QLEV);
    par->pitch = pow(10.0,par->pitch);
    
    /* Quantize gain terms with uniform log quantizer	*/
    q_gain(par->gain, par->gain_index,GN_QLO,GN_QUP,GN_QLEV);
    
    /* Quantize jitter and bandpass voicing */
    quant_u(&par->jitter,&par->jit_index,0.0,MAX_JITTER,2);
    par->uv_flag = q_bpvc(&par->bpvc[0],&par->bpvc_index,bpthresh,
			  NUM_BANDS);
    
    /*	Calculate Fourier coefficients of residual signal from quantized LPC */
    fill(par->fs_mag,1.0,NUM_HARM);
    if (par->bpvc[0] > bpthresh) {
	lpc_lsp2pred(par->lsf,lpc,LPC_ORD);
	zerflt(&speech[(FRAME_END-(LPC_FRAME/2))],lpc,sigbuf,
	       LPC_ORD,LPC_FRAME);
	window(sigbuf,win_cof,sigbuf,LPC_FRAME);
	find_harm(sigbuf, par->fs_mag, par->pitch, NUM_HARM, LPC_FRAME);
    }
    
    /* quantize Fourier coefficients */
    /* pre-weight vector, then use Euclidean distance */
    window(&par->fs_mag[0],w_fs,&par->fs_mag[0],NUM_HARM);
    fsvq_enc(&par->fs_mag[0], &par->fs_mag[0], fs_vq_par);
    
    /* Set MELP indeces to point to same array */
    par->fsvq_index = fs_vq_par.indices;

    /* Update MSVQ information */
    par->msvq_stages = vq_par.num_stages;
    par->msvq_bits = vq_par.num_bits;

    /* Write channel bitstream */
    melp_chn_write(par);

    /* Update delay buffers for next frame */
    v_equ(&speech[0],&speech[FRAME],IN_BEG);
    fpitch[BEGIN] = fpitch[END];
}