void *melp_analysis3()
{
    int i = 2;
    while(1)
    {
        sem_wait(&start_melp_3ana);
        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);
        sem_post(&melp_3ana);
    }
}
Пример #2
0
void main(int argc, char **argv)

{
    void parse(int argc, char **argv);

    int length, frame, eof_reached;
    int num_frames = 0;
    float speech_in[FRAME];
    float speech_out[FRAME];
    static struct melp_param melp_par;      /* melp parameters */
    unsigned int chbuf[CHSIZE];
    FILE *fp_in, *fp_out;

    /* Print user message */
    printf("\n2.4 kb/s Proposed Federal Standard MELP speech coder\n");
    printf("  C simulation, version 1.2\n\n");

    /* Get input parameters from command line */
    parse(argc, argv);

    /* 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);
	length = ftell(fp_in);
	rewind(fp_in);

	num_frames = 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) {

	/* Perform MELP analysis */
	if (melpmode != SYNTHESIS) {
	    /* read input speech */
	    length = readbl(speech_in,fp_in,FRAME);
	    if (length < FRAME) {
		v_zap(&speech_in[length],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) {
		fwrite((void *) chbuf,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 *) chbuf,sizeof(int),CHSIZE,fp_in);

		/* 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)
	  fwrite((void *) chbuf,sizeof(int),melp_par.chptr-chbuf+1,fp_out);
	else
	  fwrite((void *) chbuf,sizeof(int),melp_par.chptr-chbuf,fp_out);
    }

    fclose(fp_in);
    fclose(fp_out);

}
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);
}