MusicSheet.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. import {Fraction} from "../Common/DataObjects/Fraction";
  2. import {MusicPartManager} from "./MusicParts/MusicPartManager";
  3. import {SourceMeasure} from "./VoiceData/SourceMeasure";
  4. import {Repetition} from "./MusicSource/Repetition";
  5. import {DynamicsContainer} from "./VoiceData/HelperObjects/DynamicsContainer";
  6. import {InstrumentalGroup} from "./InstrumentalGroup";
  7. import {Instrument} from "./Instrument";
  8. import {Label} from "./Label";
  9. import {Staff} from "./VoiceData/Staff";
  10. import {MusicPartManagerIterator} from "./MusicParts/MusicPartManagerIterator";
  11. import {VerticalSourceStaffEntryContainer} from "./VoiceData/VerticalSourceStaffEntryContainer";
  12. import {Voice} from "./VoiceData/Voice";
  13. import {MusicSheetErrors} from "../Common/DataObjects/MusicSheetErrors";
  14. import {MultiTempoExpression} from "./VoiceData/Expressions/MultiTempoExpression";
  15. import {EngravingRules} from "./Graphical/EngravingRules";
  16. import {NoteState} from "./Graphical/DrawingEnums";
  17. import {Note} from "./VoiceData/Note";
  18. import {VoiceEntry} from "./VoiceData/VoiceEntry";
  19. import log from "loglevel";
  20. // FIXME Andrea: Commented out some unnecessary/not-ported-yet code, have a look at (*)
  21. export class PlaybackSettings {
  22. public rhythm: Fraction;
  23. }
  24. /**
  25. * This is the representation of a complete piece of sheet music.
  26. * It includes the contents of a MusicXML file after the reading.
  27. * Notes: the musicsheet might not need the Rules, e.g. in the testframework. The EngravingRules Constructor
  28. * fails when no FontInfo exists, which needs a TextMeasurer
  29. */
  30. export class MusicSheet /*implements ISettableMusicSheet, IComparable<MusicSheet>*/ {
  31. constructor() {
  32. this.playbackSettings = new PlaybackSettings();
  33. // FIXME?
  34. // initialize SheetPlaybackSetting with default values
  35. this.playbackSettings.rhythm = new Fraction(4, 4, 0, false);
  36. this.userStartTempoInBPM = 100;
  37. this.pageWidth = 120;
  38. // create MusicPartManager
  39. this.MusicPartManager = new MusicPartManager(this);
  40. this.hasBPMInfo = false;
  41. }
  42. public static defaultTitle: string = "[no title given]";
  43. public userStartTempoInBPM: number;
  44. public pageWidth: number;
  45. private idString: string = "random idString, not initialized";
  46. private sourceMeasures: SourceMeasure[] = [];
  47. private repetitions: Repetition[] = [];
  48. private dynListStaves: DynamicsContainer[][] = [];
  49. private timestampSortedDynamicExpressionsList: DynamicsContainer[] = [];
  50. private timestampSortedTempoExpressionsList: MultiTempoExpression[] = [];
  51. private instrumentalGroups: InstrumentalGroup[] = [];
  52. /** The parts in the sheet, e.g. piano left hand, or piano right hand, or violin. */
  53. private instruments: Instrument[] = []; // Should be renamed from instruments to parts and part, though this will be a big refactor
  54. private playbackSettings: PlaybackSettings;
  55. private path: string;
  56. private title: Label;
  57. private subtitle: Label;
  58. private composer: Label;
  59. private lyricist: Label;
  60. // private languages: Language[] = [];
  61. // private activeLanguage: Language;
  62. private musicPartManager: MusicPartManager = undefined;
  63. private musicSheetErrors: MusicSheetErrors = new MusicSheetErrors();
  64. private staves: Staff[] = [];
  65. private selectionStart: Fraction;
  66. private selectionEnd: Fraction;
  67. private transpose: number = 0;
  68. private defaultStartTempoInBpm: number = 0;
  69. private drawErroneousMeasures: boolean = false;
  70. private hasBeenOpenedForTheFirstTime: boolean = false;
  71. private currentEnrolledPosition: Fraction = new Fraction(0, 1);
  72. // (*) private musicSheetParameterObject: MusicSheetParameterObject = undefined;
  73. private rules: EngravingRules;
  74. // (*) private musicSheetParameterChangedDelegate: MusicSheetParameterChangedDelegate;
  75. /* Whether BPM info is present in the sheet. If it is set to false, each measure's BPM was set to a default of 120. */
  76. private hasBPMInfo: boolean;
  77. /**
  78. * Get the global index within the music sheet for this staff.
  79. * @param staff
  80. * @returns {number}
  81. */
  82. public static getIndexFromStaff(staff: Staff): number {
  83. return staff.idInMusicSheet;
  84. }
  85. public get SourceMeasures(): SourceMeasure[] {
  86. return this.sourceMeasures;
  87. }
  88. public set SourceMeasures(value: SourceMeasure[]) {
  89. this.sourceMeasures = value;
  90. }
  91. public get Repetitions(): Repetition[] {
  92. return this.repetitions;
  93. }
  94. public set Repetitions(value: Repetition[]) {
  95. this.repetitions = value;
  96. }
  97. public get DynListStaves(): DynamicsContainer[][] {
  98. return this.dynListStaves;
  99. }
  100. public get TimestampSortedTempoExpressionsList(): MultiTempoExpression[] {
  101. return this.timestampSortedTempoExpressionsList;
  102. }
  103. public get TimestampSortedDynamicExpressionsList(): DynamicsContainer[] {
  104. return this.timestampSortedDynamicExpressionsList;
  105. }
  106. public get InstrumentalGroups(): InstrumentalGroup[] {
  107. return this.instrumentalGroups;
  108. }
  109. public get Parts(): Instrument[] { // Instrument should be renamed to Part
  110. return this.instruments;
  111. }
  112. public get Instruments(): Instrument[] { // deprecated
  113. // this method name should remain for a while to maintain compatibility even when Instrument is renamed to Part
  114. return this.instruments;
  115. }
  116. public get SheetPlaybackSetting(): PlaybackSettings {
  117. return this.playbackSettings;
  118. }
  119. public set SheetPlaybackSetting(value: PlaybackSettings) {
  120. this.playbackSettings = value;
  121. }
  122. public get DrawErroneousMeasures(): boolean {
  123. return this.drawErroneousMeasures;
  124. }
  125. public set DrawErroneousMeasures(value: boolean) {
  126. this.drawErroneousMeasures = value;
  127. }
  128. public get HasBeenOpenedForTheFirstTime(): boolean {
  129. return this.hasBeenOpenedForTheFirstTime;
  130. }
  131. public set HasBeenOpenedForTheFirstTime(value: boolean) {
  132. this.hasBeenOpenedForTheFirstTime = value;
  133. }
  134. public InitializeStartTempoInBPM(startTempo: number): void {
  135. // (*) this.playbackSettings.BeatsPerMinute = startTempo;
  136. this.userStartTempoInBPM = startTempo;
  137. }
  138. public get DefaultStartTempoInBpm(): number {
  139. return this.defaultStartTempoInBpm;
  140. }
  141. public set DefaultStartTempoInBpm(value: number) {
  142. this.defaultStartTempoInBpm = value;
  143. this.InitializeStartTempoInBPM(value);
  144. }
  145. public get Path(): string {
  146. return this.path;
  147. }
  148. public set Path(value: string) {
  149. this.path = value;
  150. }
  151. public get Staves(): Staff[] {
  152. return this.staves;
  153. }
  154. public get TitleString(): string {
  155. if (this.title) {
  156. return this.title.text;
  157. } else {
  158. return "";
  159. }
  160. }
  161. public set TitleString(value: string) {
  162. this.Title = new Label(value);
  163. }
  164. public get SubtitleString(): string {
  165. if (this.subtitle) {
  166. return this.subtitle.text;
  167. } else {
  168. return "";
  169. }
  170. }
  171. public set SubtitleString(value: string) {
  172. this.Subtitle = new Label(value);
  173. }
  174. public get ComposerString(): string {
  175. if (this.composer) {
  176. return this.composer.text;
  177. } else {
  178. return "";
  179. }
  180. }
  181. public set ComposerString(value: string) {
  182. this.Composer = new Label(value);
  183. }
  184. public get LyricistString(): string {
  185. if (this.lyricist) {
  186. return this.lyricist.text;
  187. } else {
  188. return "";
  189. }
  190. }
  191. public set LyricistString(value: string) {
  192. this.Lyricist = new Label(value);
  193. }
  194. public get Title(): Label {
  195. return this.title;
  196. }
  197. public set Title(value: Label) {
  198. this.title = value;
  199. }
  200. public get Subtitle(): Label {
  201. return this.subtitle;
  202. }
  203. public set Subtitle(value: Label) {
  204. this.subtitle = value;
  205. }
  206. public get Composer(): Label {
  207. return this.composer;
  208. }
  209. public set Composer(value: Label) {
  210. this.composer = value;
  211. }
  212. public get Lyricist(): Label {
  213. return this.lyricist;
  214. }
  215. public set Lyricist(value: Label) {
  216. this.lyricist = value;
  217. }
  218. public get Rules(): EngravingRules {
  219. if (!this.rules) {
  220. log.debug("warning: sheet.Rules was undefined. Creating new EngravingRules.");
  221. this.rules = new EngravingRules();
  222. }
  223. return this.rules;
  224. }
  225. public set Rules(value: EngravingRules) {
  226. this.rules = value;
  227. }
  228. public get SheetErrors(): MusicSheetErrors {
  229. return this.musicSheetErrors;
  230. }
  231. public get SelectionStart(): Fraction {
  232. return this.selectionStart;
  233. }
  234. public set SelectionStart(value: Fraction) {
  235. this.selectionStart = value.clone();
  236. this.currentEnrolledPosition = value.clone();
  237. }
  238. public get SelectionEnd(): Fraction {
  239. return this.selectionEnd;
  240. }
  241. public set SelectionEnd(value: Fraction) {
  242. this.selectionEnd = value;
  243. }
  244. public set HasBPMInfo(value: boolean) {
  245. this.hasBPMInfo = value;
  246. }
  247. public get HasBPMInfo(): boolean {
  248. return this.hasBPMInfo;
  249. }
  250. // (*) public get MusicSheetParameterObject(): MusicSheetParameterObject {
  251. // return this.musicSheetParameterObject;
  252. //}
  253. // (*) public set MusicSheetParameterObject(value: MusicSheetParameterObject) {
  254. // this.musicSheetParameterObject = value;
  255. // this.Title = new Label(this.musicSheetParameterObject.Title);
  256. // this.Composer = new Label(this.musicSheetParameterObject.Composer);
  257. //}
  258. public addMeasure(measure: SourceMeasure): void {
  259. this.sourceMeasures.push(measure);
  260. measure.measureListIndex = this.sourceMeasures.length - 1;
  261. }
  262. public checkForInstrumentWithNoVoice(): void {
  263. for (let idx: number = 0, len: number = this.instruments.length; idx < len; ++idx) {
  264. const instrument: Instrument = this.instruments[idx];
  265. if (instrument.Voices.length === 0) {
  266. const voice: Voice = new Voice(instrument, 1);
  267. instrument.Voices.push(voice);
  268. }
  269. }
  270. }
  271. /**
  272. *
  273. * @param staffIndexInMusicSheet - The global staff index, iterating through all staves of all instruments.
  274. * @returns {Staff}
  275. */
  276. public getStaffFromIndex(staffIndexInMusicSheet: number): Staff {
  277. return this.staves[staffIndexInMusicSheet];
  278. }
  279. public fillStaffList(): void {
  280. let i: number = 0;
  281. for (let idx: number = 0, len: number = this.instruments.length; idx < len; ++idx) {
  282. const instrument: Instrument = this.instruments[idx];
  283. for (let idx2: number = 0, len2: number = instrument.Staves.length; idx2 < len2; ++idx2) {
  284. const staff: Staff = instrument.Staves[idx2];
  285. staff.idInMusicSheet = i;
  286. this.staves.push(staff);
  287. i++;
  288. }
  289. }
  290. }
  291. public get MusicPartManager(): MusicPartManager {
  292. return this.musicPartManager;
  293. }
  294. public set MusicPartManager(value: MusicPartManager) {
  295. this.musicPartManager = value;
  296. }
  297. public getCompleteNumberOfStaves(): number {
  298. let num: number = 0;
  299. for (let idx: number = 0, len: number = this.instruments.length; idx < len; ++idx) {
  300. const instrument: Instrument = this.instruments[idx];
  301. num += instrument.Staves.length;
  302. }
  303. return num;
  304. }
  305. /**
  306. * Return a sourceMeasureList, where the given indices correspond to the whole SourceMeasureList of the MusicSheet.
  307. * @param start
  308. * @param end
  309. * @returns {SourceMeasure[]}
  310. */
  311. public getListOfMeasuresFromIndeces(start: number, end: number): SourceMeasure[] {
  312. const measures: SourceMeasure[] = [];
  313. for (let i: number = start; i <= end; i++) {
  314. measures.push(this.sourceMeasures[i]);
  315. }
  316. return measures;
  317. }
  318. /**
  319. * Returns the next SourceMeasure from a given SourceMeasure.
  320. * @param measure
  321. */
  322. public getNextSourceMeasure(measure: SourceMeasure): SourceMeasure {
  323. const index: number = this.sourceMeasures.indexOf(measure);
  324. if (index === this.sourceMeasures.length - 1) {
  325. return measure;
  326. }
  327. return this.sourceMeasures[index + 1];
  328. }
  329. /**
  330. * Returns the first SourceMeasure of MusicSheet.
  331. */
  332. public getFirstSourceMeasure(): SourceMeasure {
  333. return this.sourceMeasures[0];
  334. }
  335. /**
  336. * Returns the last SourceMeasure of MusicSheet.
  337. */
  338. public getLastSourceMeasure(): SourceMeasure {
  339. return this.sourceMeasures[this.sourceMeasures.length - 1];
  340. }
  341. public resetAllNoteStates(): void {
  342. const iterator: MusicPartManagerIterator = this.MusicPartManager.getIterator();
  343. while (!iterator.EndReached && iterator.CurrentVoiceEntries) {
  344. for (let idx: number = 0, len: number = iterator.CurrentVoiceEntries.length; idx < len; ++idx) {
  345. const voiceEntry: VoiceEntry = iterator.CurrentVoiceEntries[idx];
  346. for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
  347. const note: Note = voiceEntry.Notes[idx2];
  348. note.state = NoteState.Normal;
  349. }
  350. }
  351. iterator.moveToNext();
  352. }
  353. }
  354. public getMusicSheetInstrumentIndex(instrument: Instrument): number {
  355. return this.Instruments.indexOf(instrument);
  356. }
  357. public getGlobalStaffIndexOfFirstStaff(instrument: Instrument): number {
  358. const instrumentIndex: number = this.getMusicSheetInstrumentIndex(instrument);
  359. let staffLineIndex: number = 0;
  360. for (let i: number = 0; i < instrumentIndex; i++) {
  361. staffLineIndex += this.Instruments[i].Staves.length;
  362. }
  363. return staffLineIndex;
  364. }
  365. /**
  366. * Set to the index-given Repetition a new (set from user) value.
  367. * @param index
  368. * @param value
  369. */
  370. public setRepetitionNewUserNumberOfRepetitions(index: number, value: number): void {
  371. let repIndex: number = 0;
  372. for (let i: number = 0; i < this.repetitions.length; i++) {
  373. if (this.repetitions[i] instanceof Repetition) { // FIXME
  374. if (index === repIndex) {
  375. this.repetitions[i].UserNumberOfRepetitions = value;
  376. break;
  377. } else {
  378. repIndex++;
  379. }
  380. }
  381. }
  382. }
  383. /**
  384. * Return the [[Repetition]] from the given index.
  385. * @param index
  386. * @returns {any}
  387. */
  388. public getRepetitionByIndex(index: number): Repetition {
  389. let repIndex: number = 0;
  390. for (let i: number = 0; i < this.repetitions.length; i++) {
  391. if (this.repetitions[i] instanceof Repetition) {
  392. if (index === repIndex) {
  393. return <Repetition>this.repetitions[i];
  394. }
  395. repIndex++;
  396. }
  397. }
  398. return undefined;
  399. }
  400. public CompareTo(other: MusicSheet): number {
  401. return this.Title.text.localeCompare(other.Title.text);
  402. }
  403. // (*)
  404. //public get IInstruments(): IInstrument[] {
  405. // return this.instruments.slice()
  406. //}
  407. //public get IInitializableInstruments(): ISettableInstrument[] {
  408. // return this.instruments.slice();
  409. //}
  410. //public get IRepetitions(): IRepetition[] {
  411. // try {
  412. // let repetitions: IRepetition[] = [];
  413. // for (let idx: number = 0, len: number = this.repetitions.length; idx < len; ++idx) {
  414. // let partListEntry: PartListEntry = this.repetitions[idx];
  415. // if (partListEntry instanceof Repetition) {
  416. // repetitions.push(<Repetition>partListEntry);
  417. // }
  418. // }
  419. // return repetitions;
  420. // } catch (ex) {
  421. // log.info("MusicSheet.IRepetitions get: ", ex);
  422. // return undefined;
  423. // }
  424. //
  425. //}
  426. public getExpressionsStartTempoInBPM(): number {
  427. if (this.TimestampSortedTempoExpressionsList.length > 0) {
  428. const me: MultiTempoExpression = this.TimestampSortedTempoExpressionsList[0];
  429. if (me.InstantaneousTempo) {
  430. return me.InstantaneousTempo.TempoInBpm;
  431. } else if (me.ContinuousTempo) {
  432. return me.ContinuousTempo.StartTempo;
  433. }
  434. }
  435. return this.userStartTempoInBPM;
  436. }
  437. public get Errors(): { [n: number]: string[] } {
  438. return this.musicSheetErrors.measureErrors;
  439. }
  440. public get FirstMeasureNumber(): number {
  441. try {
  442. return this.getFirstSourceMeasure().MeasureNumber;
  443. } catch (ex) {
  444. log.info("MusicSheet.FirstMeasureNumber: ", ex);
  445. return 0;
  446. }
  447. }
  448. public get LastMeasureNumber(): number {
  449. try {
  450. return this.getLastSourceMeasure().MeasureNumber;
  451. } catch (ex) {
  452. log.info("MusicSheet.LastMeasureNumber: ", ex);
  453. return 0;
  454. }
  455. }
  456. public get CurrentEnrolledPosition(): Fraction {
  457. return this.currentEnrolledPosition.clone();
  458. }
  459. public set CurrentEnrolledPosition(value: Fraction) {
  460. this.currentEnrolledPosition = value.clone();
  461. }
  462. public get Transpose(): number {
  463. return this.transpose;
  464. }
  465. public set Transpose(value: number) {
  466. this.transpose = value;
  467. }
  468. // (*)
  469. //public SetMusicSheetParameter(parameter: MusicSheetParameters, value: Object): void {
  470. // if (this.PhonicScoreInterface) {
  471. // this.PhonicScoreInterface.RequestMusicSheetParameter(parameter, value);
  472. // } else {
  473. // let oldValue: Object = 0;
  474. // if (!parameter) { // FIXME MusicSheetParameters.MusicSheetTranspose) {
  475. // oldValue = this.Transpose;
  476. // this.Transpose = value;
  477. // }
  478. // if (!parameter) { // FIXME MusicSheetParameters.StartTempoInBPM) {
  479. // oldValue = this.UserStartTempoInBPM;
  480. // this.UserStartTempoInBPM = value;
  481. // }
  482. // if (!parameter) { // FIXME MusicSheetParameters.HighlightErrors) {
  483. // oldValue = value;
  484. // }
  485. // if (this.MusicSheetParameterChanged) {
  486. // this.musicSheetParameterChangedDelegate(undefined, parameter, value, oldValue);
  487. // }
  488. // }
  489. //}
  490. //public get MusicSheetParameterChanged(): MusicSheetParameterChangedDelegate {
  491. // return this.musicSheetParameterChangedDelegate;
  492. //}
  493. //public set MusicSheetParameterChanged(value: MusicSheetParameterChangedDelegate) {
  494. // this.musicSheetParameterChangedDelegate = value;
  495. //}
  496. public get FullNameString(): string {
  497. return this.ComposerString + " " + this.TitleString;
  498. }
  499. public get IdString(): string {
  500. return this.idString;
  501. }
  502. public set IdString(value: string) {
  503. this.idString = value;
  504. }
  505. // (*)
  506. // public Dispose(): void {
  507. // this.MusicSheetParameterChanged = undefined;
  508. // for (let idx: number = 0, len: number = this.instruments.length; idx < len; ++idx) {
  509. // let instrument: Instrument = this.instruments[idx];
  510. // instrument.dispose();
  511. // }
  512. // }
  513. public getEnrolledSelectionStartTimeStampWorkaround(): Fraction {
  514. const iter: MusicPartManagerIterator = this.MusicPartManager.getIterator(this.SelectionStart);
  515. return Fraction.createFromFraction(iter.CurrentEnrolledTimestamp);
  516. }
  517. public get SheetEndTimestamp(): Fraction {
  518. const lastMeasure: SourceMeasure = this.getLastSourceMeasure();
  519. return Fraction.plus(lastMeasure.AbsoluteTimestamp, lastMeasure.Duration);
  520. }
  521. /**
  522. * Works only if the [[SourceMeasure]]s are already filled with VerticalStaffEntryContainers!
  523. * @param timeStamp
  524. * @returns {SourceMeasure}
  525. */
  526. public getSourceMeasureFromTimeStamp(timeStamp: Fraction): SourceMeasure {
  527. for (let idx: number = 0, len: number = this.sourceMeasures.length; idx < len; ++idx) {
  528. const sm: SourceMeasure = this.sourceMeasures[idx];
  529. for (let idx2: number = 0, len2: number = sm.VerticalSourceStaffEntryContainers.length; idx2 < len2; ++idx2) {
  530. const vssec: VerticalSourceStaffEntryContainer = sm.VerticalSourceStaffEntryContainers[idx2];
  531. if (timeStamp.Equals(vssec.getAbsoluteTimestamp())) {
  532. return sm;
  533. }
  534. }
  535. }
  536. return this.findSourceMeasureFromTimeStamp(timeStamp);
  537. }
  538. public findSourceMeasureFromTimeStamp(timestamp: Fraction): SourceMeasure {
  539. for (const sm of this.sourceMeasures) {
  540. if (sm.AbsoluteTimestamp.lte(timestamp) && timestamp.lt(Fraction.plus(sm.AbsoluteTimestamp, sm.Duration))) {
  541. return sm;
  542. }
  543. }
  544. }
  545. public getVisibleInstruments(): Instrument[] {
  546. const visInstruments: Instrument[] = [];
  547. for (let idx: number = 0, len: number = this.Instruments.length; idx < len; ++idx) {
  548. const instrument: Instrument = this.Instruments[idx];
  549. if (instrument.Voices.length > 0 && instrument.Voices[0].Visible) {
  550. visInstruments.push(instrument);
  551. }
  552. }
  553. return visInstruments;
  554. }
  555. }