aboutsummaryrefslogtreecommitdiffstats
path: root/tools/sofa2wavs.c
blob: 1f1075b22f8d1cf0ab6c2a16050250267a90347e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (c) 2017 Paul B Mahol
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <mysofa.h>

int main(int argc, char **argv)
{
    struct MYSOFA_HRTF *hrtf;
    int sample_rate;
    int err, i, j;

    if (argc < 3) {
        printf("usage: %s input_SOFA_file output_directory\n", argv[0]);
        return 1;
    }

    hrtf = mysofa_load(argv[1], &err);
    if (!hrtf || err) {
        printf("invalid input SOFA file: %s\n", argv[1]);
        return 1;
    }

    if (hrtf->DataSamplingRate.elements != 1)
        goto fail;
    sample_rate = hrtf->DataSamplingRate.values[0];

    err = mkdir(argv[2], 0744);
    if (err)
        goto fail;

    err = chdir(argv[2]);
    if (err)
        goto fail;

    for (i = 0; i < hrtf->M; i++) {
        FILE *file;
        int bps = 32;
        int blkalign = 8;
        int bytespersec = blkalign * sample_rate;
        char filename[1024];
        int azi = hrtf->SourcePosition.values[i * 3];
        int ele = hrtf->SourcePosition.values[i * 3 + 1];
        int dis = hrtf->SourcePosition.values[i * 3 + 2];
        int size = 8 * hrtf->N;
        int offset = i * 2 * hrtf->N;

        snprintf(filename, sizeof(filename), "azi_%d_ele_%d_dis_%d.wav", azi, ele, dis);
        file = fopen(filename, "w+");
        fwrite("RIFF", 4, 1, file);
        fwrite("\xFF\xFF\xFF\xFF", 4, 1, file);
        fwrite("WAVE", 4, 1, file);
        fwrite("fmt ", 4, 1, file);
        fwrite("\x10\x00\00\00", 4, 1, file);
        fwrite("\x03\x00", 2, 1, file);
        fwrite("\x02\x00", 2, 1, file);
        fwrite(&sample_rate, 4, 1, file);
        fwrite(&bytespersec, 4, 1, file);
        fwrite(&blkalign, 2, 1, file);
        fwrite(&bps, 2, 1, file);
        fwrite("data", 4, 1, file);
        fwrite(&size, 4, 1, file);

        for (j = 0; j < hrtf->N; j++) {
            float l, r;

            l = hrtf->DataIR.values[offset + j];
            r = hrtf->DataIR.values[offset + j + hrtf->N];
            fwrite(&l, 4, 1, file);
            fwrite(&r, 4, 1, file);
        }
        fclose(file);
    }

fail:
    mysofa_free(hrtf);

    return 0;
}