FFmpeg
sofa2wavs.c
Go to the documentation of this file.
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 
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <mysofa.h>
26 
27 #ifdef _WIN32
28 #include <direct.h>
29 #undef mkdir
30 #define mkdir(a, b) _mkdir(a)
31 #endif
32 
33 int main(int argc, char **argv)
34 {
35  struct MYSOFA_HRTF *hrtf;
36  int sample_rate;
37  int err, i, j;
38 
39  if (argc < 3) {
40  printf("usage: %s input_SOFA_file output_directory\n", argv[0]);
41  return 1;
42  }
43 
44  hrtf = mysofa_load(argv[1], &err);
45  if (!hrtf || err) {
46  printf("invalid input SOFA file: %s\n", argv[1]);
47  return 1;
48  }
49 
50  if (hrtf->DataSamplingRate.elements != 1)
51  goto fail;
52  sample_rate = hrtf->DataSamplingRate.values[0];
53 
54  err = mkdir(argv[2], 0744);
55  if (err)
56  goto fail;
57 
58  err = chdir(argv[2]);
59  if (err)
60  goto fail;
61 
62  for (i = 0; i < hrtf->M; i++) {
63  FILE *file;
64  int bps = 32;
65  int blkalign = 8;
66  int bytespersec = blkalign * sample_rate;
67  char filename[1024];
68  int azi = hrtf->SourcePosition.values[i * 3];
69  int ele = hrtf->SourcePosition.values[i * 3 + 1];
70  int dis = hrtf->SourcePosition.values[i * 3 + 2];
71  int size = 8 * hrtf->N;
72  int offset = i * 2 * hrtf->N;
73 
74  snprintf(filename, sizeof(filename), "azi_%d_ele_%d_dis_%d.wav", azi, ele, dis);
75  file = fopen(filename, "w+");
76  fwrite("RIFF", 4, 1, file);
77  fwrite("\xFF\xFF\xFF\xFF", 4, 1, file);
78  fwrite("WAVE", 4, 1, file);
79  fwrite("fmt ", 4, 1, file);
80  fwrite("\x10\x00\00\00", 4, 1, file);
81  fwrite("\x03\x00", 2, 1, file);
82  fwrite("\x02\x00", 2, 1, file);
83  fwrite(&sample_rate, 4, 1, file);
84  fwrite(&bytespersec, 4, 1, file);
85  fwrite(&blkalign, 2, 1, file);
86  fwrite(&bps, 2, 1, file);
87  fwrite("data", 4, 1, file);
88  fwrite(&size, 4, 1, file);
89 
90  for (j = 0; j < hrtf->N; j++) {
91  float l, r;
92 
93  l = hrtf->DataIR.values[offset + j];
94  r = hrtf->DataIR.values[offset + j + hrtf->N];
95  fwrite(&l, 4, 1, file);
96  fwrite(&r, 4, 1, file);
97  }
98  fclose(file);
99  }
100 
101 fail:
102  mysofa_free(hrtf);
103 
104  return 0;
105 }
r
const char * r
Definition: vf_curves.c:127
printf
__device__ int printf(const char *,...)
main
int main(int argc, char **argv)
Definition: sofa2wavs.c:33
fail
#define fail()
Definition: checkasm.h:214
bps
unsigned bps
Definition: movenc.c:2033
size
int size
Definition: twinvq_data.h:10344
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
snprintf
#define snprintf
Definition: snprintf.h:34