]> git.sesse.net Git - ffmpeg/blob - tools/sofa2wavs.c
avformat/avio: Add Metacube support
[ffmpeg] / tools / sofa2wavs.c
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 int main(int argc, char **argv)
28 {
29     struct MYSOFA_HRTF *hrtf;
30     int sample_rate;
31     int err, i, j;
32
33     if (argc < 3) {
34         printf("usage: %s input_SOFA_file output_directory\n", argv[0]);
35         return 1;
36     }
37
38     hrtf = mysofa_load(argv[1], &err);
39     if (!hrtf || err) {
40         printf("invalid input SOFA file: %s\n", argv[1]);
41         return 1;
42     }
43
44     if (hrtf->DataSamplingRate.elements != 1)
45         goto fail;
46     sample_rate = hrtf->DataSamplingRate.values[0];
47
48     err = mkdir(argv[2], 0744);
49     if (err)
50         goto fail;
51
52     err = chdir(argv[2]);
53     if (err)
54         goto fail;
55
56     for (i = 0; i < hrtf->M; i++) {
57         FILE *file;
58         int bps = 32;
59         int blkalign = 8;
60         int bytespersec = blkalign * sample_rate;
61         char filename[1024];
62         int azi = hrtf->SourcePosition.values[i * 3];
63         int ele = hrtf->SourcePosition.values[i * 3 + 1];
64         int dis = hrtf->SourcePosition.values[i * 3 + 2];
65         int size = 8 * hrtf->N;
66         int offset = i * 2 * hrtf->N;
67
68         snprintf(filename, sizeof(filename), "azi_%d_ele_%d_dis_%d.wav", azi, ele, dis);
69         file = fopen(filename, "w+");
70         fwrite("RIFF", 4, 1, file);
71         fwrite("\xFF\xFF\xFF\xFF", 4, 1, file);
72         fwrite("WAVE", 4, 1, file);
73         fwrite("fmt ", 4, 1, file);
74         fwrite("\x10\x00\00\00", 4, 1, file);
75         fwrite("\x03\x00", 2, 1, file);
76         fwrite("\x02\x00", 2, 1, file);
77         fwrite(&sample_rate, 4, 1, file);
78         fwrite(&bytespersec, 4, 1, file);
79         fwrite(&blkalign, 2, 1, file);
80         fwrite(&bps, 2, 1, file);
81         fwrite("data", 4, 1, file);
82         fwrite(&size, 4, 1, file);
83
84         for (j = 0; j < hrtf->N; j++) {
85             float l, r;
86
87             l = hrtf->DataIR.values[offset + j];
88             r = hrtf->DataIR.values[offset + j + hrtf->N];
89             fwrite(&l, 4, 1, file);
90             fwrite(&r, 4, 1, file);
91         }
92         fclose(file);
93     }
94
95 fail:
96     mysofa_free(hrtf);
97
98     return 0;
99 }