aboutsummaryrefslogtreecommitdiffstats
path: root/utils/sofa-info.cpp
blob: a9a8bd2fa8ad81046401e4cb349e13c8919af1ec (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * SOFA info utility for inspecting SOFA file metrics and determining HRTF
 * utility compatible layouts.
 *
 * Copyright (C) 2018-2019  Christopher Fitzgerald
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Or visit:  http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */

#include <stdio.h>

#include <algorithm>
#include <array>
#include <cmath>
#include <memory>
#include <vector>

#include <mysofa.h>

#include "win_main_utf8.h"


using uint = unsigned int;
using double3 = std::array<double,3>;

struct MySofaDeleter {
    void operator()(MYSOFA_HRTF *sofa) { mysofa_free(sofa); }
};
using MySofaHrtfPtr = std::unique_ptr<MYSOFA_HRTF,MySofaDeleter>;

// Per-field measurement info.
struct HrirFdT {
    double mDistance{0.0};
    uint mEvCount{0u};
    uint mEvStart{0u};
    std::vector<uint> mAzCounts;
};

static const char *SofaErrorStr(int err)
{
    switch(err)
    {
    case MYSOFA_OK: return "OK";
    case MYSOFA_INVALID_FORMAT: return "Invalid format";
    case MYSOFA_UNSUPPORTED_FORMAT: return "Unsupported format";
    case MYSOFA_INTERNAL_ERROR: return "Internal error";
    case MYSOFA_NO_MEMORY: return "Out of memory";
    case MYSOFA_READ_ERROR: return "Read error";
    }
    return "Unknown";
}

static void PrintSofaAttributes(const char *prefix, struct MYSOFA_ATTRIBUTE *attribute)
{
    while(attribute)
    {
        fprintf(stdout, "%s.%s: %s\n", prefix, attribute->name, attribute->value);
        attribute = attribute->next;
    }
}

static void PrintSofaArray(const char *prefix, struct MYSOFA_ARRAY *array)
{
    PrintSofaAttributes(prefix, array->attributes);

    for(uint i{0u};i < array->elements;i++)
        fprintf(stdout, "%s[%u]: %.6f\n", prefix, i, array->values[i]);
}

/* Produces a sorted array of unique elements from a particular axis of the
 * triplets array.  The filters are used to focus on particular coordinates
 * of other axes as necessary.  The epsilons are used to constrain the
 * equality of unique elements.
 */
static std::vector<double> GetUniquelySortedElems(const uint m, const double3 *aers,
    const uint axis, const double *const (&filters)[3], const double (&epsilons)[3])
{
    std::vector<double> elems;
    for(uint i{0u};i < m;++i)
    {
        const double elem{aers[i][axis]};

        uint j;
        for(j = 0;j < 3;j++)
        {
            if(filters[j] && std::abs(aers[i][j] - *filters[j]) > epsilons[j])
                break;
        }
        if(j < 3)
            continue;

        auto iter = elems.begin();
        for(;iter != elems.end();++iter)
        {
            const double delta{elem - *iter};
            if(delta > epsilons[axis]) continue;
            if(delta >= -epsilons[axis]) break;

            iter = elems.emplace(iter, elem);
            break;
        }
        if(iter == elems.end())
            elems.emplace_back(elem);
    }
    return elems;
}

/* Given a list of azimuths, this will produce the smallest step size that can
 * uniformly cover the list. Ideally this will be over half, but in degenerate
 * cases this can fall to a minimum of 5 (the lower limit).
 */
static double GetUniformAzimStep(const double epsilon, const size_t m, const double *elems)
{
    if(m < 5) return 0.0;

    /* Get the maximum count possible (limit to 255), given the first two
     * elements. It would be impossible to have more than this since the first
     * element must be included.
     */
    uint count{static_cast<uint>(std::ceil(360.0 / (elems[1]-elems[0])))};
    count = std::min(count, 255u);

    for(;count >= 5;--count)
    {
        /* Given the stepping value for this number of elements, check each
         * multiple to ensure there's a matching element.
         */
        const double step{360.0 / count};
        bool good{true};
        size_t idx{1u};
        for(uint mult{1u};mult < count && good;++mult)
        {
            const double target{step*mult + elems[0]};
            while(idx < m && target-elems[idx] > epsilon)
                ++idx;
            good &= (idx < m) && !(std::abs(target-elems[idx++]) > epsilon);
        }
        if(good)
            return step;
    }
    return 0.0;
}

/* Given a list of elevations, this will produce the smallest step size that
 * can uniformly cover the list. Ideally this will be over half, but in
 * degenerate cases this can fall to a minimum of 5 (the lower limit).
 */
static double GetUniformElevStep(const double epsilon, const size_t m, const double *elems)
{
    if(m < 5) return 0.0;

    uint count{static_cast<uint>(std::ceil(180.0 / (elems[1]-elems[0])))};
    count = std::min(count, 255u);

    for(;count >= 5;--count)
    {
        const double step{180.0 / count};
        bool good{true};
        size_t idx{1u};
        /* Elevations don't need to match all multiples if there's not enough
         * elements to check. Missing elevations can be synthesized.
         */
        for(uint mult{1u};mult <= count && idx < m && good;++mult)
        {
            const double target{step*mult + elems[0]};
            while(idx < m && target-elems[idx] > epsilon)
                ++idx;
            good &= !(idx < m) || !(std::abs(target-elems[idx++]) > epsilon);
        }
        if(good)
            return step;
    }
    return 0.0;
}

/* Attempts to produce a compatible layout.  Most data sets tend to be
 * uniform and have the same major axis as used by OpenAL Soft's HRTF model.
 * This will remove outliers and produce a maximally dense layout when
 * possible.  Those sets that contain purely random measurements or use
 * different major axes will fail.
 */
static void PrintCompatibleLayout(const uint m, const float *xyzs)
{
    fprintf(stdout, "\n");

    auto aers = std::vector<double3>(m, double3{});
    for(uint i{0u};i < m;++i)
    {
        float aer[3]{xyzs[i*3], xyzs[i*3 + 1], xyzs[i*3 + 2]};
        mysofa_c2s(&aer[0]);
        aers[i][0] = aer[0];
        aers[i][1] = aer[1];
        aers[i][2] = aer[2];
    }

    auto radii = GetUniquelySortedElems(m, aers.data(), 2, {}, {0.1, 0.1, 0.001});
    if(radii.size() > (m / 3))
    {
        fprintf(stdout, "Incompatible layout (inumerable radii).\n");
        return;
    }

    auto fds = std::vector<HrirFdT>(radii.size());
    for(size_t fi{0u};fi < radii.size();fi++)
        fds[fi].mDistance = radii[fi];

    for(uint fi{0u};fi < fds.size();)
    {
        const double dist{fds[fi].mDistance};
        auto elevs = GetUniquelySortedElems(m, aers.data(), 1, {nullptr, nullptr, &dist},
            {0.1, 0.1, 0.001});

        /* Remove elevations that don't have a valid set of azimuths. */
        auto invalid_elev = [&dist,&aers,m](const double ev) -> bool
        {
            auto azim = GetUniquelySortedElems(m, aers.data(), 0, {nullptr, &ev, &dist},
                {0.1, 0.1, 0.001});

            if(std::abs(90.0 - std::abs(ev)) < 0.1)
                return azim.size() != 1;
            if(azim.empty() || !(std::abs(azim[0]) < 0.1))
                return true;
            return GetUniformAzimStep(0.1, azim.size(), azim.data()) <= 0.0;
        };
        elevs.erase(std::remove_if(elevs.begin(), elevs.end(), invalid_elev), elevs.end());

        /* Reverse the elevations so it increments starting with -90 (flipped
         * from +90). This makes it easier to work out a proper stepping value.
         */
        std::reverse(elevs.begin(), elevs.end());
        for(auto &ev : elevs) ev *= -1.0;

        double step{GetUniformElevStep(0.1, elevs.size(), elevs.data())};
        if(step <= 0.0)
        {
            fprintf(stdout, "Non-uniform elevations on field distance %f.\n", dist);
            fds.erase(fds.begin() + static_cast<ptrdiff_t>(fi));
            continue;
        }

        /* Re-reverse the elevations to restore the correct order. */
        for(auto &ev : elevs) ev *= -1.0;
        std::reverse(elevs.begin(), elevs.end());

        uint evStart{0u};
        for(uint ei{0u};ei < elevs.size();ei++)
        {
            if(!(elevs[ei] < 0.0))
            {
                fprintf(stdout, "Too many missing elevations on field distance %f.\n", dist);
                return;
            }

            double eif{(90.0+elevs[ei]) / step};
            const double ev_start{std::round(eif)};

            if(std::abs(eif - ev_start) < (0.1/step))
            {
                evStart = static_cast<uint>(ev_start);
                break;
            }
        }

        const auto evCount = static_cast<uint>(std::round(180.0 / step)) + 1;
        if(evCount < 5)
        {
            fprintf(stdout, "Too few uniform elevations on field distance %f.\n", dist);
            fds.erase(fds.begin() + static_cast<ptrdiff_t>(fi));
            continue;
        }

        fds[fi].mEvCount = evCount;
        fds[fi].mEvStart = evStart;
        fds[fi].mAzCounts.resize(evCount);
        auto &azCounts = fds[fi].mAzCounts;

        for(uint ei{evStart};ei < evCount;ei++)
        {
            double ev{-90.0 + ei*180.0/(evCount - 1)};
            auto azims = GetUniquelySortedElems(m, aers.data(), 0, { nullptr, &ev, &dist },
                { 0.1, 0.1, 0.001 });

            if(ei == 0 || ei == (evCount-1))
            {
                if(azims.size() != 1)
                {
                    fprintf(stdout, "Non-singular poles on field distance %f.\n", dist);
                    return;
                }
                azCounts[ei] = 1;
            }
            else
            {
                step = GetUniformAzimStep(0.1, azims.size(), azims.data());
                if(step <= 0.0)
                {
                    fprintf(stdout, "Non-uniform azimuths on elevation %f, field distance %f.\n",
                        ev, dist);
                    return;
                }
                azCounts[ei] = static_cast<uint>(std::round(360.0f / step));
            }
        }

        for(uint ei{0u};ei < evStart;ei++)
            azCounts[ei] = azCounts[evCount - ei - 1];
        ++fi;
    }
    if(fds.empty())
    {
        fprintf(stdout, "No compatible field layouts in SOFA file.\n");
        return;
    }

    fprintf(stdout, "Compatible Layout:\n\ndistance = %.3f", fds[0].mDistance);
    for(size_t fi{1u};fi < fds.size();fi++)
        fprintf(stdout, ", %.3f", fds[fi].mDistance);

    fprintf(stdout, "\nazimuths = ");
    for(size_t fi{0u};fi < fds.size();fi++)
    {
        for(uint ei{0u};ei < fds[fi].mEvCount;ei++)
            fprintf(stdout, "%d%s", fds[fi].mAzCounts[ei],
                (ei < (fds[fi].mEvCount - 1)) ? ", " :
                (fi < (fds.size() - 1)) ? ";\n           " : "\n");
    }
}

// Load and inspect the given SOFA file.
static void SofaInfo(const char *filename)
{
    int err;
    MySofaHrtfPtr sofa{mysofa_load(filename, &err)};
    if(!sofa)
    {
        fprintf(stdout, "Error: Could not load source file '%s'.\n", filename);
        return;
    }

    /* NOTE: Some valid SOFA files are failing this check. */
    err = mysofa_check(sofa.get());
    if(err != MYSOFA_OK)
        fprintf(stdout, "Warning: Supposedly malformed source file '%s' (%s).\n", filename,
            SofaErrorStr(err));

    mysofa_tocartesian(sofa.get());

    PrintSofaAttributes("Info", sofa->attributes);

    fprintf(stdout, "Measurements: %u\n", sofa->M);
    fprintf(stdout, "Receivers: %u\n", sofa->R);
    fprintf(stdout, "Emitters: %u\n", sofa->E);
    fprintf(stdout, "Samples: %u\n", sofa->N);

    PrintSofaArray("SampleRate", &sofa->DataSamplingRate);
    PrintSofaArray("DataDelay", &sofa->DataDelay);

    PrintCompatibleLayout(sofa->M, sofa->SourcePosition.values);
}

int main(int argc, char *argv[])
{
    GET_UNICODE_ARGS(&argc, &argv);

    if(argc != 2)
    {
        fprintf(stdout, "Usage: %s <sofa-file>\n", argv[0]);
        return 0;
    }

    SofaInfo(argv[1]);

    return 0;
}