Linux uses Speex library to compress audio __linux

Source: Internet
Author: User


int Sound::encode ()

{

File *fin,*fout1,*fout2,*fout3;//Three files pointer, Fin is a recorded audio file

Short in[frame_size];

Short out[frame_size];

float Input[frame_size];

Float output[frame_size];//set several buffers

Char cbits[200];

int nbbytes;//is used to record the amount of data encoded each time

Qdebug () << "I love china!!";

void *stateencode;

void *statedecode;

Qdebug () << "loveing china!!";

Speexbits Bitsencode;

Speexbits Bitsdecode;

Qdebug () << "hello!!";

int i,tmp;

Stateencode = Speex_encoder_init (&speex_nb_mode);//Initialize encoder,

Qdebug () << "world!! And the Stateencode is: <<stateencode;

Statedecode = Speex_decoder_init (&speex_nb_mode);

Qdebug () << "power and the Statedecode are:" <<statedecode;

Tmp=0;

int k=0;

K=speex_encoder_ctl (STATEENCODE,SPEEX_SET_VBR,&TMP);

Qdebug () << "Ooxx set VBR" <<k;

float q=8.0;

tmp=8;

K=speex_encoder_ctl (STATEENCODE,SPEEX_SET_VBR_QUALITY,&Q);

Qdebug () << "!!!!!!!!!!!!!!!!! SET VBR QUALITY "<<k;

K=speex_encoder_ctl (STATEENCODE,SPEEX_SET_QUALITY,&TMP);

Qdebug () << "***********************" <<k;

Fin = fopen ("./test.wav", "RB");

FOUT1 =fopen ("./demo.raw", "WB");

Fout2 = fopen ("./demo.wav", "WB");

Fout3= fopen ("./demoslience", "WB");

Qdebug () << "After open the file!!";

Speex_bits_init (&bitsencode);

Qdebug () << "Speex bits init encode" <<k;

Speex_bits_init (&bitsdecode);

Qdebug () << "Speex bits init decode" <<k;

Qdebug () << "after the bits init";

int j=0;

Speexpreprocessstate *m_st;

Speexpreprocessstate *echo_state;

Qdebug () << "aftre the Speexpreprocessstat";

M_st=speex_preprocess_state_init (160,8000);

Qdebug () << "after the Speex_preprocess_state" <<m_st;

int denoise = 1;

int noisesuppress=-25;

k= Speex_preprocess_ctl (m_st,speex_preprocess_set_denoise,&denoise);

K=speex_preprocess_ctl (m_st,speex_preprocess_set_noise_suppress,&noisesuppress);

int AGC = 1;

Q = 24000;

K=speex_preprocess_ctl (M_ST,SPEEX_PREPROCESS_SET_AGC,&AGC);

K=speex_preprocess_ctl (M_ST,SPEEX_PREPROCESS_SET_AGC_LEVEL,&Q);

int VAD = 1;

int vadprobstart = 80;

int vadprobcontinue = 65;

Qdebug () << "before the SET VAD";

K=speex_preprocess_ctl (M_ST,SPEEX_PREPROCESS_SET_VAD,&VAD);

K=speex_preprocess_ctl (M_st,speex_preprocess_set_prob_start,&vadprobstart);

K=speex_preprocess_ctl (m_st,speex_preprocess_set_prob_continue,&vadprobcontinue);

while (1)

{

Qdebug () << "in";

Memset (out,0,160*sizeof (short));

j + +;

Qdebug () << "before" the read the file and the J is: "<<j;

int R=fread (in,sizeof (short), 160,fin);

Qdebug () << "After the" the read the file and the R is "<<r;

if (feof (Fin))

Break

Qdebug () << "Ooxxoxxooxxoxxoox";

spx_int16_t *ptr= (spx_int16_t *) in;

Qdebug () << "*************************" <<ptr;

if (Speex_preprocess_run (m_st,ptr))

{

printf ("speech,");

Fwrite (in,sizeof (short), FRAME_SIZE,FOUT3);

}

Else

{

printf ("slience,");

Fwrite (in,sizeof (short), FRAME_SIZE,FOUT3);

}

Qdebug () << "The In-is" <<sizeof (in) << "the": "<<in;

i = fwrite (in,sizeof (short), FRAME_SIZE,FOUT3);

R = fflush (FOUT3);

for (i=0;i<frame_size;i++)

Input[i]=in[i];

Qdebug () << "It is starting decode ...";

Speex_bits_reset (&bitsencode);

Qdebug () << "after bits reset and the K are:" <<k;

int ret =speex_encode (stateencode,input,&bitsencode);

Qdebug () << "after the Speex encode" <<ret;

Nbbytes = Speex_bits_write (&bitsencode,cbits,200);

Qdebug () << "after Speex bits write";

K=fwrite (Cbits,sizeof (char), NBBYTES,FOUT1);

R = fflush (FOUT1);

Qdebug () << "Check the Fflush fout1 and the result is:" <<r;

Qdebug () << "After the" FOUT1: "<<k;

Qdebug () << "Nbbytes is," <<nbBytes;

Speex_bits_reset (&bitsdecode);

Qdebug () << "after the second bits reset";

Speex_bits_read_from (&bitsdecode,cbits,nbbytes);

Qdebug () << "after Speex bits read from";

Qdebug () << "Statedecode:" <<statedecode;

Qdebug () << "&bitsdecode:" <<&bitsdecode;

Memset (statedecode,0,160*sizeof (short));

Qdebug () << "Bitsdecode:" <<bitsdecode;

Qdebug () << "The sizeof of" <<sizeof (out);

Qdebug () << "The Out of" <<out;

Qdebug () << "Bitsdecode" <<bitsdecode.buf_size;

Qdebug () << "Bitsdecode" <<bitsdecode.overflow;

Qdebug () << "Bitsdecode" <<bitsdecode.owner;

ret = Speex_decode_int (statedecode,&bitsdecode,out);

Qdebug () << "after the Speex decode and the ' RET is:" <<ret;

/* for (i=0;i<128;i++)

out[i]=output[i];*/

R = fwrite (out,sizeof (short), 128,fout2);

Qdebug () << "after to write into the Fout2:" <<r;

R = fflush (FOUT2);

Qdebug () << "Check the fflush fout2" <<r;

}

Speex_encoder_destroy (Stateencode);

Speex_bits_destroy (&bitsencode);

Speex_decoder_destroy (Statedecode);

Speex_bits_destroy (&bitsdecode);

Fclose (Fin);

Fclose (FOUT1);

Fclose (FOUT2);

Fclose (FOUT3);

return 0;

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.