MusicSheet.ts 20 KB

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