ChordSymbolContainer.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import {Pitch} from "../../Common/DataObjects/Pitch";
  2. import {KeyInstruction} from "./Instructions/KeyInstruction";
  3. import {MusicSheetCalculator} from "../Graphical/MusicSheetCalculator";
  4. import {AccidentalEnum} from "../../Common/DataObjects/Pitch";
  5. export class ChordSymbolContainer {
  6. private rootPitch: Pitch;
  7. private chordKind: ChordSymbolEnum;
  8. private bassPitch: Pitch;
  9. private degree: Degree;
  10. constructor(rootPitch: Pitch, chordKind: ChordSymbolEnum, bassPitch: Pitch, chordDegree: Degree) {
  11. this.rootPitch = rootPitch;
  12. this.chordKind = chordKind;
  13. this.bassPitch = bassPitch;
  14. this.degree = chordDegree;
  15. }
  16. public get RootPitch(): Pitch {
  17. return this.rootPitch;
  18. }
  19. public get ChordKind(): ChordSymbolEnum {
  20. return this.chordKind;
  21. }
  22. public get BassPitch(): Pitch {
  23. return this.bassPitch;
  24. }
  25. public get ChordDegree(): Degree {
  26. return this.degree;
  27. }
  28. public static calculateChordText(chordSymbol: ChordSymbolContainer, transposeHalftones: number, keyInstruction: KeyInstruction): string {
  29. let transposedRootPitch: Pitch = chordSymbol.RootPitch;
  30. if (MusicSheetCalculator.transposeCalculator !== undefined) {
  31. transposedRootPitch = MusicSheetCalculator.transposeCalculator.transposePitch(
  32. chordSymbol.RootPitch,
  33. keyInstruction,
  34. transposeHalftones
  35. );
  36. }
  37. // main Note
  38. let text: string = Pitch.getNoteEnumString(transposedRootPitch.FundamentalNote);
  39. // main alteration
  40. if (transposedRootPitch.Accidental !== AccidentalEnum.NONE) {
  41. text += this.getTextForAccidental(transposedRootPitch.Accidental);
  42. }
  43. // chord kind text
  44. text += ChordSymbolContainer.getTextFromChordKindEnum(chordSymbol.ChordKind);
  45. // degree
  46. if (chordSymbol.ChordDegree !== undefined) {
  47. switch (chordSymbol.ChordDegree.text) {
  48. case ChordDegreeText.add:
  49. text += "add";
  50. text += chordSymbol.ChordDegree.value.toString();
  51. break;
  52. case ChordDegreeText.alter:
  53. if (chordSymbol.ChordDegree.alteration !== AccidentalEnum.NONE) {
  54. text += this.getTextForAccidental(chordSymbol.ChordDegree.alteration);
  55. }
  56. text += chordSymbol.ChordDegree.value.toString();
  57. break;
  58. case ChordDegreeText.subtract:
  59. text += "(omit";
  60. text += chordSymbol.ChordDegree.value.toString();
  61. text += ")";
  62. break;
  63. default:
  64. }
  65. }
  66. // bass
  67. if (chordSymbol.BassPitch !== undefined) {
  68. let transposedBassPitch: Pitch = chordSymbol.BassPitch;
  69. if (MusicSheetCalculator.transposeCalculator !== undefined) {
  70. transposedBassPitch = MusicSheetCalculator.transposeCalculator.transposePitch(
  71. chordSymbol.BassPitch,
  72. keyInstruction,
  73. transposeHalftones
  74. );
  75. }
  76. text += "/";
  77. text += Pitch.getNoteEnumString(transposedBassPitch.FundamentalNote);
  78. text += this.getTextForAccidental(transposedBassPitch.Accidental);
  79. }
  80. return text;
  81. }
  82. private static getTextForAccidental(alteration: AccidentalEnum): string {
  83. let text: string = "";
  84. switch (alteration) {
  85. case AccidentalEnum.DOUBLEFLAT:
  86. text += "bb";
  87. break;
  88. case AccidentalEnum.FLAT:
  89. text += "b";
  90. break;
  91. case AccidentalEnum.SHARP:
  92. text += "#";
  93. break;
  94. case AccidentalEnum.DOUBLESHARP:
  95. text += "x";
  96. break;
  97. default:
  98. }
  99. return text;
  100. }
  101. private static getTextFromChordKindEnum(kind: ChordSymbolEnum): string {
  102. let text: string = "";
  103. switch (kind) {
  104. case ChordSymbolEnum.major:
  105. break;
  106. case ChordSymbolEnum.minor:
  107. text += "m";
  108. break;
  109. case ChordSymbolEnum.augmented:
  110. text += "aug";
  111. break;
  112. case ChordSymbolEnum.diminished:
  113. text += "dim";
  114. break;
  115. case ChordSymbolEnum.dominant:
  116. text += "7";
  117. break;
  118. case ChordSymbolEnum.majorseventh:
  119. text += "maj7";
  120. break;
  121. case ChordSymbolEnum.minorseventh:
  122. text += "m7";
  123. break;
  124. case ChordSymbolEnum.diminishedseventh:
  125. text += "dim7";
  126. break;
  127. case ChordSymbolEnum.augmentedseventh:
  128. text += "aug7";
  129. break;
  130. case ChordSymbolEnum.halfdiminished:
  131. text += "m7b5";
  132. break;
  133. case ChordSymbolEnum.majorminor:
  134. text += "";
  135. break;
  136. case ChordSymbolEnum.majorsixth:
  137. text += "maj6";
  138. break;
  139. case ChordSymbolEnum.minorsixth:
  140. text += "m6";
  141. break;
  142. case ChordSymbolEnum.dominantninth:
  143. text += "9";
  144. break;
  145. case ChordSymbolEnum.majorninth:
  146. text += "maj9";
  147. break;
  148. case ChordSymbolEnum.minorninth:
  149. text += "m9";
  150. break;
  151. case ChordSymbolEnum.dominant11th:
  152. text += "11";
  153. break;
  154. case ChordSymbolEnum.major11th:
  155. text += "maj11";
  156. break;
  157. case ChordSymbolEnum.minor11th:
  158. text += "m11";
  159. break;
  160. case ChordSymbolEnum.dominant13th:
  161. text += "13";
  162. break;
  163. case ChordSymbolEnum.major13th:
  164. text += "maj13";
  165. break;
  166. case ChordSymbolEnum.minor13th:
  167. text += "m13";
  168. break;
  169. case ChordSymbolEnum.suspendedsecond:
  170. text += "sus2";
  171. break;
  172. case ChordSymbolEnum.suspendedfourth:
  173. text += "sus4";
  174. break;
  175. case ChordSymbolEnum.Neapolitan:
  176. case ChordSymbolEnum.Italian:
  177. case ChordSymbolEnum.French:
  178. case ChordSymbolEnum.German:
  179. case ChordSymbolEnum.pedal:
  180. case ChordSymbolEnum.power:
  181. case ChordSymbolEnum.Tristan:
  182. break;
  183. default:
  184. break;
  185. }
  186. return text;
  187. }
  188. }
  189. export class Degree {
  190. constructor(value: number, alteration: AccidentalEnum, text: ChordDegreeText) {
  191. this.value = value;
  192. this.alteration = alteration;
  193. this.text = text;
  194. }
  195. public value: number;
  196. public alteration: AccidentalEnum;
  197. public text: ChordDegreeText;
  198. }
  199. export enum ChordDegreeText {
  200. add,
  201. alter,
  202. subtract
  203. }
  204. export enum ChordSymbolEnum {
  205. major,
  206. minor,
  207. augmented,
  208. diminished,
  209. dominant,
  210. majorseventh,
  211. minorseventh,
  212. diminishedseventh,
  213. augmentedseventh,
  214. halfdiminished,
  215. majorminor,
  216. majorsixth,
  217. minorsixth,
  218. dominantninth,
  219. majorninth,
  220. minorninth,
  221. dominant11th,
  222. major11th,
  223. minor11th,
  224. dominant13th,
  225. major13th,
  226. minor13th,
  227. suspendedsecond,
  228. suspendedfourth,
  229. Neapolitan,
  230. Italian,
  231. French,
  232. German,
  233. pedal,
  234. power,
  235. Tristan
  236. }