testecho.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include "speex/speex_echo.h"
  10. #include "speex/speex_preprocess.h"
  11. #define NN 128
  12. #define TAIL 1024
  13. int main(int argc, char **argv)
  14. {
  15. FILE *echo_fd, *ref_fd, *e_fd;
  16. short echo_buf[NN], ref_buf[NN], e_buf[NN];
  17. SpeexEchoState *st;
  18. SpeexPreprocessState *den;
  19. int sampleRate = 8000;
  20. if (argc != 4)
  21. {
  22. fprintf(stderr, "testecho mic_signal.sw speaker_signal.sw output.sw\n");
  23. exit(1);
  24. }
  25. echo_fd = fopen(argv[2], "rb");
  26. ref_fd = fopen(argv[1], "rb");
  27. e_fd = fopen(argv[3], "wb");
  28. st = speex_echo_state_init(NN, TAIL);
  29. den = speex_preprocess_state_init(NN, sampleRate);
  30. speex_echo_ctl(st, SPEEX_ECHO_SET_SAMPLING_RATE, &sampleRate);
  31. speex_preprocess_ctl(den, SPEEX_PREPROCESS_SET_ECHO_STATE, st);
  32. while (!feof(ref_fd) && !feof(echo_fd))
  33. {
  34. fread(ref_buf, sizeof(short), NN, ref_fd);
  35. fread(echo_buf, sizeof(short), NN, echo_fd);
  36. speex_echo_cancellation(st, ref_buf, echo_buf, e_buf);
  37. speex_preprocess_run(den, e_buf);
  38. fwrite(e_buf, sizeof(short), NN, e_fd);
  39. }
  40. speex_echo_state_destroy(st);
  41. speex_preprocess_state_destroy(den);
  42. fclose(e_fd);
  43. fclose(echo_fd);
  44. fclose(ref_fd);
  45. return 0;
  46. }