DialogsTests84.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. // --- Export options, MP3 encoder, plugin performance monitor, clip properties / arrangements, new project, touch
  2. // keyboard (core/ExportOptions.h, core/Mp3Encoder.h, core/PluginMonitor.h, core/PlaylistOps.h, core/Templates.h,
  3. // core/TouchKeyboard.h) - round 84 ------------------------------------------------------------------------------
  4. TEST (export_options_text_round_trip_and_clamping)
  5. {
  6. using namespace lynx::exportopts;
  7. Options o;
  8. o.format = Format::mp3;
  9. o.mp3Kbps = 192;
  10. o.oggKbps = 160;
  11. o.flacLevel = 8;
  12. o.flacBits = 16;
  13. o.wavBits = 32;
  14. o.sampleRate = 96000.0;
  15. o.dither = DitherMode::tpdf;
  16. o.tail = TailMode::wrap;
  17. o.maxTailSeconds = 4.5;
  18. o.range = Range::selection;
  19. o.loops = 3;
  20. o.stems = Stems::channels;
  21. o.stemsIncludeMaster = true;
  22. o.normalize = true;
  23. o.masterEffects = false;
  24. o.trimPdc = false;
  25. o.channels = Channels::monoMerged;
  26. CHECK (fromText (toText (o)) == o);
  27. CHECK (fromText ("") == Options {});
  28. const auto bad = fromText ("format=9\nmp3Kbps=300\nsampleRate=50000\nloops=99\nwavBits=20\nflacLevel=-3\nmaxTail=nan\ntail=7\nchannels=x\n");
  29. CHECK (bad.format == Options {}.format && bad.mp3Kbps == 320 && bad.sampleRate == 48000.0 && bad.loops == 16);
  30. CHECK (bad.wavBits == 24 && bad.flacLevel == 0 && bad.tail == TailMode::leave && bad.channels == Channels::stereo);
  31. CHECK (bad.maxTailSeconds == Options {}.maxTailSeconds);
  32. CHECK (std::string (extension (Format::mp3)) == ".mp3" && std::string (extension (Format::flac)) == ".flac");
  33. Options w;
  34. w.wavBits = 16;
  35. CHECK (usesDither (w) && fileBits (w) == 16);
  36. w.wavBits = 32;
  37. CHECK (! usesDither (w) && fileBits (w) == 32);
  38. w.wavBits = 24;
  39. w.dither = DitherMode::none;
  40. CHECK (! usesDither (w));
  41. w.format = Format::ogg;
  42. w.dither = DitherMode::shaped;
  43. CHECK (! usesDither (w) && fileBits (w) == 16);
  44. CHECK (mp3SampleRate (44100.0) == 44100.0 && mp3SampleRate (96000.0) == 48000.0 && mp3SampleRate (88200.0) == 44100.0);
  45. CHECK (formatDuration (65.4) == "1:05" && formatDuration (3725.0) == "1:02:05" && formatDuration (-3.0) == "0:00");
  46. }
  47. TEST (export_render_song_ranges_loops_and_tail)
  48. {
  49. using namespace lynx;
  50. using namespace lynx::exportopts;
  51. auto song = Song::makeDefault();
  52. const auto bar = (std::int64_t) ppq * song.beatsPerBar;
  53. Clip a;
  54. a.pattern = 0;
  55. a.start = 0;
  56. a.length = 2 * bar;
  57. Clip b = a;
  58. b.start = 3 * bar;
  59. b.length = 2 * bar;
  60. b.track = 1;
  61. song.playlist = { a, b };
  62. song.loopStart = bar; // selection: bars 2..4
  63. song.loopEnd = 4 * bar;
  64. Options o;
  65. std::int64_t music = 0, loop = 0;
  66. auto whole = makeRenderSong (song, o, 0, music, loop);
  67. CHECK (music == 5 * bar && loop == 5 * bar && whole.mode == PlayMode::song && whole.loopEnd == 0 && whole.playlist.size() == 2);
  68. o.range = Range::selection;
  69. auto sel = makeRenderSong (song, o, 0, music, loop);
  70. CHECK (music == 3 * bar && loop == 3 * bar);
  71. CHECK (sel.playlist.size() == 2);
  72. if (sel.playlist.size() == 2)
  73. {
  74. // clip a cut at the selection's start (its second bar, offset by one bar); clip b cut at its end
  75. CHECK (sel.playlist[0].start == 0 && sel.playlist[0].length == bar && sel.playlist[0].offset == bar);
  76. CHECK (sel.playlist[1].start == 2 * bar && sel.playlist[1].length == bar && sel.playlist[1].offset == 0);
  77. }
  78. o.loops = 3;
  79. o.masterEffects = false;
  80. auto looped = makeRenderSong (song, o, 2 * bar, music, loop);
  81. CHECK (loop == 3 * bar && music == 9 * bar);
  82. CHECK (looped.playlist.size() == 7); // 2 clips x 3 passes + the tail
  83. CHECK (looped.playlist.back().start == 9 * bar && looped.patterns.back().name == "(tail)");
  84. CHECK (looped.mixer[0].effects.empty() && ! song.mixer[0].effects.empty());
  85. o = {};
  86. o.range = Range::pattern;
  87. auto pat = makeRenderSong (song, o, 0, music, loop);
  88. CHECK (pat.playlist.size() == 1 && music == song.patterns[0].effectiveLength (song.meter()));
  89. // an empty selection still renders its length (silence)
  90. song.playlist.clear();
  91. o.range = Range::selection;
  92. makeRenderSong (song, o, 0, music, loop);
  93. CHECK (music == 3 * bar);
  94. song.loopEnd = 0;
  95. CHECK (! hasSelection (song));
  96. }
  97. TEST (export_tail_wrap_cut_normalize_mono)
  98. {
  99. using namespace lynx::exportopts;
  100. std::vector<float> l { 1, 2, 3, 4, 10, 20, 30 }, r { 0, 0, 0, 0, 1, 1, 1 };
  101. auto l2 = l, r2 = r;
  102. applyTail (l2, r2, 4, TailMode::wrap);
  103. CHECK ((l2 == std::vector<float> { 11, 22, 33, 4 }) && (r2 == std::vector<float> { 1, 1, 1, 0 }));
  104. l2 = l, r2 = r;
  105. applyTail (l2, r2, 4, TailMode::cut);
  106. CHECK ((l2 == std::vector<float> { 1, 2, 3, 4 }) && r2.size() == 4);
  107. l2 = l, r2 = r;
  108. applyTail (l2, r2, 4, TailMode::leave);
  109. CHECK (l2 == l);
  110. l2 = l, r2 = r; // a tail longer than the loop wraps around more than once
  111. applyTail (l2, r2, 2, TailMode::wrap);
  112. CHECK ((l2 == std::vector<float> { 1 + 3 + 10 + 30, 2 + 4 + 20 }));
  113. l2 = { 1, 2 }, r2 = { 0, 0 }; // a render shorter than the loop is padded with silence
  114. applyTail (l2, r2, 4, TailMode::cut);
  115. CHECK ((l2 == std::vector<float> { 1, 2, 0, 0 }) && r2.size() == 4);
  116. CHECK (normalizeGain (0.5f) == 2.0f && normalizeGain (0.0f) == 1.0f);
  117. float a[2] { 0.2f, 0.4f };
  118. const float b[2] { 0.6f, -0.4f };
  119. toChannels (a, b, 2, Channels::monoMerged);
  120. CHECK (std::abs (a[0] - 0.4f) < 1e-6f && std::abs (a[1]) < 1e-6f);
  121. toChannels (a, b, 2, Channels::monoRight);
  122. CHECK (a[0] == 0.6f && a[1] == -0.4f);
  123. CHECK (channelCount (Channels::stereo) == 2 && channelCount (Channels::monoLeft) == 1);
  124. }
  125. TEST (export_stems_tracks_channels_and_names)
  126. {
  127. using namespace lynx;
  128. using namespace lynx::exportopts;
  129. auto song = Song::makeDefault(); // channels 0..4 on inserts 1..5, notes in pattern 0
  130. song.setRoute (5, 7, true, 0.5f);
  131. const auto tracks = stemTracks (song);
  132. CHECK ((tracks == std::vector<int> { 1, 2, 3, 4, 5, 7 }));
  133. Clip c;
  134. c.pattern = 0;
  135. c.length = ppq * 4;
  136. song.playlist = { c };
  137. const auto channels = stemChannels (song, Range::song);
  138. std::set<int> withNotes;
  139. for (const auto& n : song.patterns[0].notes)
  140. withNotes.insert (n.channel);
  141. CHECK (channels == std::vector<int> (withNotes.begin(), withNotes.end()));
  142. song.playlist[0].muted = true;
  143. CHECK (stemChannels (song, Range::song).empty());
  144. CHECK (stemChannels (song, Range::pattern).size() == withNotes.size());
  145. const auto solo = makeChannelStemSong (song, 1);
  146. CHECK (! solo.channels[1].muted && solo.channels[0].muted && solo.channels[4].muted);
  147. CHECK (stemFileName ("Night", 3, "Lead: \"hi\"/2", ".wav") == "Night - 3 Lead_ _hi__2.wav");
  148. CHECK (stemFileName ("Night", 1, " ..", ".mp3") == "Night - 1 .mp3" || stemFileName ("Night", 1, "", ".mp3") == "Night - 1.mp3");
  149. }
  150. TEST (export_dither_without_noise_shaping)
  151. {
  152. lynx::Dither shaped (16), flat (16, false);
  153. std::vector<float> a (4096), b (4096);
  154. for (std::size_t i = 0; i < a.size(); ++i)
  155. a[i] = b[i] = 0.25f * (float) std::sin ((double) i * 0.05);
  156. shaped.process (a.data(), (int) a.size(), 0);
  157. flat.process (b.data(), (int) b.size(), 0);
  158. double e = 0.0;
  159. for (std::size_t i = 0; i < b.size(); ++i)
  160. e = std::max (e, std::abs ((double) b[i] - flat.writerBias() - 0.25 * std::sin ((double) i * 0.05)));
  161. CHECK (e <= 1.5 / 32768.0); // plain TPDF: at most 1.5 LSB away
  162. CHECK (a != b);
  163. // both land on the 16-bit grid
  164. const auto steps = (b[100] - flat.writerBias()) * 32768.0f;
  165. CHECK (std::abs (steps - std::round (steps)) < 1e-3f);
  166. }
  167. TEST (export_mp3_encoder_frames)
  168. {
  169. for (const auto [rate, kbps, channels] : { std::tuple { 44100, 320, 2 }, std::tuple { 48000, 128, 1 } })
  170. {
  171. lynx::Mp3Encoder enc;
  172. std::string error;
  173. CHECK (enc.open (rate, rate, channels, kbps, error, "Night Drive", "LYNX"));
  174. std::vector<float> l ((std::size_t) rate), r ((std::size_t) rate);
  175. for (std::size_t i = 0; i < l.size(); ++i)
  176. {
  177. l[i] = 0.5f * (float) std::sin (6.283185307 * 440.0 * (double) i / rate);
  178. r[i] = 0.5f * (float) std::sin (6.283185307 * 660.0 * (double) i / rate);
  179. }
  180. std::vector<std::uint8_t> bytes;
  181. for (std::size_t done = 0; done < l.size(); done += 4096)
  182. CHECK (enc.encode (l.data() + done, r.data() + done, (int) std::min<std::size_t> (4096, l.size() - done), bytes));
  183. CHECK (enc.finish (bytes));
  184. CHECK (bytes.size() > 3 && bytes[0] == 'I' && bytes[1] == 'D' && bytes[2] == '3');
  185. const auto scan = lynx::scanMp3 (bytes);
  186. // one second = rate / 1152 frames (plus the encoder's delay and padding frames)
  187. CHECK (scan.frames >= rate / 1152 && scan.frames <= rate / 1152 + 4);
  188. CHECK (scan.kbps == kbps && scan.sampleRate == rate && scan.channels == channels);
  189. }
  190. lynx::Mp3Encoder resampled; // 96 kHz in, 48 kHz out
  191. std::string error;
  192. CHECK (resampled.open (96000, 48000, 2, 256, error));
  193. std::vector<float> silence (96000, 0.0f);
  194. std::vector<std::uint8_t> bytes;
  195. resampled.encode (silence.data(), silence.data(), (int) silence.size(), bytes);
  196. resampled.finish (bytes);
  197. const auto scan = lynx::scanMp3 (bytes);
  198. CHECK (scan.sampleRate == 48000 && scan.frames >= 41 && scan.frames <= 46);
  199. }
  200. TEST (perfmon_sources_loads_and_find)
  201. {
  202. using namespace lynx;
  203. using namespace lynx::perfmon;
  204. auto song = Song::makeDefault();
  205. song.channels[4].instrument = InstrumentKind::plugin;
  206. song.channels[4].plugin.name = "Serum";
  207. EffectSlot fx;
  208. fx.kind = EffectKind::plugin;
  209. fx.plugin.name = "Pro-Q";
  210. song.mixer[3].effects.push_back (fx);
  211. const auto list = sources (song, [] (const ChannelDef& c) { return "Built-in " + c.name; });
  212. CHECK (list.size() == song.channels.size() + 2); // + the Master limiter and Pro-Q
  213. CHECK (list[0].name == "Built-in Kick" && list[0].channel == 0 && ! list[0].plugin);
  214. CHECK (list[4].name == "Serum" && list[4].plugin);
  215. const auto& proq = list.back();
  216. CHECK (proq.name == "Pro-Q" && proq.track == 3 && proq.slot == 0 && proq.key == effectKey (3, 0) && proq.plugin);
  217. Monitor m;
  218. std::vector<std::uint64_t> busy (keyCount, 0);
  219. m.update (list, busy, 0, 0); // starting point
  220. busy[4] = 50'000'000; // Serum: 50 ms for 100 ms of audio = 50 %
  221. busy[(std::size_t) effectKey (3, 0)] = 10'000'000;
  222. m.update (list, busy, 100'000'000, 70'000'000);
  223. auto rows = m.rows();
  224. CHECK (rows.size() == list.size() && rows[0].source.name == "Serum");
  225. CHECK (std::abs (rows[0].current - 25.0) < 1e-9 && std::abs (rows[0].peak - 50.0) < 1e-9 && std::abs (rows[0].average - 50.0) < 1e-9);
  226. CHECK (std::abs (m.rackPeak() - 70.0) < 1e-9);
  227. busy[4] += 50'000'000;
  228. m.update (list, busy, 200'000'000, 140'000'000);
  229. rows = m.rows ("serum");
  230. CHECK (rows.size() == 1 && std::abs (rows[0].current - 37.5) < 1e-9 && std::abs (rows[0].average - 50.0) < 1e-9);
  231. CHECK (m.rows ("insert 3 pro").size() == 1 && m.rows ("nothing like this").empty());
  232. m.reset();
  233. busy[4] += 10'000'000;
  234. m.update (list, busy, 300'000'000, 150'000'000);
  235. rows = m.rows ("serum");
  236. CHECK (rows.size() == 1 && std::abs (rows[0].peak - 10.0) < 1e-9 && std::abs (rows[0].average - 10.0) < 1e-9);
  237. // something else in the same slot starts with a fresh history
  238. auto other = list;
  239. other[4].name = "Vital";
  240. busy[4] += 90'000'000;
  241. m.update (other, busy, 400'000'000, 160'000'000);
  242. rows = m.rows ("vital");
  243. CHECK (rows.size() == 1 && rows[0].peak == 0.0 && rows[0].current == 0.0);
  244. CHECK (m.rows ({}, SortBy::name).front().source.name < m.rows ({}, SortBy::name).back().source.name);
  245. }
  246. TEST (clipprops_read_apply_and_groups)
  247. {
  248. using namespace lynx;
  249. using namespace lynx::clipprops;
  250. auto song = Song::makeDefault();
  251. song.patterns.emplace_back().name = "Drop";
  252. ChannelDef audio;
  253. audio.name = "Vox";
  254. audio.instrument = InstrumentKind::audioClip;
  255. song.channels.push_back (audio);
  256. const auto vox = (int) song.channels.size() - 1;
  257. Clip p;
  258. p.pattern = 0;
  259. p.length = ppq * 4;
  260. Clip a;
  261. a.kind = ClipKind::audio;
  262. a.channel = vox;
  263. a.start = ppq * 8;
  264. a.length = ppq * 4;
  265. song.playlist = { p, a };
  266. Props props;
  267. CHECK (read (song, 0, props) && props.kind == ClipKind::pattern && props.name == song.patterns[0].name && props.source == 0);
  268. CHECK (! read (song, 5, props));
  269. props.source = 1;
  270. props.name = "Drop B";
  271. props.colour = 0xff336699u;
  272. props.start = -50;
  273. props.length = 0;
  274. props.offset = ppq;
  275. CHECK (apply (song, 0, props));
  276. const auto& c0 = song.playlist[0];
  277. CHECK (c0.pattern == 1 && c0.start == 0 && c0.length == 1 && c0.offset == ppq);
  278. CHECK (song.patterns[1].name == "Drop B" && song.patterns[1].colour == 0xff336699u);
  279. CHECK (read (song, 1, props) && props.kind == ClipKind::audio && props.name == "Vox");
  280. props.gain = 9.0f;
  281. props.fadeIn = ppq * 10;
  282. props.fadeOut = ppq * 10;
  283. props.pitch = 40.0f;
  284. props.stretchBeats = 0.0f;
  285. props.keepPitch = true;
  286. props.source = 0; // channel 0 is not an audio clip channel: ignored
  287. CHECK (apply (song, 1, props));
  288. const auto& c1 = song.playlist[1];
  289. CHECK (c1.channel == vox && c1.gain == 4.0f && c1.fadeIn == c1.length && c1.fadeOut == 0);
  290. // (round 163: "keep length" stays without a stretch — it keeps a pitched clip's length)
  291. CHECK (song.channels[(std::size_t) vox].sampler.pitch == 24.0f && song.channels[(std::size_t) vox].sampler.keepPitch);
  292. changeMute (song, { 0, 1 }, MuteChange::muteAll);
  293. CHECK (song.playlist[0].muted && song.playlist[1].muted);
  294. song.playlist[0].muted = false;
  295. changeMute (song, { 0, 1, 7 }, MuteChange::flip);
  296. CHECK (song.playlist[0].muted && ! song.playlist[1].muted);
  297. changeGain (song, { 0, 1 }, 0.5f, true);
  298. CHECK (song.playlist[1].gain == 2.0f && song.playlist[0].gain == 1.0f); // pattern clips have no gain
  299. changeGain (song, { 1 }, 0.25f, false);
  300. CHECK (song.playlist[1].gain == 0.25f);
  301. CHECK (sourcesFor (song, ClipKind::audio).size() == 1 && sourcesFor (song, ClipKind::audio)[0].first == vox);
  302. CHECK (sourcesFor (song, ClipKind::pattern).size() == 2);
  303. }
  304. TEST (arrangements_merge_insert_replace_move)
  305. {
  306. using namespace lynx;
  307. using namespace lynx::arrangeops;
  308. Song song;
  309. song.beatsPerBar = 4;
  310. const auto bar = (std::int64_t) ppq * 4;
  311. auto clip = [] (std::int64_t start, std::int64_t length, int track) {
  312. Clip c;
  313. c.start = start;
  314. c.length = length;
  315. c.track = track;
  316. return c;
  317. };
  318. song.playlist = { clip (0, 4 * bar, 0), clip (4 * bar, 2 * bar, 1) }; // current: 6 bars
  319. song.addMarker ({ 5 * bar, "Chorus" });
  320. song.arrangements.push_back ({ "Intro", { clip (0, bar, 2), clip (bar, bar, 3) }, 0, 0, {} }); // 2 bars
  321. CHECK (lengthOf (song, 0) == 6 * bar && lengthOf (song, 1) == 2 * bar);
  322. CHECK (positionTick (song, MergePosition::end, 0) == 6 * bar && positionTick (song, MergePosition::current, 77) == 77);
  323. CHECK (! merge (song, 0, MergeMode::merge, 0));
  324. auto ins = song;
  325. CHECK (merge (ins, 1, MergeMode::insert, 2 * bar));
  326. // the 4-bar clip is split at bar 2: its second half moves after the 2 inserted bars
  327. CHECK (ins.playlist.size() == 5);
  328. CHECK (lengthOf (ins, 0) == 8 * bar);
  329. CHECK (ins.markers()[0].tick == 7 * bar);
  330. int at2 = 0;
  331. for (const auto& c : ins.playlist)
  332. if (c.start == 2 * bar && (c.track == 2))
  333. ++at2;
  334. CHECK (at2 == 1);
  335. auto mer = song;
  336. CHECK (merge (mer, 1, MergeMode::merge, 0));
  337. CHECK (mer.playlist.size() == 4 && lengthOf (mer, 0) == 6 * bar);
  338. auto rep = song;
  339. CHECK (merge (rep, 1, MergeMode::replace, 3 * bar));
  340. // [3, 5) bars cleared: the first clip keeps 3 bars, the second keeps its last bar (from bar 5)
  341. std::int64_t covered = 0;
  342. for (const auto& c : rep.playlist)
  343. if (c.track < 2)
  344. covered += c.length;
  345. CHECK (covered == 4 * bar && rep.playlist.size() == 4);
  346. std::vector<Clip> list { clip (0, 10 * bar, 0) };
  347. clearRange (list, 2 * bar, 3 * bar);
  348. CHECK (list.size() == 2 && list[0].length == 2 * bar && list[1].start == 3 * bar && list[1].offset == 3 * bar);
  349. song.arrangements.push_back ({ "Outro", {}, 0, 0, {} });
  350. song.selectArrangement (2);
  351. move (song, 2, 0);
  352. CHECK (song.currentArrangement == 0 && song.arrangements[0].name == "Outro");
  353. move (song, 1, 2);
  354. CHECK (song.currentArrangement == 0 && song.arrangements[2].name == "Arrangement 1");
  355. rename (song, 1, "Verse");
  356. rename (song, 1, "");
  357. CHECK (song.arrangements[1].name == "Verse");
  358. const auto copy = duplicate (song, 2);
  359. CHECK (copy == 3 && song.arrangements[3].name == "Arrangement 1 (copy)" && song.arrangements[3].clips.size() == 2);
  360. }
  361. TEST (new_project_setup_and_templates)
  362. {
  363. using namespace lynx;
  364. CHECK (legalProjectName (" My: song?. ") == "My_ song_" && legalProjectName (" .. ") == "Untitled");
  365. NewProjectSetup s;
  366. s.name = "Night Drive";
  367. CHECK (newProjectPath (s, ".lynx").empty());
  368. s.folder = "C:\\Music\\";
  369. CHECK (newProjectPath (s, ".lynx") == "C:\\Music/Night Drive/Night Drive.lynx");
  370. s.ownFolder = false;
  371. s.folder = "/home/x";
  372. CHECK (newProjectPath (s, ".lynx") == "/home/x/Night Drive.lynx");
  373. s.bpm = 900.0;
  374. s.beatsPerBar = 7;
  375. s.info.author = "Me";
  376. auto song = makeTemplate (0);
  377. applySetup (song, s);
  378. CHECK (song.bpm == 522.0 && song.beatsPerBar == 7 && song.info.author == "Me" && song.info.title == "Night Drive");
  379. CHECK (templateDisplayName ("C:/T/Trap kit.lynx") == "Trap kit" && templateDisplayName (".hidden") == ".hidden");
  380. }
  381. // The "Empty" template is FL's empty project: one Sampler channel and nothing in the patterns. The "Default" new project
  382. // keeps the channel set (the New project window's "Drums and a synth on the first inserts").
  383. TEST (empty_template_is_one_sampler)
  384. {
  385. using namespace lynx;
  386. const auto empty = makeTemplate (builtInTemplateIndex ("Empty"));
  387. CHECK (empty.channels.size() == 1);
  388. if (empty.channels.size() == 1)
  389. {
  390. CHECK (empty.channels[0].instrument == InstrumentKind::sampler);
  391. CHECK (empty.channels[0].name == "Sampler");
  392. CHECK (empty.channels[0].mixerTrack == 0); // (FL 2026: "---", the Master; round 190qj)
  393. CHECK (empty.channels[0].samplePath.empty());
  394. }
  395. CHECK (empty.patterns.size() == 1 && empty.patterns[0].notes.empty());
  396. // FL's Empty.flp (round 190qj): nothing on the Master, 140 BPM; the default new project keeps its limiter
  397. CHECK (empty.mixer[0].effects.empty() && empty.bpm == 140.0);
  398. CHECK (! makeNewProject().mixer[0].effects.empty());
  399. CHECK (makeNewProject (builtInTemplateIndex ("Empty")).channels.size() == 1);
  400. // the default new project: still the channel set, with empty steps
  401. const auto defaults = makeNewProject();
  402. CHECK (defaults.channels.size() == 5);
  403. CHECK (defaults.channels.front().name == "Kick" && defaults.channels.back().name == "Synth");
  404. CHECK (defaults.patterns.size() == 1 && defaults.patterns[0].notes.empty());
  405. }
  406. // Issue #123: the default template's saved form (FL's Options › General › "Default template")
  407. TEST (default_template_choice_text_round_trip_and_rejects_paths)
  408. {
  409. using namespace lynx;
  410. using K = TemplateChoice::Kind;
  411. CHECK (TemplateChoice::parse ("").isLynxDefault());
  412. CHECK (TemplateChoice {}.toText().empty());
  413. const TemplateChoice house { K::builtIn, "House" };
  414. CHECK (house.toText() == "builtin:House" && TemplateChoice::parse ("builtin:House") == house);
  415. const TemplateChoice mine { K::user, "Vocal chain.lynx" };
  416. CHECK (mine.toText() == "user:Vocal chain.lynx" && TemplateChoice::parse (mine.toText()) == mine);
  417. // unknown built-ins, paths, empty names and other prefixes read as LYNX's default
  418. for (const char* bad : { "builtin:Nope", "user:../x.lynx", "user:C:\\x.lynx", "user:a/b.lynx", "user:", "builtin:", "user:..",
  419. "file:x.lynx", "garbage" })
  420. CHECK (TemplateChoice::parse (bad).isLynxDefault());
  421. CHECK (builtInTemplateIndex ("Empty") == 0 && builtInTemplateIndex ("House") == 3 && builtInTemplateIndex ("house") == -1);
  422. // FL's "Startup project" choice
  423. for (auto c : { StartupProject::emptyProject, StartupProject::defaultTemplate, StartupProject::lastProject })
  424. CHECK (parseStartupProject (startupProjectText (c)) == c);
  425. CHECK (parseStartupProject ("") == StartupProject::defaultTemplate && parseStartupProject ("x") == StartupProject::defaultTemplate);
  426. // every built-in survives the round trip
  427. for (const auto& name : templateNames())
  428. CHECK (TemplateChoice::parse (TemplateChoice { K::builtIn, name }.toText()).name == name);
  429. }
  430. TEST (touch_keyboard_geometry_velocity_and_no_stuck_notes)
  431. {
  432. using namespace lynx::touchkb;
  433. Layout k;
  434. k.lowest = 48;
  435. k.whiteKeys = 14;
  436. k.width = 140.0f;
  437. k.height = 100.0f;
  438. CHECK (k.highest() == 71);
  439. CHECK (k.noteAt (5.0f, 90.0f) == 48); // C, bottom
  440. CHECK (k.noteAt (10.0f, 30.0f) == 49); // C# over the C/D line
  441. CHECK (k.noteAt (10.0f, 80.0f) == 50 || k.noteAt (10.0f, 80.0f) == 48); // below the black key: a white key
  442. CHECK (k.noteAt (15.0f, 80.0f) == 50); // D
  443. CHECK (k.noteAt (35.0f, 20.0f) == 53); // F: no black key between E and F
  444. CHECK (k.noteAt (139.0f, 90.0f) == 71 && k.noteAt (141.0f, 50.0f) == -1 && k.noteAt (5.0f, -1.0f) == -1);
  445. CHECK (isBlack (61) && ! isBlack (60) && isBlack (-2) == isBlack (10));
  446. CHECK (std::abs (k.velocityAt (48, 0.0f) - 0.2f) < 1e-6f && std::abs (k.velocityAt (48, 100.0f) - 1.0f) < 1e-6f);
  447. CHECK (k.velocityAt (49, 61.0f) > 0.99f); // black keys reach full velocity at their own bottom
  448. CHECK (k.velocityAt (48, 50.0f) > k.velocityAt (48, 20.0f));
  449. CHECK (shiftOctave (48, 1, 21) == 60 && shiftOctave (48, -10, 21) == 0 && shiftOctave (100, 5, 21) <= 127 - 35);
  450. CHECK (shiftOctave (50, 0, 21) == 48);
  451. // gestures through HeldNotes: press, glide to another key, release, leave: every on gets its off
  452. std::map<std::pair<int, int>, int> balance;
  453. int ons = 0;
  454. {
  455. lynx::HeldNotes notes ([&] (int channel, int key, float, bool on) {
  456. balance[{ channel, key }] += on ? 1 : -1;
  457. ons += on ? 1 : 0;
  458. });
  459. const int pointer = 0;
  460. notes.press (2, k.noteAt (5.0f, 90.0f), 0.8f, pointer);
  461. notes.releaseTag (pointer); // glide: the old key stops ...
  462. notes.press (2, k.noteAt (15.0f, 80.0f), 0.8f, pointer); // ... the new one starts
  463. notes.press (2, k.noteAt (35.0f, 20.0f), 0.5f, 1); // a second finger
  464. notes.releaseTag (pointer);
  465. // the mouse leaves with the second finger still down: releaseAll; then the window closes (destructor)
  466. notes.press (3, 60, 1.0f, 2);
  467. }
  468. CHECK (ons == 4);
  469. CHECK (std::all_of (balance.begin(), balance.end(), [] (const auto& e) { return e.second == 0; }));
  470. }
  471. TEST (clipprops_bar_beat_tick_text)
  472. {
  473. using namespace lynx;
  474. using namespace lynx::clipprops;
  475. const auto beat = (std::int64_t) ppq, bar = beat * 4;
  476. CHECK (formatPosition (0, lynx::Meter (4, 4)) == "1:1:000" && formatLength (0, lynx::Meter (4, 4)) == "0:0:000");
  477. CHECK (formatPosition (2 * bar + beat + 5, lynx::Meter (4, 4)) == "3:2:005" && formatLength (2 * bar + beat + 5, lynx::Meter (4, 4)) == "2:1:005");
  478. CHECK (parsePosition ("3:2:005", lynx::Meter (4, 4)) == 2 * bar + beat + 5 && parseLength ("2:1:005", lynx::Meter (4, 4)) == 2 * bar + beat + 5);
  479. CHECK (parsePosition ("5", lynx::Meter (4, 4)) == 4 * bar && parsePosition ("5:3", lynx::Meter (4, 4)) == 4 * bar + 2 * beat && parseLength ("2", lynx::Meter (4, 4)) == 2 * bar);
  480. CHECK (parsePosition ("0:1:000", lynx::Meter (4, 4)) == -1 && parsePosition ("1:5:000", lynx::Meter (4, 4)) == -1 && parsePosition ("1:1:x", lynx::Meter (4, 4)) == -1 && parsePosition ("", lynx::Meter (4, 4)) == -1);
  481. CHECK (parsePosition ("1:4", lynx::Meter (3, 4)) == -1 && parsePosition ("2:3", lynx::Meter (3, 4)) == 3 * beat + 2 * beat);
  482. for (std::int64_t t : { (std::int64_t) 0, beat * 7 + 33, bar * 100 + 1 })
  483. CHECK (parsePosition (formatPosition (t, lynx::Meter (4, 4)), lynx::Meter (4, 4)) == t && parseLength (formatLength (t, lynx::Meter (7, 4)), lynx::Meter (7, 4)) == t);
  484. }
  485. // FL names new projects for you (FL 2026's Save as window for a new project, seen in round 190qj: "Project_1" where a
  486. // "Project_1" folder was missing, "Project_2" where one was there) — the first free name in the folder, never an "already
  487. // exists" error (core/Templates.cpp freeProjectName, the New project window's Name field). PR #266, FL-exact in 190qj.
  488. TEST (new_project_names_are_filled_in_and_never_clash)
  489. {
  490. namespace fs = std::filesystem;
  491. using namespace lynx;
  492. std::error_code ec;
  493. const auto dir = (fs::temp_directory_path (ec) / ("lynx_newproject_" + processTempTag())).string();
  494. fs::remove_all (dir, ec);
  495. fs::create_directories (dir, ec);
  496. const auto write = [] (const std::string& path) {
  497. fs::create_directories (fs::path (path).parent_path());
  498. std::ofstream out (path);
  499. out << "x";
  500. };
  501. // nothing there: the name the window shows
  502. CHECK (freeProjectName (dir, "", true, ".lynx") == "Project_1");
  503. CHECK (freeProjectName (dir, " ", true, ".lynx") == "Project_1");
  504. // its folder and file taken: "Project_2", then "Project_3"
  505. write (dir + "/Project_1/Project_1.lynx");
  506. CHECK (freeProjectName (dir, "", true, ".lynx") == "Project_2");
  507. write (dir + "/Project_2/Project_2.lynx");
  508. CHECK (freeProjectName (dir, "", true, ".lynx") == "Project_3");
  509. // an empty folder counts as taken too: the new project gets a folder of its own
  510. fs::create_directories (dir + "/Project_3", ec);
  511. CHECK (freeProjectName (dir, "", true, ".lynx") == "Project_4");
  512. // without the project's own folder only the project file matters
  513. write (dir + "/flat/Project_1.lynx");
  514. CHECK (freeProjectName (dir + "/flat", "", false, ".lynx") == "Project_2");
  515. CHECK (freeProjectName (dir + "/flat", "Sketch", false, ".lynx") == "Sketch");
  516. // a name typed by the user that is taken gets FL's "_2", "_3"; characters the file system refuses become '_'
  517. write (dir + "/My Song/My Song.lynx");
  518. CHECK (freeProjectName (dir, "My Song", true, ".lynx") == "My Song_2");
  519. write (dir + "/My Song_2/My Song_2.lynx");
  520. CHECK (freeProjectName (dir, "My Song", true, ".lynx") == "My Song_3");
  521. CHECK (freeProjectName (dir, "My: Song?", true, ".lynx") == "My_ Song_");
  522. // no folder yet: the name as it is (the save window comes later)
  523. CHECK (freeProjectName ({}, "", true, ".lynx") == "Project_1");
  524. CHECK (freeProjectName ({}, "Sketch", true, ".lynx") == "Sketch");
  525. fs::remove_all (dir, ec);
  526. }