ARAPitchInterpretation.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. //------------------------------------------------------------------------------
  2. //! \file ARAPitchInterpretation.cpp
  3. //! classes to find proper names for pitches, ARAContentChord and ARAContentKeySignature
  4. //! if not provided by the API partner
  5. //! \project ARA SDK Library
  6. //! \copyright Copyright (c) 2018-2021, Celemony Software GmbH, All Rights Reserved.
  7. //! \license Licensed under the Apache License, Version 2.0 (the "License");
  8. //! you may not use this file except in compliance with the License.
  9. //! You may obtain a copy of the License at
  10. //!
  11. //! http://www.apache.org/licenses/LICENSE-2.0
  12. //!
  13. //! Unless required by applicable law or agreed to in writing, software
  14. //! distributed under the License is distributed on an "AS IS" BASIS,
  15. //! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. //! See the License for the specific language governing permissions and
  17. //! limitations under the License.
  18. //------------------------------------------------------------------------------
  19. #include "ARAPitchInterpretation.h"
  20. #include "ARA_Library/Dispatch/ARADispatchBase.h"
  21. #include <algorithm>
  22. #include <cctype>
  23. #include <cstring>
  24. namespace ARA {
  25. // some multi-byte UTF8 chars used in musical notation
  26. #define MUSIC_FLAT_SIGN "\xE2\x99\xAD"
  27. #define MUSIC_SHARP_SIGN "\xE2\x99\xAF"
  28. #define INCREMENT "\xE2\x88\x86"
  29. #define O_WITH_STROKE "\xC3\xB8"
  30. #define DEGREE_SIGN "\xC2\xB0"
  31. std::string PitchInterpreter::getNoteNameForCircleOfFifthIndex (ARACircleOfFifthsIndex index) const
  32. {
  33. constexpr char englishNames[] = { 'F', 'C', 'G', 'D', 'A', 'E', 'B' };
  34. constexpr auto firstValueIndex { -1 };
  35. constexpr auto lastValueIndex { 5 };
  36. static_assert (lastValueIndex - firstValueIndex + 1 == sizeof (englishNames) / sizeof (englishNames[0]), "array size/index mismatch");
  37. static_assert (lastValueIndex - firstValueIndex + 1 == 7, "array size/index mismatch");
  38. static_assert (englishNames[-firstValueIndex] == 'C', "array size/index mismatch");
  39. ARAUtf8String accidental { "" };
  40. auto accidentalCount { 0U };
  41. char noteName;
  42. if (index < 0)
  43. {
  44. while (index < firstValueIndex)
  45. {
  46. ++accidentalCount;
  47. index += 7;
  48. }
  49. index -= firstValueIndex;
  50. noteName = englishNames[index];
  51. if (_germanNoteNames && (noteName == 'B'))
  52. --accidentalCount;
  53. if (accidentalCount != 0)
  54. accidental = getFlatSymbol ();
  55. }
  56. else
  57. {
  58. while (index > lastValueIndex)
  59. {
  60. ++accidentalCount;
  61. index -= 7;
  62. }
  63. index -= firstValueIndex;
  64. noteName = englishNames[index];
  65. if (_germanNoteNames && (noteName == 'B'))
  66. noteName = 'H';
  67. if (accidentalCount != 0)
  68. accidental = getSharpSymbol ();
  69. }
  70. std::string result;
  71. result.reserve (1 + accidentalCount * std::strlen (accidental));
  72. result.append (1, noteName);
  73. while (accidentalCount-- > 0)
  74. result.append (accidental);
  75. return result;
  76. }
  77. ARAUtf8String PitchInterpreter::getFlatSymbol () const noexcept
  78. {
  79. return _asciiSymbols ? "b" : MUSIC_FLAT_SIGN;
  80. }
  81. ARAUtf8String PitchInterpreter::getSharpSymbol () const noexcept
  82. {
  83. return _asciiSymbols ? "#" : MUSIC_SHARP_SIGN;
  84. }
  85. /*******************************************************************************/
  86. bool ChordInterpreter::isNoChord (const ARAContentChord& chord) noexcept
  87. {
  88. return std::all_of (std::begin (chord.intervals), std::end (chord.intervals), [] (ARAChordIntervalUsage i) noexcept { return i == kARAChordIntervalUnused; });
  89. }
  90. static inline bool isUnisonChord (const ARAContentChord& chord) noexcept
  91. {
  92. return std::all_of (std::next (std::begin (chord.intervals)), std::end (chord.intervals), [] (ARAChordIntervalUsage i) noexcept { return i == kARAChordIntervalUnused; });
  93. }
  94. static inline bool testInterval (const ARAChordIntervalUsage* intervals, int interval) noexcept
  95. {
  96. return (intervals[interval] != kARAChordIntervalUnused);
  97. }
  98. static inline bool testInterval (const ARAChordIntervalUsage* intervals, int interval, ARAChordIntervalUsage usage) noexcept
  99. {
  100. return (intervals[interval] == usage);
  101. }
  102. static inline bool testAndClearInterval (ARAChordIntervalUsage* intervals, int interval) noexcept
  103. {
  104. const bool result { testInterval (intervals, interval) };
  105. if (result)
  106. intervals[interval] = kARAChordIntervalUnused;
  107. return result;
  108. }
  109. static inline bool testAndClearInterval (ARAChordIntervalUsage* intervals, int interval, ARAChordIntervalUsage usage) noexcept
  110. {
  111. const bool result { testInterval (intervals, interval, usage) };
  112. if (result)
  113. intervals[interval] = kARAChordIntervalUnused;
  114. return result;
  115. }
  116. // after evaluating a certain degree at certain intervals, this function is used to clear out
  117. // any other, unexpected usage of that degree by replacing it with kARAChordIntervalUsed
  118. static inline bool cleanupRemainingOccurencesOfDegree (ARAChordIntervalUsage* intervals, ARAChordIntervalUsage usage) noexcept
  119. {
  120. bool isClean { true };
  121. for (auto i { 0 }; i < 12; ++i)
  122. {
  123. if (intervals[i] == usage)
  124. {
  125. intervals[i] = kARAChordIntervalUsed;
  126. isClean = false;
  127. }
  128. }
  129. return isClean;
  130. }
  131. static inline void appendInterval (std::string& text, const ARAUtf8String prefix, const ARAUtf8String sign, const ARAUtf8String interval) noexcept
  132. {
  133. if ((prefix == nullptr) && (sign == nullptr) && std::isdigit (static_cast<unsigned char> (text.back ())))
  134. text += '/'; // separate two consecutive digits by slash
  135. if (prefix)
  136. text.append (prefix);
  137. if (sign)
  138. text.append (sign);
  139. text.append (interval);
  140. }
  141. std::string ChordInterpreter::getNameForChord (const ARAContentChord& chord) const
  142. {
  143. if (isNoChord (chord))
  144. return "N.C.";
  145. auto result { getNoteNameForCircleOfFifthIndex (chord.root) };
  146. if (isUnisonChord (chord) && (chord.root == chord.bass))
  147. {
  148. result.append (" bass");
  149. return result;
  150. }
  151. const auto prefixOmit { "omit" };
  152. const auto prefixAdd { "add" };
  153. ARAChordIntervalUsage intervals[12];
  154. std::copy (std::begin (chord.intervals), std::end (chord.intervals), intervals);
  155. std::string altered;
  156. std::string added;
  157. std::string omitted;
  158. bool canBePowerChord { true };
  159. // unless a degree of at least 7 is specified, power chord can have no notes (except root and fifth) below minor 7
  160. for (auto i { 1 }; i < 10; ++i)
  161. {
  162. if ((i == 7) || (intervals[i] == kARAChordIntervalUnused))
  163. continue;
  164. if ((intervals[i] == kARAChordIntervalUsed) ||
  165. (intervals[i] < kARAChordDiatonicDegree7))
  166. {
  167. canBePowerChord = false;
  168. break;
  169. }
  170. }
  171. // root note
  172. if (!testAndClearInterval (intervals, 0)) // ignoring potential degree on root
  173. {
  174. appendInterval (omitted, prefixOmit, nullptr, "1");
  175. canBePowerChord = false;
  176. }
  177. canBePowerChord &= cleanupRemainingOccurencesOfDegree (intervals, kARAChordDiatonicDegree1);
  178. // first interval: third (or sus)
  179. // precedence: check major third before minor, major or minor before sus2 or sus4, sus4 before sus2
  180. int third { 0 };
  181. if (testAndClearInterval (intervals, 4, kARAChordDiatonicDegree3))
  182. third = 4;
  183. else if (testAndClearInterval (intervals, 3, kARAChordDiatonicDegree3))
  184. third = 3;
  185. else if (testAndClearInterval (intervals, 5, kARAChordDiatonicDegree4) ||
  186. testAndClearInterval (intervals, 5, kARAChordDiatonicDegree3))
  187. third = 5;
  188. else if (testAndClearInterval (intervals, 2, kARAChordDiatonicDegree2) ||
  189. testAndClearInterval (intervals, 2, kARAChordDiatonicDegree3))
  190. third = 2;
  191. else if (testAndClearInterval (intervals, 4, kARAChordIntervalUsed))
  192. third = 4;
  193. else if (testAndClearInterval (intervals, 3, kARAChordIntervalUsed))
  194. third = 3;
  195. else if (testInterval (intervals, 7) && // sus4 shall only be implicitly deduced if there's a perfect fifth
  196. testAndClearInterval (intervals, 5, kARAChordIntervalUsed))
  197. third = 5;
  198. else if (testInterval (intervals, 7) && // sus2 shall only be implicitly deduced if there's a perfect fifth
  199. testAndClearInterval (intervals, 2, kARAChordIntervalUsed))
  200. third = 2;
  201. else
  202. appendInterval (omitted, prefixOmit, nullptr, "3");
  203. canBePowerChord &= (third == 0);
  204. canBePowerChord &= cleanupRemainingOccurencesOfDegree (intervals, kARAChordDiatonicDegree2);
  205. canBePowerChord &= cleanupRemainingOccurencesOfDegree (intervals, kARAChordDiatonicDegree3);
  206. canBePowerChord &= cleanupRemainingOccurencesOfDegree (intervals, kARAChordDiatonicDegree4);
  207. // second interval: fifth
  208. // precedence: perfect, flat, sharp
  209. int fifth { 0 };
  210. if (testAndClearInterval (intervals, 7)) // ignoring potential degree on fifth
  211. fifth = 7;
  212. else if (testAndClearInterval (intervals, 6, kARAChordDiatonicDegree5))
  213. fifth = 6;
  214. else if (testAndClearInterval (intervals, 8, kARAChordDiatonicDegree5))
  215. fifth = 8;
  216. else if (testAndClearInterval (intervals, 6, kARAChordIntervalUsed))
  217. fifth = 6;
  218. else if (testAndClearInterval (intervals, 8, kARAChordIntervalUsed))
  219. fifth = 8;
  220. else
  221. appendInterval (omitted, prefixOmit, nullptr, "5");
  222. canBePowerChord &= (fifth == 7);
  223. canBePowerChord &= cleanupRemainingOccurencesOfDegree (intervals, kARAChordDiatonicDegree5);
  224. // third interval: seventh (or 6th)
  225. // precedence: major 7, minor 7, major 6, minor 6
  226. int seventh { 0 };
  227. if (testAndClearInterval (intervals, 11)) // ignoring potential degree on major seventh
  228. seventh = 11;
  229. else if (testAndClearInterval (intervals, 10)) // ignoring potential degree on minor seventh
  230. seventh = 10;
  231. else if (testAndClearInterval (intervals, 9, kARAChordDiatonicDegree7))
  232. seventh = 9;
  233. if (testAndClearInterval (intervals, 9, kARAChordDiatonicDegree6))
  234. {
  235. if (seventh != 0)
  236. appendInterval (added, prefixAdd, nullptr, "6");
  237. else
  238. seventh = 9;
  239. }
  240. else if (testAndClearInterval (intervals, 8, kARAChordDiatonicDegree6))
  241. {
  242. if (seventh != 0)
  243. appendInterval (added, prefixAdd, getFlatSymbol (), "6");
  244. else
  245. seventh = 8;
  246. }
  247. if (seventh == 0)
  248. {
  249. if (testAndClearInterval (intervals, 8, kARAChordIntervalUsed))
  250. seventh = 8;
  251. else if (testAndClearInterval (intervals, 9, kARAChordIntervalUsed))
  252. seventh = 9;
  253. }
  254. canBePowerChord &= (seventh == 0 || seventh >= 10);
  255. canBePowerChord &= cleanupRemainingOccurencesOfDegree (intervals, kARAChordDiatonicDegree6);
  256. cleanupRemainingOccurencesOfDegree (intervals, kARAChordDiatonicDegree7);
  257. // append chord type to string
  258. bool pending4 { false };
  259. bool pending7 { seventh >= 10 };
  260. bool impliesflat5 { false };
  261. bool impliessharp5 { false };
  262. if (canBePowerChord)
  263. {
  264. result.append ("5");
  265. }
  266. else if (third == 4)
  267. {
  268. if (fifth == 8)
  269. {
  270. result.append ("+");
  271. impliessharp5 = true;
  272. }
  273. }
  274. else if (third == 3)
  275. {
  276. if ((fifth == 6) && (seventh == 10))
  277. {
  278. result.append (usesASCIISymbols () ? "halfdim" : O_WITH_STROKE);
  279. pending7 = false;
  280. impliesflat5 = true;
  281. }
  282. else if ((fifth == 6) && (seventh != 10))
  283. {
  284. result.append (usesASCIISymbols () ? "dim" : DEGREE_SIGN);
  285. impliesflat5 = true;
  286. }
  287. else
  288. {
  289. result.append ("m");
  290. }
  291. }
  292. else if (third == 2)
  293. {
  294. result.append ("sus2");
  295. }
  296. else if (third == 5)
  297. {
  298. result.append ("sus");
  299. pending4 = (seventh == 0);
  300. }
  301. if (seventh == 11)
  302. result.append (usesASCIISymbols () ? "maj" : INCREMENT);
  303. else if (seventh == 8)
  304. appendInterval (result, nullptr, getFlatSymbol (), "6");
  305. else if ((seventh == 9) && ((third != 3) || (fifth != 6)))
  306. appendInterval (result, nullptr, nullptr, "6");
  307. // first octave alterations
  308. if ((fifth == 6) && !impliesflat5)
  309. appendInterval (altered, nullptr, getFlatSymbol (), "5");
  310. if ((fifth == 8) && !impliessharp5)
  311. appendInterval (altered, nullptr, getSharpSymbol (), "5");
  312. // ninth
  313. bool willBeAdd9 { seventh == 0 };
  314. int pending9 { 0 };
  315. if (testInterval (intervals, 2))
  316. {
  317. if (willBeAdd9)
  318. appendInterval (added, prefixAdd, nullptr, "9");
  319. else
  320. pending9 = 14;
  321. willBeAdd9 = true;
  322. }
  323. if (testInterval (intervals, 1))
  324. {
  325. if (willBeAdd9)
  326. appendInterval (added, prefixAdd, getFlatSymbol (), "9");
  327. else
  328. pending9 = 13;
  329. willBeAdd9 = true;
  330. }
  331. if (testInterval (intervals, 3))
  332. {
  333. if (willBeAdd9)
  334. appendInterval (added, prefixAdd, getSharpSymbol (), "9");
  335. else
  336. pending9 = 15;
  337. }
  338. if (pending9 != 0)
  339. pending7 = false;
  340. // eleventh
  341. bool willBeAdd11 { pending9 == 0 };
  342. int pending11 { 0 };
  343. if (testInterval (intervals, 5))
  344. {
  345. if (willBeAdd11)
  346. appendInterval (added, prefixAdd, nullptr, "11");
  347. else
  348. pending11 = 17;
  349. willBeAdd11 = true;
  350. }
  351. if (testInterval (intervals, 4))
  352. {
  353. if (willBeAdd11)
  354. appendInterval (added, prefixAdd, getFlatSymbol (), "11");
  355. else
  356. pending11 = 16;
  357. willBeAdd11 = true;
  358. }
  359. if (testInterval (intervals, 6))
  360. {
  361. if (willBeAdd11)
  362. appendInterval (added, prefixAdd, getSharpSymbol (), "11");
  363. else
  364. pending11 = 18;
  365. }
  366. // thirteenth (will be added immediatley to result or added)
  367. bool willBeAdd13 { pending9 == 0 };
  368. bool hasNonAdd13 { false };
  369. if (testInterval (intervals, 9))
  370. {
  371. if (willBeAdd13)
  372. appendInterval (added, prefixAdd, nullptr, "13");
  373. else
  374. appendInterval (result, nullptr, nullptr, "13");
  375. hasNonAdd13 |= !willBeAdd13;
  376. willBeAdd13 = true;
  377. }
  378. if (testInterval (intervals, 8))
  379. {
  380. if (willBeAdd13)
  381. appendInterval (added, prefixAdd, getFlatSymbol (), "13");
  382. else
  383. appendInterval (result, nullptr, getFlatSymbol (), "13");
  384. hasNonAdd13 |= !willBeAdd13;
  385. willBeAdd13 = true;
  386. }
  387. if (testInterval (intervals, 10))
  388. {
  389. if (willBeAdd13)
  390. appendInterval (added, prefixAdd, getSharpSymbol (), "13");
  391. else
  392. appendInterval (result, nullptr, getSharpSymbol (), "13");
  393. hasNonAdd13 |= !willBeAdd13;
  394. }
  395. if ((pending11 != 0) || hasNonAdd13)
  396. {
  397. if (pending9 == 13)
  398. appendInterval (altered, nullptr, getFlatSymbol (), "9");
  399. else if (pending9 == 15)
  400. appendInterval (altered, nullptr, getSharpSymbol (), "9");
  401. pending9 = 0;
  402. }
  403. // extensions
  404. if (pending4)
  405. result.append ("4"); // continues "sus", can never have digit before
  406. if (pending7)
  407. appendInterval (result, nullptr, nullptr, "7");
  408. if (pending9 != 0)
  409. {
  410. if (pending9 == 13)
  411. appendInterval (result, nullptr, getFlatSymbol (), "9");
  412. else if (pending9 == 15)
  413. appendInterval (result, nullptr, getSharpSymbol (), "9");
  414. else
  415. appendInterval (result, nullptr, nullptr, "9");
  416. }
  417. if (pending11 != 0)
  418. {
  419. auto& addTo { (hasNonAdd13) ? added : result };
  420. if (pending11 == 16)
  421. appendInterval (addTo, (hasNonAdd13) ? prefixAdd : nullptr, getFlatSymbol (), "11");
  422. else if (pending11 == 18)
  423. appendInterval (addTo, (hasNonAdd13) ? prefixAdd : nullptr, getSharpSymbol (), "11");
  424. else
  425. appendInterval (addTo, (hasNonAdd13) ? prefixAdd : nullptr, nullptr, "11");
  426. }
  427. // additions, alterations, omits
  428. if (!canBePowerChord)
  429. result.append (altered);
  430. result.append (added);
  431. if (!canBePowerChord)
  432. result.append (omitted);
  433. // bass note
  434. if (chord.root != chord.bass)
  435. {
  436. result.append ("/");
  437. result.append (getNoteNameForCircleOfFifthIndex (chord.bass));
  438. }
  439. return result;
  440. }
  441. /*******************************************************************************/
  442. static_assert (sizeof (ARAContentKeySignature::intervals) / sizeof (ARAContentKeySignature::intervals[0] == 12), "key signature note count mismatch");
  443. // for easy comparison, scales are encoded as a 12-bit mask, with highest bit matching lowest byte in the key signature intervals
  444. // this function rotates the scale by one note
  445. static inline void rotateScale (unsigned int& scale) noexcept
  446. {
  447. constexpr unsigned int overflowMask { 1 << 12 };
  448. scale = scale << 1;
  449. if (scale >= overflowMask)
  450. scale -= overflowMask - 1;
  451. }
  452. KeySignatureInterpreter::ScaleMode KeySignatureInterpreter::getScaleMode (const ARAContentKeySignature& keySignature) noexcept
  453. {
  454. // convert input to bit mask
  455. unsigned int inputScale { 0 };
  456. for (const auto& interval : keySignature.intervals)
  457. {
  458. rotateScale (inputScale);
  459. if (interval != kARAKeySignatureIntervalUnused)
  460. inputScale += 1;
  461. }
  462. // starting from ionian mode, rotate through all the modes and test for match
  463. unsigned int currentScale { 0xAD5 };
  464. for (int currentMode { ScaleMode::kIonian }; currentMode <= ScaleMode::kLocrian; ++currentMode)
  465. {
  466. if (inputScale == currentScale)
  467. return static_cast<ScaleMode> (currentMode);
  468. rotateScale (currentScale);
  469. if ((currentScale & (1 << 11)) == 0) // if root is not set, rotate one further
  470. rotateScale (currentScale);
  471. }
  472. return ScaleMode::kInvalid;
  473. }
  474. std::string KeySignatureInterpreter::getNameForKeySignature (const ARAContentKeySignature& keySignature) const
  475. {
  476. const auto noteName { getNoteNameForCircleOfFifthIndex (keySignature.root) };
  477. switch (getScaleMode (keySignature))
  478. {
  479. case ScaleMode::kInvalid: break;
  480. case ScaleMode::kIonian: return noteName;
  481. case ScaleMode::kDorian: return noteName + " Dorian";
  482. case ScaleMode::kPhrygian: return noteName + " Phrygian";
  483. case ScaleMode::kLydian: return noteName + " Lydian";
  484. case ScaleMode::kMixolydian: return noteName + " Mixolydian";
  485. case ScaleMode::kAeolian: return noteName + "m";
  486. case ScaleMode::kLocrian: return noteName + " Locrian";
  487. }
  488. return "";
  489. }
  490. #undef MUSIC_FLAT_SIGN
  491. #undef MUSIC_SHARP_SIGN
  492. #undef INCREMENT
  493. #undef O_WITH_STROKE
  494. #undef DEGREE_SIGN
  495. } // namespace ARA