MusicSheet.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 !== undefined) {
  156. return this.title.text;
  157. } else {
  158. return "";
  159. }
  160. }
  161. public get SubtitleString(): string {
  162. if (this.subtitle !== undefined) {
  163. return this.subtitle.text;
  164. } else {
  165. return "";
  166. }
  167. }
  168. public get ComposerString(): string {
  169. if (this.composer !== undefined) {
  170. return this.composer.text;
  171. } else {
  172. return "";
  173. }
  174. }
  175. public get LyricistString(): string {
  176. if (this.lyricist !== undefined) {
  177. return this.lyricist.text;
  178. } else {
  179. return "";
  180. }
  181. }
  182. public get Title(): Label {
  183. return this.title;
  184. }
  185. public set Title(value: Label) {
  186. this.title = value;
  187. }
  188. public get Subtitle(): Label {
  189. return this.subtitle;
  190. }
  191. public set Subtitle(value: Label) {
  192. this.subtitle = value;
  193. }
  194. public get Composer(): Label {
  195. return this.composer;
  196. }
  197. public set Composer(value: Label) {
  198. this.composer = value;
  199. }
  200. public get Lyricist(): Label {
  201. return this.lyricist;
  202. }
  203. public set Lyricist(value: Label) {
  204. this.lyricist = value;
  205. }
  206. public get Rules(): EngravingRules {
  207. if (!this.rules) {
  208. log.debug("warning: sheet.Rules was undefined. Creating new EngravingRules.");
  209. this.rules = new EngravingRules();
  210. }
  211. return this.rules;
  212. }
  213. public set Rules(value: EngravingRules) {
  214. this.rules = value;
  215. }
  216. public get SheetErrors(): MusicSheetErrors {
  217. return this.musicSheetErrors;
  218. }
  219. public get SelectionStart(): Fraction {
  220. return this.selectionStart;
  221. }
  222. public set SelectionStart(value: Fraction) {
  223. this.selectionStart = value.clone();
  224. this.currentEnrolledPosition = value.clone();
  225. }
  226. public get SelectionEnd(): Fraction {
  227. return this.selectionEnd;
  228. }
  229. public set SelectionEnd(value: Fraction) {
  230. this.selectionEnd = value;
  231. }
  232. public set HasBPMInfo(value: boolean) {
  233. this.hasBPMInfo = value;
  234. }
  235. public get HasBPMInfo(): boolean {
  236. return this.hasBPMInfo;
  237. }
  238. // (*) public get MusicSheetParameterObject(): MusicSheetParameterObject {
  239. // return this.musicSheetParameterObject;
  240. //}
  241. // (*) public set MusicSheetParameterObject(value: MusicSheetParameterObject) {
  242. // this.musicSheetParameterObject = value;
  243. // this.Title = new Label(this.musicSheetParameterObject.Title);
  244. // this.Composer = new Label(this.musicSheetParameterObject.Composer);
  245. //}
  246. public addMeasure(measure: SourceMeasure): void {
  247. this.sourceMeasures.push(measure);
  248. measure.measureListIndex = this.sourceMeasures.length - 1;
  249. }
  250. public checkForInstrumentWithNoVoice(): void {
  251. for (let idx: number = 0, len: number = this.instruments.length; idx < len; ++idx) {
  252. const instrument: Instrument = this.instruments[idx];
  253. if (instrument.Voices.length === 0) {
  254. const voice: Voice = new Voice(instrument, 1);
  255. instrument.Voices.push(voice);
  256. }
  257. }
  258. }
  259. /**
  260. *
  261. * @param staffIndexInMusicSheet - The global staff index, iterating through all staves of all instruments.
  262. * @returns {Staff}
  263. */
  264. public getStaffFromIndex(staffIndexInMusicSheet: number): Staff {
  265. return this.staves[staffIndexInMusicSheet];
  266. }
  267. public fillStaffList(): void {
  268. let i: number = 0;
  269. for (let idx: number = 0, len: number = this.instruments.length; idx < len; ++idx) {
  270. const instrument: Instrument = this.instruments[idx];
  271. for (let idx2: number = 0, len2: number = instrument.Staves.length; idx2 < len2; ++idx2) {
  272. const staff: Staff = instrument.Staves[idx2];
  273. staff.idInMusicSheet = i;
  274. this.staves.push(staff);
  275. i++;
  276. }
  277. }
  278. }
  279. public get MusicPartManager(): MusicPartManager {
  280. return this.musicPartManager;
  281. }
  282. public set MusicPartManager(value: MusicPartManager) {
  283. this.musicPartManager = value;
  284. }
  285. public getCompleteNumberOfStaves(): number {
  286. let num: number = 0;
  287. for (let idx: number = 0, len: number = this.instruments.length; idx < len; ++idx) {
  288. const instrument: Instrument = this.instruments[idx];
  289. num += instrument.Staves.length;
  290. }
  291. return num;
  292. }
  293. /**
  294. * Return a sourceMeasureList, where the given indices correspond to the whole SourceMeasureList of the MusicSheet.
  295. * @param start
  296. * @param end
  297. * @returns {SourceMeasure[]}
  298. */
  299. public getListOfMeasuresFromIndeces(start: number, end: number): SourceMeasure[] {
  300. const measures: SourceMeasure[] = [];
  301. for (let i: number = start; i <= end; i++) {
  302. measures.push(this.sourceMeasures[i]);
  303. }
  304. return measures;
  305. }
  306. /**
  307. * Returns the next SourceMeasure from a given SourceMeasure.
  308. * @param measure
  309. */
  310. public getNextSourceMeasure(measure: SourceMeasure): SourceMeasure {
  311. const index: number = this.sourceMeasures.indexOf(measure);
  312. if (index === this.sourceMeasures.length - 1) {
  313. return measure;
  314. }
  315. return this.sourceMeasures[index + 1];
  316. }
  317. /**
  318. * Returns the first SourceMeasure of MusicSheet.
  319. */
  320. public getFirstSourceMeasure(): SourceMeasure {
  321. return this.sourceMeasures[0];
  322. }
  323. /**
  324. * Returns the last SourceMeasure of MusicSheet.
  325. */
  326. public getLastSourceMeasure(): SourceMeasure {
  327. return this.sourceMeasures[this.sourceMeasures.length - 1];
  328. }
  329. public resetAllNoteStates(): void {
  330. const iterator: MusicPartManagerIterator = this.MusicPartManager.getIterator();
  331. while (!iterator.EndReached && iterator.CurrentVoiceEntries !== undefined) {
  332. for (let idx: number = 0, len: number = iterator.CurrentVoiceEntries.length; idx < len; ++idx) {
  333. const voiceEntry: VoiceEntry = iterator.CurrentVoiceEntries[idx];
  334. for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
  335. const note: Note = voiceEntry.Notes[idx2];
  336. note.state = NoteState.Normal;
  337. }
  338. }
  339. iterator.moveToNext();
  340. }
  341. }
  342. public getMusicSheetInstrumentIndex(instrument: Instrument): number {
  343. return this.Instruments.indexOf(instrument);
  344. }
  345. public getGlobalStaffIndexOfFirstStaff(instrument: Instrument): number {
  346. const instrumentIndex: number = this.getMusicSheetInstrumentIndex(instrument);
  347. let staffLineIndex: number = 0;
  348. for (let i: number = 0; i < instrumentIndex; i++) {
  349. staffLineIndex += this.Instruments[i].Staves.length;
  350. }
  351. return staffLineIndex;
  352. }
  353. /**
  354. * Set to the index-given Repetition a new (set from user) value.
  355. * @param index
  356. * @param value
  357. */
  358. public setRepetitionNewUserNumberOfRepetitions(index: number, value: number): void {
  359. let repIndex: number = 0;
  360. for (let i: number = 0; i < this.repetitions.length; i++) {
  361. if (this.repetitions[i] instanceof Repetition) { // FIXME
  362. if (index === repIndex) {
  363. this.repetitions[i].UserNumberOfRepetitions = value;
  364. break;
  365. } else {
  366. repIndex++;
  367. }
  368. }
  369. }
  370. }
  371. /**
  372. * Return the [[Repetition]] from the given index.
  373. * @param index
  374. * @returns {any}
  375. */
  376. public getRepetitionByIndex(index: number): Repetition {
  377. let repIndex: number = 0;
  378. for (let i: number = 0; i < this.repetitions.length; i++) {
  379. if (this.repetitions[i] instanceof Repetition) {
  380. if (index === repIndex) {
  381. return <Repetition>this.repetitions[i];
  382. }
  383. repIndex++;
  384. }
  385. }
  386. return undefined;
  387. }
  388. public CompareTo(other: MusicSheet): number {
  389. return this.Title.text.localeCompare(other.Title.text);
  390. }
  391. // (*)
  392. //public get IInstruments(): IInstrument[] {
  393. // return this.instruments.slice()
  394. //}
  395. //public get IInitializableInstruments(): ISettableInstrument[] {
  396. // return this.instruments.slice();
  397. //}
  398. //public get IRepetitions(): IRepetition[] {
  399. // try {
  400. // let repetitions: IRepetition[] = [];
  401. // for (let idx: number = 0, len: number = this.repetitions.length; idx < len; ++idx) {
  402. // let partListEntry: PartListEntry = this.repetitions[idx];
  403. // if (partListEntry instanceof Repetition) {
  404. // repetitions.push(<Repetition>partListEntry);
  405. // }
  406. // }
  407. // return repetitions;
  408. // } catch (ex) {
  409. // log.info("MusicSheet.IRepetitions get: ", ex);
  410. // return undefined;
  411. // }
  412. //
  413. //}
  414. //public GetExpressionsStartTempoInBPM(): number {
  415. // if (this.TimestampSortedTempoExpressionsList.length > 0) {
  416. // let me: MultiTempoExpression = this.TimestampSortedTempoExpressionsList[0];
  417. // if (me.InstantaneousTempo !== undefined) {
  418. // return me.InstantaneousTempo.TempoInBpm;
  419. // } else if (me.ContinuousTempo !== undefined) {
  420. // return me.ContinuousTempo.StartTempo;
  421. // }
  422. // }
  423. // return this.UserStartTempoInBPM;
  424. //}
  425. public get Errors(): { [n: number]: string[]; } {
  426. return this.musicSheetErrors.measureErrors;
  427. }
  428. public get FirstMeasureNumber(): number {
  429. try {
  430. return this.getFirstSourceMeasure().MeasureNumber;
  431. } catch (ex) {
  432. log.info("MusicSheet.FirstMeasureNumber: ", ex);
  433. return 0;
  434. }
  435. }
  436. public get LastMeasureNumber(): number {
  437. try {
  438. return this.getLastSourceMeasure().MeasureNumber;
  439. } catch (ex) {
  440. log.info("MusicSheet.LastMeasureNumber: ", ex);
  441. return 0;
  442. }
  443. }
  444. public get CurrentEnrolledPosition(): Fraction {
  445. return this.currentEnrolledPosition.clone();
  446. }
  447. public set CurrentEnrolledPosition(value: Fraction) {
  448. this.currentEnrolledPosition = value.clone();
  449. }
  450. public get Transpose(): number {
  451. return this.transpose;
  452. }
  453. public set Transpose(value: number) {
  454. this.transpose = value;
  455. }
  456. // (*)
  457. //public SetMusicSheetParameter(parameter: MusicSheetParameters, value: Object): void {
  458. // if (this.PhonicScoreInterface !== undefined) {
  459. // this.PhonicScoreInterface.RequestMusicSheetParameter(parameter, value);
  460. // } else {
  461. // let oldValue: Object = 0;
  462. // if (parameter === undefined) { // FIXME MusicSheetParameters.MusicSheetTranspose) {
  463. // oldValue = this.Transpose;
  464. // this.Transpose = value;
  465. // }
  466. // if (parameter === undefined) { // FIXME MusicSheetParameters.StartTempoInBPM) {
  467. // oldValue = this.UserStartTempoInBPM;
  468. // this.UserStartTempoInBPM = value;
  469. // }
  470. // if (parameter === undefined) { // FIXME MusicSheetParameters.HighlightErrors) {
  471. // oldValue = value;
  472. // }
  473. // if (this.MusicSheetParameterChanged !== undefined) {
  474. // this.musicSheetParameterChangedDelegate(undefined, parameter, value, oldValue);
  475. // }
  476. // }
  477. //}
  478. //public get MusicSheetParameterChanged(): MusicSheetParameterChangedDelegate {
  479. // return this.musicSheetParameterChangedDelegate;
  480. //}
  481. //public set MusicSheetParameterChanged(value: MusicSheetParameterChangedDelegate) {
  482. // this.musicSheetParameterChangedDelegate = value;
  483. //}
  484. public get FullNameString(): string {
  485. return this.ComposerString + " " + this.TitleString;
  486. }
  487. public get IdString(): string {
  488. return this.idString;
  489. }
  490. public set IdString(value: string) {
  491. this.idString = value;
  492. }
  493. // (*)
  494. // public Dispose(): void {
  495. // this.MusicSheetParameterChanged = undefined;
  496. // for (let idx: number = 0, len: number = this.instruments.length; idx < len; ++idx) {
  497. // let instrument: Instrument = this.instruments[idx];
  498. // instrument.dispose();
  499. // }
  500. // }
  501. public getEnrolledSelectionStartTimeStampWorkaround(): Fraction {
  502. const iter: MusicPartManagerIterator = this.MusicPartManager.getIterator(this.SelectionStart);
  503. return Fraction.createFromFraction(iter.CurrentEnrolledTimestamp);
  504. }
  505. public get SheetEndTimestamp(): Fraction {
  506. const lastMeasure: SourceMeasure = this.getLastSourceMeasure();
  507. return Fraction.plus(lastMeasure.AbsoluteTimestamp, lastMeasure.Duration);
  508. }
  509. /**
  510. * Works only if the [[SourceMeasure]]s are already filled with VerticalStaffEntryContainers!
  511. * @param timeStamp
  512. * @returns {SourceMeasure}
  513. */
  514. public getSourceMeasureFromTimeStamp(timeStamp: Fraction): SourceMeasure {
  515. for (let idx: number = 0, len: number = this.sourceMeasures.length; idx < len; ++idx) {
  516. const sm: SourceMeasure = this.sourceMeasures[idx];
  517. for (let idx2: number = 0, len2: number = sm.VerticalSourceStaffEntryContainers.length; idx2 < len2; ++idx2) {
  518. const vssec: VerticalSourceStaffEntryContainer = sm.VerticalSourceStaffEntryContainers[idx2];
  519. if (timeStamp.Equals(vssec.getAbsoluteTimestamp())) {
  520. return sm;
  521. }
  522. }
  523. }
  524. return this.findSourceMeasureFromTimeStamp(timeStamp);
  525. }
  526. public findSourceMeasureFromTimeStamp(timestamp: Fraction): SourceMeasure {
  527. for (const sm of this.sourceMeasures) {
  528. if (sm.AbsoluteTimestamp.lte(timestamp) && timestamp.lt(Fraction.plus(sm.AbsoluteTimestamp, sm.Duration))) {
  529. return sm;
  530. }
  531. }
  532. }
  533. public getVisibleInstruments(): Instrument[] {
  534. const visInstruments: Instrument[] = [];
  535. for (let idx: number = 0, len: number = this.Instruments.length; idx < len; ++idx) {
  536. const instrument: Instrument = this.Instruments[idx];
  537. if (instrument.Voices.length > 0 && instrument.Voices[0].Visible) {
  538. visInstruments.push(instrument);
  539. }
  540. }
  541. return visInstruments;
  542. }
  543. }