#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
#include <speex/speex_preprocess.h>
#include "speex/speex_echo.h"
#include <speex/speex.h>
#define NN 128//每次处理的帧长度,这个一般是从10ms(80) 到30ms(240) 的处理长度
#define TAIL 1024
int main(void) {
int sampleRate = 16000;
FILE *mic_fd, *speaker_fd, *out_fd;
mic_fd = fopen("./audio/mic.wav", "rb");
speaker_fd = fopen("./audio/spk.wav", "rb");
out_fd = fopen("./audio/aec.wav", "wb");
short mic_buf[NN], speaker_buf[NN], out_buf[NN];
SpeexEchoState *echoState = speex_echo_state_init(NN, TAIL);
SpeexPreprocessState *preprocessState = speex_preprocess_state_init(NN, sampleRate);
speex_echo_ctl(echoState, SPEEX_ECHO_SET_SAMPLING_RATE, &sampleRate);
speex_preprocess_ctl(preprocessState, SPEEX_PREPROCESS_SET_ECHO_STATE, echoState);
while (!feof(speaker_fd) && !feof(mic_fd)) {
fread(speaker_buf, sizeof(short), NN, speaker_fd);
fread(mic_buf, sizeof(short), NN, mic_fd);
speex_echo_cancellation(echoState, mic_buf, speaker_buf, out_buf);
speex_preprocess_run(preprocessState, out_buf);
fwrite(out_buf, sizeof(short), NN, out_fd);
}
speex_echo_state_destroy(echoState);
speex_preprocess_state_destroy(preprocessState);
fclose(out_fd);
fclose(mic_fd);
fclose(speaker_fd);
printf("=====over\n");
return 0;
}