#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#include <speex/speex_preprocess.h>
#define HEADLEN 44
#define SAMPLE_RATE (16000) #采样率
#define SAMPLES_PER_FRAME (1024) #每帧的采样数
#define FRAME_SIZE (SAMPLES_PER_FRAME * 1000/ SAMPLE_RATE) #每帧的时长ms
#define FRAME_BYTES (SAMPLES_PER_FRAME)
//typedef enum {false, true} bool;
char *splitword(char *word,char *neword){
char *out="out_";
for(int j=0;j<strlen(word)+4;j++){
if (j<4)
neword[j]=out[j];
else
neword[j]=word[j-4];
}
return neword;
}
int main(int argc,char ** argv){
size_t n = 0;
FILE *inFile, *outFile;
char * copy = malloc(sizeof(char)*(strlen(argv[1]) + 1));
strcpy(copy, argv[1]);
char *neword;
neword=(char *)malloc(strlen(argv[1])+4);
char *output=splitword(copy,neword);
inFile=fopen(argv[1], "rb");
outFile=fopen(output, "wb");
char *headBuf = (char*)malloc(HEADLEN);
char *dataBuf = (char*)malloc(FRAME_BYTES * 2 );
memset(headBuf, 0, HEADLEN);
memset(dataBuf, 0, FRAME_BYTES);
assert(headBuf != NULL);
assert(dataBuf != NULL);
SpeexPreprocessState *state = speex_preprocess_state_init(1024,SAMPLE_RATE);
int denoise = 1;
int noiseSuppress = -25;
speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_DENOISE, &denoise);
speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_NOISE_SUPPRESS, &noiseSuppress);
int i;
i = 0;
speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_AGC, &i);
i = 9000;
speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_AGC_LEVEL, &i);
// i = 10000;
// speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_DEREVERB, &i);
//静音检测的效果一般
/*
int vad = 1;
int vadProbStart = 80;
int vadProbContinue = 65;
speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_VAD, &vad);
speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_PROB_START, &vadProbStart);
speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_PROB_CONTINUE, &vadProbContinue);
*/
bool flag=true;
while (1){
if (flag == true){
flag = false;
n = fread(headBuf, 1, HEADLEN, inFile);
if (n == 0)
break;
fwrite(headBuf, 1, HEADLEN, outFile);
}
else{
n = fread(dataBuf, 1, SAMPLES_PER_FRAME, inFile);
if (n == 0)
break;
speex_preprocess_run(state, (spx_int16_t*)(dataBuf));
fwrite(dataBuf, 1, SAMPLES_PER_FRAME, outFile);
}
}
free(headBuf);
free(dataBuf);
fclose(inFile);
fclose(outFile);
speex_preprocess_state_destroy(state);
return 0;
}