VoiceEntry.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import {Fraction} from "../../Common/DataObjects/Fraction";
  2. import {Voice} from "./Voice";
  3. import {SourceStaffEntry} from "./SourceStaffEntry";
  4. import {Note} from "./Note";
  5. import {Pitch} from "../../Common/DataObjects/Pitch";
  6. import {LyricsEntry} from "./Lyrics/LyricsEntry";
  7. import {TechnicalInstruction} from "./Instructions/TechnicalInstruction";
  8. import {OrnamentContainer} from "./OrnamentContainer";
  9. import {KeyInstruction} from "./Instructions/KeyInstruction";
  10. import {OrnamentEnum} from "./OrnamentContainer";
  11. import {AccidentalEnum} from "../../Common/DataObjects/Pitch";
  12. import Dictionary from "typescript-collections/dist/lib/Dictionary";
  13. /**
  14. * A [[VoiceEntry]] contains the notes in a voice at a timestamp.
  15. */
  16. export class VoiceEntry {
  17. /**
  18. *
  19. * @param timestamp - The relative timestamp within the source measure.
  20. * @param parentVoice
  21. * @param parentSourceStaffEntry
  22. */
  23. constructor(timestamp: Fraction, parentVoice: Voice, parentSourceStaffEntry: SourceStaffEntry) {
  24. this.timestamp = timestamp;
  25. this.parentVoice = parentVoice;
  26. this.parentSourceStaffEntry = parentSourceStaffEntry;
  27. }
  28. public graceVoiceEntriesBefore: VoiceEntry[];
  29. public graceVoiceEntriesAfter: VoiceEntry[];
  30. private parentVoice: Voice;
  31. private parentSourceStaffEntry: SourceStaffEntry;
  32. private timestamp: Fraction;
  33. private notes: Note[] = [];
  34. private articulations: ArticulationEnum[] = [];
  35. private technicalInstructions: TechnicalInstruction[] = [];
  36. private lyricsEntries: Dictionary<number, LyricsEntry> = new Dictionary<number, LyricsEntry>();
  37. private arpeggiosNotesIndices: number[] = [];
  38. private ornamentContainer: OrnamentContainer;
  39. private stemDirection: StemDirectionType = StemDirectionType.Undefined;
  40. public get ParentSourceStaffEntry(): SourceStaffEntry {
  41. return this.parentSourceStaffEntry;
  42. }
  43. public get ParentVoice(): Voice {
  44. return this.parentVoice;
  45. }
  46. public get Timestamp(): Fraction {
  47. return this.timestamp;
  48. }
  49. public set Timestamp(value: Fraction) {
  50. this.timestamp = value;
  51. }
  52. public get Notes(): Note[] {
  53. return this.notes;
  54. }
  55. public get Articulations(): ArticulationEnum[] {
  56. return this.articulations;
  57. }
  58. public get TechnicalInstructions(): TechnicalInstruction[] {
  59. return this.technicalInstructions;
  60. }
  61. public get LyricsEntries(): Dictionary<number, LyricsEntry> {
  62. return this.lyricsEntries;
  63. }
  64. public get ArpeggiosNotesIndices(): number[] {
  65. return this.arpeggiosNotesIndices;
  66. }
  67. public set ArpeggiosNotesIndices(value: number[]) {
  68. this.arpeggiosNotesIndices = value;
  69. }
  70. public get OrnamentContainer(): OrnamentContainer {
  71. return this.ornamentContainer;
  72. }
  73. public set OrnamentContainer(value: OrnamentContainer) {
  74. this.ornamentContainer = value;
  75. }
  76. public get StemDirection(): StemDirectionType {
  77. return this.stemDirection;
  78. }
  79. public set StemDirection(value: StemDirectionType) {
  80. this.stemDirection = value;
  81. }
  82. public static isSupportedArticulation(articulation: ArticulationEnum): boolean {
  83. switch (articulation) {
  84. case ArticulationEnum.accent:
  85. case ArticulationEnum.strongaccent:
  86. case ArticulationEnum.invertedstrongaccent:
  87. case ArticulationEnum.staccato:
  88. case ArticulationEnum.staccatissimo:
  89. case ArticulationEnum.spiccato:
  90. case ArticulationEnum.tenuto:
  91. case ArticulationEnum.fermata:
  92. case ArticulationEnum.invertedfermata:
  93. case ArticulationEnum.breathmark:
  94. case ArticulationEnum.caesura:
  95. case ArticulationEnum.lefthandpizzicato:
  96. case ArticulationEnum.naturalharmonic:
  97. case ArticulationEnum.snappizzicato:
  98. case ArticulationEnum.upbow:
  99. case ArticulationEnum.downbow:
  100. return true;
  101. default:
  102. return false;
  103. }
  104. }
  105. public hasTie(): boolean {
  106. for (let idx: number = 0, len: number = this.Notes.length; idx < len; ++idx) {
  107. const note: Note = this.Notes[idx];
  108. if (note.NoteTie !== undefined) { return true; }
  109. }
  110. return false;
  111. }
  112. public hasSlur(): boolean {
  113. for (let idx: number = 0, len: number = this.Notes.length; idx < len; ++idx) {
  114. const note: Note = this.Notes[idx];
  115. if (note.NoteSlurs.length > 0) { return true; }
  116. }
  117. return false;
  118. }
  119. public isStaccato(): boolean {
  120. for (let idx: number = 0, len: number = this.Articulations.length; idx < len; ++idx) {
  121. const articulation: ArticulationEnum = this.Articulations[idx];
  122. if (articulation === ArticulationEnum.staccato) { return true; }
  123. }
  124. return false;
  125. }
  126. public isAccent(): boolean {
  127. for (let idx: number = 0, len: number = this.Articulations.length; idx < len; ++idx) {
  128. const articulation: ArticulationEnum = this.Articulations[idx];
  129. if (articulation === ArticulationEnum.accent || articulation === ArticulationEnum.strongaccent) {
  130. return true;
  131. }
  132. }
  133. return false;
  134. }
  135. public getVerseNumberForLyricEntry(lyricsEntry: LyricsEntry): number {
  136. let verseNumber: number = 1;
  137. this.lyricsEntries.forEach((key: number, value: LyricsEntry): void => {
  138. if (lyricsEntry === value) {
  139. verseNumber = key;
  140. }
  141. });
  142. return verseNumber;
  143. }
  144. //public createVoiceEntriesForOrnament(activeKey: KeyInstruction): VoiceEntry[] {
  145. // return this.createVoiceEntriesForOrnament(this, activeKey);
  146. //}
  147. public createVoiceEntriesForOrnament(voiceEntryWithOrnament: VoiceEntry, activeKey: KeyInstruction): VoiceEntry[] {
  148. if (voiceEntryWithOrnament === undefined) {
  149. voiceEntryWithOrnament = this;
  150. }
  151. const voiceEntries: VoiceEntry[] = [];
  152. if (voiceEntryWithOrnament.ornamentContainer === undefined) {
  153. return;
  154. }
  155. const baseNote: Note = this.notes[0];
  156. const baselength: Fraction = baseNote.calculateNoteLengthWithoutTie();
  157. const baseVoice: Voice = voiceEntryWithOrnament.ParentVoice;
  158. const baseTimestamp: Fraction = voiceEntryWithOrnament.Timestamp;
  159. let currentTimestamp: Fraction = Fraction.createFromFraction(baseTimestamp);
  160. //let length: Fraction;
  161. switch (voiceEntryWithOrnament.ornamentContainer.GetOrnament) {
  162. case OrnamentEnum.Trill: {
  163. const length: Fraction = new Fraction(baselength.Numerator, baselength.Denominator * 8);
  164. const higherPitch: Pitch = baseNote.Pitch.getTransposedPitch(1);
  165. let alteration: AccidentalEnum = activeKey.getAlterationForPitch(higherPitch);
  166. if (voiceEntryWithOrnament.OrnamentContainer.AccidentalAbove !== AccidentalEnum.NONE) {
  167. alteration = <AccidentalEnum><number>voiceEntryWithOrnament.ornamentContainer.AccidentalAbove;
  168. }
  169. for (let i: number = 0; i < 8; i++) {
  170. currentTimestamp = Fraction.plus(baseTimestamp, new Fraction(i * length.Numerator, length.Denominator));
  171. if ((i % 2) === 0) {
  172. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  173. } else {
  174. this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, higherPitch, alteration, voiceEntries);
  175. }
  176. }
  177. break;
  178. }
  179. case OrnamentEnum.Turn: {
  180. const length: Fraction = new Fraction(baselength.Numerator, baselength.Denominator * 4);
  181. const lowerPitch: Pitch = baseNote.Pitch.getTransposedPitch(-1);
  182. const lowerAlteration: AccidentalEnum = activeKey.getAlterationForPitch(lowerPitch);
  183. const higherPitch: Pitch = baseNote.Pitch.getTransposedPitch(1);
  184. const higherAlteration: AccidentalEnum = activeKey.getAlterationForPitch(higherPitch);
  185. this.createAlteratedVoiceEntry(
  186. currentTimestamp, length, baseVoice, higherPitch, higherAlteration, voiceEntries
  187. );
  188. currentTimestamp.Add(length);
  189. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  190. currentTimestamp.Add(length);
  191. this.createAlteratedVoiceEntry(
  192. currentTimestamp, length, baseVoice, lowerPitch, lowerAlteration, voiceEntries
  193. );
  194. currentTimestamp.Add(length);
  195. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  196. break;
  197. }
  198. case OrnamentEnum.InvertedTurn: {
  199. const length: Fraction = new Fraction(baselength.Numerator, baselength.Denominator * 4);
  200. const lowerPitch: Pitch = baseNote.Pitch.getTransposedPitch(-1);
  201. const lowerAlteration: AccidentalEnum = activeKey.getAlterationForPitch(lowerPitch);
  202. const higherPitch: Pitch = baseNote.Pitch.getTransposedPitch(1);
  203. const higherAlteration: AccidentalEnum = activeKey.getAlterationForPitch(higherPitch);
  204. this.createAlteratedVoiceEntry(
  205. currentTimestamp, length, baseVoice, lowerPitch, lowerAlteration, voiceEntries
  206. );
  207. currentTimestamp.Add(length);
  208. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  209. currentTimestamp.Add(length);
  210. this.createAlteratedVoiceEntry(
  211. currentTimestamp, length, baseVoice, higherPitch, higherAlteration, voiceEntries
  212. );
  213. currentTimestamp.Add(length);
  214. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  215. break;
  216. }
  217. case OrnamentEnum.DelayedTurn: {
  218. const length: Fraction = new Fraction(baselength.Numerator, baselength.Denominator * 2);
  219. const lowerPitch: Pitch = baseNote.Pitch.getTransposedPitch(-1);
  220. const lowerAlteration: AccidentalEnum = activeKey.getAlterationForPitch(lowerPitch);
  221. const higherPitch: Pitch = baseNote.Pitch.getTransposedPitch(1);
  222. const higherAlteration: AccidentalEnum = activeKey.getAlterationForPitch(higherPitch);
  223. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  224. currentTimestamp = Fraction.plus(baseTimestamp, length);
  225. length.Denominator = baselength.Denominator * 8;
  226. this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, higherPitch, higherAlteration, voiceEntries);
  227. currentTimestamp.Add(length);
  228. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  229. currentTimestamp.Add(length);
  230. this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, lowerPitch, lowerAlteration, voiceEntries);
  231. currentTimestamp.Add(length);
  232. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  233. break;
  234. }
  235. case OrnamentEnum.DelayedInvertedTurn: {
  236. const length: Fraction = new Fraction(baselength.Numerator, baselength.Denominator * 2);
  237. const lowerPitch: Pitch = baseNote.Pitch.getTransposedPitch(-1);
  238. const lowerAlteration: AccidentalEnum = activeKey.getAlterationForPitch(lowerPitch);
  239. const higherPitch: Pitch = baseNote.Pitch.getTransposedPitch(1);
  240. const higherAlteration: AccidentalEnum = activeKey.getAlterationForPitch(higherPitch);
  241. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  242. currentTimestamp = Fraction.plus(baseTimestamp, length);
  243. length.Denominator = baselength.Denominator * 8;
  244. this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, lowerPitch, lowerAlteration, voiceEntries);
  245. currentTimestamp.Add(length);
  246. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  247. currentTimestamp.Add(length);
  248. this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, higherPitch, higherAlteration, voiceEntries);
  249. currentTimestamp.Add(length);
  250. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  251. break;
  252. }
  253. case OrnamentEnum.Mordent: {
  254. const length: Fraction = new Fraction(baselength.Numerator, baselength.Denominator * 4);
  255. const higherPitch: Pitch = baseNote.Pitch.getTransposedPitch(1);
  256. const alteration: AccidentalEnum = activeKey.getAlterationForPitch(higherPitch);
  257. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  258. currentTimestamp.Add(length);
  259. this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, higherPitch, alteration, voiceEntries);
  260. length.Denominator = baselength.Denominator * 2;
  261. currentTimestamp = Fraction.plus(baseTimestamp, length);
  262. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  263. break;
  264. }
  265. case OrnamentEnum.InvertedMordent: {
  266. const length: Fraction = new Fraction(baselength.Numerator, baselength.Denominator * 4);
  267. const lowerPitch: Pitch = baseNote.Pitch.getTransposedPitch(-1);
  268. const alteration: AccidentalEnum = activeKey.getAlterationForPitch(lowerPitch);
  269. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  270. currentTimestamp.Add(length);
  271. this.createAlteratedVoiceEntry(currentTimestamp, length, baseVoice, lowerPitch, alteration, voiceEntries);
  272. length.Denominator = baselength.Denominator * 2;
  273. currentTimestamp = Fraction.plus(baseTimestamp, length);
  274. this.createBaseVoiceEntry(currentTimestamp, length, baseVoice, baseNote, voiceEntries);
  275. break;
  276. }
  277. default:
  278. throw new RangeError();
  279. }
  280. return voiceEntries;
  281. }
  282. private createBaseVoiceEntry(
  283. currentTimestamp: Fraction, length: Fraction, baseVoice: Voice, baseNote: Note, voiceEntries: VoiceEntry[]
  284. ): void {
  285. const voiceEntry: VoiceEntry = new VoiceEntry(currentTimestamp, baseVoice, baseNote.ParentStaffEntry);
  286. const pitch: Pitch = new Pitch(baseNote.Pitch.FundamentalNote, baseNote.Pitch.Octave, baseNote.Pitch.Accidental);
  287. const note: Note = new Note(voiceEntry, undefined, length, pitch);
  288. voiceEntry.Notes.push(note);
  289. voiceEntries.push(voiceEntry);
  290. }
  291. private createAlteratedVoiceEntry(
  292. currentTimestamp: Fraction, length: Fraction, baseVoice: Voice, higherPitch: Pitch,
  293. alteration: AccidentalEnum, voiceEntries: VoiceEntry[]
  294. ): void {
  295. const voiceEntry: VoiceEntry = new VoiceEntry(currentTimestamp, baseVoice, undefined);
  296. const pitch: Pitch = new Pitch(higherPitch.FundamentalNote, higherPitch.Octave, alteration);
  297. const note: Note = new Note(voiceEntry, undefined, length, pitch);
  298. voiceEntry.Notes.push(note);
  299. voiceEntries.push(voiceEntry);
  300. }
  301. }
  302. export enum ArticulationEnum {
  303. accent,
  304. strongaccent,
  305. invertedstrongaccent,
  306. staccato,
  307. staccatissimo,
  308. spiccato,
  309. tenuto,
  310. fermata,
  311. invertedfermata,
  312. breathmark,
  313. caesura,
  314. lefthandpizzicato,
  315. naturalharmonic,
  316. snappizzicato,
  317. upbow,
  318. downbow,
  319. scoop,
  320. plop,
  321. doit,
  322. falloff,
  323. stress,
  324. unstress,
  325. detachedlegato,
  326. otherarticulation
  327. }
  328. export enum StemDirectionType {
  329. Undefined = -1,
  330. Up = 0,
  331. Down = 1,
  332. None = 2
  333. }