NoteChance187cTests.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. // Round 187c: note Chance, Velocity Range and probability groups (Live 12's note probability; core/NoteChance.h).
  2. // The statistics run 10 000 passes of a loop through the real schedulers; reproducibility is checked across block sizes,
  3. // seeds, a save as .als and an import of what Live writes (Live 12.4's MidiNoteEvent attributes [DATA]).
  4. #include "core/AlsExport.h"
  5. #include "core/AlsImport.h"
  6. #include "core/NoteChance.h"
  7. #include "core/NoteTools.h"
  8. #include "core/RackRenderer.h"
  9. namespace nc187c
  10. {
  11. constexpr double rate = 48000.0;
  12. // a pattern of one bar with `notes`; one Sampler channel
  13. inline lynx::Song songWith (const std::vector<lynx::Note>& notes)
  14. {
  15. lynx::Song s;
  16. s.bpm = 120.0;
  17. s.channels.resize (1);
  18. auto& p = s.patterns.emplace_back();
  19. p.notes = notes;
  20. p.lengthTicks = 4 * lynx::ppq;
  21. p.sortNotes();
  22. return s;
  23. }
  24. inline lynx::Note note (std::int64_t start, int key, float velocity = 0.5f, float chance = 1.0f, float range = 0.0f)
  25. {
  26. lynx::Note n;
  27. n.start = start;
  28. n.length = lynx::ppq / 4;
  29. n.key = key;
  30. n.velocity = velocity;
  31. n.chance = chance;
  32. n.velocityRange = range;
  33. return n;
  34. }
  35. struct Event
  36. {
  37. std::int64_t sample = 0;
  38. int type = 0, key = 0;
  39. float velocity = 0.0f;
  40. bool operator== (const Event&) const = default;
  41. };
  42. // Pattern mode: `passes` loops of the pattern in blocks of `block` samples
  43. inline std::vector<Event> playPattern (const lynx::Song& s, int passes, int block)
  44. {
  45. const auto tps = lynx::ticksPerSample (s.bpm, rate);
  46. const auto& p = s.patterns[0];
  47. const auto length = p.effectiveLength (s.meter());
  48. const auto total = (std::int64_t) std::llround ((double) length * passes / tps);
  49. std::vector<Event> out;
  50. lynx::EventBuffer ev;
  51. for (std::int64_t at = 0; at < total; at += block)
  52. {
  53. ev.clear();
  54. const auto n = (int) std::min<std::int64_t> (block, total - at);
  55. lynx::schedulePatternLoop (p, length, (double) at * tps, n, tps, ev, 0.0f, &s.channels, false, -1.0, s.chanceSeed);
  56. for (const auto& e : ev)
  57. out.push_back ({ at + e.sampleOffset, (int) e.type, e.key, e.velocity });
  58. }
  59. return out;
  60. }
  61. // the "on"s per pass (pass = sample / samples per pass)
  62. inline std::vector<std::vector<Event>> onsByPass (const lynx::Song& s, const std::vector<Event>& events, int passes)
  63. {
  64. const auto tps = lynx::ticksPerSample (s.bpm, rate);
  65. const auto perPass = (double) s.patterns[0].effectiveLength (s.meter()) / tps;
  66. std::vector<std::vector<Event>> by ((std::size_t) passes);
  67. for (const auto& e : events)
  68. if (e.type == (int) lynx::NoteEvent::Type::on)
  69. by[(std::size_t) std::min<std::int64_t> (passes - 1, (std::int64_t) ((double) e.sample / perPass))].push_back (e);
  70. return by;
  71. }
  72. inline int count (const std::vector<Event>& events, lynx::NoteEvent::Type type, int key = -1)
  73. {
  74. return (int) std::count_if (events.begin(), events.end(), [&] (const Event& e) { return e.type == (int) type && (key < 0 || e.key == key); });
  75. }
  76. } // namespace nc187c
  77. TEST (nc187c_notes_without_chance_play_as_before)
  78. {
  79. using namespace nc187c;
  80. auto s = songWith ({ note (0, 60, 0.7f), note (lynx::ppq, 62, 0.3f), note (2 * lynx::ppq, 64, 1.0f) });
  81. CHECK (! s.patterns[0].notes[0].hasChance());
  82. const auto a = playPattern (s, 8, 512);
  83. s.chanceSeed = 12345; // the seed doesn't touch them
  84. CHECK (playPattern (s, 8, 512) == a);
  85. CHECK (count (a, lynx::NoteEvent::Type::on) == 24 && count (a, lynx::NoteEvent::Type::off) == 24);
  86. for (const auto& e : a)
  87. if (e.type == (int) lynx::NoteEvent::Type::on)
  88. CHECK (e.velocity == (e.key == 60 ? 0.7f : e.key == 62 ? 0.3f : 1.0f));
  89. }
  90. TEST (nc187c_chance_10000_passes)
  91. {
  92. using namespace nc187c;
  93. constexpr int passes = 10000;
  94. for (const auto p : { 0.3f, 0.5f, 0.9f })
  95. {
  96. // two notes of the same chance draw on their own; a 0 and a 1 never / always play
  97. auto s = songWith ({ note (0, 60, 0.5f, p), note (lynx::ppq, 61, 0.5f, p), note (2 * lynx::ppq, 62, 0.5f, 0.0f), note (3 * lynx::ppq, 63, 0.5f, 1.0f) });
  98. s.chanceSeed = 7;
  99. // a big block covers many passes: cheap, and the pass index comes from emitOccurrences' own count
  100. const auto ev = playPattern (s, passes, 8192);
  101. const auto on60 = count (ev, lynx::NoteEvent::Type::on, 60), on61 = count (ev, lynx::NoteEvent::Type::on, 61);
  102. const auto sigma = std::sqrt ((double) passes * p * (1.0 - p));
  103. std::printf (" chance %.2f: %d and %d of %d passes (expected %.0f, sigma %.1f)\n", p, on60, on61, passes, passes * p, sigma);
  104. CHECK (std::abs (on60 - passes * p) < 4.0 * sigma);
  105. CHECK (std::abs (on61 - passes * p) < 4.0 * sigma);
  106. CHECK (count (ev, lynx::NoteEvent::Type::on, 62) == 0 && count (ev, lynx::NoteEvent::Type::off, 62) == 0);
  107. CHECK (count (ev, lynx::NoteEvent::Type::on, 63) == passes);
  108. // every "on" that plays has its "off", and a note that doesn't play sends none
  109. CHECK (count (ev, lynx::NoteEvent::Type::off, 60) == on60 && count (ev, lynx::NoteEvent::Type::off, 61) == on61);
  110. // the two notes are independent: both play in about p² of the passes
  111. const auto by = onsByPass (s, ev, passes);
  112. int both = 0;
  113. for (const auto& pass : by)
  114. both += count (pass, lynx::NoteEvent::Type::on, 60) == 1 && count (pass, lynx::NoteEvent::Type::on, 61) == 1;
  115. const auto pp = (double) p * p;
  116. CHECK (std::abs (both - passes * pp) < 4.0 * std::sqrt (passes * pp * (1.0 - pp)) + 1.0);
  117. // and from one pass to the next (no stuck draw): runs of "played" as long as a fair coin's
  118. int changes = 0;
  119. for (int k = 1; k < passes; ++k)
  120. changes += (count (by[(std::size_t) k], lynx::NoteEvent::Type::on, 60) == 1) != (count (by[(std::size_t) k - 1], lynx::NoteEvent::Type::on, 60) == 1);
  121. const auto expectedChanges = 2.0 * p * (1.0 - p) * (passes - 1);
  122. CHECK (std::abs (changes - expectedChanges) < 5.0 * std::sqrt (expectedChanges) + 1.0);
  123. }
  124. }
  125. TEST (nc187c_velocity_range_is_even_between_velocity_and_velocity_plus_range)
  126. {
  127. using namespace nc187c;
  128. constexpr int passes = 10000;
  129. // Live's manual: +20 on velocity 60 plays between 60 and 80; a negative range goes down
  130. for (const auto range : { 20.0f / 127.0f, -30.0f / 127.0f })
  131. {
  132. auto s = songWith ({ note (0, 60, 60.0f / 127.0f, 1.0f, range) });
  133. s.chanceSeed = 99;
  134. const auto ev = playPattern (s, passes, 8192);
  135. double sum = 0.0, lo = 2.0, hi = -1.0;
  136. int quarter[4] = {};
  137. for (const auto& e : ev)
  138. if (e.type == (int) lynx::NoteEvent::Type::on)
  139. {
  140. sum += e.velocity;
  141. lo = std::min (lo, (double) e.velocity);
  142. hi = std::max (hi, (double) e.velocity);
  143. const auto u = ((double) e.velocity - 60.0 / 127.0) / (double) range;
  144. ++quarter[std::clamp ((int) (u * 4.0), 0, 3)];
  145. }
  146. CHECK (count (ev, lynx::NoteEvent::Type::on) == passes);
  147. const auto mean = sum / passes, expected = 60.0 / 127.0 + range * 0.5;
  148. std::printf (" velocity range %+.0f: mean %.2f (expected %.2f), %.2f..%.2f, quarters %d %d %d %d\n", range * 127.0, mean * 127.0,
  149. expected * 127.0, lo * 127.0, hi * 127.0, quarter[0], quarter[1], quarter[2], quarter[3]);
  150. CHECK (std::abs (mean - expected) < 0.01 * std::abs (range) + 0.002);
  151. CHECK (std::min (lo, hi) >= std::min (60.0, 60.0 + range * 127.0) / 127.0 - 1e-6);
  152. CHECK (std::max (lo, hi) <= std::max (60.0, 60.0 + range * 127.0) / 127.0 + 1e-6);
  153. for (auto q : quarter)
  154. CHECK (std::abs (q - passes / 4) < 4.0 * std::sqrt (passes * 0.25 * 0.75));
  155. }
  156. // never below Live's lowest velocity (1) nor above 127
  157. auto s = songWith ({ note (0, 60, 5.0f / 127.0f, 1.0f, -1.0f), note (lynx::ppq, 61, 120.0f / 127.0f, 1.0f, 1.0f) });
  158. for (const auto& e : playPattern (s, 500, 8192))
  159. if (e.type == (int) lynx::NoteEvent::Type::on)
  160. CHECK (e.velocity >= 1.0f / 127.0f - 1e-6f && e.velocity <= 1.0f);
  161. }
  162. TEST (nc187c_probability_groups_play_all_and_play_one)
  163. {
  164. using namespace nc187c;
  165. constexpr int passes = 10000;
  166. // Play all, chance 0.5: each pass plays the three notes or none
  167. {
  168. std::vector<lynx::Note> notes { note (0, 60, 0.5f, 0.5f), note (lynx::ppq, 64, 0.5f, 0.5f), note (2 * lynx::ppq, 67, 0.5f, 0.5f) };
  169. for (auto& n : notes)
  170. n.chanceGroup = 1;
  171. auto s = songWith (notes);
  172. const auto ev = playPattern (s, passes, 8192);
  173. int all = 0, mixed = 0;
  174. for (const auto& pass : onsByPass (s, ev, passes))
  175. {
  176. all += pass.size() == 3;
  177. mixed += pass.size() != 3 && ! pass.empty();
  178. }
  179. std::printf (" play all: %d of %d passes played the group, %d mixed\n", all, passes, mixed);
  180. CHECK (mixed == 0);
  181. CHECK (std::abs (all - passes / 2) < 4.0 * std::sqrt (passes * 0.25));
  182. }
  183. // Play one, chance 1: exactly one note a pass, each a third of the time; another group and a free note beside it
  184. {
  185. std::vector<lynx::Note> notes { note (0, 60), note (lynx::ppq, 64), note (2 * lynx::ppq, 67), note (0, 72), note (lynx::ppq, 76), note (3 * lynx::ppq, 48) };
  186. for (int i = 0; i < 3; ++i)
  187. notes[(std::size_t) i].chanceGroup = 1, notes[(std::size_t) i].chanceOne = true;
  188. for (int i = 3; i < 5; ++i)
  189. notes[(std::size_t) i].chanceGroup = 2, notes[(std::size_t) i].chanceOne = true;
  190. auto s = songWith (notes);
  191. const auto ev = playPattern (s, passes, 8192);
  192. std::map<int, int> byKey;
  193. bool exactlyOne = true;
  194. for (const auto& pass : onsByPass (s, ev, passes))
  195. {
  196. int g1 = 0, g2 = 0, free = 0;
  197. for (const auto& e : pass)
  198. {
  199. ++byKey[e.key];
  200. g1 += e.key == 60 || e.key == 64 || e.key == 67;
  201. g2 += e.key == 72 || e.key == 76;
  202. free += e.key == 48;
  203. }
  204. exactlyOne = exactlyOne && g1 == 1 && g2 == 1 && free == 1;
  205. }
  206. std::printf (" play one: C %d, E %d, G %d | %d %d\n", byKey[60], byKey[64], byKey[67], byKey[72], byKey[76]);
  207. CHECK (exactlyOne);
  208. for (const auto key : { 60, 64, 67 })
  209. CHECK (std::abs (byKey[key] - passes / 3.0) < 4.0 * std::sqrt (passes * (1.0 / 3.0) * (2.0 / 3.0)));
  210. for (const auto key : { 72, 76 })
  211. CHECK (std::abs (byKey[key] - passes / 2.0) < 4.0 * std::sqrt (passes * 0.25));
  212. }
  213. }
  214. TEST (nc187c_draws_are_reproducible_and_seeded)
  215. {
  216. using namespace nc187c;
  217. auto s = songWith ({ note (0, 60, 0.5f, 0.5f, 0.2f), note (lynx::ppq / 2, 62, 0.6f, 0.3f), note (lynx::ppq * 3, 64, 0.4f, 0.7f, -0.1f) });
  218. s.chanceSeed = 2026;
  219. // the same whatever the block size: the draw belongs to the pass, not to the block
  220. const auto a = playPattern (s, 64, 512);
  221. CHECK (playPattern (s, 64, 333) == a);
  222. CHECK (playPattern (s, 64, 4096) == a);
  223. // another seed plays otherwise
  224. auto t = s;
  225. t.chanceSeed = 2027;
  226. CHECK (playPattern (t, 64, 512) != a);
  227. // an edit to one note leaves the others' draws alone
  228. auto u = s;
  229. u.patterns[0].notes[2].velocity = 0.9f;
  230. const auto b = playPattern (u, 64, 512);
  231. const auto keyOnly = [] (const std::vector<Event>& v, int key) {
  232. std::vector<std::int64_t> r;
  233. for (const auto& e : v)
  234. if (e.key == key && e.type == (int) lynx::NoteEvent::Type::on)
  235. r.push_back (e.sample);
  236. return r;
  237. };
  238. CHECK (keyOnly (a, 60) == keyOnly (b, 60) && keyOnly (a, 62) == keyOnly (b, 62));
  239. }
  240. TEST (nc187c_song_clips_draw_on_their_own)
  241. {
  242. using namespace nc187c;
  243. auto s = songWith ({ note (0, 60, 0.8f, 0.5f), note (lynx::ppq, 62, 0.8f, 0.5f), note (2 * lynx::ppq, 64, 0.8f, 0.5f), note (3 * lynx::ppq, 65, 0.8f, 0.5f) });
  244. s.mode = lynx::PlayMode::song;
  245. s.chanceSeed = 5;
  246. // 64 clips of the pattern one after the other: 256 notes at 0.5
  247. for (int k = 0; k < 64; ++k)
  248. {
  249. lynx::Clip c;
  250. c.pattern = 0;
  251. c.start = (std::int64_t) k * 4 * lynx::ppq;
  252. c.length = 4 * lynx::ppq;
  253. s.playlist.push_back (c);
  254. }
  255. const auto tps = lynx::ticksPerSample (s.bpm, rate);
  256. const auto index = lynx::makeScheduleIndex (s);
  257. auto play = [&] (const lynx::Song& song, int block) {
  258. std::vector<Event> out;
  259. lynx::EventBuffer ev;
  260. const auto total = (std::int64_t) std::llround ((double) index.songLength / tps);
  261. for (std::int64_t at = 0; at < total; at += block)
  262. {
  263. ev.clear();
  264. lynx::scheduleSong (song, (double) at * tps, (int) std::min<std::int64_t> (block, total - at), tps, ev, at == 0, &index, false);
  265. for (const auto& e : ev)
  266. out.push_back ({ at + e.sampleOffset, (int) e.type, e.key, e.velocity });
  267. }
  268. return out;
  269. };
  270. const auto a = play (s, 512);
  271. const auto ons = count (a, lynx::NoteEvent::Type::on);
  272. std::printf (" 64 clips: %d of 256 notes played\n", ons);
  273. CHECK (std::abs (ons - 128) < 4.0 * 8.0);
  274. CHECK (count (a, lynx::NoteEvent::Type::off) == ons);
  275. CHECK (play (s, 700) == a);
  276. // the clips don't all repeat the first clip's pattern of draws
  277. const auto perClip = (double) 4 * lynx::ppq / tps;
  278. std::set<int> masks;
  279. for (int k = 0; k < 64; ++k)
  280. {
  281. int mask = 0;
  282. for (const auto& e : a)
  283. if (e.type == (int) lynx::NoteEvent::Type::on && (int) ((double) e.sample / perClip) == k)
  284. mask |= 1 << (e.key - 60);
  285. masks.insert (mask);
  286. }
  287. CHECK (masks.size() >= 8);
  288. // the realised notes (MIDI export): the first pass as it plays
  289. const auto real = lynx::chance::realised (s.patterns[0].notes, 5);
  290. CHECK (real.size() <= 4);
  291. for (const auto& n : real)
  292. CHECK (! n.hasChance());
  293. CHECK (lynx::chance::realised (s.patterns[0].notes, 5).size() == real.size());
  294. }
  295. TEST (nc187c_als_probability_and_velocity_deviation_round_trip)
  296. {
  297. using namespace nc187c;
  298. // what Live 12.4 writes (attribute order of a Live-saved set [DATA]: Time Duration Velocity VelocityDeviation
  299. // OffVelocity Probability IsEnabled NoteId; Live 12 leaves out the defaults)
  300. auto s = lynx::Song::makeDefault();
  301. s.mode = lynx::PlayMode::song;
  302. auto& p = s.patterns[0];
  303. p.notes.clear();
  304. p.notes = { note (0, 60, 100.0f / 127.0f, 0.7083332539f), note (lynx::ppq, 62, 60.0f / 127.0f, 1.0f, 20.0f / 127.0f),
  305. note (2 * lynx::ppq, 64, 90.0f / 127.0f, 0.25f, -12.0f / 127.0f), note (3 * lynx::ppq, 65, 0.5f) };
  306. for (auto& n : p.notes)
  307. n.channel = 0;
  308. p.notes[1].release = 30.0f / 127.0f;
  309. p.lengthTicks = 4 * lynx::ppq;
  310. s.playlist.clear();
  311. lynx::Clip c;
  312. c.pattern = 0;
  313. c.length = 4 * lynx::ppq;
  314. s.playlist.push_back (c);
  315. for (const auto version : { lynx::AlsVersion::live12, lynx::AlsVersion::live11 })
  316. {
  317. lynx::AlsExportOptions o;
  318. o.version = version;
  319. const auto out = lynx::exportAls (s, o);
  320. const auto has = [&out] (const char* text) { return out.xml.find (text) != std::string::npos; };
  321. CHECK (has ("OffVelocity=\"63.5\" Probability=\"0.70833325"));
  322. CHECK (has ("VelocityDeviation=\"20\" OffVelocity=\"30.0000"));
  323. CHECK (has ("VelocityDeviation=\"-12\" OffVelocity=\"63.5\" Probability=\"0.25\""));
  324. if (version == lynx::AlsVersion::live12) // the defaults left out
  325. CHECK (has ("Velocity=\"63.5\" OffVelocity=\"63.5\" NoteId"));
  326. else
  327. CHECK (has ("Velocity=\"63.5\" VelocityDeviation=\"0\" OffVelocity=\"63.5\" Probability=\"1\" IsEnabled=\"true\""));
  328. std::string error;
  329. const auto back = lynx::importAls (out.bytes, {}, &error);
  330. CHECK (back.has_value());
  331. if (! back)
  332. continue;
  333. std::vector<lynx::Note> got;
  334. for (const auto& pat : back->patterns)
  335. got.insert (got.end(), pat.notes.begin(), pat.notes.end());
  336. std::sort (got.begin(), got.end(), [] (const lynx::Note& a, const lynx::Note& b) { return a.key < b.key; });
  337. CHECK (got.size() == 4);
  338. if (got.size() != 4)
  339. continue;
  340. CHECK (std::abs (got[0].chance - 0.7083332539f) < 1e-5f && got[0].velocityRange == 0.0f);
  341. CHECK (got[1].chance == 1.0f && std::abs (got[1].velocityRange * 127.0f - 20.0f) < 1e-3f);
  342. CHECK (std::abs (got[1].release * 127.0f - 30.0f) < 1e-3f);
  343. CHECK (std::abs (got[2].chance - 0.25f) < 1e-6f && std::abs (got[2].velocityRange * 127.0f + 12.0f) < 1e-3f);
  344. CHECK (! got[3].hasChance());
  345. }
  346. }
  347. TEST (nc187c_renders_are_reproducible)
  348. {
  349. using namespace nc187c;
  350. // a synth plays a bar of 16th notes at chance 0.5 with a velocity range, Pattern mode, four passes
  351. std::vector<lynx::Note> notes;
  352. for (int k = 0; k < 16; ++k)
  353. notes.push_back (note ((std::int64_t) k * lynx::ppq / 4, 60 + (k % 5), 0.6f, 0.5f, 0.3f));
  354. auto s = songWith (notes);
  355. s.channels[0].instrument = lynx::InstrumentKind::synth;
  356. s.mixer.resize (2);
  357. s.chanceSeed = 11;
  358. auto render = [] (const lynx::Song& song, int block) {
  359. auto state = lynx::RackState::build (song, nullptr, rate);
  360. lynx::RackRenderer rr;
  361. rr.prepare (rate, 1024);
  362. const auto total = (int) (8.0 * rate);
  363. std::vector<float> l ((std::size_t) total), r ((std::size_t) total);
  364. const auto tps = lynx::ticksPerSample (song.bpm, rate);
  365. double tick = 0.0;
  366. for (int at = 0; at < total; at += block)
  367. {
  368. const auto n = std::min (block, total - at);
  369. rr.process (state.get(), true, tick, n, l.data() + at, r.data() + at);
  370. tick += n * tps;
  371. }
  372. return l;
  373. };
  374. const auto a = render (s, 512);
  375. double energy = 0.0;
  376. for (auto v : a)
  377. energy += (double) v * v;
  378. CHECK (energy > 0.0);
  379. CHECK (render (s, 512) == a); // the same project renders the same, sample for sample
  380. auto t = s;
  381. t.chanceSeed = 12;
  382. CHECK (render (t, 512) != a); // another seed, other draws
  383. // with every chance at 1 and no range the notes play exactly as before the feature
  384. auto plain = s, full = s;
  385. for (auto& n : plain.patterns[0].notes)
  386. n.chance = 1.0f, n.velocityRange = 0.0f;
  387. for (auto& n : full.patterns[0].notes)
  388. n.chance = 1.0f, n.velocityRange = 0.0f;
  389. full.chanceSeed = 999;
  390. CHECK (render (plain, 512) == render (full, 512));
  391. }
  392. TEST (nc187c_group_tools)
  393. {
  394. using namespace nc187c;
  395. lynx::Pattern p;
  396. p.notes = { note (0, 60, 0.5f, 0.4f), note (0, 64, 0.5f, 0.9f), note (0, 67), note (lynx::ppq, 60) };
  397. lynx::notetools::groupChance (p, { 0, 1, 2 }, true);
  398. CHECK (p.notes[0].chanceGroup == 1 && p.notes[1].chanceGroup == 1 && p.notes[2].chanceGroup == 1 && p.notes[3].chanceGroup == 0);
  399. CHECK (p.notes[0].chance == 1.0f && p.notes[1].chance == 1.0f && p.notes[2].chanceOne); // a new group is at 100 % (Live)
  400. p.notes[2].chance = 0.75f;
  401. lynx::notetools::spreadGroupChance (p, { 2 });
  402. CHECK (p.notes[0].chance == 0.75f && p.notes[1].chance == 0.75f && p.notes[3].chance == 1.0f);
  403. lynx::notetools::groupChance (p, { 3 }, false); // one note: no group
  404. CHECK (p.notes[3].chanceGroup == 0);
  405. // pasted copies of a group form a group of their own
  406. p.notes.push_back (p.notes[0]);
  407. p.notes.push_back (p.notes[1]);
  408. lynx::notetools::regroup (p, { 4, 5 });
  409. CHECK (p.notes[4].chanceGroup == 2 && p.notes[5].chanceGroup == 2 && p.notes[0].chanceGroup == 1);
  410. lynx::notetools::ungroupChance (p, { 0, 1, 2 });
  411. CHECK (p.notes[0].chanceGroup == 0 && ! p.notes[2].chanceOne && p.notes[0].chance == 0.75f);
  412. }
  413. TEST (nc187c_als_probability_groups)
  414. {
  415. using namespace nc187c;
  416. // the test set of round 187c: C/D Play One and E/F# Play All, as Live 12.4 saved it (live/nc-saved Project)
  417. auto s = lynx::Song::makeDefault();
  418. s.mode = lynx::PlayMode::song;
  419. auto& p = s.patterns[0];
  420. p.notes.clear();
  421. for (int k = 0; k < 8; ++k)
  422. {
  423. auto n = note ((std::int64_t) k * lynx::ppq / 2, 60 + (k % 4) * 2, (60.0f + 8.0f * k) / 127.0f, k == 5 ? 0.1f : 0.5f);
  424. n.chanceGroup = (k % 4) < 2 ? 1 : 2;
  425. n.chanceOne = (k % 4) < 2;
  426. n.chance = 1.0f;
  427. p.notes.push_back (n);
  428. }
  429. p.notes[2].chance = p.notes[3].chance = p.notes[6].chance = p.notes[7].chance = 0.5f; // the Play All group at 50 %
  430. p.lengthTicks = 4 * lynx::ppq;
  431. s.playlist.clear();
  432. lynx::Clip c;
  433. c.pattern = 0;
  434. c.length = 4 * lynx::ppq;
  435. s.playlist.push_back (c);
  436. const auto out = lynx::exportAls (s, {});
  437. const auto has = [&out] (const char* text) { return out.xml.find (text) != std::string::npos; };
  438. // NoteIds go key by key (C: 1 2, D: 3 4, E: 5 6, F#: 7 8), as Live numbers them
  439. CHECK (has ("<NoteProbabilityGroup Id=\"0\">"));
  440. CHECK (has ("<GroupType Value=\"1\" />"));
  441. CHECK (has ("<GroupType Value=\"0\" />"));
  442. CHECK (has ("<NoteId NoteId=\"4\" />"));
  443. CHECK (has ("<Probability Value=\"0.5\" />"));
  444. CHECK (has ("<GroupId Value=\"2\" />"));
  445. std::string error;
  446. auto check = [&] (const std::vector<std::uint8_t>& bytes, float allChance) {
  447. const auto back = lynx::importAls (bytes, {}, &error);
  448. CHECK (back.has_value());
  449. if (! back)
  450. return;
  451. std::vector<lynx::Note> got;
  452. for (const auto& pat : back->patterns)
  453. got.insert (got.end(), pat.notes.begin(), pat.notes.end());
  454. CHECK (got.size() == 8);
  455. for (const auto& n : got)
  456. {
  457. const auto cd = n.key == 60 || n.key == 62;
  458. CHECK (n.chanceGroup > 0 && n.chanceOne == cd);
  459. CHECK (n.chance == (cd ? 1.0f : allChance));
  460. }
  461. std::set<int> ids;
  462. for (const auto& n : got)
  463. ids.insert (n.chanceGroup);
  464. CHECK (ids.size() == 2);
  465. };
  466. check (out.bytes, 0.5f);
  467. // Live's own block (12.4 save of this set, groups as above at 100 %), put in place of LYNX's
  468. auto xml = out.xml;
  469. const auto from = xml.find ("<NoteProbabilityGroups>"), to = xml.find ("</NoteProbabilityGroups>");
  470. CHECK (from != std::string::npos && to != std::string::npos);
  471. if (from == std::string::npos || to == std::string::npos)
  472. return;
  473. xml.replace (from, to - from, "<NoteProbabilityGroups><NoteProbabilityGroup Id=\"0\"><GroupType Value=\"1\" /><Notes><NoteId NoteId=\"1\" />"
  474. "<NoteId NoteId=\"2\" /><NoteId NoteId=\"3\" /><NoteId NoteId=\"4\" /></Notes><Probability Value=\"1\" />"
  475. "<GroupId Value=\"1\" /></NoteProbabilityGroup><NoteProbabilityGroup Id=\"2\"><GroupType Value=\"0\" /><Notes>"
  476. "<NoteId NoteId=\"5\" /><NoteId NoteId=\"6\" /><NoteId NoteId=\"7\" /><NoteId NoteId=\"8\" /></Notes>"
  477. "<Probability Value=\"1\" /><GroupId Value=\"3\" /></NoteProbabilityGroup>");
  478. check (std::vector<std::uint8_t> (xml.begin(), xml.end()), 1.0f);
  479. }