MusicSheet.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. // FIXME
  20. //type MusicSheetParameters = any;
  21. //type MultiTempoExpression = any;
  22. //type PlaybackSettings = any;
  23. //type MusicSheetParameterObject = any;
  24. //type EngravingRules = any;
  25. //type MusicSheetErrors = any;
  26. //type IPhonicScoreInterface = any;
  27. //type MusicSheetParameterChangedDelegate = any;
  28. //type IInstrument = any;
  29. //type ISettableInstrument = any;
  30. //type IRepetition = any;
  31. // FIXME Andrea: Commented out some things, have a look at (*)
  32. export class PlaybackSettings {
  33. public rhythm: Fraction;
  34. }
  35. export class MusicSheet /*implements ISettableMusicSheet, IComparable<MusicSheet>*/ {
  36. constructor() {
  37. this.rules = EngravingRules.Rules;
  38. // (*) this.playbackSettings = new PlaybackSettings(new Fraction(4, 4, false), 100);
  39. this.userStartTempoInBPM = 100;
  40. this.pageWidth = 120;
  41. this.MusicPartManager = new MusicPartManager(this);
  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. 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. let instrument: Instrument = this.instruments[idx];
  233. if (instrument.Voices.length === 0) {
  234. let voice: Voice = new Voice(instrument, 1);
  235. instrument.Voices.push(voice);
  236. }
  237. }
  238. }
  239. public getStaffFromIndex(staffIndexInMusicSheet: number): Staff {
  240. return this.staves[staffIndexInMusicSheet];
  241. }
  242. public fillStaffList(): void {
  243. let i: number = 0;
  244. for (let idx: number = 0, len: number = this.instruments.length; idx < len; ++idx) {
  245. let instrument: Instrument = this.instruments[idx];
  246. for (let idx2: number = 0, len2: number = instrument.Staves.length; idx2 < len2; ++idx2) {
  247. let staff: Staff = instrument.Staves[idx2];
  248. staff.idInMusicSheet = i;
  249. this.staves.push(staff);
  250. i++;
  251. }
  252. }
  253. }
  254. public get MusicPartManager(): MusicPartManager {
  255. return this.musicPartManager;
  256. }
  257. public set MusicPartManager(value: MusicPartManager) {
  258. this.musicPartManager = value;
  259. }
  260. public getCompleteNumberOfStaves(): number {
  261. let num: number = 0;
  262. for (let idx: number = 0, len: number = this.instruments.length; idx < len; ++idx) {
  263. let instrument: Instrument = this.instruments[idx];
  264. num += instrument.Staves.length;
  265. }
  266. return num;
  267. }
  268. public getListOfMeasuresFromIndeces(start: number, end: number): SourceMeasure[] {
  269. let measures: SourceMeasure[] = [];
  270. for (let i: number = start; i <= end; i++) {
  271. measures.push(this.sourceMeasures[i]);
  272. }
  273. return measures;
  274. }
  275. public getNextSourceMeasure(measure: SourceMeasure): SourceMeasure {
  276. let index: number = this.sourceMeasures.indexOf(measure);
  277. if (index === this.sourceMeasures.length - 1) {
  278. return measure;
  279. }
  280. return this.sourceMeasures[index + 1];
  281. }
  282. public getFirstSourceMeasure(): SourceMeasure {
  283. return this.sourceMeasures[0];
  284. }
  285. public getLastSourceMeasure(): SourceMeasure {
  286. return this.sourceMeasures[this.sourceMeasures.length - 1];
  287. }
  288. public resetAllNoteStates(): void {
  289. let iterator: MusicPartManagerIterator = this.MusicPartManager.getIterator();
  290. while (!iterator.EndReached && iterator.CurrentVoiceEntries !== undefined) {
  291. for (let idx: number = 0, len: number = iterator.CurrentVoiceEntries.length; idx < len; ++idx) {
  292. let voiceEntry: VoiceEntry = iterator.CurrentVoiceEntries[idx];
  293. for (let idx2: number = 0, len2: number = voiceEntry.Notes.length; idx2 < len2; ++idx2) {
  294. let note: Note = voiceEntry.Notes[idx2];
  295. note.state = NoteState.Normal;
  296. }
  297. }
  298. iterator.moveToNext();
  299. }
  300. }
  301. public getMusicSheetInstrumentIndex(instrument: Instrument): number {
  302. return this.Instruments.indexOf(instrument);
  303. }
  304. public getGlobalStaffIndexOfFirstStaff(instrument: Instrument): number {
  305. let instrumentIndex: number = this.getMusicSheetInstrumentIndex(instrument);
  306. let staffLineIndex: number = 0;
  307. for (let i: number = 0; i < instrumentIndex; i++) {
  308. staffLineIndex += this.Instruments[i].Staves.length;
  309. }
  310. return staffLineIndex;
  311. }
  312. public setRepetitionNewUserNumberOfRepetitions(index: number, value: number): void {
  313. let repIndex: number = 0;
  314. for (let i: number = 0; i < this.repetitions.length; i++) {
  315. if (this.repetitions[i] instanceof Repetition) { // FIXME
  316. if (index === repIndex) {
  317. this.repetitions[i].UserNumberOfRepetitions = value;
  318. break;
  319. } else {
  320. repIndex++;
  321. }
  322. }
  323. }
  324. }
  325. public getRepetitionByIndex(index: number): Repetition {
  326. let repIndex: number = 0;
  327. for (let i: number = 0; i < this.repetitions.length; i++) {
  328. if (this.repetitions[i] instanceof Repetition) {
  329. if (index === repIndex) {
  330. return <Repetition>this.repetitions[i];
  331. }
  332. repIndex++;
  333. }
  334. }
  335. return undefined;
  336. }
  337. public CompareTo(other: MusicSheet): number {
  338. return this.Title.text.localeCompare(other.Title.text);
  339. }
  340. // (*)
  341. //public get IInstruments(): IInstrument[] {
  342. // return this.instruments.slice()
  343. //}
  344. //public get IInitializableInstruments(): ISettableInstrument[] {
  345. // return this.instruments.slice();
  346. //}
  347. //public get IRepetitions(): IRepetition[] {
  348. // try {
  349. // let repetitions: IRepetition[] = [];
  350. // for (let idx: number = 0, len: number = this.repetitions.length; idx < len; ++idx) {
  351. // let partListEntry: PartListEntry = this.repetitions[idx];
  352. // if (partListEntry instanceof Repetition) {
  353. // repetitions.push(<Repetition>partListEntry);
  354. // }
  355. // }
  356. // return repetitions;
  357. // } catch (ex) {
  358. // console.log(/*Logger.DefaultLogger.LogError(LogLevel.NORMAL, FIXME */ "MusicSheet.IRepetitions get: ", ex);
  359. // return undefined;
  360. // }
  361. //
  362. //}
  363. //public GetExpressionsStartTempoInBPM(): number {
  364. // if (this.TimestampSortedTempoExpressionsList.length > 0) {
  365. // let me: MultiTempoExpression = this.TimestampSortedTempoExpressionsList[0];
  366. // if (me.InstantaniousTempo !== undefined) {
  367. // return me.InstantaniousTempo.TempoInBpm;
  368. // } else if (me.ContinuousTempo !== undefined) {
  369. // return me.ContinuousTempo.StartTempo;
  370. // }
  371. // }
  372. // return this.UserStartTempoInBPM;
  373. //}
  374. public get Errors(): { [n: number]: string[]; } {
  375. return this.musicSheetErrors.measureErrors;
  376. }
  377. public get FirstMeasureNumber(): number {
  378. try {
  379. return this.getFirstSourceMeasure().MeasureNumber;
  380. } catch (ex) {
  381. console.log(/* FIXME LogLevel.NORMAL, */ "MusicSheet.FirstMeasureNumber: ", ex);
  382. return 0;
  383. }
  384. }
  385. public get LastMeasureNumber(): number {
  386. try {
  387. return this.getLastSourceMeasure().MeasureNumber;
  388. } catch (ex) {
  389. console.log(/* FIXME LogLevel.NORMAL, */ "MusicSheet.LastMeasureNumber: ", ex);
  390. return 0;
  391. }
  392. }
  393. public get CurrentEnrolledPosition(): Fraction {
  394. return this.currentEnrolledPosition.clone();
  395. }
  396. public set CurrentEnrolledPosition(value: Fraction) {
  397. this.currentEnrolledPosition = value.clone();
  398. }
  399. public get Transpose(): number {
  400. return this.transpose;
  401. }
  402. public set Transpose(value: number) {
  403. this.transpose = value;
  404. }
  405. // (*)
  406. //public SetMusicSheetParameter(parameter: MusicSheetParameters, value: Object): void {
  407. // if (this.PhonicScoreInterface !== undefined) {
  408. // this.PhonicScoreInterface.RequestMusicSheetParameter(parameter, value);
  409. // } else {
  410. // let oldValue: Object = 0;
  411. // if (parameter === undefined) { // FIXME MusicSheetParameters.MusicSheetTranspose) {
  412. // oldValue = this.Transpose;
  413. // this.Transpose = <number>value;
  414. // }
  415. // if (parameter === undefined) { // FIXME MusicSheetParameters.StartTempoInBPM) {
  416. // oldValue = this.UserStartTempoInBPM;
  417. // this.UserStartTempoInBPM = <number>value;
  418. // }
  419. // if (parameter === undefined) { // FIXME MusicSheetParameters.HighlightErrors) {
  420. // oldValue = value;
  421. // }
  422. // if (this.MusicSheetParameterChanged !== undefined) {
  423. // this.musicSheetParameterChangedDelegate(undefined, parameter, value, oldValue);
  424. // }
  425. // }
  426. //}
  427. //public get MusicSheetParameterChanged(): MusicSheetParameterChangedDelegate {
  428. // return this.musicSheetParameterChangedDelegate;
  429. //}
  430. //public set MusicSheetParameterChanged(value: MusicSheetParameterChangedDelegate) {
  431. // this.musicSheetParameterChangedDelegate = value;
  432. //}
  433. public get FullNameString(): string {
  434. return this.ComposerString + " " + this.TitleString;
  435. }
  436. public get IdString(): string {
  437. return this.idString;
  438. }
  439. public set IdString(value: string) {
  440. this.idString = value;
  441. }
  442. // public Dispose(): void {
  443. // this.MusicSheetParameterChanged = undefined;
  444. // for (let idx: number = 0, len: number = this.instruments.length; idx < len; ++idx) {
  445. // let instrument: Instrument = this.instruments[idx];
  446. // instrument.dispose(); // FIXME
  447. // }
  448. // }
  449. public getEnrolledSelectionStartTimeStampWorkaround(): Fraction {
  450. let iter: MusicPartManagerIterator = this.MusicPartManager.getIterator(this.SelectionStart);
  451. return Fraction.createFromFraction(iter.CurrentEnrolledTimestamp);
  452. }
  453. public get SheetEndTimestamp(): Fraction {
  454. let lastMeasure: SourceMeasure = this.getLastSourceMeasure();
  455. return Fraction.plus(lastMeasure.AbsoluteTimestamp, lastMeasure.Duration);
  456. }
  457. public getSourceMeasureFromTimeStamp(timeStamp: Fraction): SourceMeasure {
  458. for (let idx: number = 0, len: number = this.sourceMeasures.length; idx < len; ++idx) {
  459. let sm: SourceMeasure = this.sourceMeasures[idx];
  460. for (let idx2: number = 0, len2: number = sm.VerticalSourceStaffEntryContainers.length; idx2 < len2; ++idx2) {
  461. let vssec: VerticalSourceStaffEntryContainer = sm.VerticalSourceStaffEntryContainers[idx2];
  462. if (Fraction.Equal(timeStamp, vssec.getAbsoluteTimestamp())) {
  463. return sm;
  464. }
  465. }
  466. }
  467. return this.findSourceMeasureFromTimeStamp(timeStamp);
  468. }
  469. public findSourceMeasureFromTimeStamp(timestamp: Fraction): SourceMeasure {
  470. for (let sm of this.sourceMeasures) {
  471. if (sm.AbsoluteTimestamp.lte(timestamp) && timestamp.lt(Fraction.plus(sm.AbsoluteTimestamp, sm.Duration))) {
  472. return sm;
  473. }
  474. }
  475. }
  476. public getVisibleInstruments(): Instrument[] {
  477. let visInstruments: Instrument[] = [];
  478. for (let idx: number = 0, len: number = this.Instruments.length; idx < len; ++idx) {
  479. let instrument: Instrument = this.Instruments[idx];
  480. if (instrument.Voices.length > 0 && instrument.Voices[0].Visible) {
  481. visInstruments.push(instrument);
  482. }
  483. }
  484. return visInstruments;
  485. }
  486. }