VoiceEntry.ts 16 KB

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