MusicSheet.ts 21 KB

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