LiveWf190efMapTests.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Round 190ef: the reference DAW's MIDI Map Mode / Key Map Mode rules (core/RemoteMap.h): mapping ranges (Min / Max,
  2. // inverted), button edges, the lesson's two refusals, the Mapping Browser's list model, mappings following their scene
  3. // rows, and what a MIDI event or a key fires. docs/analise/REA_rodada190ef.md.
  4. #include "core/ClipLauncher.h"
  5. #include "core/RemoteMap.h"
  6. namespace live190efmap
  7. {
  8. inline lynx::Song gridSong()
  9. {
  10. lynx::Song s;
  11. s.playlistTracks.resize (3);
  12. if (s.patterns.empty())
  13. s.patterns.emplace_back();
  14. lynx::Clip c;
  15. c.pattern = 0;
  16. c.length = 4 * lynx::ppq;
  17. lynx::launcher::setClip (s, 0, 0, c);
  18. lynx::launcher::setClip (s, 1, 1, c);
  19. lynx::launcher::insertScene (s, 2); // three scenes (setClip made two)
  20. return s;
  21. }
  22. inline const lynx::AutomationTarget volume { lynx::AutomationTargetKind::mixerVolume, 1, 0, 0 };
  23. inline const lynx::AutomationTarget pan { lynx::AutomationTargetKind::mixerPan, 1, 0, 0 };
  24. } // namespace live190efmap
  25. TEST (live190ef_map_range_maps_the_travel_onto_min_max)
  26. {
  27. using namespace lynx::remotemap;
  28. CHECK (applyRange (0.0f, 0.0f, 1.0f) == 0.0f && applyRange (1.0f, 0.0f, 1.0f) == 1.0f);
  29. CHECK (std::abs (applyRange (0.5f, 0.0f, 1.0f) - 0.5f) < 1e-6f); // the default range keeps the position
  30. CHECK (std::abs (applyRange (0.0f, 0.25f, 0.75f) - 0.25f) < 1e-6f);
  31. CHECK (std::abs (applyRange (1.0f, 0.25f, 0.75f) - 0.75f) < 1e-6f);
  32. CHECK (std::abs (applyRange (0.5f, 0.25f, 0.75f) - 0.5f) < 1e-6f);
  33. // Min above Max inverts (the lesson: "Mappings can be inverted by setting the value of Min to be greater than Max")
  34. CHECK (std::abs (applyRange (0.0f, 1.0f, 0.0f) - 1.0f) < 1e-6f && std::abs (applyRange (1.0f, 1.0f, 0.0f)) < 1e-6f);
  35. CHECK (std::abs (applyRange (0.25f, 0.8f, 0.2f) - 0.65f) < 1e-6f);
  36. // out-of-range input and a range outside 0..1 stay inside 0..1
  37. CHECK (applyRange (2.0f, 0.0f, 1.0f) == 1.0f && applyRange (-1.0f, 0.0f, 1.0f) == 0.0f);
  38. CHECK (applyRange (std::nanf (""), 0.0f, 1.0f) == 0.0f);
  39. }
  40. TEST (live190ef_map_key_toggles_between_min_and_max)
  41. {
  42. using namespace lynx::remotemap;
  43. // round 190em (Live 12.4.16, live check 190ei: Track Volume at 0 dB, the 1st press -inf, the 2nd +6 dB, the 3rd -inf)
  44. CHECK (keyToggle (0.3f, 0.0f, 1.0f) == 0.0f); // anywhere but Min: to Min first
  45. CHECK (keyToggle (0.0f, 0.0f, 1.0f) == 1.0f); // at Min: to Max
  46. CHECK (keyToggle (1.0f, 0.0f, 1.0f) == 0.0f); // at Max: to Min
  47. {
  48. auto v = 0.8f; // three presses from "0 dB"
  49. v = keyToggle (v, 0.0f, 1.0f);
  50. CHECK (v == 0.0f);
  51. v = keyToggle (v, 0.0f, 1.0f);
  52. CHECK (v == 1.0f);
  53. v = keyToggle (v, 0.0f, 1.0f);
  54. CHECK (v == 0.0f);
  55. }
  56. CHECK (std::abs (keyToggle (0.75f, 0.25f, 0.75f) - 0.25f) < 1e-6f);
  57. CHECK (std::abs (keyToggle (0.25f, 0.25f, 0.75f) - 0.75f) < 1e-6f);
  58. CHECK (std::abs (keyToggle (0.5f, 0.25f, 0.75f) - 0.25f) < 1e-6f);
  59. // inverted: Min is the higher value, the toggle still alternates
  60. CHECK (std::abs (keyToggle (0.5f, 0.9f, 0.1f) - 0.9f) < 1e-6f && std::abs (keyToggle (0.9f, 0.9f, 0.1f) - 0.1f) < 1e-6f);
  61. }
  62. TEST (live190ef_map_button_edges)
  63. {
  64. using namespace lynx::remotemap;
  65. const auto note = lynx::linkNoteBase + 36;
  66. CHECK (midiEdge (note, 100, -1) == Edge::press && midiEdge (note, 0, 100) == Edge::release);
  67. CHECK (midiEdge (note, 1, 1) == Edge::press); // every note on presses, even at the same velocity
  68. CHECK (midiEdge (20, 127, -1) == Edge::press && midiEdge (20, 0, 127) == Edge::release);
  69. CHECK (midiEdge (20, 127, 127) == Edge::none && midiEdge (20, 80, 127) == Edge::none); // still down
  70. CHECK (midiEdge (20, 63, -1) == Edge::none && midiEdge (20, 64, 63) == Edge::press && midiEdge (20, 63, 64) == Edge::release);
  71. }
  72. TEST (live190ef_map_assigning_a_parameter_makes_one_link)
  73. {
  74. using namespace lynx::remotemap;
  75. lynx::Song s;
  76. CHECK (assignMidi (s, Target::parameter (live190efmap::volume), { 0, 16, -1 }) == Refusal::none);
  77. CHECK (s.midiLinks.size() == 1 && s.midiLinks[0].cc == 16 && s.midiLinks[0].channel == 0 && s.midiLinks[0].target == live190efmap::volume);
  78. CHECK (s.remoteMappings.empty()); // (a parameter's MIDI mapping is FL's link: one storage)
  79. // a new control for the same parameter replaces the old one; the same control may drive another parameter too
  80. CHECK (assignMidi (s, Target::parameter (live190efmap::volume), { 0, 17, 3 }) == Refusal::none);
  81. CHECK (s.midiLinks.size() == 1 && s.midiLinks[0].cc == 17 && s.midiLinks[0].port == 3);
  82. CHECK (assignMidi (s, Target::parameter (live190efmap::pan), { 0, 17, 3 }) == Refusal::none);
  83. CHECK (s.midiLinks.size() == 2);
  84. const auto a = midiOf (s, Target::parameter (live190efmap::pan));
  85. CHECK (a.found && a.channel == 0 && a.number == 17);
  86. CHECK (assignMidi (s, Target::parameter ({}), { 0, 1, -1 }) == Refusal::invalid);
  87. CHECK (assignMidi (s, Target::parameter (live190efmap::pan), { 0, 200, -1 }) == Refusal::invalid);
  88. }
  89. TEST (live190ef_map_slot_and_scene_rules_of_the_lesson)
  90. {
  91. using namespace lynx::remotemap;
  92. auto s = live190efmap::gridSong();
  93. const MidiControl pad { 9, lynx::linkNoteBase + 36, -1 };
  94. CHECK (assignMidi (s, Target::slot (0, 0), pad) == Refusal::none);
  95. // "can be assigned to multiple clips so long as they all reside in different tracks"
  96. CHECK (assignMidi (s, Target::slot (1, 1), pad) == Refusal::none);
  97. CHECK (assignMidi (s, Target::slot (0, 2), pad) == Refusal::sameTrack);
  98. // "you can't assign a MIDI controller to a scene if it has already been assigned to a clip"
  99. CHECK (assignMidi (s, Target::sceneLaunch (1), pad) == Refusal::usedBySlot);
  100. CHECK (assignMidi (s, Target::sceneLaunch (1), { 9, lynx::linkNoteBase + 37, -1 }) == Refusal::none);
  101. CHECK (s.remoteMappings.size() == 3);
  102. // the slot's mapping again with another control: replaced, not added
  103. CHECK (assignMidi (s, Target::slot (0, 0), { 9, lynx::linkNoteBase + 40, -1 }) == Refusal::none);
  104. CHECK (s.remoteMappings.size() == 3 && midiOf (s, Target::slot (0, 0)).number == lynx::linkNoteBase + 40);
  105. // now track 0 is free for the first pad again
  106. CHECK (assignMidi (s, Target::slot (0, 2), pad) == Refusal::none);
  107. // keys follow the same rules
  108. CHECK (assignKey (s, Target::slot (0, 0), 'A') == Refusal::none);
  109. CHECK (assignKey (s, Target::slot (0, 1), 'A') == Refusal::sameTrack);
  110. CHECK (assignKey (s, Target::slot (2, 1), 'A') == Refusal::none);
  111. CHECK (assignKey (s, Target::sceneLaunch (0), 'A') == Refusal::usedBySlot);
  112. CHECK (assignKey (s, Target::of (lynx::RemoteAction::play), ' ') == Refusal::none);
  113. CHECK (assignKey (s, Target::of (lynx::RemoteAction::play), 0) == Refusal::invalid);
  114. CHECK (keyOf (s, Target::slot (2, 1)).key == 'A' && ! keyOf (s, Target::slot (1, 1)).found);
  115. // unmap: one kind only
  116. CHECK (unmap (s, Target::slot (0, 0), Mode::key) == 1 && midiOf (s, Target::slot (0, 0)).found && ! keyOf (s, Target::slot (0, 0)).found);
  117. CHECK (unmap (s, Target::slot (0, 0), Mode::midi) == 1 && ! midiOf (s, Target::slot (0, 0)).found);
  118. }
  119. TEST (live190ef_map_browser_rows_min_max_invert_delete)
  120. {
  121. using namespace lynx::remotemap;
  122. auto s = live190efmap::gridSong();
  123. assignMidi (s, Target::parameter (live190efmap::volume), { 0, 16, -1 });
  124. assignMidi (s, Target::slot (0, 0), { 0, 60, -1 });
  125. assignKey (s, Target::parameter (live190efmap::pan), 'Q');
  126. assignKey (s, Target::sceneLaunch (2), '1');
  127. const auto midi = rows (s, Mode::midi), keys = rows (s, Mode::key);
  128. CHECK (midi.size() == 2 && keys.size() == 2 && rows (s, Mode::off).empty());
  129. CHECK (midi[0].link && midi[0].hasRange && midi[0].number == 16 && midi[0].target == Target::parameter (live190efmap::volume));
  130. CHECK (! midi[1].link && ! midi[1].hasRange && midi[1].target == Target::slot (0, 0)); // a slot has no range
  131. CHECK (keys[0].key == 'Q' && keys[0].hasRange && keys[1].key == '1' && ! keys[1].hasRange);
  132. // Min / Max of a link and of a key mapping
  133. CHECK (setRange (s, midi[0], 0.2f, 0.9f) && s.midiLinks[0].min == 0.2f && s.midiLinks[0].max == 0.9f);
  134. CHECK (s.midiLinks[0].hasSettings()); // (saved with the link's other settings)
  135. CHECK (setRange (s, keys[0], 0.1f, 0.6f));
  136. CHECK (! setRange (s, midi[1], 0.1f, 0.6f)); // not for a slot
  137. CHECK (setRange (s, midi[0], -1.0f, 3.0f) && s.midiLinks[0].min == 0.0f && s.midiLinks[0].max == 1.0f); // clamped
  138. setRange (s, midi[0], 0.2f, 0.9f);
  139. CHECK (invertRange (s, rows (s, Mode::midi)[0]) && s.midiLinks[0].min == 0.9f && s.midiLinks[0].max == 0.2f);
  140. // Delete
  141. CHECK (removeRow (s, rows (s, Mode::midi)[1]) && rows (s, Mode::midi).size() == 1 && rows (s, Mode::key).size() == 2);
  142. CHECK (removeRow (s, rows (s, Mode::midi)[0]) && s.midiLinks.empty());
  143. CHECK (! removeRow (s, Row { true, 5 }));
  144. // the list's texts
  145. CHECK (controlText (16) == "CC 16" && controlText (lynx::linkNoteBase + 60) == "C3" && controlText (lynx::linkNoteBase + 61) == "C#3");
  146. CHECK (noteText (0) == "C-2" && noteText (127) == "G8");
  147. }
  148. TEST (live190ef_map_mappings_follow_their_scene_rows)
  149. {
  150. using namespace lynx::remotemap;
  151. auto s = live190efmap::gridSong();
  152. assignMidi (s, Target::slot (1, 1), { 0, 61, -1 });
  153. assignKey (s, Target::sceneLaunch (2), 'S');
  154. assignKey (s, Target::trackStop (1), 'T');
  155. lynx::launcher::insertScene (s, 1); // a row above: both move down
  156. CHECK (midiOf (s, Target::slot (1, 2)).found && ! midiOf (s, Target::slot (1, 1)).found);
  157. CHECK (keyOf (s, Target::sceneLaunch (3)).found);
  158. CHECK (keyOf (s, Target::trackStop (1)).found); // (a track's stop button has no row)
  159. lynx::launcher::removeScene (s, 2); // the slot's row goes: its mapping goes with it
  160. CHECK (! midiOf (s, Target::slot (1, 2)).found && s.remoteMappings.size() == 2);
  161. CHECK (keyOf (s, Target::sceneLaunch (2)).found); // the scene below moved up
  162. }
  163. TEST (live190ef_map_what_an_event_or_a_key_fires)
  164. {
  165. using namespace lynx::remotemap;
  166. auto s = live190efmap::gridSong();
  167. assignMidi (s, Target::slot (0, 0), { 0, 20, 2 });
  168. assignMidi (s, Target::slot (1, 1), { 0, 20, 2 });
  169. assignMidi (s, Target::of (lynx::RemoteAction::stopAllClips), { 0, 21, -1 });
  170. assignMidi (s, Target::parameter (live190efmap::volume), { 0, 22, -1 }); // (a link: never a "fire")
  171. auto fires = midiFires (s, 0, 20, 127, -1, 2);
  172. CHECK (fires.size() == 2 && fires[0].edge == Edge::press && fires[0].target == Target::slot (0, 0) && fires[1].target == Target::slot (1, 1));
  173. CHECK (midiFires (s, 0, 20, 127, -1, 3).empty()); // another port
  174. CHECK (midiFires (s, 1, 20, 127, -1, 2).empty()); // another channel
  175. CHECK (midiFires (s, 0, 20, 127, 127, 2).empty()); // no edge
  176. fires = midiFires (s, 0, 20, 0, 127, 2);
  177. CHECK (fires.size() == 2 && fires[0].edge == Edge::release);
  178. CHECK (midiFires (s, 0, 21, 100, 0, -1).size() == 1 && midiFires (s, 0, 22, 127, 0, -1).empty());
  179. // a mapping made for "any channel" hears every channel
  180. s.remoteMappings[2].channel = -1;
  181. CHECK (midiFires (s, 7, 21, 100, 0, -1).size() == 1);
  182. // keys: parameters fire too (they toggle), with their range
  183. assignKey (s, Target::parameter (live190efmap::pan), 'P');
  184. setRange (s, rows (s, Mode::key)[0], 0.2f, 0.8f);
  185. const auto keyFired = keyFires (s, 'P', true);
  186. CHECK (keyFired.size() == 1 && keyFired[0].target.action == lynx::RemoteAction::parameter && keyFired[0].min == 0.2f && keyFired[0].max == 0.8f);
  187. CHECK (keyFires (s, 'P', false)[0].edge == Edge::release && keyFires (s, 'X', true).empty());
  188. // the MIDI numbers the mappings take from the instruments
  189. std::uint64_t ccs[2] {}, notes[2] {};
  190. assignMidi (s, Target::sceneLaunch (0), { 0, lynx::linkNoteBase + 100, -1 });
  191. usedNumbers (s, ccs, notes);
  192. CHECK ((ccs[0] >> 20 & 1u) && (ccs[0] >> 21 & 1u) && ! (ccs[0] >> 22 & 1u) && (notes[1] >> (100 - 64) & 1u));
  193. }
  194. TEST (live190ef_map_parameter_mappings_follow_and_leave_with_their_target)
  195. {
  196. using namespace lynx::remotemap;
  197. lynx::Song s;
  198. s.channels.resize (2);
  199. assignKey (s, Target::parameter ({ lynx::AutomationTargetKind::channelVolume, 1, 0, 0 }), 'V');
  200. assignKey (s, Target::of (lynx::RemoteAction::metronome), 'M');
  201. auto visited = 0;
  202. s.forEachTarget ([&] (lynx::AutomationTarget&) { ++visited; });
  203. CHECK (visited >= 1);
  204. s.forEachTarget ([] (lynx::AutomationTarget& t) {
  205. if (t.kind == lynx::AutomationTargetKind::channelVolume)
  206. t.kind = lynx::AutomationTargetKind::none; // (as deleting the channel does)
  207. });
  208. s.removeDeadMidiLinks();
  209. CHECK (s.remoteMappings.size() == 1 && s.remoteMappings[0].action == lynx::RemoteAction::metronome);
  210. // a plain link has no settings until its range changes
  211. lynx::MidiLink l;
  212. CHECK (! l.hasSettings());
  213. l.max = 0.5f;
  214. CHECK (l.hasSettings());
  215. }