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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
|
#include "config.h"
#include "ambdec.h"
#include <algorithm>
#include <cctype>
#include <cstddef>
#include <iterator>
#include <sstream>
#include <string>
#include "alfstream.h"
#include "logging.h"
namespace {
template<typename T, std::size_t N>
constexpr inline std::size_t size(const T(&)[N]) noexcept
{ return N; }
int readline(std::istream &f, std::string &output)
{
while(f.good() && f.peek() == '\n')
f.ignore();
return std::getline(f, output) && !output.empty();
}
bool read_clipped_line(std::istream &f, std::string &buffer)
{
while(readline(f, buffer))
{
std::size_t pos{0};
while(pos < buffer.length() && std::isspace(buffer[pos]))
pos++;
buffer.erase(0, pos);
std::size_t cmtpos{buffer.find_first_of('#')};
if(cmtpos < buffer.length())
buffer.resize(cmtpos);
while(!buffer.empty() && std::isspace(buffer.back()))
buffer.pop_back();
if(!buffer.empty())
return true;
}
return false;
}
std::string read_word(std::istream &f)
{
std::string ret;
f >> ret;
return ret;
}
bool is_at_end(const std::string &buffer, std::size_t endpos)
{
while(endpos < buffer.length() && std::isspace(buffer[endpos]))
++endpos;
return !(endpos < buffer.length());
}
bool load_ambdec_speakers(al::vector<AmbDecConf::SpeakerConf> &spkrs, const std::size_t num_speakers, std::istream &f, std::string &buffer)
{
while(spkrs.size() < num_speakers)
{
std::istringstream istr{buffer};
std::string cmd{read_word(istr)};
if(cmd.empty())
{
if(!read_clipped_line(f, buffer))
{
ERR("Unexpected end of file\n");
return false;
}
continue;
}
if(cmd == "add_spkr")
{
spkrs.emplace_back();
AmbDecConf::SpeakerConf &spkr = spkrs.back();
const size_t spkr_num{spkrs.size()};
istr >> spkr.Name;
if(istr.fail()) WARN("Name not specified for speaker %zu\n", spkr_num);
istr >> spkr.Distance;
if(istr.fail()) WARN("Distance not specified for speaker %zu\n", spkr_num);
istr >> spkr.Azimuth;
if(istr.fail()) WARN("Azimuth not specified for speaker %zu\n", spkr_num);
istr >> spkr.Elevation;
if(istr.fail()) WARN("Elevation not specified for speaker %zu\n", spkr_num);
istr >> spkr.Connection;
if(istr.fail()) TRACE("Connection not specified for speaker %zu\n", spkr_num);
}
else
{
ERR("Unexpected speakers command: %s\n", cmd.c_str());
return false;
}
istr.clear();
const auto endpos = static_cast<std::size_t>(istr.tellg());
if(!is_at_end(buffer, endpos))
{
ERR("Unexpected junk on line: %s\n", buffer.c_str()+endpos);
return false;
}
buffer.clear();
}
return true;
}
bool load_ambdec_matrix(float (&gains)[MAX_AMBI_ORDER+1], al::vector<AmbDecConf::CoeffArray> &matrix, const std::size_t maxrow, std::istream &f, std::string &buffer)
{
bool gotgains{false};
std::size_t cur{0u};
while(cur < maxrow)
{
std::istringstream istr{buffer};
std::string cmd{read_word(istr)};
if(cmd.empty())
{
if(!read_clipped_line(f, buffer))
{
ERR("Unexpected end of file\n");
return false;
}
continue;
}
if(cmd == "order_gain")
{
std::size_t curgain{0u};
float value;
while(istr.good())
{
istr >> value;
if(istr.fail()) break;
if(!istr.eof() && !std::isspace(istr.peek()))
{
ERR("Extra junk on gain %zu: %s\n", curgain+1,
buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
return false;
}
if(curgain < size(gains))
gains[curgain++] = value;
}
std::fill(std::begin(gains)+curgain, std::end(gains), 0.0f);
gotgains = true;
}
else if(cmd == "add_row")
{
matrix.emplace_back();
AmbDecConf::CoeffArray &mtxrow = matrix.back();
std::size_t curidx{0u};
float value{};
while(istr.good())
{
istr >> value;
if(istr.fail()) break;
if(!istr.eof() && !std::isspace(istr.peek()))
{
ERR("Extra junk on matrix element %zux%zu: %s\n", curidx,
matrix.size(), buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
matrix.pop_back();
return false;
}
if(curidx < mtxrow.size())
mtxrow[curidx++] = value;
}
std::fill(mtxrow.begin()+curidx, mtxrow.end(), 0.0f);
cur++;
}
else
{
ERR("Unexpected matrix command: %s\n", cmd.c_str());
return false;
}
istr.clear();
const auto endpos = static_cast<std::size_t>(istr.tellg());
if(!is_at_end(buffer, endpos))
{
ERR("Unexpected junk on line: %s\n", buffer.c_str()+endpos);
return false;
}
buffer.clear();
}
if(!gotgains)
{
ERR("Matrix order_gain not specified\n");
return false;
}
return true;
}
} // namespace
int AmbDecConf::load(const char *fname) noexcept
{
al::ifstream f{fname};
if(!f.is_open())
{
ERR("Failed to open: %s\n", fname);
return 0;
}
std::size_t num_speakers{0u};
std::string buffer;
while(read_clipped_line(f, buffer))
{
std::istringstream istr{buffer};
std::string command{read_word(istr)};
if(command.empty())
{
ERR("Malformed line: %s\n", buffer.c_str());
return 0;
}
if(command == "/description")
istr >> Description;
else if(command == "/version")
{
istr >> Version;
if(!istr.eof() && !std::isspace(istr.peek()))
{
ERR("Extra junk after version: %s\n",
buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
return 0;
}
if(Version != 3)
{
ERR("Unsupported version: %u\n", Version);
return 0;
}
}
else if(command == "/dec/chan_mask")
{
istr >> std::hex >> ChanMask >> std::dec;
if(!istr.eof() && !std::isspace(istr.peek()))
{
ERR("Extra junk after mask: %s\n",
buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
return 0;
}
}
else if(command == "/dec/freq_bands")
{
istr >> FreqBands;
if(!istr.eof() && !std::isspace(istr.peek()))
{
ERR("Extra junk after freq_bands: %s\n",
buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
return 0;
}
if(FreqBands != 1 && FreqBands != 2)
{
ERR("Invalid freq_bands value: %u\n", FreqBands);
return 0;
}
}
else if(command == "/dec/speakers")
{
istr >> num_speakers;
if(!istr.eof() && !std::isspace(istr.peek()))
{
ERR("Extra junk after speakers: %s\n",
buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
return 0;
}
Speakers.reserve(num_speakers);
LFMatrix.reserve(num_speakers);
HFMatrix.reserve(num_speakers);
}
else if(command == "/dec/coeff_scale")
{
std::string scale = read_word(istr);
if(scale == "n3d") CoeffScale = AmbDecScale::N3D;
else if(scale == "sn3d") CoeffScale = AmbDecScale::SN3D;
else if(scale == "fuma") CoeffScale = AmbDecScale::FuMa;
else
{
ERR("Unsupported coeff scale: %s\n", scale.c_str());
return 0;
}
}
else if(command == "/opt/xover_freq")
{
istr >> XOverFreq;
if(!istr.eof() && !std::isspace(istr.peek()))
{
ERR("Extra junk after xover_freq: %s\n",
buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
return 0;
}
}
else if(command == "/opt/xover_ratio")
{
istr >> XOverRatio;
if(!istr.eof() && !std::isspace(istr.peek()))
{
ERR("Extra junk after xover_ratio: %s\n",
buffer.c_str()+static_cast<std::size_t>(istr.tellg()));
return 0;
}
}
else if(command == "/opt/input_scale" || command == "/opt/nfeff_comp" ||
command == "/opt/delay_comp" || command == "/opt/level_comp")
{
/* Unused */
read_word(istr);
}
else if(command == "/speakers/{")
{
const auto endpos = static_cast<std::size_t>(istr.tellg());
if(!is_at_end(buffer, endpos))
{
ERR("Unexpected junk on line: %s\n", buffer.c_str()+endpos);
return 0;
}
buffer.clear();
if(!load_ambdec_speakers(Speakers, num_speakers, f, buffer))
return 0;
if(!read_clipped_line(f, buffer))
{
ERR("Unexpected end of file\n");
return 0;
}
std::istringstream istr2{buffer};
std::string endmark{read_word(istr2)};
if(endmark != "/}")
{
ERR("Expected /} after speaker definitions, got %s\n", endmark.c_str());
return 0;
}
istr.swap(istr2);
}
else if(command == "/lfmatrix/{" || command == "/hfmatrix/{" || command == "/matrix/{")
{
const auto endpos = static_cast<std::size_t>(istr.tellg());
if(!is_at_end(buffer, endpos))
{
ERR("Unexpected junk on line: %s\n", buffer.c_str()+endpos);
return 0;
}
buffer.clear();
if(FreqBands == 1)
{
if(command != "/matrix/{")
{
ERR("Unexpected \"%s\" type for a single-band decoder\n", command.c_str());
return 0;
}
if(!load_ambdec_matrix(HFOrderGain, HFMatrix, num_speakers, f, buffer))
return 0;
}
else
{
if(command == "/lfmatrix/{")
{
if(!load_ambdec_matrix(LFOrderGain, LFMatrix, num_speakers, f, buffer))
return 0;
}
else if(command == "/hfmatrix/{")
{
if(!load_ambdec_matrix(HFOrderGain, HFMatrix, num_speakers, f, buffer))
return 0;
}
else
{
ERR("Unexpected \"%s\" type for a dual-band decoder\n", command.c_str());
return 0;
}
}
if(!read_clipped_line(f, buffer))
{
ERR("Unexpected end of file\n");
return 0;
}
std::istringstream istr2{buffer};
std::string endmark{read_word(istr2)};
if(endmark != "/}")
{
ERR("Expected /} after matrix definitions, got %s\n", endmark.c_str());
return 0;
}
istr.swap(istr2);
}
else if(command == "/end")
{
const auto endpos = static_cast<std::size_t>(istr.tellg());
if(!is_at_end(buffer, endpos))
{
ERR("Unexpected junk on end: %s\n", buffer.c_str()+endpos);
return 0;
}
return 1;
}
else
{
ERR("Unexpected command: %s\n", command.c_str());
return 0;
}
istr.clear();
const auto endpos = static_cast<std::size_t>(istr.tellg());
if(!is_at_end(buffer, endpos))
{
ERR("Unexpected junk on line: %s\n", buffer.c_str()+endpos);
return 0;
}
buffer.clear();
}
ERR("Unexpected end of file\n");
return 0;
}
|