MusicSheet.ts 20 KB

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