MusicSheet.ts 20 KB

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