🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

538 lines
20 KiB

  1. /*
  2. * 2-channel UHJ Decoder
  3. *
  4. * Copyright (c) Chris Robinson <chris.kcat@gmail.com>
  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 <array>
  26. #include <complex>
  27. #include <cstring>
  28. #include <memory>
  29. #include <stddef.h>
  30. #include <string>
  31. #include <utility>
  32. #include <vector>
  33. #include "albit.h"
  34. #include "albyte.h"
  35. #include "alcomplex.h"
  36. #include "almalloc.h"
  37. #include "alnumbers.h"
  38. #include "alspan.h"
  39. #include "vector.h"
  40. #include "opthelpers.h"
  41. #include "phase_shifter.h"
  42. #include "sndfile.h"
  43. #include "win_main_utf8.h"
  44. struct FileDeleter {
  45. void operator()(FILE *file) { fclose(file); }
  46. };
  47. using FilePtr = std::unique_ptr<FILE,FileDeleter>;
  48. struct SndFileDeleter {
  49. void operator()(SNDFILE *sndfile) { sf_close(sndfile); }
  50. };
  51. using SndFilePtr = std::unique_ptr<SNDFILE,SndFileDeleter>;
  52. using ubyte = unsigned char;
  53. using ushort = unsigned short;
  54. using uint = unsigned int;
  55. using complex_d = std::complex<double>;
  56. using byte4 = std::array<al::byte,4>;
  57. constexpr ubyte SUBTYPE_BFORMAT_FLOAT[]{
  58. 0x03, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11, 0x86, 0x44, 0xc8, 0xc1,
  59. 0xca, 0x00, 0x00, 0x00
  60. };
  61. void fwrite16le(ushort val, FILE *f)
  62. {
  63. ubyte data[2]{ static_cast<ubyte>(val&0xff), static_cast<ubyte>((val>>8)&0xff) };
  64. fwrite(data, 1, 2, f);
  65. }
  66. void fwrite32le(uint val, FILE *f)
  67. {
  68. ubyte data[4]{ static_cast<ubyte>(val&0xff), static_cast<ubyte>((val>>8)&0xff),
  69. static_cast<ubyte>((val>>16)&0xff), static_cast<ubyte>((val>>24)&0xff) };
  70. fwrite(data, 1, 4, f);
  71. }
  72. template<al::endian = al::endian::native>
  73. byte4 f32AsLEBytes(const float &value) = delete;
  74. template<>
  75. byte4 f32AsLEBytes<al::endian::little>(const float &value)
  76. {
  77. byte4 ret{};
  78. std::memcpy(ret.data(), &value, 4);
  79. return ret;
  80. }
  81. template<>
  82. byte4 f32AsLEBytes<al::endian::big>(const float &value)
  83. {
  84. byte4 ret{};
  85. std::memcpy(ret.data(), &value, 4);
  86. std::swap(ret[0], ret[3]);
  87. std::swap(ret[1], ret[2]);
  88. return ret;
  89. }
  90. constexpr uint BufferLineSize{1024};
  91. using FloatBufferLine = std::array<float,BufferLineSize>;
  92. using FloatBufferSpan = al::span<float,BufferLineSize>;
  93. struct UhjDecoder {
  94. constexpr static size_t sFilterDelay{1024};
  95. alignas(16) std::array<float,BufferLineSize+sFilterDelay> mS{};
  96. alignas(16) std::array<float,BufferLineSize+sFilterDelay> mD{};
  97. alignas(16) std::array<float,BufferLineSize+sFilterDelay> mT{};
  98. alignas(16) std::array<float,BufferLineSize+sFilterDelay> mQ{};
  99. /* History for the FIR filter. */
  100. alignas(16) std::array<float,sFilterDelay-1> mDTHistory{};
  101. alignas(16) std::array<float,sFilterDelay-1> mSHistory{};
  102. alignas(16) std::array<float,BufferLineSize + sFilterDelay*2> mTemp{};
  103. void decode(const float *RESTRICT InSamples, const size_t InChannels,
  104. const al::span<FloatBufferLine> OutSamples, const size_t SamplesToDo);
  105. void decode2(const float *RESTRICT InSamples, const al::span<FloatBufferLine,3> OutSamples,
  106. const size_t SamplesToDo);
  107. DEF_NEWDEL(UhjDecoder)
  108. };
  109. const PhaseShifterT<UhjDecoder::sFilterDelay*2> PShift{};
  110. /* Decoding UHJ is done as:
  111. *
  112. * S = Left + Right
  113. * D = Left - Right
  114. *
  115. * W = 0.981532*S + 0.197484*j(0.828331*D + 0.767820*T)
  116. * X = 0.418496*S - j(0.828331*D + 0.767820*T)
  117. * Y = 0.795968*D - 0.676392*T + j(0.186633*S)
  118. * Z = 1.023332*Q
  119. *
  120. * where j is a +90 degree phase shift. 3-channel UHJ excludes Q, while 2-
  121. * channel excludes Q and T. The B-Format signal reconstructed from 2-channel
  122. * UHJ should not be run through a normal B-Format decoder, as it needs
  123. * different shelf filters.
  124. *
  125. * NOTE: Some sources specify
  126. *
  127. * S = (Left + Right)/2
  128. * D = (Left - Right)/2
  129. *
  130. * However, this is incorrect. It's halving Left and Right even though they
  131. * were already halved during encoding, causing S and D to be half what they
  132. * initially were at the encoding stage. This division is not present in
  133. * Gerzon's original paper for deriving Sigma (S) or Delta (D) from the L and R
  134. * signals. As proof, taking Y for example:
  135. *
  136. * Y = 0.795968*D - 0.676392*T + j(0.186633*S)
  137. *
  138. * * Plug in the encoding parameters, using ? as a placeholder for whether S
  139. * and D should receive an extra 0.5 factor
  140. * Y = 0.795968*(j(-0.3420201*W + 0.5098604*X) + 0.6554516*Y)*? -
  141. * 0.676392*(j(-0.1432*W + 0.6512*X) - 0.7071068*Y) +
  142. * 0.186633*j(0.9396926*W + 0.1855740*X)*?
  143. *
  144. * * Move common factors in
  145. * Y = (j(-0.3420201*0.795968*?*W + 0.5098604*0.795968*?*X) + 0.6554516*0.795968*?*Y) -
  146. * (j(-0.1432*0.676392*W + 0.6512*0.676392*X) - 0.7071068*0.676392*Y) +
  147. * j(0.9396926*0.186633*?*W + 0.1855740*0.186633*?*X)
  148. *
  149. * * Clean up extraneous groupings
  150. * Y = j(-0.3420201*0.795968*?*W + 0.5098604*0.795968*?*X) + 0.6554516*0.795968*?*Y -
  151. * j(-0.1432*0.676392*W + 0.6512*0.676392*X) + 0.7071068*0.676392*Y +
  152. * j*(0.9396926*0.186633*?*W + 0.1855740*0.186633*?*X)
  153. *
  154. * * Move phase shifts together and combine them
  155. * Y = j(-0.3420201*0.795968*?*W + 0.5098604*0.795968*?*X - -0.1432*0.676392*W -
  156. * 0.6512*0.676392*X + 0.9396926*0.186633*?*W + 0.1855740*0.186633*?*X) +
  157. * 0.6554516*0.795968*?*Y + 0.7071068*0.676392*Y
  158. *
  159. * * Reorder terms
  160. * Y = j(-0.3420201*0.795968*?*W + 0.1432*0.676392*W + 0.9396926*0.186633*?*W +
  161. * 0.5098604*0.795968*?*X + -0.6512*0.676392*X + 0.1855740*0.186633*?*X) +
  162. * 0.7071068*0.676392*Y + 0.6554516*0.795968*?*Y
  163. *
  164. * * Move common factors out
  165. * Y = j((-0.3420201*0.795968*? + 0.1432*0.676392 + 0.9396926*0.186633*?)*W +
  166. * ( 0.5098604*0.795968*? + -0.6512*0.676392 + 0.1855740*0.186633*?)*X) +
  167. * (0.7071068*0.676392 + 0.6554516*0.795968*?)*Y
  168. *
  169. * * Result w/ 0.5 factor:
  170. * -0.3420201*0.795968*0.5 + 0.1432*0.676392 + 0.9396926*0.186633*0.5 = 0.04843*W
  171. * 0.5098604*0.795968*0.5 + -0.6512*0.676392 + 0.1855740*0.186633*0.5 = -0.22023*X
  172. * 0.7071068*0.676392 + 0.6554516*0.795968*0.5 = 0.73914*Y
  173. * -> Y = j(0.04843*W + -0.22023*X) + 0.73914*Y
  174. *
  175. * * Result w/o 0.5 factor:
  176. * -0.3420201*0.795968 + 0.1432*0.676392 + 0.9396926*0.186633 = 0.00000*W
  177. * 0.5098604*0.795968 + -0.6512*0.676392 + 0.1855740*0.186633 = 0.00000*X
  178. * 0.7071068*0.676392 + 0.6554516*0.795968 = 1.00000*Y
  179. * -> Y = j(0.00000*W + 0.00000*X) + 1.00000*Y
  180. *
  181. * Not halving produces a result matching the original input.
  182. */
  183. void UhjDecoder::decode(const float *RESTRICT InSamples, const size_t InChannels,
  184. const al::span<FloatBufferLine> OutSamples, const size_t SamplesToDo)
  185. {
  186. ASSUME(SamplesToDo > 0);
  187. float *woutput{OutSamples[0].data()};
  188. float *xoutput{OutSamples[1].data()};
  189. float *youtput{OutSamples[2].data()};
  190. /* Add a delay to the input channels, to align it with the all-passed
  191. * signal.
  192. */
  193. /* S = Left + Right */
  194. for(size_t i{0};i < SamplesToDo;++i)
  195. mS[sFilterDelay+i] = InSamples[i*InChannels + 0] + InSamples[i*InChannels + 1];
  196. /* D = Left - Right */
  197. for(size_t i{0};i < SamplesToDo;++i)
  198. mD[sFilterDelay+i] = InSamples[i*InChannels + 0] - InSamples[i*InChannels + 1];
  199. if(InChannels > 2)
  200. {
  201. /* T */
  202. for(size_t i{0};i < SamplesToDo;++i)
  203. mT[sFilterDelay+i] = InSamples[i*InChannels + 2];
  204. }
  205. if(InChannels > 3)
  206. {
  207. /* Q */
  208. for(size_t i{0};i < SamplesToDo;++i)
  209. mQ[sFilterDelay+i] = InSamples[i*InChannels + 3];
  210. }
  211. /* Precompute j(0.828331*D + 0.767820*T) and store in xoutput. */
  212. auto tmpiter = std::copy(mDTHistory.cbegin(), mDTHistory.cend(), mTemp.begin());
  213. std::transform(mD.cbegin(), mD.cbegin()+SamplesToDo+sFilterDelay, mT.cbegin(), tmpiter,
  214. [](const float d, const float t) noexcept { return 0.828331f*d + 0.767820f*t; });
  215. std::copy_n(mTemp.cbegin()+SamplesToDo, mDTHistory.size(), mDTHistory.begin());
  216. PShift.process({xoutput, SamplesToDo}, mTemp.data());
  217. for(size_t i{0};i < SamplesToDo;++i)
  218. {
  219. /* W = 0.981532*S + 0.197484*j(0.828331*D + 0.767820*T) */
  220. woutput[i] = 0.981532f*mS[i] + 0.197484f*xoutput[i];
  221. /* X = 0.418496*S - j(0.828331*D + 0.767820*T) */
  222. xoutput[i] = 0.418496f*mS[i] - xoutput[i];
  223. }
  224. /* Precompute j*S and store in youtput. */
  225. tmpiter = std::copy(mSHistory.cbegin(), mSHistory.cend(), mTemp.begin());
  226. std::copy_n(mS.cbegin(), SamplesToDo+sFilterDelay, tmpiter);
  227. std::copy_n(mTemp.cbegin()+SamplesToDo, mSHistory.size(), mSHistory.begin());
  228. PShift.process({youtput, SamplesToDo}, mTemp.data());
  229. for(size_t i{0};i < SamplesToDo;++i)
  230. {
  231. /* Y = 0.795968*D - 0.676392*T + j(0.186633*S) */
  232. youtput[i] = 0.795968f*mD[i] - 0.676392f*mT[i] + 0.186633f*youtput[i];
  233. }
  234. if(OutSamples.size() > 3)
  235. {
  236. float *zoutput{OutSamples[3].data()};
  237. /* Z = 1.023332*Q */
  238. for(size_t i{0};i < SamplesToDo;++i)
  239. zoutput[i] = 1.023332f*mQ[i];
  240. }
  241. std::copy(mS.begin()+SamplesToDo, mS.begin()+SamplesToDo+sFilterDelay, mS.begin());
  242. std::copy(mD.begin()+SamplesToDo, mD.begin()+SamplesToDo+sFilterDelay, mD.begin());
  243. std::copy(mT.begin()+SamplesToDo, mT.begin()+SamplesToDo+sFilterDelay, mT.begin());
  244. std::copy(mQ.begin()+SamplesToDo, mQ.begin()+SamplesToDo+sFilterDelay, mQ.begin());
  245. }
  246. /* This is an alternative equation for decoding 2-channel UHJ. Not sure what
  247. * the intended benefit is over the above equation as this slightly reduces the
  248. * amount of the original left response and has more of the phase-shifted
  249. * forward response on the left response.
  250. *
  251. * This decoding is done as:
  252. *
  253. * S = Left + Right
  254. * D = Left - Right
  255. *
  256. * W = 0.981530*S + j*0.163585*D
  257. * X = 0.418504*S - j*0.828347*D
  258. * Y = 0.762956*D + j*0.384230*S
  259. *
  260. * where j is a +90 degree phase shift.
  261. *
  262. * NOTE: As above, S and D should not be halved. The only consequence of
  263. * halving here is merely a -6dB reduction in output, but it's still incorrect.
  264. */
  265. void UhjDecoder::decode2(const float *RESTRICT InSamples,
  266. const al::span<FloatBufferLine,3> OutSamples, const size_t SamplesToDo)
  267. {
  268. ASSUME(SamplesToDo > 0);
  269. float *woutput{OutSamples[0].data()};
  270. float *xoutput{OutSamples[1].data()};
  271. float *youtput{OutSamples[2].data()};
  272. /* S = Left + Right */
  273. for(size_t i{0};i < SamplesToDo;++i)
  274. mS[sFilterDelay+i] = InSamples[i*2 + 0] + InSamples[i*2 + 1];
  275. /* D = Left - Right */
  276. for(size_t i{0};i < SamplesToDo;++i)
  277. mD[sFilterDelay+i] = InSamples[i*2 + 0] - InSamples[i*2 + 1];
  278. /* Precompute j*D and store in xoutput. */
  279. auto tmpiter = std::copy(mDTHistory.cbegin(), mDTHistory.cend(), mTemp.begin());
  280. std::copy_n(mD.cbegin(), SamplesToDo+sFilterDelay, tmpiter);
  281. std::copy_n(mTemp.cbegin()+SamplesToDo, mDTHistory.size(), mDTHistory.begin());
  282. PShift.process({xoutput, SamplesToDo}, mTemp.data());
  283. for(size_t i{0};i < SamplesToDo;++i)
  284. {
  285. /* W = 0.981530*S + j*0.163585*D */
  286. woutput[i] = 0.981530f*mS[i] + 0.163585f*xoutput[i];
  287. /* X = 0.418504*S - j*0.828347*D */
  288. xoutput[i] = 0.418504f*mS[i] - 0.828347f*xoutput[i];
  289. }
  290. /* Precompute j*S and store in youtput. */
  291. tmpiter = std::copy(mSHistory.cbegin(), mSHistory.cend(), mTemp.begin());
  292. std::copy_n(mS.cbegin(), SamplesToDo+sFilterDelay, tmpiter);
  293. std::copy_n(mTemp.cbegin()+SamplesToDo, mSHistory.size(), mSHistory.begin());
  294. PShift.process({youtput, SamplesToDo}, mTemp.data());
  295. for(size_t i{0};i < SamplesToDo;++i)
  296. {
  297. /* Y = 0.762956*D + j*0.384230*S */
  298. youtput[i] = 0.762956f*mD[i] + 0.384230f*youtput[i];
  299. }
  300. std::copy(mS.begin()+SamplesToDo, mS.begin()+SamplesToDo+sFilterDelay, mS.begin());
  301. std::copy(mD.begin()+SamplesToDo, mD.begin()+SamplesToDo+sFilterDelay, mD.begin());
  302. }
  303. int main(int argc, char **argv)
  304. {
  305. if(argc < 2 || std::strcmp(argv[1], "-h") == 0 || std::strcmp(argv[1], "--help") == 0)
  306. {
  307. printf("Usage: %s <[options] filename.wav...>\n\n"
  308. " Options:\n"
  309. " --general Use the general equations for 2-channel UHJ (default).\n"
  310. " --alternative Use the alternative equations for 2-channel UHJ.\n"
  311. "\n"
  312. "Note: When decoding 2-channel UHJ to an .amb file, the result should not use\n"
  313. "the normal B-Format shelf filters! Only 3- and 4-channel UHJ can accurately\n"
  314. "reconstruct the original B-Format signal.",
  315. argv[0]);
  316. return 1;
  317. }
  318. size_t num_files{0}, num_decoded{0};
  319. bool use_general{true};
  320. for(int fidx{1};fidx < argc;++fidx)
  321. {
  322. if(std::strcmp(argv[fidx], "--general") == 0)
  323. {
  324. use_general = true;
  325. continue;
  326. }
  327. if(std::strcmp(argv[fidx], "--alternative") == 0)
  328. {
  329. use_general = false;
  330. continue;
  331. }
  332. ++num_files;
  333. SF_INFO ininfo{};
  334. SndFilePtr infile{sf_open(argv[fidx], SFM_READ, &ininfo)};
  335. if(!infile)
  336. {
  337. fprintf(stderr, "Failed to open %s\n", argv[fidx]);
  338. continue;
  339. }
  340. if(sf_command(infile.get(), SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
  341. {
  342. fprintf(stderr, "%s is already B-Format\n", argv[fidx]);
  343. continue;
  344. }
  345. uint outchans{};
  346. if(ininfo.channels == 2)
  347. outchans = 3;
  348. else if(ininfo.channels == 3 || ininfo.channels == 4)
  349. outchans = static_cast<uint>(ininfo.channels);
  350. else
  351. {
  352. fprintf(stderr, "%s is not a 2-, 3-, or 4-channel file\n", argv[fidx]);
  353. continue;
  354. }
  355. printf("Converting %s from %d-channel UHJ%s...\n", argv[fidx], ininfo.channels,
  356. (ininfo.channels == 2) ? use_general ? " (general)" : " (alternative)" : "");
  357. std::string outname{argv[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)*sizeof(float)*outchans, 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, 1, 16, 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", strerror(errno), errno);
  401. continue;
  402. }
  403. auto DataStart = ftell(outfile.get());
  404. auto decoder = std::make_unique<UhjDecoder>();
  405. auto inmem = std::make_unique<float[]>(BufferLineSize*static_cast<uint>(ininfo.channels));
  406. auto decmem = al::vector<std::array<float,BufferLineSize>, 16>(outchans);
  407. auto outmem = std::make_unique<byte4[]>(BufferLineSize*outchans);
  408. /* A number of initial samples need to be skipped to cut the lead-in
  409. * from the all-pass filter delay. The same number of samples need to
  410. * be fed through the decoder after reaching the end of the input file
  411. * to ensure none of the original input is lost.
  412. */
  413. size_t LeadIn{UhjDecoder::sFilterDelay};
  414. sf_count_t LeadOut{UhjDecoder::sFilterDelay};
  415. while(LeadOut > 0)
  416. {
  417. sf_count_t sgot{sf_readf_float(infile.get(), inmem.get(), BufferLineSize)};
  418. sgot = std::max<sf_count_t>(sgot, 0);
  419. if(sgot < BufferLineSize)
  420. {
  421. const sf_count_t remaining{std::min(BufferLineSize - sgot, LeadOut)};
  422. std::fill_n(inmem.get() + sgot*ininfo.channels, remaining*ininfo.channels, 0.0f);
  423. sgot += remaining;
  424. LeadOut -= remaining;
  425. }
  426. auto got = static_cast<size_t>(sgot);
  427. if(ininfo.channels > 2 || use_general)
  428. decoder->decode(inmem.get(), static_cast<uint>(ininfo.channels), decmem, got);
  429. else
  430. decoder->decode2(inmem.get(), decmem, got);
  431. if(LeadIn >= got)
  432. {
  433. LeadIn -= got;
  434. continue;
  435. }
  436. got -= LeadIn;
  437. for(size_t i{0};i < got;++i)
  438. {
  439. /* Attenuate by -3dB for FuMa output levels. */
  440. constexpr auto inv_sqrt2 = static_cast<float>(1.0/al::numbers::sqrt2);
  441. for(size_t j{0};j < outchans;++j)
  442. outmem[i*outchans + j] = f32AsLEBytes(decmem[j][LeadIn+i] * inv_sqrt2);
  443. }
  444. LeadIn = 0;
  445. size_t wrote{fwrite(outmem.get(), sizeof(byte4)*outchans, got, outfile.get())};
  446. if(wrote < got)
  447. {
  448. fprintf(stderr, "Error writing wave data: %s (%d)\n", strerror(errno), errno);
  449. break;
  450. }
  451. }
  452. auto DataEnd = ftell(outfile.get());
  453. if(DataEnd > DataStart)
  454. {
  455. long dataLen{DataEnd - DataStart};
  456. if(fseek(outfile.get(), 4, SEEK_SET) == 0)
  457. fwrite32le(static_cast<uint>(DataEnd-8), outfile.get()); // 'WAVE' header len
  458. if(fseek(outfile.get(), DataStart-4, SEEK_SET) == 0)
  459. fwrite32le(static_cast<uint>(dataLen), outfile.get()); // 'data' header len
  460. }
  461. fflush(outfile.get());
  462. ++num_decoded;
  463. }
  464. if(num_decoded == 0)
  465. fprintf(stderr, "Failed to decode any input files\n");
  466. else if(num_decoded < num_files)
  467. fprintf(stderr, "Decoded %zu of %zu files\n", num_decoded, num_files);
  468. else
  469. printf("Decoded %zu file%s\n", num_decoded, (num_decoded==1)?"":"s");
  470. return 0;
  471. }