KeyGuess184aTests.inc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // --- Round 184a S11: "Guess from notes" for the project key (core/KeyGuess.h) on synthetic patterns.
  2. #include "core/KeyGuess.h"
  3. namespace keyguess184a
  4. {
  5. // One pattern on channel 0: `keys` in turn, each `len` ticks; channel 1 plays a kick on one key (percussion).
  6. inline lynx::Song songOf (std::initializer_list<int> keys, std::int64_t len = lynx::ppq)
  7. {
  8. lynx::Song s;
  9. s.channels.resize (2);
  10. s.patterns.resize (1);
  11. std::int64_t t = 0;
  12. for (auto k : keys)
  13. {
  14. lynx::Note n;
  15. n.channel = 0;
  16. n.key = k;
  17. n.start = t;
  18. n.length = len;
  19. s.patterns[0].notes.push_back (n);
  20. t += len;
  21. }
  22. for (int i = 0; i < 16; ++i) // a four-on-the-floor kick on C5, never a pitch
  23. {
  24. lynx::Note n;
  25. n.channel = 1;
  26. n.key = 61; // C#: would pull every guess off if it counted
  27. n.start = i * lynx::ppq;
  28. n.length = lynx::ppq / 4;
  29. s.patterns[0].notes.push_back (n);
  30. }
  31. return s;
  32. }
  33. inline int scaleIndex (const char* name)
  34. {
  35. const auto& list = lynx::notetools::flReferenceScales();
  36. for (int i = 0; i < (int) list.size(); ++i)
  37. if (std::string (list[(std::size_t) i].name) == name)
  38. return i;
  39. return -1;
  40. }
  41. } // namespace keyguess184a
  42. TEST (simple184a_keyguess_major_minor_modes)
  43. {
  44. using namespace lynx;
  45. using keyguess184a::scaleIndex;
  46. using keyguess184a::songOf;
  47. const auto major = scaleIndex ("Major (Ionian)"), minor = scaleIndex ("Minor Natural (Aeolian)");
  48. const auto dorian = scaleIndex ("Other Dorian"), mixolydian = scaleIndex ("Other Mixolydian"), harmonic = scaleIndex ("Minor Harmonic");
  49. CHECK (major >= 0 && minor >= 0 && dorian >= 0 && mixolydian >= 0 && harmonic >= 0);
  50. // C major: the scale up and down, starting and ending on C, with C and G held longest
  51. auto s = songOf ({ 60, 62, 64, 65, 67, 69, 71, 72, 67, 64, 60, 60, 67 });
  52. auto g = keyguess::guess (s);
  53. CHECK (g.size() == 3);
  54. CHECK (g[0].key == (projectkey::Key { 0, major }));
  55. CHECK (g[0].notes == 13 && g[0].notesInScale == 13); // the kick is not counted
  56. CHECK (g[0].score > g[1].score);
  57. // A minor: the same notes, on A and E
  58. s = songOf ({ 57, 59, 60, 62, 64, 65, 67, 69, 64, 60, 57, 57, 64, 45, 45 });
  59. g = keyguess::guess (s);
  60. CHECK (g[0].key == (projectkey::Key { 9, minor }));
  61. // D Dorian: D minor with a B natural, centred on D
  62. s = songOf ({ 62, 64, 65, 67, 69, 71, 72, 74, 69, 71, 62, 62, 50, 50, 57 });
  63. g = keyguess::guess (s);
  64. CHECK (g[0].key == (projectkey::Key { 2, dorian }));
  65. // G Mixolydian: G major with F natural, centred on G
  66. s = songOf ({ 55, 57, 59, 60, 62, 64, 65, 67, 65, 62, 55, 55, 43, 43, 62 });
  67. g = keyguess::guess (s);
  68. CHECK (g[0].key == (projectkey::Key { 7, mixolydian }));
  69. // E harmonic minor: E minor with D#
  70. s = songOf ({ 52, 54, 55, 57, 59, 60, 63, 64, 63, 64, 59, 52, 40, 40, 59 });
  71. g = keyguess::guess (s);
  72. CHECK (g[0].key == (projectkey::Key { 4, harmonic }));
  73. CHECK (g[0].notesInScale == g[0].notes);
  74. }
  75. TEST (simple184a_keyguess_edges)
  76. {
  77. using namespace lynx;
  78. using keyguess184a::songOf;
  79. // nothing pitched: no guess
  80. Song empty;
  81. CHECK (keyguess::guess (empty).empty());
  82. auto drumsOnly = songOf ({});
  83. CHECK (keyguess::guess (drumsOnly).empty());
  84. // a wrong note is counted out of the total, not dropped
  85. auto s = songOf ({ 60, 62, 64, 65, 67, 69, 71, 72, 60, 61 });
  86. auto g = keyguess::guess (s, 5);
  87. CHECK (g.size() == 5);
  88. CHECK (g[0].key.root == 0 && g[0].notes == 10 && g[0].notesInScale == 9);
  89. // muted notes are skipped; longer notes weigh more (a held A over short Cs makes A the centre)
  90. s = songOf ({ 60, 64, 67, 69, 72 });
  91. for (auto& n : s.patterns[0].notes)
  92. if (n.channel == 0 && n.key == 72)
  93. n.muted = true;
  94. CHECK (keyguess::histogram (s).keys.size() == 4);
  95. s = songOf ({ 57, 60, 64, 57, 62, 65, 57, 59, 64 }, lynx::ppq / 4);
  96. for (auto& n : s.patterns[0].notes)
  97. if (n.channel == 0 && n.key == 57)
  98. n.length = 4 * lynx::ppq;
  99. g = keyguess::guess (s);
  100. CHECK (g[0].key.root == 9);
  101. // the guess never changes the song
  102. const auto before = s.scaleHighlight;
  103. keyguess::guess (s);
  104. CHECK (s.scaleHighlight == before);
  105. // count 0
  106. CHECK (keyguess::guess (s, 0).empty());
  107. }
  108. TEST (simple184a_key_short_names)
  109. {
  110. using namespace lynx;
  111. const auto order = projectkey::commonFirst();
  112. CHECK (projectkey::shortScaleName (order[0]) == "major");
  113. CHECK (projectkey::shortScaleName (order[1]) == "minor");
  114. for (int i = 0; i < keyguess::commonScales; ++i) // the menu's common scales all have a short name
  115. CHECK (projectkey::shortScaleName (order[(std::size_t) i]) != notetools::flReferenceScales()[(std::size_t) order[(std::size_t) i]].name);
  116. CHECK (projectkey::shortScaleName (order.back()) == notetools::flReferenceScales()[(std::size_t) order.back()].name); // the rest: FL's name
  117. CHECK (projectkey::shortScaleName (-2).empty() && projectkey::shortScaleName (9999).empty());
  118. }