int sound::encode()
{
FILE *fin,*fout1,*fout2,*fout3;//三个文件指针,fin为已录制好的音频文件
short in[FRAME_SIZE];
short out[FRAME_SIZE];
float input[FRAME_SIZE];
float output[FRAME_SIZE];//设置几个缓冲区
char cbits[200];
int nbBytes;//用来记录每次编码出来的数据量
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);//初始化编码器,
qDebug()<<"world!! and the stateencode is:"< statedecode = speex_decoder_init(&speex_nb_mode); qDebug()<<"power and the statedecode is:"< tmp=0; int k=0; k=speex_encoder_ctl(stateencode,SPEEX_SET_VBR,&tmp); qDebug()<<"ooxx set VBR "< float q=8.0; tmp=8; k=speex_encoder_ctl(stateencode,SPEEX_SET_VBR_QUALITY,&q); qDebug()<<"!!!!!!!!!!!!!!!!! SET VBR QUALITY "< k=speex_encoder_ctl(stateencode,SPEEX_SET_QUALITY,&tmp); qDebug()<<"***********************"< 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 "< speex_bits_init(&bitsdecode); qDebug()<<"speex bits init decode "< 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"< 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 the while"; memset(out,0,160*sizeof(short)); j++; qDebug()<<"before the read the file and the j is:"< int r=fread(in,sizeof(short),160,fin); qDebug()<<"after the read the file and the r is"< if(feof(fin)) break; qDebug()<<"ooxxoxxooxxoxxoox"; spx_int16_t *ptr=(spx_int16_t *)in; qDebug()<<"*************************"< 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 size is"< i = fwrite(in,sizeof(short),FRAME_SIZE,fout3); r = fflush(fout3); for(i=0;i input[i]=in[i]; qDebug()<<"It is starting decode..."; speex_bits_reset(&bitsencode); qDebug()<<"after bits reset and the k is:"< int ret =speex_encode(stateencode,input,&bitsencode); qDebug()<<"after the speex encode"< 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:"< qDebug()<<"after the write in to the fout1: "< qDebug()<<"nbBytes is,"< 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:"< qDebug()<<"&bitsdecode:"<<&bitsdecode; //memset(statedecode,0,160*sizeof(short)); //qDebug()<<"bitsdecode:"< qDebug()<<"the sizeof of out is"< qDebug()<<"the out is "< qDebug()<<"bitsdecode"< qDebug()<<"bitsdecode"< qDebug()<<"bitsdecode"< ret = speex_decode_int(statedecode,&bitsdecode,out); qDebug()<<"after the speex decode and the ret is:"< /* 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 = fflush(fout2); qDebug()<<"check the fflush fout2"< } 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; }