Edison190cdTests.inc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. // Round 190cd: the audio editor as a mixer-slot plugin (core/EdisonFx.h) — FL's Edison. The record modes' state
  2. // machine, the effect (pass-through, mute input, the take, playback), the session that keeps a slot's audio, the WAV
  3. // file of a project and FL's saved state. The project file's side is in LYNX --edisonslottest (engine/ProjectFile.cpp).
  4. namespace edison190cd
  5. {
  6. namespace ef = lynx::edisonfx;
  7. namespace ae = lynx::audioedit;
  8. inline lynx::EffectParams defaults()
  9. {
  10. return lynx::makeEffectSlot (lynx::EffectKind::edison).params;
  11. }
  12. // `blocks` blocks of `n` frames of a constant `level` through the processor (returns the last block's left output)
  13. inline std::vector<float> run (ef::Processor& p, const lynx::EffectParams& params, float level, int blocks, int n = 441)
  14. {
  15. std::vector<float> l, r;
  16. for (int b = 0; b < blocks; ++b)
  17. {
  18. l.assign ((std::size_t) n, level);
  19. r.assign ((std::size_t) n, -level);
  20. p.process (l.data(), r.data(), n, params, 120.0);
  21. }
  22. return l;
  23. }
  24. inline ae::Document ramp (int frames, double rate = 44100.0)
  25. {
  26. ae::Document d;
  27. d.audio.sampleRate = rate;
  28. d.audio.channels.assign (2, std::vector<float> ((std::size_t) frames));
  29. for (int i = 0; i < frames; ++i)
  30. {
  31. d.audio.channels[0][(std::size_t) i] = (float) (i % 200) / 400.0f;
  32. d.audio.channels[1][(std::size_t) i] = -(float) (i % 300) / 600.0f;
  33. }
  34. return d;
  35. }
  36. inline void put32 (std::vector<std::uint8_t>& b, std::uint32_t v)
  37. {
  38. for (int i = 0; i < 4; ++i)
  39. b.push_back ((std::uint8_t) (v >> (8 * i)));
  40. }
  41. inline void putDouble (std::vector<std::uint8_t>& b, double v)
  42. {
  43. std::uint8_t raw[8];
  44. std::memcpy (raw, &v, 8);
  45. b.insert (b.end(), raw, raw + 8);
  46. }
  47. // A state in the layout FL writes for the plugin (REA round 190cd): version, 12 bytes, 64-bit size + WAV image, an
  48. // empty name, selection / view / noise threshold, (envelopes), the 20 bytes of switches.
  49. inline std::vector<std::uint8_t> flState (const std::vector<std::uint8_t>& wav, int mode, bool newProject, bool loop, bool mute,
  50. int maxTime, bool slave, bool ignoreOwn, float threshold)
  51. {
  52. std::vector<std::uint8_t> b;
  53. put32 (b, 0x000d0003u);
  54. b.insert (b.end(), 12, 0);
  55. put32 (b, (std::uint32_t) wav.size());
  56. put32 (b, 0);
  57. b.insert (b.end(), wav.begin(), wav.end());
  58. b.push_back (0); // name
  59. putDouble (b, -1.0);
  60. putDouble (b, -1.0);
  61. putDouble (b, 0.0);
  62. putDouble (b, 1.0);
  63. std::uint8_t t[4];
  64. std::memcpy (t, &threshold, 4);
  65. b.insert (b.end(), t, t + 4);
  66. b.insert (b.end(), 40, 0); // (what lies between is not read)
  67. put32 (b, (std::uint32_t) mode);
  68. b.push_back (newProject);
  69. b.push_back (loop);
  70. b.push_back (mute);
  71. b.push_back (0);
  72. put32 (b, (std::uint32_t) maxTime);
  73. put32 (b, 0);
  74. b.push_back (slave);
  75. b.push_back (0);
  76. b.push_back (ignoreOwn);
  77. b.push_back (0);
  78. return b;
  79. }
  80. } // namespace edison190cd
  81. TEST (edison190cd_the_plugin_is_a_slot_kind_with_fl_defaults)
  82. {
  83. namespace ef = lynx::edisonfx;
  84. const auto slot = lynx::makeEffectSlot (lynx::EffectKind::edison);
  85. CHECK (slot.kind == lynx::EffectKind::edison);
  86. // FL: the mode list opens on "On input", Append is on, the longest recording is 30 minutes (the sixth choice; live FL
  87. // check 190cq, shot edison190cd-05e), threshold 0.02
  88. CHECK (ef::modeOf (slot.params) == ef::Mode::onInput);
  89. CHECK (slot.params[ef::append] >= 0.5f);
  90. CHECK (slot.params[ef::muteInput] < 0.5f);
  91. CHECK (slot.params[ef::slave] < 0.5f);
  92. CHECK (ef::maxTimeIndexOf (slot.params) == 5);
  93. CHECK (ef::maxMinutes[5] == 30);
  94. CHECK (std::abs (slot.params[ef::noiseThreshold] - 0.02f) < 1.0e-6f);
  95. CHECK (ef::maxFramesOf (slot.params, 48000.0) == 30 * 60 * 48000);
  96. auto forever = slot.params;
  97. forever[ef::maxTime] = 0.0f;
  98. CHECK (ef::maxFramesOf (forever, 48000.0) == 0);
  99. CHECK (std::string (ef::modeName (ef::Mode::now)) == "Now");
  100. CHECK (std::string (ef::modeName (ef::Mode::onInput)) == "On input");
  101. CHECK (std::string (ef::modeName (ef::Mode::input)) == "Input");
  102. CHECK (std::string (ef::modeName (ef::Mode::onPlay)) == "On play");
  103. for (int m = 0; m < ef::modeCount; ++m)
  104. {
  105. auto p = slot.params;
  106. p[ef::recordMode] = ef::modeValue ((ef::Mode) m);
  107. CHECK (ef::modeOf (p) == (ef::Mode) m);
  108. }
  109. }
  110. TEST (edison190cd_gate_now_writes_every_block)
  111. {
  112. namespace ef = lynx::edisonfx;
  113. ef::Gate g;
  114. g.prepare (44100.0);
  115. CHECK (g.phase() == ef::Phase::off);
  116. CHECK (! g.block (1.0f, 512, true, false, 0.02f).write); // not armed: nothing
  117. g.arm (ef::Mode::now);
  118. CHECK (g.phase() == ef::Phase::recording);
  119. for (int i = 0; i < 4; ++i)
  120. {
  121. const auto d = g.block (0.0f, 512, false, false, 0.02f); // silence, song stopped: still written
  122. CHECK (d.write && ! d.preRoll && ! d.jumpMarker);
  123. }
  124. g.disarm();
  125. CHECK (g.phase() == ef::Phase::off);
  126. CHECK (! g.block (1.0f, 512, true, false, 0.02f).write);
  127. }
  128. TEST (edison190cd_gate_on_input_starts_with_the_first_sound_and_never_stops)
  129. {
  130. namespace ef = lynx::edisonfx;
  131. ef::Gate g;
  132. g.prepare (44100.0);
  133. CHECK (g.holdFrames() == 44100); // 1 s
  134. CHECK (g.preRollFrames() == 4410); // 0.1 s
  135. g.arm (ef::Mode::onInput);
  136. CHECK (g.phase() == ef::Phase::waiting);
  137. CHECK (! g.block (0.01f, 512, true, false, 0.02f).write); // below the threshold
  138. CHECK (! g.block (0.02f, 512, true, false, 0.02f).write); // at it: not above
  139. CHECK (g.phase() == ef::Phase::waiting);
  140. const auto first = g.block (0.5f, 512, false, false, 0.02f);
  141. CHECK (first.write && first.preRoll);
  142. CHECK (g.phase() == ef::Phase::recording);
  143. // minutes of silence later it still records (only "Input" runs out)
  144. for (int i = 0; i < 20000; ++i)
  145. {
  146. const auto d = g.block (0.0f, 512, false, false, 0.02f);
  147. if (! d.write || d.preRoll)
  148. {
  149. CHECK (false);
  150. break;
  151. }
  152. }
  153. CHECK (g.phase() == ef::Phase::recording);
  154. }
  155. TEST (edison190cd_gate_input_pauses_after_the_hold_and_resumes_with_pre_roll)
  156. {
  157. namespace ef = lynx::edisonfx;
  158. ef::Gate g;
  159. g.prepare (48000.0);
  160. CHECK (g.holdFrames() == 48000);
  161. g.arm (ef::Mode::input);
  162. CHECK (! g.block (0.0f, 480, false, false, 0.02f).write);
  163. auto d = g.block (0.3f, 480, false, false, 0.02f);
  164. CHECK (d.write && d.preRoll);
  165. d = g.block (0.3f, 480, false, false, 0.02f);
  166. CHECK (d.write && ! d.preRoll); // going on: no second pre-roll
  167. // silence: 1 s of hold = 100 blocks of 480; the 99 first are still written, the 100th is not
  168. int writtenBlocks = 0;
  169. for (int i = 0; i < 100; ++i)
  170. writtenBlocks += g.block (0.0f, 480, false, false, 0.02f).write ? 1 : 0;
  171. CHECK (writtenBlocks == 99);
  172. CHECK (g.phase() == ef::Phase::waiting);
  173. CHECK (! g.block (0.0f, 480, false, false, 0.02f).write);
  174. d = g.block (0.3f, 480, false, false, 0.02f);
  175. CHECK (d.write && d.preRoll); // heard again: a new piece with its pre-roll
  176. // a sound inside the hold starts the hold again
  177. for (int i = 0; i < 50; ++i)
  178. g.block (0.0f, 480, false, false, 0.02f);
  179. g.block (0.3f, 480, false, false, 0.02f);
  180. writtenBlocks = 0;
  181. for (int i = 0; i < 100; ++i)
  182. writtenBlocks += g.block (0.0f, 480, false, false, 0.02f).write ? 1 : 0;
  183. CHECK (writtenBlocks == 99);
  184. // the threshold is the plugin's own
  185. CHECK (! g.block (0.3f, 480, false, false, 0.5f).write);
  186. CHECK (g.block (0.6f, 480, false, false, 0.5f).write);
  187. }
  188. TEST (edison190cd_gate_on_play_follows_the_transport_and_marks_song_jumps)
  189. {
  190. namespace ef = lynx::edisonfx;
  191. ef::Gate g;
  192. g.prepare (44100.0);
  193. g.arm (ef::Mode::onPlay);
  194. CHECK (g.phase() == ef::Phase::waiting);
  195. CHECK (! g.block (1.0f, 512, false, false, 0.02f).write); // loud but stopped
  196. auto d = g.block (0.0f, 512, true, false, 0.02f);
  197. CHECK (d.write && d.jumpMarker && ! d.preRoll); // silent but playing; FL marks where the take starts (round 190ct)
  198. CHECK (g.phase() == ef::Phase::recording);
  199. d = g.block (0.0f, 512, true, false, 0.02f);
  200. CHECK (d.write && ! d.jumpMarker);
  201. d = g.block (0.0f, 512, true, true, 0.02f);
  202. CHECK (d.write && d.jumpMarker); // the song jumped (loop, click on the timeline)
  203. d = g.block (0.0f, 512, true, false, 0.02f);
  204. CHECK (d.write && ! d.jumpMarker); // one marker per jump
  205. CHECK (! g.block (0.0f, 512, false, false, 0.02f).write); // stop pauses
  206. CHECK (g.phase() == ef::Phase::waiting);
  207. d = g.block (0.0f, 512, true, false, 0.02f);
  208. CHECK (d.write && d.jumpMarker); // the song starts again: a marker where it resumes
  209. }
  210. TEST (edison190cd_idle_passes_the_audio_through_bit_exact)
  211. {
  212. namespace ef = lynx::edisonfx;
  213. ef::Processor p;
  214. p.prepare (48000.0);
  215. const auto params = edison190cd::defaults();
  216. std::uint32_t seed = 11;
  217. bool same = true;
  218. for (int block = 0; block < 60; ++block)
  219. {
  220. const auto n = 1 + (int) (seed % 700u);
  221. std::vector<float> l ((std::size_t) n), r ((std::size_t) n);
  222. for (int i = 0; i < n; ++i)
  223. {
  224. seed = seed * 1664525u + 1013904223u;
  225. l[(std::size_t) i] = (float) (seed >> 8) / 8388608.0f - 1.0f;
  226. seed = seed * 1664525u + 1013904223u;
  227. r[(std::size_t) i] = ((float) (seed >> 8) / 8388608.0f - 1.0f) * 1.0e-30f; // (denormal-sized values too)
  228. }
  229. const auto inL = l, inR = r;
  230. p.setSongPosition (block * 37.0);
  231. p.setPlaying (block % 3 == 0);
  232. p.process (l.data(), r.data(), n, params, 128.0);
  233. same = same && std::memcmp (l.data(), inL.data(), (std::size_t) n * sizeof (float)) == 0
  234. && std::memcmp (r.data(), inR.data(), (std::size_t) n * sizeof (float)) == 0;
  235. }
  236. CHECK (same);
  237. CHECK (p.phase() == ef::Phase::off);
  238. CHECK (p.playPosition() < 0.0);
  239. // …and while it records too (recording only reads the input)
  240. p.setRecording (true);
  241. auto now = params;
  242. now[ef::recordMode] = ef::modeValue (ef::Mode::now);
  243. std::vector<float> l (256, 0.25f), r (256, -0.5f);
  244. p.process (l.data(), r.data(), 256, now, 128.0);
  245. CHECK (l[0] == 0.25f && l[255] == 0.25f && r[0] == -0.5f && r[255] == -0.5f);
  246. CHECK (p.phase() == ef::Phase::recording);
  247. p.setRecording (false);
  248. }
  249. TEST (edison190cd_mute_input_silences_the_slot_but_still_records_and_plays)
  250. {
  251. namespace ef = lynx::edisonfx;
  252. namespace ae = lynx::audioedit;
  253. ef::Processor p;
  254. p.prepare (44100.0);
  255. auto params = edison190cd::defaults();
  256. params[ef::muteInput] = 1.0f;
  257. params[ef::recordMode] = ef::modeValue (ef::Mode::now);
  258. auto out = edison190cd::run (p, params, 0.5f, 1);
  259. CHECK (out[0] == 0.0f && out[440] == 0.0f);
  260. p.setRecording (true);
  261. out = edison190cd::run (p, params, 0.5f, 3);
  262. CHECK (out[0] == 0.0f);
  263. std::vector<float> l, r;
  264. std::vector<std::int64_t> jumps;
  265. CHECK (p.drain (l, r, jumps, 0) == 3 * 441);
  266. CHECK (l.size() == 3 * 441 && l[0] == 0.5f && r[0] == -0.5f && l.back() == 0.5f);
  267. p.setRecording (false);
  268. // playback is added after the mute: the clip is heard alone
  269. const auto doc = std::make_shared<const ae::Document> (edison190cd::ramp (1000));
  270. p.play (std::shared_ptr<const ae::Audio> (doc, &doc->audio), false);
  271. std::vector<float> pl (441, 0.5f), pr (441, 0.5f);
  272. p.process (pl.data(), pr.data(), 441, params, 120.0);
  273. bool same = true;
  274. for (int i = 0; i < 441; ++i)
  275. same = same && pl[(std::size_t) i] == doc->audio.channels[0][(std::size_t) i] && pr[(std::size_t) i] == doc->audio.channels[1][(std::size_t) i];
  276. CHECK (same);
  277. CHECK (std::abs (p.playPosition() - 441.0) < 1.0e-9);
  278. // unmuted: input + clip
  279. params[ef::muteInput] = 0.0f;
  280. std::fill (pl.begin(), pl.end(), 0.5f);
  281. std::fill (pr.begin(), pr.end(), 0.5f);
  282. p.process (pl.data(), pr.data(), 441, params, 120.0);
  283. CHECK (std::abs (pl[0] - (0.5f + doc->audio.channels[0][441])) < 1.0e-7f);
  284. // the clip ends: silence added, the position gone
  285. edison190cd::run (p, params, 0.0f, 3);
  286. CHECK (p.playPosition() < 0.0);
  287. out = edison190cd::run (p, params, 0.125f, 1);
  288. CHECK (out[0] == 0.125f && out[440] == 0.125f);
  289. // a looped clip goes round
  290. p.play (std::shared_ptr<const ae::Audio> (doc, &doc->audio), true);
  291. edison190cd::run (p, params, 0.0f, 5);
  292. CHECK (p.playPosition() >= 0.0 && p.playPosition() < 1000.0);
  293. p.play (nullptr, false);
  294. out = edison190cd::run (p, params, 0.125f, 1);
  295. CHECK (out[0] == 0.125f);
  296. }
  297. TEST (edison190cd_on_input_keeps_the_pre_roll_and_the_take_is_the_input)
  298. {
  299. namespace ef = lynx::edisonfx;
  300. ef::Processor p;
  301. p.prepare (44100.0);
  302. const auto params = edison190cd::defaults(); // On input, threshold 0.02
  303. p.setRecording (true);
  304. edison190cd::run (p, params, 0.001f, 30); // 0.3 s of near-silence: waiting
  305. CHECK (p.phase() == ef::Phase::waiting);
  306. std::vector<float> l, r;
  307. std::vector<std::int64_t> jumps;
  308. CHECK (p.drain (l, r, jumps, 0) == 0);
  309. edison190cd::run (p, params, 0.5f, 2);
  310. CHECK (p.phase() == ef::Phase::recording);
  311. edison190cd::run (p, params, 0.0f, 4); // silence does not stop it
  312. CHECK (p.phase() == ef::Phase::recording);
  313. p.drain (l, r, jumps, 0);
  314. // 0.1 s of pre-roll (4410 frames of the near-silence), the two loud blocks, the four silent ones
  315. CHECK (l.size() == 4410 + 6 * 441);
  316. CHECK (l[0] == 0.001f && l[4409] == 0.001f && l[4410] == 0.5f && r[4410] == -0.5f);
  317. CHECK (l[4410 + 2 * 441] == 0.0f);
  318. CHECK (jumps.empty());
  319. CHECK (p.droppedFrames() == 0);
  320. p.setRecording (false);
  321. CHECK (p.phase() == ef::Phase::off);
  322. }
  323. TEST (edison190cd_on_play_records_only_while_the_song_plays_and_notes_jumps)
  324. {
  325. namespace ef = lynx::edisonfx;
  326. ef::Processor p;
  327. p.prepare (44100.0);
  328. auto params = edison190cd::defaults();
  329. params[ef::recordMode] = ef::modeValue (ef::Mode::onPlay);
  330. p.setRecording (true);
  331. const auto ticksPerBlock = 441.0 / 44100.0 * (120.0 / 60.0) * (double) lynx::ppq;
  332. double tick = 0.0;
  333. auto block = [&] (bool playing, float level) {
  334. p.setSongPosition (tick);
  335. p.setPlaying (playing);
  336. edison190cd::run (p, params, level, 1);
  337. if (playing)
  338. tick += ticksPerBlock;
  339. };
  340. for (int i = 0; i < 5; ++i)
  341. block (false, 0.5f);
  342. CHECK (p.phase() == ef::Phase::waiting);
  343. for (int i = 0; i < 10; ++i)
  344. block (true, 0.25f);
  345. CHECK (p.phase() == ef::Phase::recording);
  346. tick = 4.0 * lynx::ppq * 8; // the song jumps eight bars on
  347. for (int i = 0; i < 10; ++i)
  348. block (true, 0.75f);
  349. for (int i = 0; i < 5; ++i)
  350. block (false, 0.5f);
  351. CHECK (p.phase() == ef::Phase::waiting);
  352. std::vector<float> l, r;
  353. std::vector<std::int64_t> jumps;
  354. p.drain (l, r, jumps, 0);
  355. CHECK (l.size() == 20 * 441);
  356. CHECK (l[0] == 0.25f && l[10 * 441 - 1] == 0.25f && l[10 * 441] == 0.75f);
  357. CHECK (jumps.size() == 2); // the take's start (FL's "Song jump" at 0, round 190ct) and the jump
  358. if (jumps.size() == 2)
  359. CHECK (jumps[0] == 0 && jumps[1] == 10 * 441);
  360. p.setRecording (false);
  361. }
  362. TEST (edison190cd_the_maximum_recording_time_ends_the_take)
  363. {
  364. namespace ef = lynx::edisonfx;
  365. ef::Processor p;
  366. p.prepare (1000.0); // (a low rate: one minute is 60000 frames)
  367. auto params = edison190cd::defaults();
  368. params[ef::recordMode] = ef::modeValue (ef::Mode::now);
  369. params[ef::maxTime] = 1.0f / 6.0f; // 1 minute
  370. CHECK (ef::maxFramesOf (params, 1000.0) == 60000);
  371. p.setRecording (true);
  372. edison190cd::run (p, params, 0.5f, 59, 1000);
  373. CHECK (p.isRecording() && ! p.reachedLimit());
  374. edison190cd::run (p, params, 0.5f, 3, 1000);
  375. CHECK (! p.isRecording());
  376. CHECK (p.reachedLimit());
  377. std::vector<float> l, r;
  378. std::vector<std::int64_t> jumps;
  379. p.drain (l, r, jumps, 0);
  380. CHECK (l.size() == 60000);
  381. // forever: goes on
  382. params[ef::maxTime] = 0.0f;
  383. p.setRecording (true);
  384. CHECK (! p.reachedLimit());
  385. edison190cd::run (p, params, 0.5f, 70, 1000);
  386. CHECK (p.isRecording());
  387. p.setRecording (false);
  388. }
  389. TEST (edison190cd_a_take_is_appended_or_replaces_the_audio)
  390. {
  391. namespace ef = lynx::edisonfx;
  392. namespace ae = lynx::audioedit;
  393. auto base = edison190cd::ramp (1000);
  394. base.title = "Take";
  395. ae::addMarker (base, 100, "here");
  396. ae::Audio take;
  397. take.sampleRate = 44100.0;
  398. take.channels.assign (2, std::vector<float> (500, 0.5f));
  399. const auto appended = ef::withTake (base, take, { 250 }, true);
  400. CHECK (appended.audio.frames() == 1500);
  401. CHECK (appended.audio.channels[0][999] == base.audio.channels[0][999]);
  402. CHECK (appended.audio.channels[0][1000] == 0.5f && appended.audio.channels[1][1499] == 0.5f);
  403. CHECK (appended.markers.size() == 2);
  404. if (appended.markers.size() == 2)
  405. {
  406. CHECK (appended.markers[0].start == 100 && appended.markers[0].name == "here");
  407. CHECK (appended.markers[1].start == 1250 && appended.markers[1].name == "Song jump");
  408. }
  409. CHECK (appended.title == "Take");
  410. const auto replaced = ef::withTake (base, take, { 250 }, false);
  411. CHECK (replaced.audio.frames() == 500);
  412. CHECK (replaced.audio.channels[0][0] == 0.5f);
  413. CHECK (replaced.markers.size() == 1 && replaced.markers[0].start == 250);
  414. // nothing there yet: Append makes the take the audio
  415. ae::Document empty;
  416. const auto first = ef::withTake (empty, take, {}, true);
  417. CHECK (first.audio.frames() == 500 && first.audio.sampleRate == 44100.0 && first.markers.empty());
  418. // a take at another rate is converted to the audio's rate, its markers with it
  419. ae::Audio half = take;
  420. half.sampleRate = 22050.0;
  421. const auto mixed = ef::withTake (base, half, { 250 }, true);
  422. CHECK (mixed.audio.sampleRate == 44100.0);
  423. CHECK (std::abs ((int) mixed.audio.frames() - 2000) <= 2);
  424. CHECK (mixed.markers.size() == 2 && mixed.markers[1].start == 1500);
  425. }
  426. TEST (edison190cd_a_session_records_through_the_slot_effect_with_the_window_closed)
  427. {
  428. namespace ef = lynx::edisonfx;
  429. namespace ae = lynx::audioedit;
  430. auto slot = lynx::makeEffectSlot (lynx::EffectKind::edison);
  431. slot.params[ef::recordMode] = ef::modeValue (ef::Mode::now);
  432. const auto fx = ef::make (slot, 44100.0);
  433. CHECK (fx != nullptr);
  434. const auto p = std::dynamic_pointer_cast<ef::Processor> (fx);
  435. CHECK (p != nullptr);
  436. auto session = ef::findSession (slot.instance.id);
  437. CHECK (session != nullptr);
  438. if (p == nullptr || session == nullptr)
  439. return;
  440. CHECK (ef::sessionFor (slot) == session); // one per slot instance
  441. CHECK (session->document().audio.empty() && ! session->dirty() && session->path().empty());
  442. const auto revision = session->revision();
  443. CHECK (session->start (p, true));
  444. CHECK (session->recording());
  445. edison190cd::run (*p, slot.params, 0.5f, 10);
  446. CHECK (session->pump());
  447. CHECK (session->takeFrames() == 4410);
  448. CHECK (session->preview().audio.frames() == 4410);
  449. CHECK (session->document().audio.empty()); // the document changes when the take ends
  450. edison190cd::run (*p, slot.params, 0.25f, 10);
  451. session->stop();
  452. CHECK (! session->recording() && ! p->isRecording());
  453. CHECK (session->document().audio.frames() == 8820);
  454. CHECK (session->document().audio.channels[0][0] == 0.5f && session->document().audio.channels[0][8819] == 0.25f);
  455. CHECK (session->dirty());
  456. CHECK (session->revision() != revision);
  457. // a second take with Append: after the first; without: instead of it
  458. CHECK (session->start (p, true));
  459. CHECK (session->takeStart() == 8820);
  460. edison190cd::run (*p, slot.params, 0.75f, 5);
  461. session->stop();
  462. CHECK (session->document().audio.frames() == 8820 + 2205);
  463. CHECK (session->document().audio.channels[0][8820] == 0.75f);
  464. CHECK (session->start (p, false));
  465. CHECK (session->takeStart() == 0);
  466. edison190cd::run (*p, slot.params, 0.125f, 5);
  467. session->stop();
  468. CHECK (session->document().audio.frames() == 2205);
  469. CHECK (session->document().audio.channels[0][0] == 0.125f);
  470. // a recording with nothing in it leaves the audio alone
  471. CHECK (session->start (p, false));
  472. session->stop();
  473. CHECK (session->document().audio.frames() == 2205);
  474. // a copy of the song (same instance) finds the same audio; a duplicate slot is a new, empty one
  475. auto copy = slot;
  476. CHECK (ef::sessionFor (copy) == session);
  477. copy.instance.renew();
  478. CHECK (ef::findSession (copy.instance.id) == nullptr);
  479. // the session lives as long as the effect or a window holds it
  480. const auto id = slot.instance.id;
  481. session.reset();
  482. CHECK (ef::findSession (id) != nullptr);
  483. }
  484. TEST (edison190cd_slave_playback_plays_the_content_at_the_song_time)
  485. {
  486. namespace ef = lynx::edisonfx;
  487. namespace ae = lynx::audioedit;
  488. auto slot = lynx::makeEffectSlot (lynx::EffectKind::edison);
  489. slot.params[ef::slave] = 1.0f;
  490. const auto fx = ef::make (slot, 44100.0);
  491. const auto p = std::dynamic_pointer_cast<ef::Processor> (fx);
  492. const auto session = ef::findSession (slot.instance.id);
  493. CHECK (p != nullptr && session != nullptr);
  494. if (p == nullptr || session == nullptr)
  495. return;
  496. const auto doc = edison190cd::ramp (44100);
  497. session->setDocument (doc);
  498. std::vector<float> l (441, 0.0f), r (441, 0.0f);
  499. // stopped: nothing is added
  500. p->setPlaying (false);
  501. p->setSongPosition (0.0);
  502. p->process (l.data(), r.data(), 441, slot.params, 120.0);
  503. CHECK (l[5] == 0.0f);
  504. // playing from the start of the song: the content from its first frame
  505. p->setPlaying (true);
  506. p->process (l.data(), r.data(), 441, slot.params, 120.0);
  507. bool same = true;
  508. for (int i = 0; i < 441; ++i)
  509. same = same && l[(std::size_t) i] == doc.audio.channels[0][(std::size_t) i];
  510. CHECK (same);
  511. // the song jumps to 0.5 s (one beat at 120): the content follows
  512. std::fill (l.begin(), l.end(), 0.0f);
  513. std::fill (r.begin(), r.end(), 0.0f);
  514. p->setSongPosition ((double) lynx::ppq);
  515. p->process (l.data(), r.data(), 441, slot.params, 120.0);
  516. CHECK (std::abs (l[0] - doc.audio.channels[0][22050]) < 1.0e-6f);
  517. CHECK (std::abs (r[440] - doc.audio.channels[1][22050 + 440]) < 1.0e-6f);
  518. // with a selection the content starts at it (FL: song time + selection start); "ignore own selection" undoes that
  519. session->setSlaveOffset (1000);
  520. std::fill (l.begin(), l.end(), 0.0f);
  521. p->setSongPosition (0.0);
  522. p->process (l.data(), r.data(), 441, slot.params, 120.0);
  523. CHECK (std::abs (l[0] - doc.audio.channels[0][1000]) < 1.0e-6f);
  524. auto ignoring = slot.params;
  525. ignoring[ef::ignoreOwnSelection] = 1.0f;
  526. std::fill (l.begin(), l.end(), 0.0f);
  527. p->setSongPosition (0.0);
  528. p->process (l.data(), r.data(), 441, ignoring, 120.0);
  529. CHECK (std::abs (l[0] - doc.audio.channels[0][0]) < 1.0e-6f);
  530. CHECK (std::abs (l[7] - doc.audio.channels[0][7]) < 1.0e-6f);
  531. // past the end of the content: silence
  532. std::fill (l.begin(), l.end(), 0.0f);
  533. p->setSongPosition (16.0 * lynx::ppq);
  534. p->process (l.data(), r.data(), 441, slot.params, 120.0);
  535. CHECK (l[0] == 0.0f && l[440] == 0.0f);
  536. // Slave off: the song plays, the content does not
  537. auto off = slot.params;
  538. off[ef::slave] = 0.0f;
  539. p->setSongPosition (0.0);
  540. p->process (l.data(), r.data(), 441, off, 120.0);
  541. CHECK (l[0] == 0.0f && l[440] == 0.0f);
  542. }
  543. TEST (edison190cd_the_audio_of_a_slot_round_trips_through_its_project_file)
  544. {
  545. namespace ef = lynx::edisonfx;
  546. namespace ae = lynx::audioedit;
  547. const auto path = (std::filesystem::temp_directory_path() / "lynx-edison190cd-take.wav").string();
  548. std::remove (path.c_str());
  549. auto slot = lynx::makeEffectSlot (lynx::EffectKind::edison);
  550. {
  551. const auto session = ef::sessionFor (slot);
  552. auto doc = edison190cd::ramp (5000, 48000.0);
  553. ae::addMarker (doc, 1200, "verse");
  554. ae::addMarker (doc, 3000);
  555. ae::setLoop (doc, { 500, 4000 });
  556. session->setDocument (doc);
  557. CHECK (session->dirty());
  558. CHECK (session->save (path));
  559. CHECK (! session->dirty() && session->path() == path);
  560. slot.samplePath = path;
  561. }
  562. CHECK (ef::findSession (slot.instance.id) == nullptr); // nothing holds it any more
  563. // "the project is opened again": a new slot instance with the saved path
  564. auto loaded = slot;
  565. loaded.instance.renew();
  566. const auto fx = ef::make (loaded, 44100.0);
  567. const auto session = ef::findSession (loaded.instance.id);
  568. CHECK (session != nullptr);
  569. if (session == nullptr)
  570. return;
  571. const auto want = edison190cd::ramp (5000, 48000.0);
  572. const auto& got = session->document();
  573. CHECK (got.audio.sampleRate == 48000.0 && got.audio.numChannels() == 2 && got.audio.frames() == 5000);
  574. CHECK (got.audio.channels == want.audio.channels); // 32-bit float: bit for bit
  575. CHECK (got.markers.size() == 2);
  576. if (got.markers.size() == 2)
  577. CHECK (got.markers[0].start == 1200 && got.markers[0].name == "verse" && got.markers[1].start == 3000);
  578. CHECK (got.loopStart == 500 && got.loopEnd == 4000);
  579. CHECK (! session->dirty() && session->path() == path);
  580. // the path changes under a running effect (a song undo past the recording, a relocation): an editor that holds
  581. // audio keeps it; an empty one reads what the slot names
  582. const auto other = (std::filesystem::temp_directory_path() / "lynx-edison190cd-other.wav").string();
  583. CHECK (ef::writeWavFile (edison190cd::ramp (700), other));
  584. loaded.samplePath = other;
  585. ef::followPath (loaded);
  586. CHECK (session->document().audio.frames() == 5000 && session->path() == path);
  587. loaded.samplePath.clear();
  588. ef::followPath (loaded);
  589. CHECK (session->document().audio.frames() == 5000);
  590. session->setDocument ({});
  591. CHECK (session->save ({}));
  592. loaded.samplePath = other;
  593. ef::followPath (loaded);
  594. CHECK (session->document().audio.frames() == 700 && session->path() == other && ! session->dirty());
  595. loaded.samplePath = (std::filesystem::temp_directory_path() / "lynx-edison190cd-missing.wav").string();
  596. ef::followPath (loaded);
  597. CHECK (session->document().audio.frames() == 700);
  598. // an emptied editor forgets its file
  599. session->setDocument ({});
  600. CHECK (session->save (path));
  601. CHECK (session->path().empty() && ! session->dirty());
  602. std::remove (path.c_str());
  603. std::remove (other.c_str());
  604. }
  605. TEST (edison190cd_fl_state_keeps_the_slot_as_the_plugin_with_settings_and_audio)
  606. {
  607. namespace ef = lynx::edisonfx;
  608. namespace ae = lynx::audioedit;
  609. auto source = edison190cd::ramp (3000);
  610. ae::addMarker (source, 1500, "drop");
  611. const auto wav = ae::encodeWav (source, ae::WavFormat::float32);
  612. const auto state = edison190cd::flState (wav, 3, true, true, true, 5, true, true, 0.05f);
  613. const auto parsed = ef::parseFlState (state);
  614. CHECK (parsed.has_value());
  615. if (! parsed)
  616. return;
  617. CHECK (parsed->version == 13);
  618. CHECK (parsed->mode == ef::Mode::onPlay);
  619. CHECK (parsed->newProject && parsed->loop && parsed->muteInput && parsed->slave && parsed->ignoreOwnSelection);
  620. CHECK (parsed->maxTime == 5);
  621. CHECK (std::abs (parsed->noiseThreshold - 0.05f) < 1.0e-7f);
  622. CHECK (parsed->wavSize == wav.size());
  623. const auto slot = lynx::effectFromFl ("Edison", state, 140.0);
  624. CHECK (slot.has_value());
  625. if (! slot)
  626. return;
  627. CHECK (slot->kind == lynx::EffectKind::edison);
  628. CHECK (ef::modeOf (slot->params) == ef::Mode::onPlay);
  629. CHECK (slot->params[ef::append] < 0.5f);
  630. CHECK (slot->params[ef::loopPlay] >= 0.5f);
  631. CHECK (slot->params[ef::muteInput] >= 0.5f);
  632. CHECK (slot->params[ef::slave] >= 0.5f);
  633. CHECK (slot->params[ef::ignoreOwnSelection] >= 0.5f);
  634. CHECK (ef::maxTimeIndexOf (slot->params) == 5);
  635. CHECK (std::abs (slot->params[ef::noiseThreshold] - 0.05f) < 1.0e-7f);
  636. CHECK (slot->state == state); // FL's bytes stay with the slot until its audio changes
  637. CHECK (slot->samplePath.empty());
  638. // the audio in the state is the slot's content
  639. const auto fx = ef::make (*slot, 44100.0);
  640. const auto session = ef::findSession (slot->instance.id);
  641. CHECK (session != nullptr);
  642. if (session != nullptr)
  643. {
  644. CHECK (session->document().audio.frames() == 3000);
  645. CHECK (session->document().audio.channels == source.audio.channels);
  646. CHECK (session->document().markers.size() == 1);
  647. CHECK (! session->dirty());
  648. }
  649. // FL's defaults (an empty plugin as FL saves it): On input, Append on, 30 minutes (index 5)
  650. const auto fresh = lynx::effectFromFl ("Edison", edison190cd::flState ({}, 1, false, false, false, 5, false, false, 0.02f), 140.0);
  651. CHECK (fresh.has_value());
  652. if (fresh)
  653. {
  654. const auto plain = lynx::makeEffectSlot (lynx::EffectKind::edison);
  655. CHECK (fresh->params == plain.params);
  656. const auto fx2 = ef::make (*fresh, 44100.0);
  657. const auto s2 = ef::findSession (fresh->instance.id);
  658. CHECK (s2 != nullptr && s2->document().audio.empty());
  659. }
  660. // garbage is not a reason to lose the slot
  661. CHECK (! ef::parseFlState ({}).has_value());
  662. const auto broken = lynx::effectFromFl ("Edison", { 1, 2, 3 }, 140.0);
  663. CHECK (broken.has_value() && broken->kind == lynx::EffectKind::edison);
  664. std::vector<std::uint8_t> cut (state.begin(), state.begin() + 40);
  665. const auto fxCut = lynx::effectFromFl ("Edison", cut, 140.0);
  666. CHECK (fxCut.has_value() && fxCut->kind == lynx::EffectKind::edison);
  667. }
  668. TEST (edison190cd_other_slots_and_old_projects_are_untouched)
  669. {
  670. // the new kind is the last one: every earlier kind keeps its number (project files name kinds, FL import maps to them)
  671. CHECK ((int) lynx::EffectKind::edison == (int) lynx::EffectKind::rackMixer + 1);
  672. // no session appears for other kinds
  673. const auto reverb = lynx::makeEffectSlot (lynx::EffectKind::newtone);
  674. CHECK (lynx::edisonfx::findSession (reverb.instance.id) == nullptr);
  675. // the generic factory makes the effect too (a slot without the renderer: thumbnails, tools)
  676. const auto fx = lynx::makeEffect (lynx::EffectKind::edison);
  677. CHECK (fx != nullptr);
  678. if (fx != nullptr)
  679. {
  680. fx->prepare (44100.0);
  681. std::vector<float> l (64, 0.5f), r (64, 0.25f);
  682. fx->process (l.data(), r.data(), 64, lynx::makeEffectSlot (lynx::EffectKind::edison).params, 120.0);
  683. CHECK (l[0] == 0.5f && r[63] == 0.25f);
  684. }
  685. }
  686. // Round 190ct (live FL check 190cq): the mixer's "Open audio editor and record" loads an Edison set to On play, 5 min,
  687. // Append off, Slave on (shot edison190cd-05b); a plain open and a fresh slot keep On input, 30 min, Append on, Slave off
  688. // (05c / 05d). Old slots keep what they saved.
  689. TEST (edison190ct_open_and_record_preset_and_fresh_defaults)
  690. {
  691. namespace ef = lynx::edisonfx;
  692. const auto fresh = lynx::makeEffectSlot (lynx::EffectKind::edison);
  693. auto armed = fresh;
  694. ef::setOpenAndRecord (armed.params);
  695. CHECK (ef::modeOf (armed.params) == ef::Mode::onPlay);
  696. CHECK (ef::maxTimeIndexOf (armed.params) == 3 && ef::maxMinutes[3] == 5);
  697. CHECK (armed.params[ef::append] < 0.5f && armed.params[ef::slave] >= 0.5f);
  698. // the rest stays the fresh plugin's
  699. CHECK (armed.params[ef::muteInput] == fresh.params[ef::muteInput] && armed.params[ef::loopPlay] == fresh.params[ef::loopPlay]
  700. && armed.params[ef::noiseThreshold] == fresh.params[ef::noiseThreshold]
  701. && armed.params[ef::ignoreOwnSelection] == fresh.params[ef::ignoreOwnSelection]);
  702. CHECK (ef::modeOf (fresh.params) == ef::Mode::onInput && ef::maxTimeIndexOf (fresh.params) == 5 && fresh.params[ef::append] >= 0.5f
  703. && fresh.params[ef::slave] < 0.5f);
  704. CHECK (ef::paramText (ef::maxTime, fresh.params[ef::maxTime]) == "30 min");
  705. // a slot saved before (5 min, the earlier default) keeps its value: the value is in the saved parameters
  706. auto old = fresh;
  707. old.params[ef::maxTime] = 3.0f / 6.0f;
  708. CHECK (ef::maxTimeIndexOf (old.params) == 3);
  709. // an FL state too old to hold the time gets FL's default
  710. CHECK (ef::FlState {}.maxTime == 5);
  711. }
  712. // Round 190ct (live FL check 190cq, shot edison190cd-01-02): in On play FL writes "Song jump" at the start of every take
  713. // — frame 0 of a replacing take, the join of an appended one — and where the song starts again while armed.
  714. TEST (edison190ct_on_play_marks_the_start_of_each_take)
  715. {
  716. namespace ef = lynx::edisonfx;
  717. namespace ae = lynx::audioedit;
  718. ef::Processor p;
  719. p.prepare (44100.0);
  720. auto params = edison190cd::defaults();
  721. params[ef::recordMode] = ef::modeValue (ef::Mode::onPlay);
  722. p.setRecording (true);
  723. double tick = 0.0;
  724. const auto ticksPerBlock = 441.0 / 44100.0 * (120.0 / 60.0) * (double) lynx::ppq;
  725. auto block = [&] (bool playing) {
  726. p.setSongPosition (tick);
  727. p.setPlaying (playing);
  728. edison190cd::run (p, params, 0.3f, 1);
  729. if (playing)
  730. tick += ticksPerBlock;
  731. };
  732. for (int i = 0; i < 3; ++i)
  733. block (false);
  734. for (int i = 0; i < 4; ++i)
  735. block (true);
  736. for (int i = 0; i < 3; ++i)
  737. block (false); // stop: the take pauses
  738. tick = 0.0; // play again from the start
  739. for (int i = 0; i < 4; ++i)
  740. block (true);
  741. std::vector<float> l, r;
  742. std::vector<std::int64_t> jumps;
  743. p.drain (l, r, jumps, 0);
  744. p.setRecording (false);
  745. CHECK (l.size() == 8 * 441);
  746. CHECK (jumps == (std::vector<std::int64_t> { 0, 4 * 441 }));
  747. // replacing: the first marker sits at 0; appending: at the join
  748. ae::Audio take;
  749. take.sampleRate = 44100.0;
  750. take.channels.assign (2, std::vector<float> (800, 0.1f));
  751. const auto base = edison190cd::ramp (1000);
  752. const auto replaced = ef::withTake (base, take, { 0 }, false);
  753. CHECK (replaced.markers.size() == 1 && replaced.markers[0].start == 0 && replaced.markers[0].name == "Song jump");
  754. const auto appended = ef::withTake (base, take, { 0 }, true);
  755. CHECK (appended.audio.frames() == 1800 && appended.markers.size() == 1 && appended.markers[0].start == 1000
  756. && appended.markers[0].name == "Song jump");
  757. // Input mode still writes no marker (live check: none at the start or a resume)
  758. ef::Gate g;
  759. g.prepare (44100.0);
  760. g.arm (ef::Mode::input);
  761. CHECK (! g.block (0.5f, 512, true, false, 0.02f).jumpMarker);
  762. }
  763. // Round 190ct (live FL check 190cq, shot edison190cd-07b): "Remove all Edison instances" empties every Edison slot (the
  764. // question comes first, in the UI); the other effects keep their rows.
  765. TEST (edison190ct_remove_all_edison_instances)
  766. {
  767. lynx::Song song;
  768. song.mixer.resize (3);
  769. song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::edison));
  770. song.mixer[1].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::newtone));
  771. song.mixer[1].effects.back().row = 3;
  772. song.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::edison));
  773. song.mixer[2].effects.push_back (lynx::makeEffectSlot (lynx::EffectKind::edison));
  774. CHECK (lynx::mixerops::removeEffectsOfKind (song, lynx::EffectKind::edison) == 3);
  775. CHECK (song.mixer[1].effects.size() == 1 && song.mixer[1].effects[0].kind == lynx::EffectKind::newtone && song.mixer[1].effects[0].row == 3);
  776. CHECK (song.mixer[2].effects.empty());
  777. CHECK (lynx::mixerops::removeEffectsOfKind (song, lynx::EffectKind::edison) == 0); // none left: nothing happens
  778. }