uhjdecoder.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * 2-channel UHJ Decoder
  3. *
  4. * Copyright (c) Chris Robinson <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "config.h"
  25. #include <algorithm>
  26. #include <array>
  27. #include <cassert>
  28. #include <cerrno>
  29. #include <complex>
  30. #include <cstddef>
  31. #include <cstdio>
  32. #include <cstring>
  33. #include <memory>
  34. #include <string>
  35. #include <string_view>
  36. #include <system_error>
  37. #include <utility>
  38. #include <vector>
  39. #include "albit.h"
  40. #include "almalloc.h"
  41. #include "alnumbers.h"
  42. #include "alspan.h"
  43. #include "alstring.h"
  44. #include "vector.h"
  45. #include "opthelpers.h"
  46. #include "phase_shifter.h"
  47. #include "sndfile.h"
  48. #include "win_main_utf8.h"
  49. namespace {
  50. struct FileDeleter {
  51. void operator()(gsl::owner<FILE*> file) { fclose(file); }
  52. };
  53. using FilePtr = std::unique_ptr<FILE,FileDeleter>;
  54. struct SndFileDeleter {
  55. void operator()(SNDFILE *sndfile) { sf_close(sndfile); }
  56. };
  57. using SndFilePtr = std::unique_ptr<SNDFILE,SndFileDeleter>;
  58. using ubyte = unsigned char;
  59. using ushort = unsigned short;
  60. using uint = unsigned int;
  61. using complex_d = std::complex<double>;
  62. using byte4 = std::array<std::byte,4>;
  63. constexpr std::array<ubyte,16> SUBTYPE_BFORMAT_FLOAT{
  64. 0x03, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11, 0x86, 0x44, 0xc8, 0xc1,
  65. 0xca, 0x00, 0x00, 0x00
  66. };
  67. void fwrite16le(ushort val, FILE *f)
  68. {
  69. std::array data{static_cast<ubyte>(val&0xff), static_cast<ubyte>((val>>8)&0xff)};
  70. fwrite(data.data(), 1, data.size(), f);
  71. }
  72. void fwrite32le(uint val, FILE *f)
  73. {
  74. std::array data{static_cast<ubyte>(val&0xff), static_cast<ubyte>((val>>8)&0xff),
  75. static_cast<ubyte>((val>>16)&0xff), static_cast<ubyte>((val>>24)&0xff)};
  76. fwrite(data.data(), 1, data.size(), f);
  77. }
  78. byte4 f32AsLEBytes(const float value)
  79. {
  80. auto ret = al::bit_cast<byte4>(value);
  81. if constexpr(al::endian::native == al::endian::big)
  82. {
  83. std::swap(ret[0], ret[3]);
  84. std::swap(ret[1], ret[2]);
  85. }
  86. return ret;
  87. }
  88. constexpr uint BufferLineSize{1024};
  89. using FloatBufferLine = std::array<float,BufferLineSize>;
  90. using FloatBufferSpan = al::span<float,BufferLineSize>;
  91. struct UhjDecoder {
  92. constexpr static std::size_t sFilterDelay{1024};
  93. alignas(16) std::array<float,BufferLineSize+sFilterDelay> mS{};
  94. alignas(16) std::array<float,BufferLineSize+sFilterDelay> mD{};
  95. alignas(16) std::array<float,BufferLineSize+sFilterDelay> mT{};
  96. alignas(16) std::array<float,BufferLineSize+sFilterDelay> mQ{};
  97. /* History for the FIR filter. */
  98. alignas(16) std::array<float,sFilterDelay-1> mDTHistory{};
  99. alignas(16) std::array<float,sFilterDelay-1> mSHistory{};
  100. alignas(16) std::array<float,BufferLineSize + sFilterDelay*2> mTemp{};
  101. void decode(const al::span<const float> InSamples, const std::size_t InChannels,
  102. const al::span<FloatBufferLine> OutSamples, const std::size_t SamplesToDo);
  103. void decode2(const al::span<const float> InSamples, const al::span<FloatBufferLine> OutSamples,
  104. const std::size_t SamplesToDo);
  105. };
  106. const PhaseShifterT<UhjDecoder::sFilterDelay*2> PShift{};
  107. /* Decoding UHJ is done as:
  108. *
  109. * S = Left + Right
  110. * D = Left - Right
  111. *
  112. * W = 0.981532*S + 0.197484*j(0.828331*D + 0.767820*T)
  113. * X = 0.418496*S - j(0.828331*D + 0.767820*T)
  114. * Y = 0.795968*D - 0.676392*T + j(0.186633*S)
  115. * Z = 1.023332*Q
  116. *
  117. * where j is a +90 degree phase shift. 3-channel UHJ excludes Q, while 2-
  118. * channel excludes Q and T. The B-Format signal reconstructed from 2-channel
  119. * UHJ should not be run through a normal B-Format decoder, as it needs
  120. * different shelf filters.
  121. *
  122. * NOTE: Some sources specify
  123. *
  124. * S = (Left + Right)/2
  125. * D = (Left - Right)/2
  126. *
  127. * However, this is incorrect. It's halving Left and Right even though they
  128. * were already halved during encoding, causing S and D to be half what they
  129. * initially were at the encoding stage. This division is not present in
  130. * Gerzon's original paper for deriving Sigma (S) or Delta (D) from the L and R
  131. * signals. As proof, taking Y for example:
  132. *
  133. * Y = 0.795968*D - 0.676392*T + j(0.186633*S)
  134. *
  135. * * Plug in the encoding parameters, using ? as a placeholder for whether S
  136. * and D should receive an extra 0.5 factor
  137. * Y = 0.795968*(j(-0.3420201*W + 0.5098604*X) + 0.6554516*Y)*? -
  138. * 0.676392*(j(-0.1432*W + 0.6512*X) - 0.7071068*Y) +
  139. * 0.186633*j(0.9396926*W + 0.1855740*X)*?
  140. *
  141. * * Move common factors in
  142. * Y = (j(-0.3420201*0.795968*?*W + 0.5098604*0.795968*?*X) + 0.6554516*0.795968*?*Y) -
  143. * (j(-0.1432*0.676392*W + 0.6512*0.676392*X) - 0.7071068*0.676392*Y) +
  144. * j(0.9396926*0.186633*?*W + 0.1855740*0.186633*?*X)
  145. *
  146. * * Clean up extraneous groupings
  147. * Y = j(-0.3420201*0.795968*?*W + 0.5098604*0.795968*?*X) + 0.6554516*0.795968*?*Y -
  148. * j(-0.1432*0.676392*W + 0.6512*0.676392*X) + 0.7071068*0.676392*Y +
  149. * j*(0.9396926*0.186633*?*W + 0.1855740*0.186633*?*X)
  150. *
  151. * * Move phase shifts together and combine them
  152. * Y = j(-0.3420201*0.795968*?*W + 0.5098604*0.795968*?*X - -0.1432*0.676392*W -
  153. * 0.6512*0.676392*X + 0.9396926*0.186633*?*W + 0.1855740*0.186633*?*X) +
  154. * 0.6554516*0.795968*?*Y + 0.7071068*0.676392*Y
  155. *
  156. * * Reorder terms
  157. * Y = j(-0.3420201*0.795968*?*W + 0.1432*0.676392*W + 0.9396926*0.186633*?*W +
  158. * 0.5098604*0.795968*?*X + -0.6512*0.676392*X + 0.1855740*0.186633*?*X) +
  159. * 0.7071068*0.676392*Y + 0.6554516*0.795968*?*Y
  160. *
  161. * * Move common factors out
  162. * Y = j((-0.3420201*0.795968*? + 0.1432*0.676392 + 0.9396926*0.186633*?)*W +
  163. * ( 0.5098604*0.795968*? + -0.6512*0.676392 + 0.1855740*0.186633*?)*X) +
  164. * (0.7071068*0.676392 + 0.6554516*0.795968*?)*Y
  165. *
  166. * * Result w/ 0.5 factor:
  167. * -0.3420201*0.795968*0.5 + 0.1432*0.676392 + 0.9396926*0.186633*0.5 = 0.04843*W
  168. * 0.5098604*0.795968*0.5 + -0.6512*0.676392 + 0.1855740*0.186633*0.5 = -0.22023*X
  169. * 0.7071068*0.676392 + 0.6554516*0.795968*0.5 = 0.73914*Y
  170. * -> Y = j(0.04843*W + -0.22023*X) + 0.73914*Y
  171. *
  172. * * Result w/o 0.5 factor:
  173. * -0.3420201*0.795968 + 0.1432*0.676392 + 0.9396926*0.186633 = 0.00000*W
  174. * 0.5098604*0.795968 + -0.6512*0.676392 + 0.1855740*0.186633 = 0.00000*X
  175. * 0.7071068*0.676392 + 0.6554516*0.795968 = 1.00000*Y
  176. * -> Y = j(0.00000*W + 0.00000*X) + 1.00000*Y
  177. *
  178. * Not halving produces a result matching the original input.
  179. */
  180. void UhjDecoder::decode(const al::span<const float> InSamples, const std::size_t InChannels,
  181. const al::span<FloatBufferLine> OutSamples, const std::size_t SamplesToDo)
  182. {
  183. ASSUME(SamplesToDo > 0);
  184. auto woutput = al::span{OutSamples[0]};
  185. auto xoutput = al::span{OutSamples[1]};
  186. auto youtput = al::span{OutSamples[2]};
  187. /* Add a delay to the input channels, to align it with the all-passed
  188. * signal.
  189. */
  190. /* S = Left + Right */
  191. for(std::size_t i{0};i < SamplesToDo;++i)
  192. mS[sFilterDelay+i] = InSamples[i*InChannels + 0] + InSamples[i*InChannels + 1];
  193. /* D = Left - Right */
  194. for(std::size_t i{0};i < SamplesToDo;++i)
  195. mD[sFilterDelay+i] = InSamples[i*InChannels + 0] - InSamples[i*InChannels + 1];
  196. if(InChannels > 2)
  197. {
  198. /* T */
  199. for(std::size_t i{0};i < SamplesToDo;++i)
  200. mT[sFilterDelay+i] = InSamples[i*InChannels + 2];
  201. }
  202. if(InChannels > 3)
  203. {
  204. /* Q */
  205. for(std::size_t i{0};i < SamplesToDo;++i)
  206. mQ[sFilterDelay+i] = InSamples[i*InChannels + 3];
  207. }
  208. /* Precompute j(0.828331*D + 0.767820*T) and store in xoutput. */
  209. auto tmpiter = std::copy(mDTHistory.cbegin(), mDTHistory.cend(), mTemp.begin());
  210. std::transform(mD.cbegin(), mD.cbegin()+SamplesToDo+sFilterDelay, mT.cbegin(), tmpiter,
  211. [](const float d, const float t) noexcept { return 0.828331f*d + 0.767820f*t; });
  212. std::copy_n(mTemp.cbegin()+SamplesToDo, mDTHistory.size(), mDTHistory.begin());
  213. PShift.process(xoutput.first(SamplesToDo), mTemp);
  214. for(std::size_t i{0};i < SamplesToDo;++i)
  215. {
  216. /* W = 0.981532*S + 0.197484*j(0.828331*D + 0.767820*T) */
  217. woutput[i] = 0.981532f*mS[i] + 0.197484f*xoutput[i];
  218. /* X = 0.418496*S - j(0.828331*D + 0.767820*T) */
  219. xoutput[i] = 0.418496f*mS[i] - xoutput[i];
  220. }
  221. /* Precompute j*S and store in youtput. */
  222. tmpiter = std::copy(mSHistory.cbegin(), mSHistory.cend(), mTemp.begin());
  223. std::copy_n(mS.cbegin(), SamplesToDo+sFilterDelay, tmpiter);
  224. std::copy_n(mTemp.cbegin()+SamplesToDo, mSHistory.size(), mSHistory.begin());
  225. PShift.process(youtput.first(SamplesToDo), mTemp);
  226. for(std::size_t i{0};i < SamplesToDo;++i)
  227. {
  228. /* Y = 0.795968*D - 0.676392*T + j(0.186633*S) */
  229. youtput[i] = 0.795968f*mD[i] - 0.676392f*mT[i] + 0.186633f*youtput[i];
  230. }
  231. if(OutSamples.size() > 3)
  232. {
  233. auto zoutput = al::span{OutSamples[3]};
  234. /* Z = 1.023332*Q */
  235. for(std::size_t i{0};i < SamplesToDo;++i)
  236. zoutput[i] = 1.023332f*mQ[i];
  237. }
  238. std::copy(mS.begin()+SamplesToDo, mS.begin()+SamplesToDo+sFilterDelay, mS.begin());
  239. std::copy(mD.begin()+SamplesToDo, mD.begin()+SamplesToDo+sFilterDelay, mD.begin());
  240. std::copy(mT.begin()+SamplesToDo, mT.begin()+SamplesToDo+sFilterDelay, mT.begin());
  241. std::copy(mQ.begin()+SamplesToDo, mQ.begin()+SamplesToDo+sFilterDelay, mQ.begin());
  242. }
  243. /* This is an alternative equation for decoding 2-channel UHJ. Not sure what
  244. * the intended benefit is over the above equation as this slightly reduces the
  245. * amount of the original left response and has more of the phase-shifted
  246. * forward response on the left response.
  247. *
  248. * This decoding is done as:
  249. *
  250. * S = Left + Right
  251. * D = Left - Right
  252. *
  253. * W = 0.981530*S + j*0.163585*D
  254. * X = 0.418504*S - j*0.828347*D
  255. * Y = 0.762956*D + j*0.384230*S
  256. *
  257. * where j is a +90 degree phase shift.
  258. *
  259. * NOTE: As above, S and D should not be halved. The only consequence of
  260. * halving here is merely a -6dB reduction in output, but it's still incorrect.
  261. */
  262. void UhjDecoder::decode2(const al::span<const float> InSamples,
  263. const al::span<FloatBufferLine> OutSamples, const std::size_t SamplesToDo)
  264. {
  265. ASSUME(SamplesToDo > 0);
  266. auto woutput = al::span{OutSamples[0]};
  267. auto xoutput = al::span{OutSamples[1]};
  268. auto youtput = al::span{OutSamples[2]};
  269. /* S = Left + Right */
  270. for(std::size_t i{0};i < SamplesToDo;++i)
  271. mS[sFilterDelay+i] = InSamples[i*2 + 0] + InSamples[i*2 + 1];
  272. /* D = Left - Right */
  273. for(std::size_t i{0};i < SamplesToDo;++i)
  274. mD[sFilterDelay+i] = InSamples[i*2 + 0] - InSamples[i*2 + 1];
  275. /* Precompute j*D and store in xoutput. */
  276. auto tmpiter = std::copy(mDTHistory.cbegin(), mDTHistory.cend(), mTemp.begin());
  277. std::copy_n(mD.cbegin(), SamplesToDo+sFilterDelay, tmpiter);
  278. std::copy_n(mTemp.cbegin()+SamplesToDo, mDTHistory.size(), mDTHistory.begin());
  279. PShift.process(xoutput.first(SamplesToDo), mTemp);
  280. for(std::size_t i{0};i < SamplesToDo;++i)
  281. {
  282. /* W = 0.981530*S + j*0.163585*D */
  283. woutput[i] = 0.981530f*mS[i] + 0.163585f*xoutput[i];
  284. /* X = 0.418504*S - j*0.828347*D */
  285. xoutput[i] = 0.418504f*mS[i] - 0.828347f*xoutput[i];
  286. }
  287. /* Precompute j*S and store in youtput. */
  288. tmpiter = std::copy(mSHistory.cbegin(), mSHistory.cend(), mTemp.begin());
  289. std::copy_n(mS.cbegin(), SamplesToDo+sFilterDelay, tmpiter);
  290. std::copy_n(mTemp.cbegin()+SamplesToDo, mSHistory.size(), mSHistory.begin());
  291. PShift.process(youtput.first(SamplesToDo), mTemp);
  292. for(std::size_t i{0};i < SamplesToDo;++i)
  293. {
  294. /* Y = 0.762956*D + j*0.384230*S */
  295. youtput[i] = 0.762956f*mD[i] + 0.384230f*youtput[i];
  296. }
  297. std::copy(mS.begin()+SamplesToDo, mS.begin()+SamplesToDo+sFilterDelay, mS.begin());
  298. std::copy(mD.begin()+SamplesToDo, mD.begin()+SamplesToDo+sFilterDelay, mD.begin());
  299. }
  300. int main(al::span<std::string_view> args)
  301. {
  302. if(args.size() < 2 || args[1] == "-h" || args[1] == "--help")
  303. {
  304. printf("Usage: %.*s <[options] filename.wav...>\n\n"
  305. " Options:\n"
  306. " --general Use the general equations for 2-channel UHJ (default).\n"
  307. " --alternative Use the alternative equations for 2-channel UHJ.\n"
  308. "\n"
  309. "Note: When decoding 2-channel UHJ to an .amb file, the result should not use\n"
  310. "the normal B-Format shelf filters! Only 3- and 4-channel UHJ can accurately\n"
  311. "reconstruct the original B-Format signal.",
  312. al::sizei(args[0]), args[0].data());
  313. return 1;
  314. }
  315. std::size_t num_files{0}, num_decoded{0};
  316. bool use_general{true};
  317. for(size_t fidx{1};fidx < args.size();++fidx)
  318. {
  319. if(args[fidx] == "--general")
  320. {
  321. use_general = true;
  322. continue;
  323. }
  324. if(args[fidx] == "--alternative")
  325. {
  326. use_general = false;
  327. continue;
  328. }
  329. ++num_files;
  330. SF_INFO ininfo{};
  331. SndFilePtr infile{sf_open(std::string{args[fidx]}.c_str(), SFM_READ, &ininfo)};
  332. if(!infile)
  333. {
  334. fprintf(stderr, "Failed to open %.*s\n", al::sizei(args[fidx]), args[fidx].data());
  335. continue;
  336. }
  337. if(sf_command(infile.get(), SFC_WAVEX_GET_AMBISONIC, nullptr, 0) == SF_AMBISONIC_B_FORMAT)
  338. {
  339. fprintf(stderr, "%.*s is already B-Format\n", al::sizei(args[fidx]),
  340. args[fidx].data());
  341. continue;
  342. }
  343. uint outchans{};
  344. if(ininfo.channels == 2)
  345. outchans = 3;
  346. else if(ininfo.channels == 3 || ininfo.channels == 4)
  347. outchans = static_cast<uint>(ininfo.channels);
  348. else
  349. {
  350. fprintf(stderr, "%.*s is not a 2-, 3-, or 4-channel file\n", al::sizei(args[fidx]),
  351. args[fidx].data());
  352. continue;
  353. }
  354. printf("Converting %.*s from %d-channel UHJ%s...\n", al::sizei(args[fidx]),
  355. args[fidx].data(), ininfo.channels,
  356. (ininfo.channels == 2) ? use_general ? " (general)" : " (alternative)" : "");
  357. std::string outname{args[fidx]};
  358. auto lastslash = outname.find_last_of('/');
  359. if(lastslash != std::string::npos)
  360. outname.erase(0, lastslash+1);
  361. auto lastdot = outname.find_last_of('.');
  362. if(lastdot != std::string::npos)
  363. outname.resize(lastdot+1);
  364. outname += "amb";
  365. FilePtr outfile{fopen(outname.c_str(), "wb")};
  366. if(!outfile)
  367. {
  368. fprintf(stderr, "Failed to create %s\n", outname.c_str());
  369. continue;
  370. }
  371. fputs("RIFF", outfile.get());
  372. fwrite32le(0xFFFFFFFF, outfile.get()); // 'RIFF' header len; filled in at close
  373. fputs("WAVE", outfile.get());
  374. fputs("fmt ", outfile.get());
  375. fwrite32le(40, outfile.get()); // 'fmt ' header len; 40 bytes for EXTENSIBLE
  376. // 16-bit val, format type id (extensible: 0xFFFE)
  377. fwrite16le(0xFFFE, outfile.get());
  378. // 16-bit val, channel count
  379. fwrite16le(static_cast<ushort>(outchans), outfile.get());
  380. // 32-bit val, frequency
  381. fwrite32le(static_cast<uint>(ininfo.samplerate), outfile.get());
  382. // 32-bit val, bytes per second
  383. fwrite32le(static_cast<uint>(ininfo.samplerate)*outchans*uint{sizeof(float)}, outfile.get());
  384. // 16-bit val, frame size
  385. fwrite16le(static_cast<ushort>(sizeof(float)*outchans), outfile.get());
  386. // 16-bit val, bits per sample
  387. fwrite16le(static_cast<ushort>(sizeof(float)*8), outfile.get());
  388. // 16-bit val, extra byte count
  389. fwrite16le(22, outfile.get());
  390. // 16-bit val, valid bits per sample
  391. fwrite16le(static_cast<ushort>(sizeof(float)*8), outfile.get());
  392. // 32-bit val, channel mask
  393. fwrite32le(0, outfile.get());
  394. // 16 byte GUID, sub-type format
  395. fwrite(SUBTYPE_BFORMAT_FLOAT.data(), 1, SUBTYPE_BFORMAT_FLOAT.size(), outfile.get());
  396. fputs("data", outfile.get());
  397. fwrite32le(0xFFFFFFFF, outfile.get()); // 'data' header len; filled in at close
  398. if(ferror(outfile.get()))
  399. {
  400. fprintf(stderr, "Error writing wave file header: %s (%d)\n",
  401. std::generic_category().message(errno).c_str(), errno);
  402. continue;
  403. }
  404. auto DataStart = ftell(outfile.get());
  405. auto decoder = std::make_unique<UhjDecoder>();
  406. auto inmem = std::vector<float>(size_t{BufferLineSize}*static_cast<uint>(ininfo.channels));
  407. auto decmem = al::vector<std::array<float,BufferLineSize>, 16>(outchans);
  408. auto outmem = std::vector<byte4>(size_t{BufferLineSize}*outchans);
  409. /* A number of initial samples need to be skipped to cut the lead-in
  410. * from the all-pass filter delay. The same number of samples need to
  411. * be fed through the decoder after reaching the end of the input file
  412. * to ensure none of the original input is lost.
  413. */
  414. std::size_t LeadIn{UhjDecoder::sFilterDelay};
  415. sf_count_t LeadOut{UhjDecoder::sFilterDelay};
  416. while(LeadOut > 0)
  417. {
  418. sf_count_t sgot{sf_readf_float(infile.get(), inmem.data(), BufferLineSize)};
  419. sgot = std::max<sf_count_t>(sgot, 0);
  420. if(sgot < BufferLineSize)
  421. {
  422. const sf_count_t remaining{std::min(BufferLineSize - sgot, LeadOut)};
  423. std::fill_n(inmem.begin() + sgot*ininfo.channels, remaining*ininfo.channels, 0.0f);
  424. sgot += remaining;
  425. LeadOut -= remaining;
  426. }
  427. auto got = static_cast<std::size_t>(sgot);
  428. if(ininfo.channels > 2 || use_general)
  429. decoder->decode(inmem, static_cast<uint>(ininfo.channels), decmem, got);
  430. else
  431. decoder->decode2(inmem, decmem, got);
  432. if(LeadIn >= got)
  433. {
  434. LeadIn -= got;
  435. continue;
  436. }
  437. got -= LeadIn;
  438. for(std::size_t i{0};i < got;++i)
  439. {
  440. /* Attenuate by -3dB for FuMa output levels. */
  441. constexpr auto inv_sqrt2 = static_cast<float>(1.0/al::numbers::sqrt2);
  442. for(std::size_t j{0};j < outchans;++j)
  443. outmem[i*outchans + j] = f32AsLEBytes(decmem[j][LeadIn+i] * inv_sqrt2);
  444. }
  445. LeadIn = 0;
  446. std::size_t wrote{fwrite(outmem.data(), sizeof(byte4)*outchans, got, outfile.get())};
  447. if(wrote < got)
  448. {
  449. fprintf(stderr, "Error writing wave data: %s (%d)\n",
  450. std::generic_category().message(errno).c_str(), errno);
  451. break;
  452. }
  453. }
  454. auto DataEnd = ftell(outfile.get());
  455. if(DataEnd > DataStart)
  456. {
  457. long dataLen{DataEnd - DataStart};
  458. if(fseek(outfile.get(), 4, SEEK_SET) == 0)
  459. fwrite32le(static_cast<uint>(DataEnd-8), outfile.get()); // 'WAVE' header len
  460. if(fseek(outfile.get(), DataStart-4, SEEK_SET) == 0)
  461. fwrite32le(static_cast<uint>(dataLen), outfile.get()); // 'data' header len
  462. }
  463. fflush(outfile.get());
  464. ++num_decoded;
  465. }
  466. if(num_decoded == 0)
  467. fprintf(stderr, "Failed to decode any input files\n");
  468. else if(num_decoded < num_files)
  469. fprintf(stderr, "Decoded %zu of %zu files\n", num_decoded, num_files);
  470. else
  471. printf("Decoded %zu file%s\n", num_decoded, (num_decoded==1)?"":"s");
  472. return 0;
  473. }
  474. } /* namespace */
  475. int main(int argc, char **argv)
  476. {
  477. assert(argc >= 0);
  478. auto args = std::vector<std::string_view>(static_cast<unsigned int>(argc));
  479. std::copy_n(argv, args.size(), args.begin());
  480. return main(al::span{args});
  481. }