sofa2wavs.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2017 Paul B Mahol
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <unistd.h>
  23. #include <stdio.h>
  24. #include <mysofa.h>
  25. int main(int argc, char **argv)
  26. {
  27. struct MYSOFA_HRTF *hrtf;
  28. int sample_rate;
  29. int err, i, j;
  30. if (argc < 3) {
  31. printf("usage: %s input_SOFA_file output_directory\n", argv[0]);
  32. return 1;
  33. }
  34. hrtf = mysofa_load(argv[1], &err);
  35. if (!hrtf || err) {
  36. printf("invalid input SOFA file: %s\n", argv[1]);
  37. return 1;
  38. }
  39. if (hrtf->DataSamplingRate.elements != 1)
  40. goto fail;
  41. sample_rate = hrtf->DataSamplingRate.values[0];
  42. err = mkdir(argv[2], 0744);
  43. if (err)
  44. goto fail;
  45. err = chdir(argv[2]);
  46. if (err)
  47. goto fail;
  48. for (i = 0; i < hrtf->M; i++) {
  49. FILE *file;
  50. int bps = 32;
  51. int blkalign = 8;
  52. int bytespersec = blkalign * sample_rate;
  53. char filename[1024];
  54. int azi = hrtf->SourcePosition.values[i * 3];
  55. int ele = hrtf->SourcePosition.values[i * 3 + 1];
  56. int dis = hrtf->SourcePosition.values[i * 3 + 2];
  57. int size = 8 * hrtf->N;
  58. int offset = i * 2 * hrtf->N;
  59. snprintf(filename, sizeof(filename), "azi_%d_ele_%d_dis_%d.wav", azi, ele, dis);
  60. file = fopen(filename, "w+");
  61. fwrite("RIFF", 4, 1, file);
  62. fwrite("\xFF\xFF\xFF\xFF", 4, 1, file);
  63. fwrite("WAVE", 4, 1, file);
  64. fwrite("fmt ", 4, 1, file);
  65. fwrite("\x10\x00\00\00", 4, 1, file);
  66. fwrite("\x03\x00", 2, 1, file);
  67. fwrite("\x02\x00", 2, 1, file);
  68. fwrite(&sample_rate, 4, 1, file);
  69. fwrite(&bytespersec, 4, 1, file);
  70. fwrite(&blkalign, 2, 1, file);
  71. fwrite(&bps, 2, 1, file);
  72. fwrite("data", 4, 1, file);
  73. fwrite(&size, 4, 1, file);
  74. for (j = 0; j < hrtf->N; j++) {
  75. float l, r;
  76. l = hrtf->DataIR.values[offset + j];
  77. r = hrtf->DataIR.values[offset + j + hrtf->N];
  78. fwrite(&l, 4, 1, file);
  79. fwrite(&r, 4, 1, file);
  80. }
  81. fclose(file);
  82. }
  83. fail:
  84. mysofa_free(hrtf);
  85. return 0;
  86. }