AudioEditTests.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // Audio editor core (round 86, core/AudioEdit.h): every edit operation, markers, history, spectrogram, WAV.
  2. // Included at the end of tests.cpp.
  3. #include "core/AudioEdit.h"
  4. namespace
  5. {
  6. namespace ae = lynx::audioedit;
  7. // stereo ramp: left[i] = i, right[i] = -i (exact values make positions easy to check)
  8. ae::Document rampDoc (std::int64_t frames, double rate = 1000.0)
  9. {
  10. ae::Document d;
  11. d.audio.sampleRate = rate;
  12. d.audio.channels.assign (2, std::vector<float> ((std::size_t) frames));
  13. for (std::int64_t i = 0; i < frames; ++i)
  14. {
  15. d.audio.channels[0][(std::size_t) i] = (float) i;
  16. d.audio.channels[1][(std::size_t) i] = -(float) i;
  17. }
  18. return d;
  19. }
  20. ae::Document sineDoc (double freq, double seconds, double rate = 44100.0, float amplitude = 0.5f, int channels = 2)
  21. {
  22. ae::Document d;
  23. d.audio.sampleRate = rate;
  24. const auto n = (std::size_t) std::llround (seconds * rate);
  25. d.audio.channels.assign ((std::size_t) channels, std::vector<float> (n));
  26. for (std::size_t i = 0; i < n; ++i)
  27. for (auto& ch : d.audio.channels)
  28. ch[i] = amplitude * (float) std::sin (2.0 * 3.141592653589793 * freq * (double) i / rate);
  29. return d;
  30. }
  31. // dominant frequency by zero crossings of channel 0 over [from, to)
  32. double zeroCrossFreq (const ae::Audio& a, std::int64_t from, std::int64_t to)
  33. {
  34. int crossings = 0;
  35. const auto& x = a.channels[0];
  36. for (auto i = from + 1; i < to; ++i)
  37. if ((x[(std::size_t) i - 1] < 0.0f) != (x[(std::size_t) i] < 0.0f))
  38. ++crossings;
  39. return crossings * 0.5 / ((double) (to - from) / a.sampleRate);
  40. }
  41. } // namespace
  42. TEST (audioedit_copy_cut_erase_insert)
  43. {
  44. auto d = rampDoc (100);
  45. const auto c = ae::copy (d.audio, { 10, 20 });
  46. CHECK (c.frames() == 10 && c.numChannels() == 2 && c.channels[0][0] == 10.0f && c.channels[1][9] == -19.0f);
  47. auto cutDoc = d;
  48. const auto clip = ae::cut (cutDoc, { 10, 20 });
  49. CHECK (clip.frames() == 10 && clip.channels[0][3] == 13.0f);
  50. CHECK (cutDoc.audio.frames() == 90 && cutDoc.audio.channels[0][10] == 20.0f && cutDoc.audio.channels[1][9] == -9.0f);
  51. auto erased = d;
  52. ae::erase (erased, { 0, 50 });
  53. CHECK (erased.audio.frames() == 50 && erased.audio.channels[0][0] == 50.0f);
  54. ae::erase (erased, { 40, 40 }); // empty: nothing
  55. CHECK (erased.audio.frames() == 50);
  56. ae::erase (erased, { 45, 999 }); // clamped
  57. CHECK (erased.audio.frames() == 45);
  58. // paste insert puts the clip back where it was
  59. ae::insert (cutDoc, 10, clip);
  60. CHECK (cutDoc.audio.frames() == 100);
  61. bool same = true;
  62. for (int i = 0; i < 100; ++i)
  63. same = same && cutDoc.audio.channels[0][(std::size_t) i] == (float) i && cutDoc.audio.channels[1][(std::size_t) i] == -(float) i;
  64. CHECK (same);
  65. // into an empty document: the clip becomes the audio
  66. ae::Document empty;
  67. ae::insert (empty, 0, clip);
  68. CHECK (empty.audio.frames() == 10 && empty.audio.sampleRate == 1000.0);
  69. }
  70. TEST (audioedit_paste_replace_mix_conform)
  71. {
  72. auto d = rampDoc (100);
  73. ae::Audio mono;
  74. mono.sampleRate = 1000.0;
  75. mono.channels = { std::vector<float> (5, 1.0f) };
  76. auto r = d;
  77. ae::pasteReplace (r, { 10, 30 }, mono); // 20 frames become 5, mono duplicated to both sides
  78. CHECK (r.audio.frames() == 85 && r.audio.channels[0][10] == 1.0f && r.audio.channels[1][14] == 1.0f && r.audio.channels[0][15] == 30.0f);
  79. auto m = d;
  80. ae::pasteMix (m, 98, mono); // summed, extends past the end
  81. CHECK (m.audio.frames() == 103 && m.audio.channels[0][98] == 99.0f && m.audio.channels[1][99] == -98.0f && m.audio.channels[0][102] == 1.0f);
  82. // stereo → mono averages; another rate is resampled to the document's
  83. ae::Audio stereo;
  84. stereo.sampleRate = 2000.0;
  85. stereo.channels = { std::vector<float> (200, 0.8f), std::vector<float> (200, 0.2f) };
  86. const auto c = ae::conform (stereo, 1, 1000.0);
  87. CHECK (c.numChannels() == 1 && c.frames() == 100 && c.sampleRate == 1000.0);
  88. CHECK (std::abs (c.channels[0][50] - 0.5f) < 1.0e-4f && std::abs (c.channels[0][0] - 0.5f) < 1.0e-3f);
  89. }
  90. TEST (audioedit_trim_clear_silence_after_loop)
  91. {
  92. auto d = rampDoc (100);
  93. ae::trim (d, { 20, 30 });
  94. CHECK (d.audio.frames() == 10 && d.audio.channels[0][0] == 20.0f && d.audio.channels[0][9] == 29.0f);
  95. auto c = rampDoc (100);
  96. ae::clear (c, { 10, 20 });
  97. CHECK (c.audio.frames() == 100 && c.audio.channels[0][10] == 0.0f && c.audio.channels[1][19] == 0.0f && c.audio.channels[0][20] == 20.0f);
  98. auto s = rampDoc (100);
  99. ae::insertSilence (s, 50, 25);
  100. CHECK (s.audio.frames() == 125 && s.audio.channels[0][50] == 0.0f && s.audio.channels[0][74] == 0.0f && s.audio.channels[0][75] == 50.0f);
  101. auto l = rampDoc (100);
  102. ae::setLoop (l, { 10, 60 });
  103. ae::deleteAfterLoop (l);
  104. CHECK (l.audio.frames() == 60 && l.loopStart == 10 && l.loopEnd == 60);
  105. }
  106. TEST (audioedit_fades_and_curves)
  107. {
  108. for (auto c : { ae::Curve::linear, ae::Curve::smooth, ae::Curve::exponential, ae::Curve::logarithmic })
  109. {
  110. CHECK (ae::curveGain (c, 0.0f) == 0.0f);
  111. CHECK (std::abs (ae::curveGain (c, 1.0f) - 1.0f) < 1.0e-6f);
  112. // monotonic
  113. float prev = -1.0f;
  114. bool rising = true;
  115. for (int i = 0; i <= 20; ++i)
  116. {
  117. const auto g = ae::curveGain (c, (float) i / 20.0f);
  118. rising = rising && g >= prev;
  119. prev = g;
  120. }
  121. CHECK (rising);
  122. }
  123. CHECK (ae::curveGain (ae::Curve::exponential, 0.5f) < 0.5f && ae::curveGain (ae::Curve::logarithmic, 0.5f) > 0.5f);
  124. CHECK (std::abs (ae::curveGain (ae::Curve::smooth, 0.5f) - 0.5f) < 1.0e-6f);
  125. ae::Document d;
  126. d.audio.sampleRate = 1000.0;
  127. d.audio.channels = { std::vector<float> (100, 1.0f) };
  128. auto in = d;
  129. ae::fadeIn (in, { 0, 11 }, ae::Curve::linear);
  130. CHECK (in.audio.channels[0][0] == 0.0f && std::abs (in.audio.channels[0][5] - 0.5f) < 1.0e-6f && in.audio.channels[0][10] == 1.0f && in.audio.channels[0][50] == 1.0f);
  131. auto out = d;
  132. ae::fadeOut (out, { 89, 100 }, ae::Curve::smooth);
  133. CHECK (out.audio.channels[0][88] == 1.0f && out.audio.channels[0][89] == 1.0f && out.audio.channels[0][99] == 0.0f);
  134. // declick: a short (4 ms) fade at the edge of the range only
  135. ae::Document k;
  136. k.audio.sampleRate = 10000.0;
  137. k.audio.channels = { std::vector<float> (1000, 1.0f) };
  138. ae::declickIn (k, {});
  139. ae::declickOut (k, {});
  140. CHECK (k.audio.channels[0][0] == 0.0f && k.audio.channels[0][40] == 1.0f && k.audio.channels[0][999] == 0.0f && k.audio.channels[0][959] == 1.0f);
  141. }
  142. TEST (audioedit_gain_normalize_polarity_dc)
  143. {
  144. auto d = sineDoc (100.0, 0.1, 8000.0, 0.25f);
  145. const auto g = ae::normalize (d, {}, 0.0f);
  146. CHECK (std::abs (g - 4.0f) < 0.01f);
  147. CHECK (std::abs (ae::peak (d.audio, { 0, d.audio.frames() }) - 1.0f) < 1.0e-5f);
  148. ae::normalize (d, {}, -6.0f);
  149. CHECK (std::abs (ae::peak (d.audio, { 0, d.audio.frames() }) - 0.501187f) < 1.0e-4f);
  150. // silence is left alone
  151. auto s = ae::Document {};
  152. s.audio = ae::silence (2, 44100.0, 100);
  153. CHECK (ae::normalize (s, {}) == 1.0f);
  154. // gain + pan: pan right turns the left side down, the right side stays
  155. auto p = rampDoc (10);
  156. ae::amp (p, {}, 0.5f, 0.5f);
  157. CHECK (std::abs (p.audio.channels[0][4] - 4.0f * 0.25f) < 1.0e-6f && std::abs (p.audio.channels[1][4] + 4.0f * 0.5f) < 1.0e-6f);
  158. auto inv = rampDoc (10);
  159. ae::invert (inv, { 2, 4 });
  160. CHECK (inv.audio.channels[0][2] == -2.0f && inv.audio.channels[1][3] == 3.0f && inv.audio.channels[0][4] == 4.0f);
  161. // DC removal: a sine on a 0.3 offset ends up centred
  162. auto dc = sineDoc (50.0, 0.2, 1000.0, 0.5f, 1);
  163. for (auto& v : dc.audio.channels[0])
  164. v += 0.3f;
  165. ae::removeDc (dc, {});
  166. double mean = 0.0;
  167. for (auto v : dc.audio.channels[0])
  168. mean += v;
  169. CHECK (std::abs (mean / (double) dc.audio.frames()) < 1.0e-5);
  170. CHECK (ae::rms (dc.audio, { 0, dc.audio.frames() }) > 0.34f && ae::rms (dc.audio, { 0, dc.audio.frames() }) < 0.36f);
  171. }
  172. TEST (audioedit_reverse_channels)
  173. {
  174. auto d = rampDoc (10);
  175. ae::addMarker (d, 2, "a");
  176. ae::addRegion (d, { 1, 3 }, "r");
  177. ae::reverse (d, { 0, 10 });
  178. CHECK (d.audio.channels[0][0] == 9.0f && d.audio.channels[1][9] == 0.0f);
  179. // markers inside are mirrored (region [1,3) → [7,9), marker 2 → 8)
  180. CHECK (d.markers.size() == 2 && d.markers[0].start == 7 && d.markers[0].end == 9 && d.markers[1].start == 8 && d.markers[1].name == "a");
  181. auto s = rampDoc (4);
  182. ae::swapChannels (s);
  183. CHECK (s.audio.channels[0][3] == -3.0f && s.audio.channels[1][3] == 3.0f);
  184. auto m = rampDoc (4);
  185. ae::toMono (m, ae::MonoSource::merged);
  186. CHECK (m.audio.numChannels() == 1 && m.audio.channels[0][3] == 0.0f);
  187. auto r = rampDoc (4);
  188. ae::toMono (r, ae::MonoSource::right);
  189. CHECK (r.audio.channels[0][2] == -2.0f);
  190. ae::toStereo (r);
  191. CHECK (r.audio.numChannels() == 2 && r.audio.channels[1][2] == -2.0f);
  192. }
  193. TEST (audioedit_resample_speed_stretch_pitch)
  194. {
  195. // resampling keeps the pitch and the duration
  196. auto d = sineDoc (440.0, 0.5, 44100.0);
  197. ae::resample (d, 22050.0);
  198. CHECK (d.audio.sampleRate == 22050.0 && d.audio.frames() == 11025);
  199. CHECK (std::abs (zeroCrossFreq (d.audio, 1000, 10000) - 440.0) < 3.0);
  200. CHECK (std::abs (ae::peak (d.audio, { 1000, 10000 }) - 0.5f) < 0.01f);
  201. // a tone above the new Nyquist is filtered out instead of aliasing
  202. auto hi = sineDoc (15000.0, 0.2, 44100.0);
  203. ae::resample (hi, 22050.0);
  204. CHECK (ae::rms (hi.audio, { 500, hi.audio.frames() - 500 }) < 0.01f);
  205. // DC stays DC (also at the edges)
  206. std::vector<float> dc (100, 0.7f);
  207. const auto up = ae::resampleChannel (dc, 333);
  208. bool flat = true;
  209. for (auto v : up)
  210. flat = flat && std::abs (v - 0.7f) < 1.0e-4f;
  211. CHECK (up.size() == 333 && flat);
  212. // speed change: half as long, one octave up
  213. auto s = sineDoc (440.0, 0.5, 44100.0);
  214. ae::changeSpeed (s, {}, 0.5);
  215. CHECK (s.audio.frames() == 11025);
  216. CHECK (std::abs (zeroCrossFreq (s.audio, 500, 10500) - 880.0) < 6.0);
  217. // time stretch: twice as long, same pitch; markers after the range shift, inside scale
  218. auto t = sineDoc (440.0, 0.5, 44100.0);
  219. ae::addMarker (t, 11025, "mid");
  220. ae::addMarker (t, 22000, "end");
  221. ae::timeStretch (t, { 0, 11025 }, 2.0);
  222. CHECK (t.audio.frames() == 22050 + 11025);
  223. CHECK (std::abs (zeroCrossFreq (t.audio, 2000, 20000) - 440.0) < 6.0);
  224. CHECK (t.markers.size() == 2 && t.markers[0].start == 22050 && t.markers[1].start == 22000 + 11025);
  225. // pitch shift: same length, +12 semitones = double frequency
  226. auto p = sineDoc (440.0, 0.5, 44100.0);
  227. ae::pitchShift (p, {}, 12.0);
  228. CHECK (p.audio.frames() == 22050);
  229. CHECK (std::abs (zeroCrossFreq (p.audio, 3000, 19000) - 880.0) < 12.0);
  230. }
  231. TEST (audioedit_markers_follow_edits)
  232. {
  233. auto d = rampDoc (100);
  234. ae::addMarker (d, 10, "a");
  235. ae::addMarker (d, 50, "b");
  236. ae::addRegion (d, { 60, 80 }, "r");
  237. ae::setLoop (d, { 40, 90 });
  238. CHECK (d.markers.size() == 3 && d.markers[2].isRegion());
  239. auto e = d;
  240. ae::erase (e, { 45, 70 }); // marker 50 dropped, region clipped to [45, 55), loop [40, 65)
  241. CHECK (e.markers.size() == 2 && e.markers[1].start == 45 && e.markers[1].end == 55 && e.loopStart == 40 && e.loopEnd == 65);
  242. auto i = d;
  243. ae::insertSilence (i, 20, 5);
  244. CHECK (i.markers[0].start == 10 && i.markers[1].start == 55 && i.markers[2].start == 65 && i.loopEnd == 95);
  245. auto t = d;
  246. ae::trim (t, { 50, 100 }); // marker at 50 stays at the new start
  247. CHECK (t.markers.size() == 2 && t.markers[0].start == 0 && t.markers[1].start == 10 && t.loopStart == 0 && t.loopEnd == 40);
  248. // erasing the loop entirely removes it
  249. auto l = d;
  250. ae::erase (l, { 30, 95 });
  251. CHECK (! l.hasLoop());
  252. // removing: markers in a range, regions overlapping it
  253. auto r = d;
  254. CHECK (ae::removeMarkers (r, { 0, 61 }) == 3);
  255. auto r2 = d;
  256. CHECK (ae::removeMarkers (r2, { 50, 50 }) == 1 && r2.markers.size() == 2);
  257. // resampling scales them
  258. auto s = rampDoc (100, 1000.0);
  259. ae::addMarker (s, 50);
  260. ae::setLoop (s, { 10, 20 });
  261. ae::resample (s, 2000.0);
  262. CHECK (s.markers[0].start == 100 && s.loopStart == 20 && s.loopEnd == 40);
  263. ae::setLoop (s, {});
  264. CHECK (! s.hasLoop());
  265. }
  266. TEST (audioedit_zero_crossing_and_onsets)
  267. {
  268. auto d = sineDoc (100.0, 0.1, 1000.0, 0.5f, 1); // crossings every 5 frames
  269. const auto z = ae::nearestZeroCrossing (d.audio, 12, 10);
  270. CHECK (z == 10 || z == 11);
  271. CHECK (ae::nearestZeroCrossing (d.audio, 12, 0) == 12);
  272. // clicks at known places on low noise
  273. ae::Document c;
  274. c.audio = ae::silence (1, 44100.0, 44100);
  275. std::uint32_t seed = 1;
  276. for (auto& v : c.audio.channels[0])
  277. {
  278. seed = seed * 1664525u + 1013904223u;
  279. v = ((float) (seed >> 8) / 16777216.0f - 0.5f) * 0.001f;
  280. }
  281. for (std::int64_t at : { 4410, 15000, 30000 })
  282. for (int i = 0; i < 2000; ++i)
  283. c.audio.channels[0][(std::size_t) (at + i)] += 0.8f * std::exp (-(float) i / 300.0f) * (float) std::sin (0.3 * i);
  284. const auto onsets = ae::detectOnsets (c.audio, 0.5f);
  285. CHECK (onsets.size() == 3);
  286. if (onsets.size() == 3)
  287. CHECK (std::abs (onsets[0] - 4410) < 300 && std::abs (onsets[1] - 15000) < 300 && std::abs (onsets[2] - 30000) < 300);
  288. }
  289. TEST (audioedit_history_undo_redo)
  290. {
  291. ae::History h (std::size_t (1) << 30);
  292. auto d = rampDoc (100);
  293. h.reset (d, "Open");
  294. CHECK (! h.canUndo() && ! h.canRedo() && h.size() == 1);
  295. ae::erase (d, { 0, 10 });
  296. h.commit (d, "Delete");
  297. ae::reverse (d, {});
  298. h.commit (d, "Reverse");
  299. CHECK (h.size() == 3 && h.getPosition() == 2 && h.label (1) == "Delete");
  300. CHECK (h.undo().audio.frames() == 90 && h.current().audio.channels[0][0] == 10.0f);
  301. CHECK (h.undo().audio.frames() == 100);
  302. CHECK (! h.canUndo() && h.canRedo());
  303. CHECK (h.redo().audio.frames() == 90);
  304. CHECK (h.jumpTo (2).audio.channels[0][0] == 99.0f);
  305. // a new edit after undo drops the redo steps
  306. h.jumpTo (0);
  307. auto e = h.current();
  308. ae::clear (e, {});
  309. h.commit (e, "Clear");
  310. CHECK (h.size() == 2 && ! h.canRedo() && h.label (1) == "Clear");
  311. // memory bound: oldest states go, the current and one before it stay
  312. ae::History small (1000);
  313. auto s = rampDoc (100); // 800 bytes per state
  314. small.reset (s, "Open");
  315. small.commit (s, "a");
  316. small.commit (s, "b");
  317. CHECK (small.size() == 2 && small.label (0) == "a" && small.canUndo());
  318. }
  319. TEST (audioedit_spectrogram)
  320. {
  321. const auto d = sineDoc (1000.0, 0.5, 44100.0, 1.0f);
  322. const auto s = ae::spectrogram (d.audio, { 0, d.audio.frames() }, 8, 2048);
  323. CHECK (s.columns == 8 && s.bins == 1024);
  324. // the strongest bin is at 1000 Hz and reads about 0 dB
  325. int best = 0;
  326. for (int b = 1; b < s.bins; ++b)
  327. if (s.at (4, b) > s.at (4, best))
  328. best = b;
  329. const auto hz = best * 44100.0 / 2048.0;
  330. CHECK (std::abs (hz - 1000.0) < 25.0);
  331. CHECK (s.at (4, best) > -3.0f && s.at (4, best) < 0.5f);
  332. CHECK (s.at (4, 600) < -60.0f);
  333. CHECK (ae::spectrogram (d.audio, { 0, 100 }, 4, 1000).columns == 0); // not a power of two
  334. }
  335. TEST (audioedit_wav_roundtrip_with_markers)
  336. {
  337. auto d = sineDoc (440.0, 0.05, 48000.0, 0.5f);
  338. ae::addMarker (d, 100, "kick");
  339. ae::addRegion (d, { 500, 900 }, "snare");
  340. ae::addMarker (d, 1200); // unnamed
  341. ae::setLoop (d, { 200, 2000 });
  342. for (auto format : { ae::WavFormat::int16, ae::WavFormat::int24, ae::WavFormat::float32 })
  343. {
  344. const auto bytes = ae::encodeWav (d, format);
  345. ae::Document back;
  346. CHECK (ae::decodeWav (bytes.data(), bytes.size(), back));
  347. CHECK (back.audio.sampleRate == 48000.0 && back.audio.numChannels() == 2 && back.audio.frames() == d.audio.frames());
  348. const auto tolerance = format == ae::WavFormat::int16 ? 1.0e-4f : format == ae::WavFormat::int24 ? 1.0e-6f : 0.0f;
  349. float err = 0.0f;
  350. for (std::size_t i = 0; i < (std::size_t) d.audio.frames(); ++i)
  351. err = std::max (err, std::abs (back.audio.channels[1][i] - d.audio.channels[1][i]));
  352. CHECK (err <= tolerance);
  353. CHECK (back.markers.size() == 3);
  354. if (back.markers.size() == 3)
  355. {
  356. CHECK (back.markers[0].start == 100 && back.markers[0].name == "kick" && ! back.markers[0].isRegion());
  357. CHECK (back.markers[1].start == 500 && back.markers[1].end == 900 && back.markers[1].name == "snare");
  358. CHECK (back.markers[2].start == 1200 && back.markers[2].name.empty());
  359. }
  360. CHECK (back.loopStart == 200 && back.loopEnd == 2000);
  361. // RIFF size is consistent
  362. CHECK ((std::size_t) (bytes[4] | bytes[5] << 8 | bytes[6] << 16 | bytes[7] << 24) == bytes.size() - 8);
  363. }
  364. // markers only (the audio comes from another decoder)
  365. const auto bytes = ae::encodeWav (d, ae::WavFormat::int16);
  366. ae::Document only;
  367. only.audio = ae::silence (2, 48000.0, d.audio.frames());
  368. CHECK (ae::decodeWavMarkers (bytes.data(), bytes.size(), only) && only.markers.size() == 3 && only.loopEnd == 2000);
  369. // mono, no markers, and not-a-WAV
  370. ae::Document mono;
  371. mono.audio = ae::silence (1, 22050.0, 10);
  372. const auto m = ae::encodeWav (mono);
  373. ae::Document mb;
  374. CHECK (ae::decodeWav (m.data(), m.size(), mb) && mb.audio.numChannels() == 1 && mb.markers.empty() && ! mb.hasLoop());
  375. const std::uint8_t junk[16] = { 'R', 'I', 'F', 'X' };
  376. CHECK (! ae::decodeWav (junk, sizeof (junk), mb));
  377. }
  378. // What FL hands a plugin about a WAV besides its audio (round 165g): cue / region starts, ACID beats
  379. TEST (audioedit_wav_extras_markers_and_acid)
  380. {
  381. auto d = sineDoc (440.0, 0.05, 44100.0, 0.5f);
  382. ae::addRegion (d, { 1500, 1900 }, "b");
  383. ae::addMarker (d, 300, "a");
  384. ae::addMarker (d, 2000);
  385. auto bytes = ae::encodeWav (d, ae::WavFormat::int16);
  386. // an ACID chunk: flags, root note, ?, ?, beats = 8, meter 4/4, tempo 120
  387. const std::uint8_t acid[8 + 24] = { 'a', 'c', 'i', 'd', 24, 0, 0, 0, 2, 0, 0, 0, 60, 0, 0, 0x80, 0, 0, 0, 0,
  388. 8, 0, 0, 0, 4, 0, 4, 0, 0, 0, 0xf0, 0x42 };
  389. bytes.insert (bytes.end(), acid, acid + sizeof (acid));
  390. const auto riff = (std::uint32_t) bytes.size() - 8;
  391. for (int k = 0; k < 4; ++k)
  392. bytes[4 + (std::size_t) k] = (std::uint8_t) (riff >> (8 * k));
  393. const auto x = ae::decodeWavExtras (bytes.data(), bytes.size(), d.audio.frames());
  394. CHECK (x.markerStarts == (std::vector<std::int64_t> { 300, 1500, 2000 }));
  395. CHECK (x.acidBeats == 8.0f);
  396. // positions past the end are clamped; no chunks / not a WAV: nothing
  397. const auto clamped = ae::decodeWavExtras (bytes.data(), bytes.size(), 1000);
  398. CHECK (clamped.markerStarts == (std::vector<std::int64_t> { 300, 999, 999 }));
  399. ae::Document plain = sineDoc (440.0, 0.01);
  400. const auto p = ae::encodeWav (plain);
  401. const auto none = ae::decodeWavExtras (p.data(), p.size(), plain.audio.frames());
  402. CHECK (none.markerStarts.empty() && none.acidBeats == 0.0f);
  403. const std::uint8_t junk[16] = { 'R', 'I', 'F', 'X' };
  404. CHECK (ae::decodeWavExtras (junk, sizeof (junk), 10).markerStarts.empty());
  405. }
  406. // FL's auto slicing (core/FlAutoSlicer, round 165g): slice starts of the installation's Fruity Granulizer DLL on this
  407. // sample (5 square-wave hits; stereo 44.1 kHz and mono 48 kHz) [EXEC FL, tmp/r165g/py/gold.py]
  408. #include "core/FlAutoSlicer.h"
  409. TEST (fl_auto_slicer_matches_fl)
  410. {
  411. const int n = 60000;
  412. std::vector<float> x ((std::size_t) n, 0.0f);
  413. std::vector<int> acc ((std::size_t) n, 0);
  414. for (const int h : { 0, 9000, 21000, 30500, 47000 })
  415. for (int i = h; i < n; ++i)
  416. {
  417. const int k = i - h, a = 20000 >> (k / 1500);
  418. if (a == 0)
  419. break;
  420. acc[(std::size_t) i] = std::clamp (acc[(std::size_t) i] + ((k / 20) % 2 == 0 ? a : -a), -32768, 32767);
  421. }
  422. for (int i = 0; i < n; ++i)
  423. x[(std::size_t) i] = (float) acc[(std::size_t) i] / 32768.0f;
  424. const std::vector<std::int64_t> fl { 0, 9000, 21000, 30480, 46980 };
  425. std::vector<float> r = x;
  426. CHECK (lynx::flslice::autoSlice (x.data(), r.data(), n, 44100.0) == fl);
  427. CHECK (lynx::flslice::autoSlice (x.data(), nullptr, n, 48000.0) == fl);
  428. CHECK (lynx::flslice::autoSlice (nullptr, nullptr, 0, 44100.0).empty());
  429. }