Skip to content

Commit 1a30bf6

Browse files
committed
tools: add sofa2wavs
Signed-off-by: Paul B Mahol <[email protected]>
1 parent d4d1fc8 commit 1a30bf6

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ target_dec_%_fuzzer$(EXESUF): target_dec_%_fuzzer.o $(FF_DEP_LIBS)
8282
$(LD) $(LDFLAGS) $(LDEXEFLAGS) $(LD_O) $^ $(ELIBS) $(FF_EXTRALIBS) $(LIBFUZZER_PATH)
8383

8484
tools/cws2fws$(EXESUF): ELIBS = $(ZLIB)
85+
tools/sofa2wavs$(EXESUF): ELIBS = $(FF_EXTRALIBS)
8586
tools/uncoded_frame$(EXESUF): $(FF_DEP_LIBS)
8687
tools/uncoded_frame$(EXESUF): ELIBS = $(FF_EXTRALIBS)
8788
tools/target_dec_%_fuzzer$(EXESUF): $(FF_DEP_LIBS)

tools/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
TOOLS = qt-faststart trasher uncoded_frame
2+
TOOLS-$(CONFIG_LIBMYSOFA) += sofa2wavs
23
TOOLS-$(CONFIG_ZLIB) += cws2fws
34

45
tools/target_dec_%_fuzzer.o: tools/target_dec_fuzzer.c

tools/sofa2wavs.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <sys/stat.h>
2+
#include <sys/types.h>
3+
#include <unistd.h>
4+
#include <stdio.h>
5+
#include <mysofa.h>
6+
7+
int main(int argc, char **argv)
8+
{
9+
struct MYSOFA_HRTF *hrtf;
10+
int sample_rate;
11+
int err, i, j;
12+
13+
if (argc < 3) {
14+
printf("usage: %s input_SOFA_file output_directory\n", argv[0]);
15+
return 1;
16+
}
17+
18+
hrtf = mysofa_load(argv[1], &err);
19+
if (!hrtf || err) {
20+
printf("invalid input SOFA file: %s\n", argv[1]);
21+
return 1;
22+
}
23+
24+
if (hrtf->DataSamplingRate.elements != 1)
25+
goto fail;
26+
sample_rate = hrtf->DataSamplingRate.values[0];
27+
28+
err = mkdir(argv[2], 0744);
29+
if (err)
30+
goto fail;
31+
32+
err = chdir(argv[2]);
33+
if (err)
34+
goto fail;
35+
36+
for (i = 0; i < hrtf->M; i++) {
37+
FILE *file;
38+
int bps = 32;
39+
int blkalign = 8;
40+
int bytespersec = blkalign * sample_rate;
41+
char filename[1024];
42+
int azi = hrtf->SourcePosition.values[i * 3];
43+
int ele = hrtf->SourcePosition.values[i * 3 + 1];
44+
int dis = hrtf->SourcePosition.values[i * 3 + 2];
45+
int size = 8 * hrtf->N;
46+
int offset = i * 2 * hrtf->N;
47+
48+
snprintf(filename, sizeof(filename), "azi_%d_ele_%d_dis_%d.wav", azi, ele, dis);
49+
file = fopen(filename, "w+");
50+
fwrite("RIFF", 4, 1, file);
51+
fwrite("\xFF\xFF\xFF\xFF", 4, 1, file);
52+
fwrite("WAVE", 4, 1, file);
53+
fwrite("fmt ", 4, 1, file);
54+
fwrite("\x10\x00\00\00", 4, 1, file);
55+
fwrite("\x03\x00", 2, 1, file);
56+
fwrite("\x02\x00", 2, 1, file);
57+
fwrite(&sample_rate, 4, 1, file);
58+
fwrite(&bytespersec, 4, 1, file);
59+
fwrite(&blkalign, 2, 1, file);
60+
fwrite(&bps, 2, 1, file);
61+
fwrite("data", 4, 1, file);
62+
fwrite(&size, 4, 1, file);
63+
64+
for (j = 0; j < hrtf->N; j++) {
65+
float l, r;
66+
67+
l = hrtf->DataIR.values[offset + j];
68+
r = hrtf->DataIR.values[offset + j + hrtf->N];
69+
fwrite(&l, 4, 1, file);
70+
fwrite(&r, 4, 1, file);
71+
}
72+
fclose(file);
73+
}
74+
75+
fail:
76+
mysofa_free(hrtf);
77+
78+
return 0;
79+
}

0 commit comments

Comments
 (0)