2
0

tsrc.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. tsrc.c
  3. David Rowe
  4. Sat Nov 3 2012
  5. Unit test for libresample code.
  6. */
  7. #include <assert.h>
  8. #include <math.h>
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include <samplerate.h>
  12. #define N8 160 /* processing buffer size at 8 kHz */
  13. #define N48 ((int)N8*(48000/8000)) /* buf size assuming 48k max sample rate */
  14. int main(int argc, char *argv[]) {
  15. FILE *f8k, *fout;
  16. short in8k_short[N8];
  17. float in8k[N8];
  18. float out[N48];
  19. short out_short[N48];
  20. SRC_STATE *src;
  21. SRC_DATA data;
  22. int error;
  23. if (argc != 4) {
  24. printf("usage %s inputRawFile OutputRawFile OutputSamplerate\n", argv[0]);
  25. exit(0);
  26. }
  27. f8k = fopen(argv[1], "rb");
  28. assert(f8k != NULL);
  29. fout = fopen(argv[2], "wb");
  30. assert(fout != NULL);
  31. src = src_new(SRC_SINC_FASTEST, 1, &error);
  32. assert(src != NULL);
  33. data.data_in = in8k;
  34. data.data_out = out;
  35. data.input_frames = N8;
  36. data.output_frames = N48;
  37. data.end_of_input = 0;
  38. data.src_ratio = atof(argv[3])/8000;
  39. printf("%f\n", data.src_ratio);
  40. while(fread(in8k_short, sizeof(short), N8, f8k) == N8) {
  41. src_short_to_float_array(in8k_short, in8k, N8);
  42. src_process(src, &data);
  43. printf("%d %d\n", (int)data.output_frames , (int)data.output_frames_gen);
  44. assert(data.output_frames_gen <= N48);
  45. src_float_to_short_array(out, out_short, data.output_frames_gen);
  46. fwrite(out_short, sizeof(short), data.output_frames_gen, fout);
  47. }
  48. fclose(fout);
  49. fclose(f8k);
  50. return 0;
  51. }