MusicSheet.ts 21 KB

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