Issue12Tests.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // Round 165p: issue #12 (BURNER1PA, Windows 10, 0.1.18)
  2. // Bug: Metronome given 'C' did nothing — the focused Playlist / Piano Roll took 'C' for its Knife first. Now the
  3. // editors' tools and letters are key map actions, and a key the user chose wins over the panels' built-in keys.
  4. TEST (issue12_user_key_wins_over_a_panel_default)
  5. {
  6. using namespace lynx::keys;
  7. KeyMap map;
  8. CHECK (map.conflicts().empty());
  9. CHECK (map.find ({ 'C' }, Context::playlist, false) == "playlist.tool.knife");
  10. CHECK (map.find ({ 'C' }, Context::pianoRoll, false) == "pianoRoll.tool.knife");
  11. CHECK (map.find ({ 'S' }, Context::playlist, false) == "playlist.tool.slip");
  12. CHECK (map.find ({ 'S' }, Context::pianoRoll, false) == "pianoRoll.slide");
  13. CHECK (map.find ({ 'N' }, Context::pianoRoll, false) == "pianoRoll.tool.drumPaint");
  14. CHECK (map.find ({ 'N' }, Context::playlist, false).empty()); // (no drum brush in the Playlist)
  15. CHECK (map.find ({ 'C' }, Context::playlist, true).empty()); // typing piano on: the letter plays a note
  16. CHECK (map.findUserBound ({ 'C' }, Context::playlist, false).empty()); // nothing chosen by the user yet
  17. map.setChords ("transport.metronome", { { 'C' } });
  18. for (auto focus : { Context::global, Context::channelRack, Context::pianoRoll, Context::playlist, Context::mixer, Context::browser })
  19. CHECK (map.findUserBound ({ 'C' }, focus, false) == "transport.metronome");
  20. CHECK (map.findUserBound ({ 'C' }, Context::playlist, true).empty()); // the typing piano still owns the letter
  21. CHECK (map.findUserBound ({ 'M', ctrl }, Context::global, false).empty()); // the old default key is gone
  22. const auto shadowed = map.shadowedBy ({ 'C' }, "transport.metronome");
  23. CHECK (shadowed.size() == 2);
  24. // a tool moved to another key works there, and its old letter is free
  25. map.setChords ("playlist.tool.knife", { { 'X' } });
  26. CHECK (map.findUserBound ({ 'X' }, Context::playlist, false) == "playlist.tool.knife");
  27. CHECK (map.findUserBound ({ 'X' }, Context::pianoRoll, false).empty());
  28. // both the user's: the focused panel's wins in the panel
  29. map.setChords ("pianoRoll.tool.listen", { { 'C' } });
  30. CHECK (map.findUserBound ({ 'C' }, Context::pianoRoll, false) == "pianoRoll.tool.listen");
  31. CHECK (map.findUserBound ({ 'C' }, Context::playlist, false) == "transport.metronome");
  32. }
  33. // Every action of the key map answers a key the user gives it, in every place it works (no panel swallows it).
  34. TEST (issue12_every_action_takes_a_user_key)
  35. {
  36. using namespace lynx::keys;
  37. const Chord picks[] = { { 'C' }, { 'Q', ctrl | shift }, { f3, alt }, { 'K', ctrl } };
  38. for (const auto& a : actions())
  39. for (const auto& c : picks)
  40. {
  41. KeyMap map;
  42. map.setChords (a.id, { c });
  43. const auto inDefaults = std::find (a.defaults.begin(), a.defaults.end(), c) != a.defaults.end();
  44. if (inDefaults)
  45. continue;
  46. const auto focus = a.context == Context::global ? Context::playlist : a.context;
  47. CHECK (map.findUserBound (c, focus, false) == a.id);
  48. CHECK (map.find (c, focus, false) == a.id || map.findUserBound (c, focus, false) == a.id);
  49. }
  50. }
  51. // Round 165q's find: on Windows JUCE's F-keys / numpad are VK | 0x10000, and the editors' towupper() turned F4 into
  52. // 'S' (the Slip tool) and F1 into 'P', so F4 (next empty pattern) did nothing with the Playlist focused.
  53. TEST (issue12_function_keys_are_not_letters)
  54. {
  55. using lynx::keys::letterOfKeyCode;
  56. for (int vk = 0x70; vk <= 0x7B; ++vk) // VK_F1…VK_F12, as JUCE's Windows key codes
  57. CHECK (letterOfKeyCode (vk | 0x10000) == 0);
  58. for (int vk = 0x60; vk <= 0x6F; ++vk) // the numpad
  59. CHECK (letterOfKeyCode (vk | 0x10000) == 0);
  60. for (int vk : { 0x25, 0x26, 0x27, 0x28, 0x21, 0x22, 0x24, 0x23, 0x2D, 0x2E }) // arrows, page, home, end, ins, del
  61. CHECK (letterOfKeyCode (vk | 0x10000) == 0);
  62. CHECK (letterOfKeyCode ('c') == 'C' && letterOfKeyCode ('S') == 'S' && letterOfKeyCode ('1') == '1');
  63. CHECK (letterOfKeyCode (' ') == 0 && letterOfKeyCode (0x7f) == 0);
  64. }
  65. // Bug: automation recorded in Song mode "cut itself": at the song's end the playhead went back to the start, the
  66. // points wrapped onto the start of the take's clip, and a pause in the gesture began a new clip. Now (song mode,
  67. // no loop region) the song plays on, the points keep their place, and the take's clip grows with the playhead.
  68. TEST (issue12_song_mode_automation_take_grows_past_the_song_end)
  69. {
  70. auto song = lynx::Song::makeDefault();
  71. song.mode = lynx::PlayMode::song;
  72. song.playlist.clear();
  73. song.automations.clear();
  74. lynx::Clip pc; // one bar of pattern: the song is one bar long
  75. pc.kind = lynx::ClipKind::pattern;
  76. pc.length = 4 * lynx::ppq;
  77. song.playlist.push_back (pc);
  78. const double bar = 4.0 * lynx::ppq;
  79. CHECK (lynx::songTickForRecorded (song, 1.5 * bar) == (std::int64_t) (0.5 * bar)); // (looping playback: wraps)
  80. CHECK (lynx::songTickForRecorded (song, 1.5 * bar, true) == (std::int64_t) (1.5 * bar));
  81. lynx::AutomationWrite w;
  82. w.target = { lynx::AutomationTargetKind::mixerVolume, 1 };
  83. w.name = "Insert 1 - Volume";
  84. lynx::commitAutomationPoints (song, w, { { 100, 0.2f }, { 400, 0.4f } }, 0, true);
  85. CHECK (w.createdByTake && w.automation == 0);
  86. auto clipOf = [&] () -> const lynx::Clip& {
  87. for (const auto& c : song.playlist)
  88. if (c.kind == lynx::ClipKind::automation)
  89. return c;
  90. return song.playlist.front();
  91. };
  92. CHECK (clipOf().start == 0 && clipOf().length == (std::int64_t) bar);
  93. // the playhead goes on (no move): the clip follows it, holding the value
  94. lynx::growTakeClip (song, w, (std::int64_t) (1.25 * bar), false);
  95. CHECK (clipOf().length == (std::int64_t) (1.25 * bar));
  96. // the gesture starts again in bar 3 (past the old song end): same clip, points where they were played
  97. w.lastLocal = -1; // (the engine: the first gesture ended)
  98. lynx::commitAutomationPoints (song, w, { { 2.5 * bar, 0.9f }, { 2.6 * bar, 1.0f } }, 0, true);
  99. int automationClips = 0;
  100. for (const auto& c : song.playlist)
  101. automationClips += c.kind == lynx::ClipKind::automation ? 1 : 0;
  102. CHECK (automationClips == 1 && song.automations.size() == 1);
  103. CHECK (clipOf().end() >= (std::int64_t) (2.6 * bar));
  104. const auto& a = song.automations[0];
  105. CHECK (std::abs (a.valueAt (150.0) - 0.25f) < 0.05f); // the start is not overwritten
  106. CHECK (std::abs (a.valueAt (1.2 * bar) - 0.4f) < 1e-6f); // held between the gestures
  107. CHECK (std::abs (a.valueAt (2.6 * bar) - 1.0f) < 1e-6f);
  108. // recording stops at 2.7 bars: the clip ends at the end of that bar
  109. lynx::growTakeClip (song, w, (std::int64_t) (2.7 * bar), true);
  110. CHECK (clipOf().end() == 3 * (std::int64_t) bar);
  111. }
  112. // Bug: a controller's pitch wheel played but was not recorded. Recording writes it as the channel's pitch bend
  113. // (MIDI pitch bend target, as for a hosted plugin); a LYNX instrument now plays that lane / clip as the wheel.
  114. TEST (issue12_recorded_pitch_bend_plays_on_a_lynx_instrument)
  115. {
  116. auto render = [] (bool withBend) {
  117. auto song = lynx::Song::makeDefault();
  118. song.mode = lynx::PlayMode::pattern;
  119. song.playlist.clear();
  120. auto& p = song.patterns[(std::size_t) song.currentPattern];
  121. p.notes.clear();
  122. lynx::Note n;
  123. n.channel = 4; // "Synth"
  124. n.key = 60;
  125. n.start = 0;
  126. n.length = 4 * lynx::ppq;
  127. p.notes.push_back (n);
  128. p.events.clear();
  129. if (withBend)
  130. {
  131. lynx::PatternEvents lane;
  132. lane.target = { lynx::AutomationTargetKind::channelParam, 4, 0, lynx::midictl::pitchBendBase };
  133. lane.events.push_back ({ 0, 1.0f }); // full bend up (+2 semitones, the channel's pitch range)
  134. p.events.push_back (lane);
  135. }
  136. auto state = lynx::RackState::build (song, nullptr, 48000.0);
  137. auto bound = false;
  138. for (const auto& b : state->pluginParams)
  139. bound = bound || (b.channel == 4 && b.param == lynx::midictl::pitchBendBase);
  140. lynx::RackRenderer renderer;
  141. renderer.prepare (48000.0, 512);
  142. std::vector<float> l (512), r (512), all;
  143. for (int block = 0; block < 16; ++block)
  144. {
  145. renderer.process (state.get(), true, block * 512.0 * lynx::ticksPerSample (song.bpm, 48000.0), 512, l.data(), r.data());
  146. all.insert (all.end(), l.begin(), l.end());
  147. }
  148. return std::make_pair (bound, all);
  149. };
  150. const auto plain = render (false), bent = render (true);
  151. CHECK (! plain.first && bent.first);
  152. // zero crossings over the same span: the bent note is higher (+2 semitones ≈ ×1.12)
  153. auto crossings = [] (const std::vector<float>& x) {
  154. int c = 0;
  155. for (std::size_t i = 4096; i + 1 < x.size(); ++i)
  156. c += (x[i] < 0.0f) != (x[i + 1] < 0.0f) ? 1 : 0;
  157. return c;
  158. };
  159. const auto a = crossings (plain.second), b = crossings (bent.second);
  160. // (C5 = 261.6 Hz: ~45 crossings in the 4096 samples measured; +2 semitones = x1.122)
  161. const auto ok = a > 30 && b * 100 > a * 105 && b * 100 < a * 120;
  162. if (! ok)
  163. std::printf (" pitch bend: %d crossings plain, %d bent\n", a, b);
  164. CHECK (ok);
  165. }
  166. // Issue #12: the Sampler's ADSR, cutoff, resonance, start and end are automation targets; the 0..1 values follow the
  167. // knobs' curves, and an automation clip / pattern event lane on them binds to the channel's instrument.
  168. TEST (issue12_sampler_knobs_are_automation_targets)
  169. {
  170. using namespace lynx;
  171. for (int k = 0; k < samplerparam::count; ++k)
  172. for (float v : { 0.0f, 0.25f, 0.5f, 0.9f, 1.0f })
  173. CHECK (std::abs (samplerparam::toValue (k, samplerparam::toNatural (k, v)) - v) < 1.0e-4f);
  174. // the knobs' mid points: attack 0.2 s, cutoff 1 kHz at half travel
  175. CHECK (std::abs (samplerparam::toNatural (samplerparam::attack, 0.5f) - 0.2f) < 1.0e-4f);
  176. CHECK (std::abs (samplerparam::toNatural (samplerparam::cutoff, 0.5f) - 1000.0f) < 0.05f);
  177. auto song = Song::makeDefault();
  178. song.channels[0].instrument = InstrumentKind::sampler;
  179. song.channels[0].sampler.flChannel = { 1, 2, 3 };
  180. const AutomationTarget cutoff { AutomationTargetKind::channelParam, 0, 0, samplerparam::cutoff };
  181. CHECK (song.setTargetValue (cutoff, 0.5f));
  182. CHECK (std::abs (song.channels[0].sampler.cutoff - 1000.0f) < 0.05f);
  183. CHECK (song.channels[0].sampler.flChannel.empty()); // like the knob turned by hand
  184. CHECK (std::abs (song.targetValue (cutoff) - 0.5f) < 1.0e-4f);
  185. CHECK (lasttweak::valueText (song, cutoff, 0.5f) == "1.0 kHz");
  186. CHECK (lasttweak::valueText (song, { AutomationTargetKind::channelParam, 0, 0, samplerparam::attack }, 0.5f) == "0.200 s");
  187. // an event lane on the sampler's End binds to the instrument
  188. auto& p = song.patterns[(std::size_t) song.currentPattern];
  189. PatternEvents lane;
  190. lane.target = { AutomationTargetKind::channelParam, 0, 0, samplerparam::end };
  191. lane.events.push_back ({ 0, 0.5f });
  192. p.events.push_back (lane);
  193. auto state = RackState::build (song, nullptr, 48000.0);
  194. auto bound = false;
  195. for (const auto& b : state->pluginParams)
  196. bound = bound || (b.channel == 0 && b.param == samplerparam::end);
  197. CHECK (bound);
  198. }
  199. // Issue #12: a route's level knob (the send knobs under the faders) is an automation target, also when the send sits
  200. // at 0 (the automation brings it up); moving inserts keeps the target on the same route.
  201. TEST (issue12_send_level_is_an_automation_target)
  202. {
  203. using namespace lynx;
  204. auto song = Song::makeDefault();
  205. while (song.mixer.size() < 4)
  206. song.mixer.emplace_back().name = "Insert " + std::to_string (song.mixer.size());
  207. song.setRoute (1, 2, true);
  208. song.setRouteLevel (1, 2, 0.0f);
  209. const AutomationTarget send { AutomationTargetKind::mixerSend, 1, 2 };
  210. CHECK (song.targetValue (send) == 0.0f);
  211. CHECK (song.setTargetValue (send, faderPosition (1.0f)));
  212. CHECK (std::abs (song.routeLevel (1, 2) - 1.0f) < 1.0e-4f);
  213. CHECK (lasttweak::valueText (song, send, faderPosition (1.0f)) == "0.0 dB");
  214. CHECK (song.targetValue ({ AutomationTargetKind::mixerSend, 1, 3 }) < 0.0f); // no such route
  215. song.setRouteLevel (1, 2, 0.0f);
  216. auto& p = song.patterns[(std::size_t) song.currentPattern];
  217. PatternEvents lane;
  218. lane.target = send;
  219. lane.events.push_back ({ 0, 0.8f });
  220. p.events.push_back (lane);
  221. auto state = RackState::build (song, nullptr, 48000.0);
  222. auto automated = false;
  223. for (const auto& in : state->mixerInputs[2])
  224. automated = automated || (in.source == 1 && in.automation >= 0);
  225. CHECK (automated);
  226. mixerops::swapTracks (song, 2, 3);
  227. CHECK (song.patterns[(std::size_t) song.currentPattern].events.back().target == (AutomationTarget { AutomationTargetKind::mixerSend, 1, 3 }));
  228. }
  229. // Crash (0.1.18, the user's crash report): cycling quickly through samples crashed in WaveformCache's disk-cache pruning
  230. // — std::sort's comparator read each file's access time on every comparison, the times changed during the sort, and
  231. // the sort ran past the array. The pruning now reads every key once (sortonce::byKey, used by engine/FileSort.h).
  232. TEST (issue12_sort_reads_each_changing_key_once)
  233. {
  234. // a key that changes on every read, like a cache file's access time while other threads read and delete files
  235. std::vector<int> items;
  236. for (int i = 0; i < 2000; ++i)
  237. items.push_back ((i * 7919) % 2000);
  238. std::vector<int> reads (2000, 0);
  239. unsigned clock = 12345;
  240. auto changingKey = [&] (int item) {
  241. ++reads[(std::size_t) item];
  242. clock = clock * 1103515245u + 12345u;
  243. return (long long) (clock >> 8) % 50; // (many ties too)
  244. };
  245. std::vector<long long> seen (2000, 0);
  246. auto recordingKey = [&] (int item) { return seen[(std::size_t) item] = changingKey (item); };
  247. lynx::sortonce::byKey<int> (items, recordingKey, [] (int a, int b) { return a < b; });
  248. CHECK (items.size() == 2000);
  249. CHECK (std::all_of (reads.begin(), reads.end(), [] (int n) { return n == 1; }));
  250. for (std::size_t i = 1; i < items.size(); ++i)
  251. {
  252. const auto a = seen[(std::size_t) items[i - 1]], b = seen[(std::size_t) items[i]];
  253. CHECK (a < b || (a == b && items[i - 1] < items[i]));
  254. }
  255. auto sorted = items;
  256. std::sort (sorted.begin(), sorted.end());
  257. for (int i = 0; i < 2000; ++i)
  258. CHECK (sorted[(std::size_t) i] == i); // nothing lost or duplicated
  259. // newest first
  260. std::vector<int> few { 3, 1, 2 };
  261. lynx::sortonce::byKey<int> (few, [] (int v) { return v * 10; }, [] (int a, int b) { return a < b; }, true);
  262. CHECK ((few == std::vector<int> { 3, 2, 1 }));
  263. }
  264. // Round 165q's find (issue #12, shortcuts): with the Undo history, Settings, Tempo tapper, Quick-add, Audio editor or a
  265. // plugin / channel window focused, Space, F12 or a user's key did nothing — those windows only knew Esc. The keys they
  266. // leave unused now run the global actions (as in FL), never a panel's keys, and a focused text field keeps its typing.
  267. TEST (issue12_secondary_windows_run_the_global_shortcuts)
  268. {
  269. using namespace lynx::keys;
  270. KeyMap map;
  271. CHECK (map.findFromSecondaryWindow ({ space }, false, false) == "transport.playStop");
  272. CHECK (map.findFromSecondaryWindow ({ f12 }, false, false) == "view.closeWindows");
  273. CHECK (map.findFromSecondaryWindow ({ f10 }, false, false) == "view.midiSettings");
  274. CHECK (map.findFromSecondaryWindow ({ 'M', ctrl }, false, false) == "transport.metronome");
  275. CHECK (map.findFromSecondaryWindow ({ 'C' }, false, false).empty()); // the Playlist's Knife stays the Playlist's
  276. CHECK (map.findFromSecondaryWindow ({ escape }, false, false).empty()); // Esc closes the window
  277. // the user's key for a global action works there too (the 'C' Metronome of the report)
  278. map.setChords ("transport.metronome", { { 'C' } });
  279. CHECK (map.findFromSecondaryWindow ({ 'C' }, false, false) == "transport.metronome");
  280. // typing into a text field: its letters, Space, arrows, Enter are text; F-keys and Ctrl chords still work
  281. CHECK (map.findFromSecondaryWindow ({ 'C' }, true, false).empty());
  282. CHECK (map.findFromSecondaryWindow ({ space }, true, false).empty());
  283. CHECK (map.findFromSecondaryWindow ({ left }, true, false).empty());
  284. CHECK (map.findFromSecondaryWindow ({ f12 }, true, false) == "view.closeWindows");
  285. // the typing piano owns its letters
  286. CHECK (map.findFromSecondaryWindow ({ 'C' }, false, true).empty());
  287. }
  288. // Issue #12: an automation clip on the Sampler's Cutoff reaches the sound (setParameter on the audio thread), keeps its
  289. // value when the song is re-applied unchanged, and lets go when the knob is turned by hand.
  290. TEST (issue12_sampler_cutoff_automation_changes_the_sound)
  291. {
  292. auto data = std::make_shared<lynx::SampleData>();
  293. data->sampleRate = 48000.0;
  294. for (int i = 0; i < 6000; ++i)
  295. data->left.push_back (std::sin (0.6f * (float) i) * 0.25f); // ~4.6 kHz
  296. lynx::ChannelDef c;
  297. c.instrument = lynx::InstrumentKind::sampler;
  298. auto energyOf = [] (lynx::Instrument& sampler) {
  299. sampler.allNotesOff();
  300. sampler.noteOn (60, 0.8f);
  301. std::vector<float> l (4000), r (4000);
  302. sampler.render (l.data(), r.data(), 4000, 0.7f, 0.7f);
  303. double e = 0.0;
  304. for (std::size_t i = 1000; i < l.size(); ++i)
  305. e += (double) l[i] * l[i];
  306. return e;
  307. };
  308. auto sampler = lynx::makeInstrument (lynx::InstrumentKind::sampler, data);
  309. sampler->prepare (48000.0);
  310. sampler->configure (c, 120.0);
  311. const auto open = energyOf (*sampler);
  312. sampler->setParameter (lynx::samplerparam::cutoff, 0.0f); // the clip at its bottom: 20 Hz
  313. const auto automated = energyOf (*sampler);
  314. sampler->configure (c, 120.0); // the song re-applied unchanged (any edit elsewhere): the automation keeps its value
  315. const auto kept = energyOf (*sampler);
  316. c.sampler.cutoff = 19000.0f; // the knob turned by hand
  317. sampler->configure (c, 120.0);
  318. const auto byHand = energyOf (*sampler);
  319. CHECK (open > 1.0);
  320. CHECK (automated < open * 0.05);
  321. CHECK (kept < open * 0.05);
  322. CHECK (byHand > open * 0.5);
  323. }