summaryrefslogtreecommitdiff
path: root/util/adt_generate_qt.m
blob: 45b3dcd033dd655842d2eb90a07e2c4b13a7f7ae (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
%{
****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is the build configuration utility of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************
%}

% Create mapping matrixes to convert ambisonic to different speaker layouts supported in Qt Multimedia
%
% This uses the ADT octave framework from https://bitbucket.org/ambidecodertoolbox/adt/src/master/
% to generate conversion matrixes between ambisonic formats and various speaker configurations
%
% We're generating two band conversion matrices with a cutoff frequency of 380 Herz. Filtering low
% and high frequencies differently is important to get a decent spatial reproduction. For details
% see the "Is my decoder Ambisonic?" paper (https://ambisonics.dreamhosters.com/BLaH3.pdf)
%
function adt_generate_qt()
    % assume speakers are 2 meters from listener. This should avoid near-field
    % effects and should work for most room setups
    radius = 2;

    [outfile,msg] = fopen("qambisonicdecoderdata_p.h",'w');
    fprintf(outfile, "/****************************************************************************\n");
    fprintf(outfile, "**\n");
    fprintf(outfile, "** Copyright (C) 2016 The Qt Company Ltd.\n");
    fprintf(outfile, "** Contact: https://www.qt.io/licensing/\n");
    fprintf(outfile, "**\n");
    fprintf(outfile, "** This file is part of the Qt Toolkit.\n");
    fprintf(outfile, "**\n");
    fprintf(outfile, "** $QT_BEGIN_LICENSE:LGPL$\n");
    fprintf(outfile, "** Commercial License Usage\n");
    fprintf(outfile, "** Licensees holding valid commercial Qt licenses may use this file in\n");
    fprintf(outfile, "** accordance with the commercial license agreement provided with the\n");
    fprintf(outfile, "** Software or, alternatively, in accordance with the terms contained in\n");
    fprintf(outfile, "** a written agreement between you and The Qt Company. For licensing terms\n");
    fprintf(outfile, "** and conditions see https://www.qt.io/terms-conditions. For further\n");
    fprintf(outfile, "** information use the contact form at https://www.qt.io/contact-us.\n");
    fprintf(outfile, "**\n");
    fprintf(outfile, "** GNU Lesser General Public License Usage\n");
    fprintf(outfile, "** Alternatively, this file may be used under the terms of the GNU Lesser\n");
    fprintf(outfile, "** General Public License version 3 as published by the Free Software\n");
    fprintf(outfile, "** Foundation and appearing in the file LICENSE.LGPL3 included in the\n");
    fprintf(outfile, "** packaging of this file. Please review the following information to\n");
    fprintf(outfile, "** ensure the GNU Lesser General Public License version 3 requirements\n");
    fprintf(outfile, "** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.\n");
    fprintf(outfile, "**\n");
    fprintf(outfile, "** GNU General Public License Usage\n");
    fprintf(outfile, "** Alternatively, this file may be used under the terms of the GNU\n");
    fprintf(outfile, "** General Public License version 2.0 or (at your option) the GNU General\n");
    fprintf(outfile, "** Public license version 3 or any later version approved by the KDE Free\n");
    fprintf(outfile, "** Qt Foundation. The licenses are as published by the Free Software\n");
    fprintf(outfile, "** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3\n");
    fprintf(outfile, "** included in the packaging of this file. Please review the following\n");
    fprintf(outfile, "** information to ensure the GNU General Public License requirements will\n");
    fprintf(outfile, "** be met: https://www.gnu.org/licenses/gpl-2.0.html and\n");
    fprintf(outfile, "** https://www.gnu.org/licenses/gpl-3.0.html.\n");
    fprintf(outfile, "**\n");
    fprintf(outfile, "** $QT_END_LICENSE$\n");
    fprintf(outfile, "**\n");
    fprintf(outfile, "****************************************************************************/\n");
    fprintf(outfile, "#ifndef QAMBISONICDECODERDATA_P_H\n");
    fprintf(outfile, "#define QAMBISONICDECODERDATA_P_H\n\n");
    fprintf(outfile, "#include <qtmultimediaglobal_p.h>\n\n");
    fprintf(outfile, "//  W A R N I N G\n");
    fprintf(outfile, "//  -------------\n");
    fprintf(outfile, "//\n");
    fprintf(outfile, "// This file is not part of the Qt API.  It exists purely as an\n");
    fprintf(outfile, "// implementation detail.  This header file may change from version to\n");
    fprintf(outfile, "// version without notice, or even be removed.\n");
    fprintf(outfile, "//\n");
    fprintf(outfile, "// We mean it.\n");
    fprintf(outfile, "//\n\n");
    fprintf(outfile, "// This file is generated by the matlab/octave file adt_generate_qt.m\n");
    fprintf(outfile, "// using the Ambisonic Decoder Toolbox (https://bitbucket.org/ambidecodertoolbox/adt/src/master/)\n");
    fprintf(outfile, "\n\n");
    fprintf(outfile, "QT_BEGIN_NAMESPACE\n\n");

    % cover top/bottom and back for mono and stereo
    imag_speakers = [0,0,radius; 0,0,-radius; 0,radius,0; 0,-radius,0; -radius,0,0];

    % Mono, one speaker up front
    S = ambi_spkr_array(...
        ... % array name
        'mono', ...
        ... % coordinate codes, unit codes
        ... % Azimuth, Elevation, Radius; Degrees, Degrees, Meters
        'AER', 'DDM', ...
        ... % speaker name, [azimuth, elevation, radius]
        'C',  [  0, 0, radius] ...
    );
    createDecoders(S, imag_speakers, outfile);

    % Stereo, assume -30 and 30 degree speakers
    S = ambi_spkr_array(...
        'stereo', ...
        'AER', 'DDM', ...
        'L',  [  30, 0, radius], ...
        'R',  [ -30, 0, radius] ...
    );
    createDecoders(S, imag_speakers, outfile);

    S.lfeRow = 3;
    S.name = "2dot1";
    createDecoders(S, imag_speakers, outfile);

    % cover top/bottom for surround
    imag_speakers = [0,0,radius; 0,0,-radius];

    % https://www.dolby.com/us/en/guide/surround-sound-speaker-setup/5-1-setup.html
    % Dolby 5.1 -- F: 22-30, S: 110-120, C in same plane as F
    S = ambi_spkr_array(...
        '5dot0', ...
        'AER', 'DDM', ...
        'L',  [  30, 0, radius], ...
        'R',  [ -30, 0, radius], ...
        'C',  [   0, 0, radius], ...
        'Ls', [ 110, 0, radius], ...
        'Rs', [-110, 0, radius] ...
    );
    createDecoders(S, imag_speakers, outfile);

    S.lfeRow = 4;
    S.name = "5dot1";
    createDecoders(S, imag_speakers, outfile);

    % https://www.dolby.com/us/en/guide/surround-sound-speaker-setup/7-1-setup.html
    % Dolby 7.1 F: 22-30, S: 90-110, B: 135-150
    S = ambi_spkr_array(...
        ... % array name
        '7dot0', ...
        ... % coordinate codes, unit codes
        ... % Azimuth, Elevation, Radius; Degrees, Degrees, Meters
        'AER', 'DDM', ...
        ... % speaker name, [azimuth, elevation, radius]
        'L',  [  30, 0, radius], ...
        'R',  [ -30, 0, radius], ...
        'C',  [   0, 0, radius], ...
        'Ls', [  90, 0, radius], ...
        'Rs', [ -90, 0, radius], ...
        'Lb', [ 150, 0, radius], ...
        'Rb', [-150, 0, radius] ...
    );
    createDecoders(S, imag_speakers, outfile);

    S.lfeRow = 4;
    S.name = "7dot1";
    createDecoders(S, imag_speakers, outfile);

    fprintf(outfile, "QT_END_NAMESPACE\n\n");
    fprintf(outfile, "#endif\n\n");
    fclose(outfile);
end

% remove rounding errors
function m = trimMatrix(m)
    for i = 1:rows(m)
        for j = 1:columns(m)
            if (abs(m(i, j)) < 1e-4)
                m(i, j) = 0;
            endif
        endfor
    endfor
end

function writeLFERow(outfile, m, suffix)
    if (strcmp(suffix, 'hf'))
        fprintf(outfile, "0.0f, "); % no need for High frequency data on the LFE channel
    else
        fprintf(outfile, "0.5f, ");
    endif
    for i = 2:columns(m)
        fprintf(outfile, "0.0f, ");
    endfor
    fprintf(outfile, "// LFE\n");
end

function writeMatrix(outfile, level, S, M, suffix)
    m = trimMatrix(M);
    hasLFE = isfield(S, "lfeRow");
    r = rows(m);
    c = columns(m);
    if (hasLFE)
        r = r + 1;
    endif
    fprintf(outfile, "// Decoder matrix for %s, ambisonic level %d\n", S.name, level);
    fprintf(outfile, "static constexpr float decoderMatrix_%s_%d_%s[%d*%d] = {\n", S.name, level, suffix, r, c);
    for i = 1:rows(S.id)
        fprintf(outfile, "%ff, ", m(i, :));
        fprintf(outfile, "// %s\n", S.id(i, 1){1});
        if (hasLFE && S.lfeRow == i + 1)
            writeLFERow(outfile, m, suffix);
        endif
    endfor
    fprintf(outfile, "};\n\n");
end

function createOneDecoder(S, imag_speakers, outfile, level)
    [D,S,M,C] = ambi_run_allrad(S, level, imag_speakers, [S.name '_' int2str(level)], false, "amb", 1, 3);
    writeMatrix(outfile, level, S, M.lf, "lf");
    m = ambi_apply_gamma(M.hf, D.hf_gains, C);
    writeMatrix(outfile, level, S, m, "hf");
end

function createDecoders(S, imag_speakers, outfile)
    for level = [1:3]
        createOneDecoder(S, imag_speakers, outfile, level)
    endfor
end