关闭

linux 用speex库压缩音频

2247人阅读 评论(0) 收藏 举报


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:"<<stateencode;

statedecode = speex_decoder_init(&speex_nb_mode);

qDebug()<<"power and the statedecode is:"<<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 the while";

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 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 size is"<<sizeof(in)<<"the in is : "<<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 is:"<<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 write in to 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 out is"<<sizeof(out);

qDebug()<<"the out is "<<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;

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:72824次
    • 积分:873
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:28篇
    • 译文:0篇
    • 评论:4条
    最新评论