AudioEditToolsTests.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. // Audio editor tools (round 165l, core/AudioEditTools.h): Edison's tool forms in LYNX. Included at the end of
  2. // tests.cpp (after AudioEditTests.inc, whose helpers sineDoc / zeroCrossFreq it uses).
  3. #include "core/AudioEditTools.h"
  4. namespace
  5. {
  6. double rmsOf (const std::vector<float>& v, std::int64_t a, std::int64_t b)
  7. {
  8. double e = 0.0;
  9. for (auto i = a; i < b; ++i)
  10. e += (double) v[(std::size_t) i] * v[(std::size_t) i];
  11. return b > a ? std::sqrt (e / (double) (b - a)) : 0.0;
  12. }
  13. // the level of a sine at `hz` in `x` (a single-bin DFT over [a, b))
  14. double toneLevel (const std::vector<float>& x, std::int64_t a, std::int64_t b, double hz, double sr)
  15. {
  16. double re = 0.0, im = 0.0;
  17. for (auto i = a; i < b; ++i)
  18. {
  19. const auto ph = 2.0 * 3.141592653589793 * hz * (double) i / sr;
  20. re += x[(std::size_t) i] * std::cos (ph);
  21. im += x[(std::size_t) i] * std::sin (ph);
  22. }
  23. return 2.0 * std::hypot (re, im) / (double) (b - a);
  24. }
  25. ae::Document noiseDoc (double seconds, float level, double rate = 44100.0, std::uint32_t seed = 3)
  26. {
  27. ae::Document d;
  28. d.audio = ae::silence (2, rate, (std::int64_t) (seconds * rate));
  29. ae::generateNoise (d, {}, level, seed);
  30. return d;
  31. }
  32. } // namespace
  33. TEST (audioedittools_envelope_eval_and_point_tools_round165l)
  34. {
  35. ae::Envelope e;
  36. e.points = { { 0.0, 0.0 }, { 0.5, 1.0 }, { 1.0, 0.0 } };
  37. CHECK (std::abs (e.at (0.25) - 0.5) < 1e-9);
  38. CHECK (std::abs (e.at (0.75) - 0.5) < 1e-9);
  39. e.points[1].tension = 0.5f; // slow start on the rising segment
  40. CHECK (e.at (0.25) < 0.5);
  41. ae::Envelope f = ae::Envelope::flat (0.5);
  42. ae::scaleLevels (f, 0.0, 2.0, 0.0, 0.0); // multiply x2 around 0
  43. CHECK (std::abs (f.points[0].y - 1.0) < 1e-9);
  44. ae::scaleLevels (f, 0.0, 1.0, -0.25, 0.0);
  45. CHECK (std::abs (f.points[0].y - 0.75) < 1e-9);
  46. ae::flipLevels (f);
  47. CHECK (std::abs (f.points[0].y - 0.25) < 1e-9);
  48. ae::Envelope g;
  49. g.points = { { 0.0, 0.2 }, { 0.5, 0.4 }, { 1.0, 0.6 } };
  50. ae::normalizeLevels (g);
  51. CHECK (std::abs (g.points[0].y) < 1e-9 && std::abs (g.points[2].y - 1.0) < 1e-9);
  52. // decimation removes the collinear middle point but keeps the corner
  53. ae::Envelope h;
  54. h.points = { { 0.0, 0.0 }, { 0.25, 0.25 }, { 0.5, 0.5 }, { 0.75, 0.0 }, { 1.0, 0.0 } };
  55. ae::decimatePoints (h, 0.5);
  56. CHECK (h.points.size() == 4u);
  57. CHECK (std::abs (h.points[1].x - 0.5) < 1e-9);
  58. ae::decimatePoints (h, 0.0); // threshold 0 keeps everything
  59. CHECK (h.points.size() == 4u);
  60. }
  61. TEST (audioedittools_smooth_filter_sequence_round165l)
  62. {
  63. // a step 0 -> 1 at x = 0.5: smooth up with a long attack rises gradually
  64. ae::Envelope step;
  65. step.points = { { 0.0, 0.0 }, { 0.5, 0.0 }, { 0.5, 1.0 }, { 1.0, 1.0 } };
  66. auto s = step;
  67. ae::smoothUp (s, 0.5, 0.5, 0.5, 0.5, 0.0, 0.2);
  68. CHECK (s.at (0.52) < 0.5);
  69. CHECK (s.at (0.99) > 0.9);
  70. CHECK (s.at (0.4) < 0.01);
  71. auto f = step;
  72. ae::filterEnvelope (f, {}, 0.3, 0.0, 0.1);
  73. CHECK (f.at (0.45) > 0.02 && f.at (0.55) < 0.98); // blurred around the step
  74. CHECK (f.at (0.05) < 0.01 && f.at (0.95) > 0.99);
  75. ae::SequenceSettings q;
  76. q.levels = { 1.0f, 0.5f };
  77. q.breaks = { false, true };
  78. q.stepsPerUnit = 2;
  79. const auto seq = ae::createSequence (q, 2.0); // 4 steps of 1/4
  80. CHECK (seq.at (0.125 * 0.3) > 0.5); // inside step 0's gate
  81. CHECK (seq.at (0.25 + 0.05) < 1e-6); // step 1 is a break
  82. CHECK (seq.at (0.5 + 0.05) > 0.5); // step 2 = level 1 again
  83. }
  84. TEST (audioedittools_amp_mixdown_and_levels_round165l)
  85. {
  86. auto d = sineDoc (440.0, 0.2, 44100.0, 0.5f);
  87. const auto n = d.audio.frames();
  88. ae::AmpSettings a;
  89. a.pan = 1.0; // hard right: left silent, right unity
  90. ae::ampTool (d, {}, a);
  91. CHECK (rmsOf (d.audio.channels[0], 0, n) < 1e-6);
  92. CHECK (std::abs (rmsOf (d.audio.channels[1], 0, n) - 0.5 / std::sqrt (2.0)) < 1e-3);
  93. auto e = sineDoc (440.0, 0.2, 44100.0, 0.5f);
  94. ae::AmpSettings b;
  95. b.pan = 0.5;
  96. b.circularLaw = true;
  97. ae::ampTool (e, {}, b);
  98. const auto l = rmsOf (e.audio.channels[0], 0, n), r = rmsOf (e.audio.channels[1], 0, n);
  99. CHECK (r > l && r <= 0.5 / std::sqrt (2.0) + 1e-6);
  100. // stereo separation -1 = mono
  101. auto m = rampDoc (100);
  102. ae::AmpSettings c;
  103. c.stereoSeparation = -1.0;
  104. ae::ampTool (m, {}, c);
  105. CHECK (std::abs (m.audio.channels[0][50]) < 1e-6 && std::abs (m.audio.channels[1][50]) < 1e-6);
  106. // volume envelope: 0.5 = unity, the end at 0 fades out
  107. auto v = sineDoc (100.0, 0.5, 44100.0, 0.5f);
  108. ae::Envelope vol;
  109. vol.points = { { 0.0, 0.5 }, { 0.5, 0.5 }, { 1.0, 0.0 } };
  110. ae::mixdownEnvelopes (v, vol, {}, {});
  111. const auto vn = v.audio.frames();
  112. CHECK (std::abs (rmsOf (v.audio.channels[0], 0, vn / 4) - 0.5 / std::sqrt (2.0)) < 0.01);
  113. CHECK (rmsOf (v.audio.channels[0], vn - vn / 50, vn) < 0.01);
  114. // lossy normalize (a phase rotation that lowers the peaks, then normalize): a click train gets much louder
  115. auto ln = noiseDoc (0.5, 0.0f);
  116. for (std::size_t i = 50; i < ln.audio.channels[0].size(); i += 100)
  117. ln.audio.channels[0][i] = 0.9f;
  118. auto plain = ln;
  119. ae::normalize (plain, {}, 0.0f);
  120. CHECK (ae::lossyNormalize (ln, {}) > 1.5f);
  121. CHECK (rmsOf (ln.audio.channels[0], 0, ln.audio.frames()) > 1.5 * rmsOf (plain.audio.channels[0], 0, plain.audio.frames()));
  122. CHECK (ae::peak (ln.audio, { 0, ln.audio.frames() }) <= 1.0f);
  123. auto mono = sineDoc (440.0, 0.05);
  124. CHECK (ae::monoIfMono (mono) && mono.audio.numChannels() == 1);
  125. auto st = rampDoc (10);
  126. CHECK (! ae::monoIfMono (st));
  127. }
  128. TEST (audioedittools_noise_gating_and_regions_round165l)
  129. {
  130. // tone 0.2 s, silence(ish) 0.3 s, tone 0.2 s
  131. const double sr = 44100.0;
  132. auto d = sineDoc (300.0, 0.7, sr, 0.5f);
  133. auto hiss = noiseDoc (0.7, 0.001f, sr);
  134. for (int c = 0; c < 2; ++c)
  135. for (auto i = (std::int64_t) (0.2 * sr); i < (std::int64_t) (0.5 * sr); ++i)
  136. d.audio.channels[(std::size_t) c][(std::size_t) i] = hiss.audio.channels[(std::size_t) c][(std::size_t) i];
  137. const auto thr = ae::acquireNoiseThreshold (d.audio, { (std::int64_t) (0.25 * sr), (std::int64_t) (0.45 * sr) });
  138. CHECK (thr > 0.0005f && thr < 0.0015f);
  139. const auto parts = ae::quietParts (d.audio, {}, thr * 1.1f);
  140. CHECK (parts.size() == 1u);
  141. auto g = d;
  142. CHECK (ae::gateNoise (g, {}, thr * 1.1f) == 1);
  143. CHECK (rmsOf (g.audio.channels[0], (std::int64_t) (0.3 * sr), (std::int64_t) (0.4 * sr)) == 0.0);
  144. auto t = d;
  145. const auto n = ae::trimNoiseAndSlice (t, {}, thr * 1.1f);
  146. CHECK (n == 2);
  147. CHECK (std::abs (t.audio.seconds() - 0.41) < 0.03);
  148. // regions: normalize each
  149. auto r = sineDoc (300.0, 1.0, sr, 0.1f);
  150. ae::addRegion (r, { 0, 10000 }, "a");
  151. ae::addRegion (r, { 20000, 30000 }, "b");
  152. CHECK (ae::normalizeRegions (r) == 2);
  153. CHECK (std::abs (ae::peak (r.audio, { 0, 10000 }) - 1.0f) < 1e-3);
  154. CHECK (ae::peak (r.audio, { 12000, 18000 }) < 0.11f);
  155. CHECK (ae::perfectRegions (r) == 2);
  156. CHECK (std::abs (r.audio.channels[0][(std::size_t) r.markers[0].start]) < 0.05f);
  157. }
  158. TEST (audioedittools_eq_and_unharmonics_round165l)
  159. {
  160. const double sr = 44100.0;
  161. auto d = sineDoc (200.0, 1.0, sr, 0.25f);
  162. auto hi = sineDoc (8000.0, 1.0, sr, 0.25f);
  163. for (int c = 0; c < 2; ++c)
  164. for (std::size_t i = 0; i < d.audio.channels[0].size(); ++i)
  165. d.audio.channels[(std::size_t) c][i] += hi.audio.channels[(std::size_t) c][i];
  166. ae::EqSettings eq;
  167. eq.freqScale = 1.0;
  168. const auto split = ae::curvePosition (1000.0, 1.0, sr);
  169. eq.gain.points = { { 0.0, 0.5 }, { split, 0.5 }, { split + 0.01, 0.0 }, { 1.0, 0.0 } }; // low-pass at ~1 kHz
  170. ae::equalize (d, {}, eq);
  171. const auto n = d.audio.frames();
  172. const auto a = n / 4, b = 3 * n / 4;
  173. CHECK (std::abs (toneLevel (d.audio.channels[0], a, b, 200.0, sr) - 0.25) < 0.01);
  174. CHECK (toneLevel (d.audio.channels[0], a, b, 8000.0, sr) < 0.0025);
  175. // a flat curve at 0.5 is transparent
  176. auto t = sineDoc (1234.0, 0.5, sr, 0.3f);
  177. const auto before = t.audio.channels[0];
  178. ae::EqSettings flat;
  179. flat.gain = ae::Envelope::flat (0.5);
  180. ae::equalize (t, {}, flat);
  181. double err = 0.0;
  182. for (std::size_t i = 0; i < before.size(); ++i)
  183. err = std::max (err, (double) std::abs (before[i] - t.audio.channels[0][i]));
  184. CHECK (err < 1e-4);
  185. CHECK (std::abs (ae::curveFrequency (ae::curvePosition (440.0, 0.6, sr), 0.6, sr) - 440.0) < 0.5);
  186. const auto u = ae::unharmonicsCurve (100.0, 1, 1.0, sr);
  187. CHECK (u.at (ae::curvePosition (300.0, 1.0, sr)) > 0.45);
  188. CHECK (u.at (ae::curvePosition (350.0, 1.0, sr)) < 0.05);
  189. }
  190. TEST (audioedittools_reverb_blur_round165l)
  191. {
  192. const double sr = 44100.0;
  193. ae::Document d;
  194. d.audio = ae::silence (2, sr, (std::int64_t) sr);
  195. d.audio.channels[0][1000] = d.audio.channels[1][1000] = 1.0f; // an impulse
  196. ae::ReverbSettings rv;
  197. rv.length = 0.5;
  198. rv.dry = 1.0;
  199. rv.wet = 0.5;
  200. rv.addTail = true;
  201. const auto before = d.audio.frames();
  202. ae::convolutionReverb (d, { 0, before }, rv);
  203. CHECK (d.audio.frames() == before + (std::int64_t) (0.5 * sr));
  204. CHECK (std::abs (d.audio.channels[0][1000] - 1.0f) < 0.1f); // the dry hit
  205. CHECK (rmsOf (d.audio.channels[0], 2000, 10000) > 1e-4); // the tail rings
  206. CHECK (rmsOf (d.audio.channels[0], 1000 + (std::int64_t) (0.6 * sr), before) < 1e-6);
  207. // blur spreads a burst in time
  208. ae::Document b;
  209. b.audio = ae::silence (1, sr, (std::int64_t) sr);
  210. for (int i = 0; i < 2000; ++i)
  211. b.audio.channels[0][(std::size_t) (10000 + i)] = (float) std::sin (i * 0.1) * 0.5f;
  212. ae::BlurSettings bs;
  213. bs.amount = 0.1;
  214. ae::blur (b, {}, bs);
  215. CHECK (rmsOf (b.audio.channels[0], 14000, 18000) > 1e-3);
  216. }
  217. TEST (audioedittools_denoise_round165l)
  218. {
  219. const double sr = 44100.0;
  220. auto noise = noiseDoc (2.0, 0.02f, sr, 9);
  221. auto tone = sineDoc (440.0, 2.0, sr, 0.3f);
  222. auto d = tone;
  223. for (int c = 0; c < 2; ++c)
  224. for (std::size_t i = 0; i < d.audio.channels[0].size(); ++i)
  225. d.audio.channels[(std::size_t) c][i] += noise.audio.channels[(std::size_t) c][i];
  226. // the first 0.5 s: noise only (the profile)
  227. for (auto& ch : d.audio.channels)
  228. for (std::int64_t i = 0; i < (std::int64_t) (0.5 * sr); ++i)
  229. ch[(std::size_t) i] = noise.audio.channels[0][(std::size_t) i];
  230. const auto profile = ae::acquireNoiseProfile (d.audio, { 0, (std::int64_t) (0.5 * sr) });
  231. CHECK (profile.valid());
  232. ae::DenoiseSettings s;
  233. s.amount = 1.0;
  234. ae::denoise (d, {}, profile, s);
  235. const auto a = (std::int64_t) (1.0 * sr), b = (std::int64_t) (1.8 * sr);
  236. CHECK (std::abs (toneLevel (d.audio.channels[0], a, b, 440.0, sr) - 0.3) < 0.03); // the tone stays
  237. CHECK (rmsOf (d.audio.channels[0], 2000, (std::int64_t) (0.45 * sr)) < 0.02 * 0.2); // the noise-only part goes
  238. // the residual (signal - tone) is much smaller than the noise was
  239. double resid = 0.0;
  240. for (auto i = a; i < b; ++i)
  241. {
  242. const auto e = d.audio.channels[0][(std::size_t) i] - tone.audio.channels[0][(std::size_t) i];
  243. resid += e * e;
  244. }
  245. CHECK (std::sqrt (resid / (double) (b - a)) < 0.02 * 0.5);
  246. // declipper: a clipped sine gets its peaks back
  247. auto c = sineDoc (100.0, 0.1, sr, 1.0f);
  248. const auto orig = c.audio.channels[0];
  249. for (auto& ch : c.audio.channels)
  250. for (auto& v : ch)
  251. v = std::clamp (v, -0.7f, 0.7f);
  252. CHECK (ae::declip (c.audio, {}, 0.99) > 0);
  253. CHECK (ae::peak (c.audio, { 0, c.audio.frames() }) > 0.85f);
  254. // safe mode: a sine clipped near full scale is rebuilt without going over it; vocal: at most +3 dB
  255. auto loud = sineDoc (100.0, 0.1, sr, 1.5f);
  256. for (auto& ch : loud.audio.channels)
  257. for (auto& v : ch)
  258. v = std::clamp (v, -0.95f, 0.95f);
  259. auto vocal = loud;
  260. CHECK (ae::declip (loud.audio, {}, 0.99, false, true) > 0);
  261. CHECK (ae::peak (loud.audio, { 0, loud.audio.frames() }) <= 1.0f && ae::peak (loud.audio, { 0, loud.audio.frames() }) > 0.96f);
  262. CHECK (ae::declip (vocal.audio, {}, 0.99, true, false) > 0);
  263. CHECK (ae::peak (vocal.audio, { 0, vocal.audio.frames() }) <= 0.95f * 0.99f * 1.42f);
  264. // declicker: a single spike in a sine is removed
  265. auto k = sineDoc (200.0, 0.2, sr, 0.3f);
  266. k.audio.channels[0][4000] += 0.6f;
  267. CHECK (ae::declick (k.audio, {}, 0.5) >= 1);
  268. CHECK (std::abs (k.audio.channels[0][4000] - 0.3f * (float) std::sin (2.0 * 3.141592653589793 * 200.0 * 4000 / sr)) < 0.05f);
  269. }
  270. TEST (audioedittools_vocal_denoise_match_spectrum_round165l)
  271. {
  272. const double sr = 44100.0;
  273. auto noise = noiseDoc (2.0, 0.03f, sr, 5);
  274. auto tone = sineDoc (300.0, 2.0, sr, 0.3f);
  275. // syllables: 150 ms on, 100 ms off (a steady tone would count as noise, like a hum)
  276. for (auto& ch : tone.audio.channels)
  277. for (std::size_t i = 0; i < ch.size(); ++i)
  278. {
  279. const auto t = std::fmod ((double) i / sr, 0.25);
  280. ch[i] *= (float) (t < 0.15 ? std::sin (3.141592653589793 * t / 0.15) : 0.0);
  281. }
  282. auto d = tone;
  283. for (int c = 0; c < 2; ++c)
  284. for (std::size_t i = 0; i < d.audio.channels[0].size(); ++i)
  285. d.audio.channels[(std::size_t) c][i] += noise.audio.channels[(std::size_t) c][i];
  286. auto v = d;
  287. ae::vocalDenoise (v, {}, 1.0);
  288. const auto a = (std::int64_t) (0.8 * sr), b = (std::int64_t) (1.8 * sr);
  289. double before = 0.0, after = 0.0;
  290. for (auto i = a; i < b; ++i)
  291. {
  292. const auto e0 = d.audio.channels[0][(std::size_t) i] - tone.audio.channels[0][(std::size_t) i];
  293. const auto e1 = v.audio.channels[0][(std::size_t) i] - tone.audio.channels[0][(std::size_t) i];
  294. before += e0 * e0;
  295. after += e1 * e1;
  296. }
  297. CHECK (after < before * 0.35);
  298. auto n = d;
  299. ae::vocalDenoise (n, {}, -1.0); // the noise only: the voice mostly goes
  300. CHECK (toneLevel (n.audio.channels[0], a, b, 300.0, sr) < 0.5 * toneLevel (d.audio.channels[0], a, b, 300.0, sr));
  301. // match spectrum: white noise matched to a low-passed noise gets darker
  302. auto ref = noiseDoc (1.0, 0.2f, sr, 11);
  303. ae::EqSettings lp;
  304. const auto split = ae::curvePosition (1000.0, 1.0, sr);
  305. lp.gain.points = { { 0.0, 0.5 }, { split, 0.5 }, { split + 0.05, 0.1 }, { 1.0, 0.1 } };
  306. ae::equalize (ref, {}, lp);
  307. auto w = noiseDoc (1.0, 0.2f, sr, 13);
  308. ae::matchSpectrum (w, {}, ref.audio, 1.0);
  309. // compare highs vs lows by a crude high-pass (first difference) energy ratio
  310. auto hpRatio = [] (const std::vector<float>& x) {
  311. double e = 0.0, h = 0.0;
  312. for (std::size_t i = 1; i < x.size(); ++i)
  313. {
  314. e += (double) x[i] * x[i];
  315. h += (double) (x[i] - x[i - 1]) * (x[i] - x[i - 1]);
  316. }
  317. return h / std::max (1e-12, e);
  318. };
  319. const auto white = noiseDoc (1.0, 0.2f, sr, 13);
  320. CHECK (hpRatio (w.audio.channels[0]) < 0.5 * hpRatio (white.audio.channels[0]));
  321. }
  322. TEST (audioedittools_claw_drumloop_loop_tuner_round165l)
  323. {
  324. const double sr = 44100.0;
  325. // claw: 120 bpm, period 1 beat (0.5 s), trash 4/4: a quarter of every beat goes
  326. auto d = sineDoc (220.0, 2.0, sr, 0.3f);
  327. ae::ClawSettings c;
  328. c.snapToMarkers = false;
  329. ae::clawMachine (d, {}, c, 120.0);
  330. CHECK (std::abs (d.audio.seconds() - 1.5) < 0.01);
  331. auto full = sineDoc (220.0, 2.0, sr, 0.3f);
  332. c.compensation = 1.0;
  333. c.resample = true;
  334. ae::clawMachine (full, {}, c, 120.0);
  335. CHECK (std::abs (full.audio.seconds() - 2.0) < 0.01);
  336. // drumloop stretch: 4 hits at 0, 0.25, 0.5, 0.75 s → ratio 2 → hits at 0, 0.5, 1.0, 1.5 s, attacks intact
  337. ae::Document loop;
  338. loop.audio = ae::silence (1, sr, (std::int64_t) sr);
  339. for (int h = 0; h < 4; ++h)
  340. for (int i = 0; i < 2000; ++i)
  341. loop.audio.channels[0][(std::size_t) (h * sr / 4 + i)] = (float) (std::sin (i * 0.3) * std::exp (-i / 400.0));
  342. for (int h = 1; h < 4; ++h)
  343. ae::addMarker (loop, (std::int64_t) (h * sr / 4));
  344. ae::DrumStretchSettings ds;
  345. ds.ratio = 2.0;
  346. ae::drumloopStretch (loop, {}, ds);
  347. CHECK (std::abs (loop.audio.seconds() - 2.0) < 0.001);
  348. for (int h = 0; h < 4; ++h)
  349. {
  350. const auto at = (std::int64_t) (h * sr / 2);
  351. CHECK (std::abs (loop.audio.channels[0][(std::size_t) (at + 5)] - (float) (std::sin (5 * 0.3) * std::exp (-5 / 400.0))) < 0.05f);
  352. }
  353. CHECK (loop.markers.size() == 3u && std::abs (loop.markers[0].start - (std::int64_t) (sr / 2)) <= 1);
  354. // loop tuner: a loop over a sine whose end does not match its start gets a smooth seam
  355. auto t = sineDoc (100.0, 1.0, sr, 0.5f);
  356. ae::setLoop (t, { 20000, 30123 });
  357. ae::LoopTuneSettings lt;
  358. lt.blurOrder = 1;
  359. ae::tuneLoop (t, {}, lt);
  360. CHECK (t.hasLoop());
  361. const auto& x = t.audio.channels[0];
  362. const auto jump = std::abs (x[(std::size_t) t.loopEnd - 1] - x[(std::size_t) t.loopStart - 1]);
  363. CHECK (jump < 0.02f); // the end now leads where the audio before the start led
  364. }
  365. #ifndef LYNX_MINI_HARNESS // (needs core/EventEdit)
  366. #include "core/EventEdit.h"
  367. TEST (eventedit_to_automation_points_round165l)
  368. {
  369. lynx::PatternEvents lane;
  370. // a ramp of events (0, 0.1, ..., 0.9) every 24 ticks, then a hold
  371. for (int i = 0; i < 10; ++i)
  372. lane.events.push_back ({ i * 24, i * 0.1f });
  373. const auto exact = lynx::eventedit::toAutomationPoints (lane, 1.0f);
  374. CHECK (exact.size() == 19u); // a step (two points) per change, plus the first
  375. CHECK (exact.front().tick == 0 && exact.back().tick == 9 * 24);
  376. const auto simple = lynx::eventedit::toAutomationPoints (lane, 0.4f);
  377. CHECK (simple.size() < 6u);
  378. CHECK (std::abs (simple.back().value - 0.9f) < 1e-6f);
  379. }
  380. #endif
  381. TEST (audioedittools_paste_mix_envelope_and_spectral_round165l)
  382. {
  383. const double sr = 44100.0;
  384. // envelope: e = 1 at the start (all clipboard), 0 at the end (all original)
  385. auto d = sineDoc (440.0, 1.0, sr, 0.5f);
  386. const auto clip = sineDoc (2000.0, 1.0, sr, 0.5f).audio;
  387. ae::Envelope mix;
  388. mix.points = { { 0.0, 1.0, 0.0f }, { 1.0, 0.0, 0.0f } };
  389. const auto r = ae::pasteMixEnvelope (d, 0, clip, mix);
  390. CHECK (r.start == 0 && r.end == clip.frames());
  391. const auto n = d.audio.frames();
  392. const auto& x = d.audio.channels[0];
  393. CHECK (toneLevel (x, 0, 2205, 2000.0, sr) > 0.4 && toneLevel (x, 0, 2205, 440.0, sr) < 0.06);
  394. CHECK (toneLevel (x, n - 2205, n, 440.0, sr) > 0.4 && toneLevel (x, n - 2205, n, 2000.0, sr) < 0.06);
  395. // past the end: the document gets longer
  396. auto shortDoc = sineDoc (440.0, 0.5, sr, 0.5f);
  397. ae::pasteMixEnvelope (shortDoc, shortDoc.audio.frames() / 2, clip, ae::Envelope::flat (0.5));
  398. CHECK (shortDoc.audio.frames() == (std::int64_t) (0.25 * sr) + clip.frames());
  399. // spectral: the clipboard above ~1 kHz, the original below
  400. auto s = sineDoc (300.0, 1.0, sr, 0.5f);
  401. for (std::size_t i = 0; i < s.audio.channels[0].size(); ++i)
  402. for (auto& ch : s.audio.channels)
  403. ch[i] += 0.5f * (float) std::sin (2.0 * 3.141592653589793 * 3000.0 * (double) i / sr);
  404. ae::Document c2;
  405. c2.audio = ae::silence (s.audio.numChannels(), sr, s.audio.frames());
  406. for (std::size_t i = 0; i < c2.audio.channels[0].size(); ++i)
  407. for (auto& ch : c2.audio.channels)
  408. ch[i] = 0.5f * (float) std::sin (2.0 * 3.141592653589793 * 5000.0 * (double) i / sr);
  409. const auto k = ae::curvePosition (1000.0, 1.0, sr);
  410. ae::Envelope band;
  411. band.points = { { 0.0, 0.0, 0.0f }, { k - 0.02, 0.0, 0.0f }, { k + 0.02, 1.0, 0.0f }, { 1.0, 1.0, 0.0f } };
  412. ae::pasteMixSpectral (s, 0, c2.audio, band, 1.0);
  413. const auto& y = s.audio.channels[0];
  414. const auto a = (std::int64_t) (0.25 * sr), b = (std::int64_t) (0.75 * sr);
  415. CHECK (toneLevel (y, a, b, 300.0, sr) > 0.4); // the original's low tone stays
  416. CHECK (toneLevel (y, a, b, 3000.0, sr) < 0.05); // its high tone is replaced...
  417. CHECK (toneLevel (y, a, b, 5000.0, sr) > 0.4); // ...by the clipboard's
  418. }
  419. TEST (audioedittools_scratch_envelope_round165l)
  420. {
  421. auto d = rampDoc (1000);
  422. const auto original = d.audio.channels[0];
  423. ae::Envelope forwards;
  424. forwards.points = { { 0.0, 0.0, 0.0f }, { 1.0, 1.0, 0.0f } };
  425. ae::scratch (d, forwards);
  426. double err = 0.0;
  427. for (std::size_t i = 0; i < original.size(); ++i)
  428. err = std::max (err, (double) std::abs (d.audio.channels[0][i] - original[i]));
  429. CHECK (err < 0.01); // (the ramp goes up to 999)
  430. ae::Envelope backwards;
  431. backwards.points = { { 0.0, 1.0, 0.0f }, { 1.0, 0.0, 0.0f } };
  432. ae::scratch (d, backwards);
  433. CHECK (std::abs (d.audio.channels[0].front() - original.back()) < 0.01);
  434. CHECK (std::abs (d.audio.channels[0].back() - original.front()) < 0.01);
  435. // twice as fast: the average of each pair (no aliasing), then held at the end
  436. auto f = rampDoc (1000);
  437. ae::Envelope fast;
  438. fast.points = { { 0.0, 0.0, 0.0f }, { 0.5, 1.0, 0.0f }, { 1.0, 1.0, 0.0f } };
  439. ae::scratch (f, fast);
  440. CHECK (std::abs (f.audio.channels[0][900] - original.back()) < 0.01);
  441. }
  442. TEST (audioedittools_stretch_tool_formants_round165l)
  443. {
  444. const double sr = 44100.0;
  445. // stretch x1.5 with insert: longer; without insert: same length
  446. auto d = sineDoc (440.0, 1.0, sr, 0.5f);
  447. ae::StretchSettings s;
  448. s.lengthRatio = 1.5;
  449. ae::stretchTool (d, {}, s);
  450. CHECK (std::abs (d.audio.seconds() - 1.5) < 0.02);
  451. auto e = sineDoc (440.0, 1.0, sr, 0.5f);
  452. s.insert = false;
  453. ae::stretchTool (e, {}, s);
  454. CHECK (e.audio.frames() == (std::int64_t) sr);
  455. // resample: +12 st = half as long
  456. auto f = sineDoc (440.0, 1.0, sr, 0.5f);
  457. ae::StretchSettings rs;
  458. rs.resample = true;
  459. rs.semitones = 12.0;
  460. ae::stretchTool (f, {}, rs);
  461. CHECK (std::abs (f.audio.seconds() - 0.5) < 0.01);
  462. // formants: a buzz through a fixed resonance at 1.5 kHz, shifted up 5 st: with preservation the energy near
  463. // 1.5 kHz stays higher than at 1.5 kHz * 2^(5/12) (where the plain shift moves the resonance)
  464. ae::Document v;
  465. v.audio = ae::silence (1, sr, (std::int64_t) sr);
  466. {
  467. double y1 = 0.0, y2 = 0.0;
  468. const auto w = 2.0 * 3.141592653589793 * 1500.0 / sr, rr = 0.995;
  469. for (std::size_t i = 0; i < v.audio.channels[0].size(); ++i)
  470. {
  471. const auto x = i % 200 == 0 ? 1.0 : 0.0; // 220.5 Hz pulses
  472. const auto y = x + 2.0 * rr * std::cos (w) * y1 - rr * rr * y2;
  473. y2 = y1;
  474. y1 = y;
  475. v.audio.channels[0][i] = (float) (y * 0.02);
  476. }
  477. }
  478. auto plain = v, kept = v;
  479. ae::StretchSettings ps;
  480. ps.semitones = 5.0;
  481. ae::stretchTool (plain, {}, ps);
  482. ps.preserveFormants = true;
  483. ae::stretchTool (kept, {}, ps);
  484. auto band = [&] (const ae::Document& doc, double lo, double hi) {
  485. double e2 = 0.0;
  486. for (double hz = lo; hz <= hi; hz += 25.0)
  487. e2 += std::pow (toneLevel (doc.audio.channels[0], 8820, 35280, hz, sr), 2.0);
  488. return e2;
  489. };
  490. const auto shifted = 1500.0 * std::pow (2.0, 5.0 / 12.0);
  491. const auto plainRatio = band (plain, 1300, 1700) / std::max (1e-12, band (plain, shifted - 200, shifted + 200));
  492. const auto keptRatio = band (kept, 1300, 1700) / std::max (1e-12, band (kept, shifted - 200, shifted + 200));
  493. CHECK (keptRatio > 2.0 * plainRatio);
  494. }
  495. TEST (audioedittools_eq_phases_round165l)
  496. {
  497. // a flat +180° phase curve inverts the audio (magnitudes unchanged)
  498. auto d = sineDoc (1000.0, 0.5, 44100.0, 0.5f);
  499. const auto orig = d.audio.channels[0];
  500. ae::EqSettings s;
  501. s.gain = ae::Envelope::flat (0.5);
  502. s.smooth = false;
  503. s.phase = ae::Envelope::flat (1.0);
  504. ae::equalize (d, {}, s);
  505. double err = 0.0;
  506. for (std::size_t i = 4410; i + 4410 < orig.size(); ++i)
  507. err = std::max (err, (double) std::abs (d.audio.channels[0][i] + orig[i]));
  508. CHECK (err < 0.01);
  509. }