2021-04-28 12:43:51 +08:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "ambdec.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
2023-02-04 15:03:54 +08:00
|
|
|
#include <cstdarg>
|
2021-04-28 12:43:51 +08:00
|
|
|
#include <cstddef>
|
2023-02-04 15:03:54 +08:00
|
|
|
#include <cstdio>
|
2021-04-28 12:43:51 +08:00
|
|
|
#include <iterator>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
#include "albit.h"
|
2021-04-28 12:43:51 +08:00
|
|
|
#include "alfstream.h"
|
2023-02-04 15:03:54 +08:00
|
|
|
#include "alspan.h"
|
|
|
|
#include "opthelpers.h"
|
2021-04-28 12:43:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
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;
|
2023-02-04 15:03:54 +08:00
|
|
|
return !(endpos < buffer.length() && buffer[endpos] != '#');
|
2021-04-28 12:43:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
enum class ReaderScope {
|
|
|
|
Global,
|
|
|
|
Speakers,
|
|
|
|
LFMatrix,
|
|
|
|
HFMatrix,
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef __USE_MINGW_ANSI_STDIO
|
|
|
|
[[gnu::format(gnu_printf,2,3)]]
|
|
|
|
#else
|
|
|
|
[[gnu::format(printf,2,3)]]
|
|
|
|
#endif
|
2023-05-31 23:57:33 +08:00
|
|
|
al::optional<std::string> make_error(size_t linenum, const char *fmt, ...)
|
2021-04-28 12:43:51 +08:00
|
|
|
{
|
2023-05-31 23:57:33 +08:00
|
|
|
al::optional<std::string> ret;
|
2023-02-04 15:03:54 +08:00
|
|
|
auto &str = ret.emplace();
|
|
|
|
|
|
|
|
str.resize(256);
|
|
|
|
int printed{std::snprintf(const_cast<char*>(str.data()), str.length(), "Line %zu: ", linenum)};
|
|
|
|
if(printed < 0) printed = 0;
|
|
|
|
auto plen = std::min(static_cast<size_t>(printed), str.length());
|
|
|
|
|
|
|
|
std::va_list args, args2;
|
|
|
|
va_start(args, fmt);
|
|
|
|
va_copy(args2, args);
|
|
|
|
const int msglen{std::vsnprintf(&str[plen], str.size()-plen, fmt, args)};
|
2023-05-03 18:59:33 +08:00
|
|
|
if(msglen >= 0 && static_cast<size_t>(msglen) >= str.size()-plen)
|
2021-04-28 12:43:51 +08:00
|
|
|
{
|
2023-02-04 15:03:54 +08:00
|
|
|
str.resize(static_cast<size_t>(msglen) + plen + 1u);
|
|
|
|
std::vsnprintf(&str[plen], str.size()-plen, fmt, args2);
|
|
|
|
}
|
|
|
|
va_end(args2);
|
|
|
|
va_end(args);
|
2021-04-28 12:43:51 +08:00
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2021-04-28 12:43:51 +08:00
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
} // namespace
|
2021-04-28 12:43:51 +08:00
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
AmbDecConf::~AmbDecConf() = default;
|
2021-04-28 12:43:51 +08:00
|
|
|
|
|
|
|
|
2023-05-31 23:57:33 +08:00
|
|
|
al::optional<std::string> AmbDecConf::load(const char *fname) noexcept
|
2021-04-28 12:43:51 +08:00
|
|
|
{
|
2023-02-04 15:03:54 +08:00
|
|
|
al::ifstream f{fname};
|
|
|
|
if(!f.is_open())
|
|
|
|
return std::string("Failed to open file \"")+fname+"\"";
|
|
|
|
|
|
|
|
ReaderScope scope{ReaderScope::Global};
|
|
|
|
size_t speaker_pos{0};
|
|
|
|
size_t lfmatrix_pos{0};
|
|
|
|
size_t hfmatrix_pos{0};
|
|
|
|
size_t linenum{0};
|
|
|
|
|
|
|
|
std::string buffer;
|
|
|
|
while(f.good() && std::getline(f, buffer))
|
2021-04-28 12:43:51 +08:00
|
|
|
{
|
2023-02-04 15:03:54 +08:00
|
|
|
++linenum;
|
|
|
|
|
2021-04-28 12:43:51 +08:00
|
|
|
std::istringstream istr{buffer};
|
2023-02-04 15:03:54 +08:00
|
|
|
std::string command{read_word(istr)};
|
|
|
|
if(command.empty() || command[0] == '#')
|
|
|
|
continue;
|
2021-04-28 12:43:51 +08:00
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
if(command == "/}")
|
2021-04-28 12:43:51 +08:00
|
|
|
{
|
2023-02-04 15:03:54 +08:00
|
|
|
if(scope == ReaderScope::Global)
|
|
|
|
return make_error(linenum, "Unexpected /} in global scope");
|
|
|
|
scope = ReaderScope::Global;
|
2021-04-28 12:43:51 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
if(scope == ReaderScope::Speakers)
|
2021-04-28 12:43:51 +08:00
|
|
|
{
|
2023-02-04 15:03:54 +08:00
|
|
|
if(command == "add_spkr")
|
2021-04-28 12:43:51 +08:00
|
|
|
{
|
2023-02-04 15:03:54 +08:00
|
|
|
if(speaker_pos == NumSpeakers)
|
|
|
|
return make_error(linenum, "Too many speakers specified");
|
|
|
|
|
|
|
|
AmbDecConf::SpeakerConf &spkr = Speakers[speaker_pos++];
|
|
|
|
istr >> spkr.Name;
|
|
|
|
istr >> spkr.Distance;
|
|
|
|
istr >> spkr.Azimuth;
|
|
|
|
istr >> spkr.Elevation;
|
|
|
|
istr >> spkr.Connection;
|
2021-04-28 12:43:51 +08:00
|
|
|
}
|
2023-02-04 15:03:54 +08:00
|
|
|
else
|
|
|
|
return make_error(linenum, "Unexpected speakers command: %s", command.c_str());
|
2021-04-28 12:43:51 +08:00
|
|
|
}
|
2023-02-04 15:03:54 +08:00
|
|
|
else if(scope == ReaderScope::LFMatrix || scope == ReaderScope::HFMatrix)
|
2021-04-28 12:43:51 +08:00
|
|
|
{
|
2023-02-04 15:03:54 +08:00
|
|
|
auto &gains = (scope == ReaderScope::LFMatrix) ? LFOrderGain : HFOrderGain;
|
|
|
|
auto *matrix = (scope == ReaderScope::LFMatrix) ? LFMatrix : HFMatrix;
|
|
|
|
auto &pos = (scope == ReaderScope::LFMatrix) ? lfmatrix_pos : hfmatrix_pos;
|
|
|
|
|
|
|
|
if(command == "order_gain")
|
2021-04-28 12:43:51 +08:00
|
|
|
{
|
2023-02-04 15:03:54 +08:00
|
|
|
size_t toread{(ChanMask > Ambi3OrderMask) ? 5u : 4u};
|
|
|
|
std::size_t curgain{0u};
|
|
|
|
float value{};
|
|
|
|
while(toread)
|
|
|
|
{
|
|
|
|
--toread;
|
|
|
|
istr >> value;
|
2023-05-31 23:57:33 +08:00
|
|
|
if(curgain < al::size(gains))
|
2023-02-04 15:03:54 +08:00
|
|
|
gains[curgain++] = value;
|
|
|
|
}
|
2021-04-28 12:43:51 +08:00
|
|
|
}
|
2023-02-04 15:03:54 +08:00
|
|
|
else if(command == "add_row")
|
|
|
|
{
|
|
|
|
if(pos == NumSpeakers)
|
|
|
|
return make_error(linenum, "Too many matrix rows specified");
|
2021-04-28 12:43:51 +08:00
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
unsigned int mask{ChanMask};
|
2022-04-25 12:02:45 +08:00
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
AmbDecConf::CoeffArray &mtxrow = matrix[pos++];
|
|
|
|
mtxrow.fill(0.0f);
|
2022-04-25 12:02:45 +08:00
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
float value{};
|
|
|
|
while(mask)
|
|
|
|
{
|
|
|
|
auto idx = static_cast<unsigned>(al::countr_zero(mask));
|
|
|
|
mask &= ~(1u << idx);
|
2021-04-28 12:43:51 +08:00
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
istr >> value;
|
|
|
|
if(idx < mtxrow.size())
|
|
|
|
mtxrow[idx] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return make_error(linenum, "Unexpected matrix command: %s", command.c_str());
|
|
|
|
}
|
|
|
|
// Global scope commands
|
|
|
|
else if(command == "/description")
|
|
|
|
{
|
|
|
|
while(istr.good() && std::isspace(istr.peek()))
|
|
|
|
istr.ignore();
|
|
|
|
std::getline(istr, Description);
|
|
|
|
while(!Description.empty() && std::isspace(Description.back()))
|
|
|
|
Description.pop_back();
|
|
|
|
}
|
2021-04-28 12:43:51 +08:00
|
|
|
else if(command == "/version")
|
|
|
|
{
|
2023-02-04 15:03:54 +08:00
|
|
|
if(Version)
|
|
|
|
return make_error(linenum, "Duplicate version definition");
|
2021-04-28 12:43:51 +08:00
|
|
|
istr >> Version;
|
|
|
|
if(Version != 3)
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Unsupported version: %d", Version);
|
2021-04-28 12:43:51 +08:00
|
|
|
}
|
|
|
|
else if(command == "/dec/chan_mask")
|
|
|
|
{
|
|
|
|
if(ChanMask)
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Duplicate chan_mask definition");
|
2021-04-28 12:43:51 +08:00
|
|
|
istr >> std::hex >> ChanMask >> std::dec;
|
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
if(!ChanMask || ChanMask > Ambi4OrderMask)
|
|
|
|
return make_error(linenum, "Invalid chan_mask: 0x%x", ChanMask);
|
|
|
|
if(ChanMask > Ambi3OrderMask && CoeffScale == AmbDecScale::FuMa)
|
|
|
|
return make_error(linenum, "FuMa not compatible with over third-order");
|
2021-04-28 12:43:51 +08:00
|
|
|
}
|
|
|
|
else if(command == "/dec/freq_bands")
|
|
|
|
{
|
|
|
|
if(FreqBands)
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Duplicate freq_bands");
|
2021-04-28 12:43:51 +08:00
|
|
|
istr >> FreqBands;
|
|
|
|
if(FreqBands != 1 && FreqBands != 2)
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Invalid freq_bands: %u", FreqBands);
|
2021-04-28 12:43:51 +08:00
|
|
|
}
|
|
|
|
else if(command == "/dec/speakers")
|
|
|
|
{
|
|
|
|
if(NumSpeakers)
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Duplicate speakers");
|
2021-04-28 12:43:51 +08:00
|
|
|
istr >> NumSpeakers;
|
|
|
|
if(!NumSpeakers)
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Invalid speakers: %zu", NumSpeakers);
|
2021-04-28 12:43:51 +08:00
|
|
|
Speakers = std::make_unique<SpeakerConf[]>(NumSpeakers);
|
|
|
|
}
|
|
|
|
else if(command == "/dec/coeff_scale")
|
|
|
|
{
|
2023-02-04 15:03:54 +08:00
|
|
|
if(CoeffScale != AmbDecScale::Unset)
|
|
|
|
return make_error(linenum, "Duplicate coeff_scale");
|
|
|
|
|
|
|
|
std::string scale{read_word(istr)};
|
2021-04-28 12:43:51 +08:00
|
|
|
if(scale == "n3d") CoeffScale = AmbDecScale::N3D;
|
|
|
|
else if(scale == "sn3d") CoeffScale = AmbDecScale::SN3D;
|
|
|
|
else if(scale == "fuma") CoeffScale = AmbDecScale::FuMa;
|
|
|
|
else
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Unexpected coeff_scale: %s", scale.c_str());
|
|
|
|
|
|
|
|
if(ChanMask > Ambi3OrderMask && CoeffScale == AmbDecScale::FuMa)
|
|
|
|
return make_error(linenum, "FuMa not compatible with over third-order");
|
2021-04-28 12:43:51 +08:00
|
|
|
}
|
|
|
|
else if(command == "/opt/xover_freq")
|
|
|
|
{
|
|
|
|
istr >> XOverFreq;
|
|
|
|
}
|
|
|
|
else if(command == "/opt/xover_ratio")
|
|
|
|
{
|
|
|
|
istr >> XOverRatio;
|
|
|
|
}
|
2023-02-04 15:03:54 +08:00
|
|
|
else if(command == "/opt/input_scale" || command == "/opt/nfeff_comp"
|
|
|
|
|| command == "/opt/delay_comp" || command == "/opt/level_comp")
|
2021-04-28 12:43:51 +08:00
|
|
|
{
|
|
|
|
/* Unused */
|
|
|
|
read_word(istr);
|
|
|
|
}
|
|
|
|
else if(command == "/speakers/{")
|
|
|
|
{
|
|
|
|
if(!NumSpeakers)
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Speakers defined without a count");
|
|
|
|
scope = ReaderScope::Speakers;
|
2021-04-28 12:43:51 +08:00
|
|
|
}
|
|
|
|
else if(command == "/lfmatrix/{" || command == "/hfmatrix/{" || command == "/matrix/{")
|
|
|
|
{
|
|
|
|
if(!NumSpeakers)
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Matrix defined without a speaker count");
|
|
|
|
if(!ChanMask)
|
|
|
|
return make_error(linenum, "Matrix defined without a channel mask");
|
2021-04-28 12:43:51 +08:00
|
|
|
|
|
|
|
if(!Matrix)
|
|
|
|
{
|
|
|
|
Matrix = std::make_unique<CoeffArray[]>(NumSpeakers * FreqBands);
|
|
|
|
LFMatrix = Matrix.get();
|
|
|
|
HFMatrix = LFMatrix + NumSpeakers*(FreqBands-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(FreqBands == 1)
|
|
|
|
{
|
|
|
|
if(command != "/matrix/{")
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Unexpected \"%s\" for a single-band decoder",
|
|
|
|
command.c_str());
|
|
|
|
scope = ReaderScope::HFMatrix;
|
2021-04-28 12:43:51 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(command == "/lfmatrix/{")
|
2023-02-04 15:03:54 +08:00
|
|
|
scope = ReaderScope::LFMatrix;
|
2021-04-28 12:43:51 +08:00
|
|
|
else if(command == "/hfmatrix/{")
|
2023-02-04 15:03:54 +08:00
|
|
|
scope = ReaderScope::HFMatrix;
|
2021-04-28 12:43:51 +08:00
|
|
|
else
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Unexpected \"%s\" for a dual-band decoder",
|
|
|
|
command.c_str());
|
2021-04-28 12:43:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(command == "/end")
|
|
|
|
{
|
|
|
|
const auto endpos = static_cast<std::size_t>(istr.tellg());
|
|
|
|
if(!is_at_end(buffer, endpos))
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Extra junk on end: %s", buffer.substr(endpos).c_str());
|
2021-04-28 12:43:51 +08:00
|
|
|
|
2023-02-04 15:03:54 +08:00
|
|
|
if(speaker_pos < NumSpeakers || hfmatrix_pos < NumSpeakers
|
|
|
|
|| (FreqBands == 2 && lfmatrix_pos < NumSpeakers))
|
|
|
|
return make_error(linenum, "Incomplete decoder definition");
|
|
|
|
if(CoeffScale == AmbDecScale::Unset)
|
|
|
|
return make_error(linenum, "No coefficient scaling defined");
|
2021-04-28 12:43:51 +08:00
|
|
|
|
2023-05-31 23:57:33 +08:00
|
|
|
return al::nullopt;
|
2021-04-28 12:43:51 +08:00
|
|
|
}
|
|
|
|
else
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Unexpected command: %s", command.c_str());
|
2021-04-28 12:43:51 +08:00
|
|
|
|
|
|
|
istr.clear();
|
|
|
|
const auto endpos = static_cast<std::size_t>(istr.tellg());
|
|
|
|
if(!is_at_end(buffer, endpos))
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Extra junk on line: %s", buffer.substr(endpos).c_str());
|
2021-04-28 12:43:51 +08:00
|
|
|
buffer.clear();
|
|
|
|
}
|
2023-02-04 15:03:54 +08:00
|
|
|
return make_error(linenum, "Unexpected end of file");
|
2021-04-28 12:43:51 +08:00
|
|
|
}
|