PlaybackManager.ts 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. import { ITimingSource } from "../Common/Interfaces/ITimingSource";
  2. import { IMessageViewer } from "../Common/Interfaces/IMessageViewer";
  3. import { IAudioPlayer } from "../Common/Interfaces/IAudioPlayer";
  4. import { MusicPartManager, MusicPartManagerIterator } from "../MusicalScore/MusicParts";
  5. import { PlaybackIterator } from "../MusicalScore/Playback/PlaybackIterator";
  6. import { Dictionary } from "typescript-collections";
  7. import { Staff, SourceMeasure, VoiceEntry, Note, MidiInstrument } from "../MusicalScore/VoiceData";
  8. import { Fraction } from "../Common/DataObjects";
  9. import { MetronomeInstrument } from "./MetronomeInstrument";
  10. import { CursorPosChangedData } from "../Common/DataObjects/CursorPosChangedData";
  11. import { Repetition } from "../MusicalScore/MusicSource";
  12. import { TextTranslation } from "../Common/Strings/TextTranslation";
  13. import { NoteState, Instrument, SubInstrument, MusicSheet } from "../MusicalScore";
  14. import { DynamicsContainer } from "../MusicalScore/VoiceData/HelperObjects";
  15. import { PlaybackEntry } from "../MusicalScore/Playback/PlaybackEntry";
  16. import { ContinuousDynamicExpression } from "../MusicalScore/VoiceData/Expressions/ContinuousExpressions";
  17. import { PlaybackNote } from "../MusicalScore/Playback/PlaybackNote";
  18. import log from "loglevel";
  19. import { IAudioMetronomePlayer } from "../Common/Interfaces/IAudioMetronomePlayer";
  20. import { ISettableInstrument } from "../Common/Interfaces/ISettableInstrument";
  21. import { PlaybackState, MessageBoxType } from "../Common/Enums/PsEnums";
  22. import { IPlaybackListener } from "../Common/Interfaces/IPlaybackListener";
  23. import { IPlaybackParametersListener } from "../Common/Interfaces/IPlaybackParametersListener";
  24. import { AbstractExpression } from "../MusicalScore/VoiceData/Expressions";
  25. export class ChannelNote {
  26. public note: PlaybackNote;
  27. public key: number;
  28. public channel: number;
  29. constructor(k: number, c: number, n: PlaybackNote = undefined) {
  30. this.note = n;
  31. this.key = k;
  32. this.channel = c;
  33. }
  34. }
  35. export class PlaybackManager implements IPlaybackParametersListener {
  36. protected timingSource: ITimingSource;
  37. protected resetRequested: boolean;
  38. protected loopTriggeredReset: boolean;
  39. protected tempoUserFactor: number;
  40. protected currentBPM: number;
  41. protected overrideBPM: number = undefined;
  42. protected listeners: IPlaybackListener[] = [];
  43. public addListener(listener: IPlaybackListener): void {
  44. if (this.listeners.includes(listener)) {
  45. return;
  46. }
  47. this.listeners.push(listener);
  48. }
  49. public removeListener(listenerToRemove: IPlaybackListener): void {
  50. const index: number = this.listeners.indexOf(listenerToRemove);
  51. if (index >= 0) {
  52. this.listeners.splice(index, 1);
  53. }
  54. }
  55. private readonly percussionChannel: number = 10; // this is a definition of the midi interface (cannot be changed)
  56. private readonly metronomeChannel: number = 9;
  57. private messageViewer: IMessageViewer;
  58. private audioMetronomePlayer: IAudioMetronomePlayer;
  59. private audioPlayer: IAudioPlayer<any>;
  60. private musicPartManager: MusicPartManager;
  61. private cursorIterator: MusicPartManagerIterator;
  62. get CursorIterator(): MusicPartManagerIterator {
  63. return this.cursorIterator;
  64. }
  65. private playbackIterator: PlaybackIterator;
  66. //private Dictionary<int, MidiChannelInfo> instrumentsPerMidiSoundDict = new Dictionary<int, MidiChannelInfo>();
  67. //private Dictionary<int, int> midiSoundToChannelMappingDict = new Dictionary<int, int>();
  68. //private int[] midiChannelToSoundArray = new int[16];
  69. //Staff is not considered Unique for key purposes here. Had to use something unique - staff ID
  70. private instrumentToStaffToMidiChannelDict: Dictionary<Staff, number> = new Dictionary<Staff, number>();
  71. //store this data just in case
  72. private instrumentIdMapping: Dictionary<number, Instrument> = new Dictionary<number, Instrument>();
  73. public get InstrumentIdMapping(): Dictionary<number, Instrument> {
  74. return this.instrumentIdMapping;
  75. }
  76. //private List<int> staffIndexToMidiChannelMapping = new List<int>();
  77. private freeMidiChannels: number[] = [];
  78. private notesToStop: Dictionary<Fraction, ChannelNote[]> = new Dictionary<Fraction, ChannelNote[]>();
  79. private metronomeNote: ChannelNote = new ChannelNote(88, this.metronomeChannel);
  80. private metronomeNoteFirstBeat: ChannelNote = new ChannelNote(64, this.metronomeChannel);
  81. private currentMeasure: SourceMeasure = undefined;
  82. private currentTimestamp: Fraction = undefined;
  83. private closestNextTimestamp: Fraction = undefined;
  84. private currentMetronomeBaseTimestamp: Fraction = undefined;
  85. private currentBeatDuration: Fraction = undefined;
  86. private currentIteratorSourceTimeStamp: Fraction = undefined;
  87. private beatCounter: number = 0;
  88. protected runningState: PlaybackState = PlaybackState.Stopped;
  89. private isRunning: boolean = false;
  90. private isInitialized: boolean = false; // make sure midi device gets opened only once
  91. private nextIteratorTimestamp: Fraction;
  92. private playNextMetronomeAt: Fraction;
  93. // private masterTranspose: number = 0;
  94. private isPlaying: boolean = false;
  95. private metronome: MetronomeInstrument;
  96. private metronomeSoundPlayed: boolean = false;
  97. /** Whether a dummy sound was played to initialize the audio context / enable sound (on iOS). */
  98. public DummySoundPlayed: boolean = false;
  99. private tempoImpactFactor: number = 1.0;
  100. private sheetStartBPM: number;
  101. private currentReferenceBPM: number;
  102. private readonly defaultVolume: number = 0.8;
  103. private currentVolume: number = this.defaultVolume;
  104. private dynamicImpactFactor: number = 0.6;
  105. private scorePositionChangedData: CursorPosChangedData = new CursorPosChangedData();
  106. private tooManyInstruments: boolean = false;
  107. private currentRepetition: Repetition;
  108. private currentMeasureIndex: number;
  109. private metronomeOnlyBPM: number = 100;
  110. // private playbackThreadSyncObject = new object(); // TODO MB: Handle this.
  111. private readonly highlightPlayedNotes: boolean = false;
  112. private startRhythmBeats: number;
  113. private startRhythmDenominator: number;
  114. private isPreCounting: boolean;
  115. public MoveCursorDuringPrecount: boolean = true;
  116. private fermataActive: boolean;
  117. private doPreCount: boolean = true;
  118. public IgnoreFixedInstrumentKeysForPercussion: boolean = true;
  119. constructor (timingSource: ITimingSource, audioMetronomePlayer: IAudioMetronomePlayer, audioPlayer: IAudioPlayer<any>, messageViewer: IMessageViewer) {
  120. const metronomeLabel: string = TextTranslation.translateText("Playback/LabelMetronome", "Metronome");
  121. this.metronome = new MetronomeInstrument(-1, metronomeLabel, false, true, 0.0, MidiInstrument.Percussion);
  122. this.timingSource = timingSource;
  123. this.audioMetronomePlayer = audioMetronomePlayer;
  124. this.audioPlayer = audioPlayer;
  125. this.messageViewer = messageViewer;
  126. }
  127. public get RunningState(): PlaybackState {
  128. return this.runningState;
  129. }
  130. public set RunningState(value: PlaybackState) {
  131. this.runningState = value;
  132. }
  133. public DoPlayback: boolean;
  134. /** Do the initial pre-count */
  135. public get DoPreCount(): boolean {
  136. return this.doPreCount;
  137. }
  138. public set DoPreCount(value: boolean) {
  139. if (this.doPreCount !== value) {
  140. this.doPreCount = value;
  141. }
  142. }
  143. public PreCountMeasures: number;
  144. public PreCountBeats: number;
  145. public get Metronome(): ISettableInstrument {
  146. return this.metronome;
  147. }
  148. public get MetronomeOnlyBPM(): number {
  149. return this.metronomeOnlyBPM;
  150. }
  151. public set MetronomeOnlyBPM(value: number) {
  152. this.metronomeOnlyBPM = value;
  153. }
  154. // public get Transpose(): number {
  155. // return this.masterTranspose;
  156. // }
  157. // public set Transpose(value: number) {
  158. // this.masterTranspose = value;
  159. // }
  160. public get OriginalBpm(): number {
  161. return this.currentReferenceBPM;
  162. }
  163. /** will be activated when any solo flag of an Instrument, Voice or Staff is set to true. */
  164. public SoloActive: boolean;
  165. public SoloAttenuationValue: number = 0;
  166. // Only used for debug and scheduling precision measurements
  167. //private wantedNextIteratorTimestampMs: number = 0;
  168. /** Play dummy sound to initialize audio context (e.g. on user click for iOS) */
  169. public playDummySound(): void {
  170. const context: AudioContext = this.audioPlayer.ac;
  171. // create empty buffer and play it (to initialize context on user click)
  172. const buffer: AudioBuffer = context.createBuffer(1, 1, 22050);
  173. const source: AudioBufferSourceNode = context.createBufferSource();
  174. source.buffer = buffer;
  175. source.connect(context.destination);
  176. // play the buffer. noteOn is the older version of start()
  177. if (source.start) {
  178. source.start(0);
  179. } else {
  180. (source as any).noteOn(0); // this was the old way to start a sound
  181. }
  182. this.DummySoundPlayed = true;
  183. }
  184. public playVoiceEntry(voiceEntry: VoiceEntry): void {
  185. const ve: VoiceEntry = voiceEntry;
  186. if (ve !== undefined) {
  187. // lock(this.playbackThreadSyncObject) {
  188. this.stopAllCurrentlyPlayingNotes();
  189. if (this.highlightPlayedNotes) {
  190. const notes: Note[] = [];
  191. for (const note of ve.Notes) {
  192. note.state = NoteState.Selected;
  193. notes.push(note);
  194. }
  195. //this.NotesPlaybackEventOccurred(notes);
  196. }
  197. //int staffIndex = this.musicPartManager.MusicSheet.getIndexFromStaff(ve.Notes[0].ParentStaff);
  198. const channel: number = this.instrumentToStaffToMidiChannelDict.getValue(ve.Notes[0].ParentStaff);
  199. const instrument: Instrument = ve.ParentVoice.Parent;
  200. const isPercussion: boolean = instrument.MidiInstrumentId === MidiInstrument.Percussion;
  201. const volume: number = 0.8;
  202. const notesToPlay: ChannelNote[] = [];
  203. const transpose: number = this.musicPartManager.MusicSheet.Transpose;
  204. const instrumentPlaybackTranspose: number = ve.ParentVoice.Parent.PlaybackTranspose;
  205. for (const note of ve.MainPlaybackEntry.Notes.filter(n => n.MidiKey !== 0)) {
  206. // play the note
  207. let key: number = note.MidiKey;
  208. if (!isPercussion) {
  209. key += instrumentPlaybackTranspose + transpose;
  210. }
  211. if (note.ParentNote.PlaybackInstrumentId !== undefined) {
  212. const notePlaybackInstrument: SubInstrument =
  213. instrument.getSubInstrument(note.ParentNote.PlaybackInstrumentId);
  214. if (notePlaybackInstrument !== undefined) {
  215. if (notePlaybackInstrument.fixedKey >= 0) {
  216. key = notePlaybackInstrument.fixedKey;
  217. }
  218. }
  219. }
  220. // calculate stop time and remember it
  221. // const stopAt: Fraction = Fraction.plus(this.cursorIterator.CurrentEnrolledTimestamp, note.Length);
  222. try {
  223. if (this.audioPlayer !== undefined) {
  224. //const noteLengthFraction: Fraction = Fraction.createFromFraction(note.Length);
  225. this.audioPlayer.playSound(channel, key, volume, 500);
  226. }
  227. } catch (ex) {
  228. log.info("PlaybackManager.playVoiceEntry: ", ex);
  229. }
  230. notesToPlay.push(new ChannelNote(key, channel, note));
  231. }
  232. // TODO MB: Handle this
  233. // Task stopper = new Task(() => {
  234. // EventWaitHandle waiter = new EventWaitHandle(false, EventResetMode.AutoReset);
  235. // waiter.WaitOne(200);
  236. // lock(this.playbackThreadSyncObject) {
  237. // if (this.audioPlayer !== undefined) {
  238. // foreach(var n in notesToPlay) {
  239. // this.audioPlayer.stopSound(n.channel, n.key);
  240. // }
  241. // }
  242. // }
  243. // // redraw to color notes normal if highlighted in playback
  244. // //this.phonicScoreInterface.RedrawGraphicalMusicSheet();
  245. // });
  246. // stopper.Start();
  247. // }
  248. }
  249. }
  250. public initialize(musicPartMng: MusicPartManager): void {
  251. // lock(this.playbackThreadSyncObject) {
  252. if (this.isInitialized) {
  253. this.stopAllCurrentlyPlayingNotes();
  254. if (this.audioPlayer !== undefined) {
  255. this.audioPlayer.close();
  256. }
  257. this.cursorIterator = undefined;
  258. this.playbackIterator = undefined;
  259. }
  260. this.isInitialized = false;
  261. this.musicPartManager = musicPartMng;
  262. if (this.musicPartManager !== undefined) {
  263. const musicSheet: MusicSheet = this.musicPartManager.MusicSheet;
  264. // TODO MB: Converted musicSheetParameterChanged to setBpm in this file. Handle following line.
  265. //musicSheet.MusicSheetParameterChanged += this.musicSheetParameterChanged;
  266. this.cursorIterator = this.musicPartManager.getIterator();
  267. this.playbackIterator = new PlaybackIterator(musicSheet);
  268. if (this.audioPlayer !== undefined) {
  269. // TODO MB: I rewrote following line in line below. Does it do what it's supposed to do? Array.from() not supported in IE
  270. // List < MidiInstrument > uniqueMidiInstruments = musicSheet.Instruments.Select(item => item.MidiInstrumentId).Distinct().ToList();
  271. const uniqueMidiInstruments: MidiInstrument[] = Array.from(new Set(musicSheet.Instruments.map(item => item.MidiInstrumentId)));
  272. this.audioPlayer.open(uniqueMidiInstruments, 16);
  273. // set drums:
  274. this.audioPlayer.setSound(this.percussionChannel, MidiInstrument.Percussion);
  275. // TODO might be unnecessary to use another channel,
  276. // if we find sounds in the percussion channel that match these for metronomeNote (88 and 64 for woodblock):
  277. this.audioPlayer.setSound(this.metronomeChannel, MidiInstrument.Woodblock);
  278. }
  279. this.currentReferenceBPM = this.sheetStartBPM = musicSheet.getExpressionsStartTempoInBPM();
  280. this.tempoUserFactor = musicSheet.userStartTempoInBPM / this.sheetStartBPM;
  281. let instrumentId: number = 0;
  282. this.tooManyInstruments = false;
  283. // reset the dicts and channel mappings
  284. //this.staffIndexToMidiChannelMapping.Clear();
  285. this.instrumentToStaffToMidiChannelDict.clear();
  286. this.instrumentIdMapping.clear();
  287. for (let i: number = 0; i < this.percussionChannel; i++) {
  288. this.freeMidiChannels.push(i);
  289. }
  290. for (let i: number = this.percussionChannel + 1; i < 16; i++) {
  291. this.freeMidiChannels.push(i);
  292. }
  293. for (const instrument of musicSheet.Instruments) {
  294. this.instrumentIdMapping.setValue(instrumentId, instrument);
  295. for (const staff of instrument.Staves) {
  296. // just add a list element - calcMidiChannel() will provide the right value.
  297. //this.staffIndexToMidiChannelMapping.Add(-1);
  298. this.instrumentToStaffToMidiChannelDict.setValue(staff, -1);
  299. }
  300. this.setSound(instrumentId, instrument.MidiInstrumentId);
  301. instrumentId++;
  302. }
  303. if (this.audioPlayer !== undefined && this.tooManyInstruments) {
  304. const errorMsg: string = TextTranslation.translateText(
  305. "MidiNumberError",
  306. "This music sheet has more parts than are supported for midi playback. " +
  307. "Some parts will not be played with the desired instrument sounds."
  308. );
  309. if (this.messageViewer !== undefined && this.messageViewer.MessageOccurred !== undefined) {
  310. this.messageViewer.MessageOccurred(MessageBoxType.Warning, errorMsg);
  311. }
  312. }
  313. this.checkForSoloDeactivated();
  314. }
  315. this.isInitialized = true;
  316. // }
  317. this.reset();
  318. }
  319. public async play(): Promise<void> {
  320. if (this.cursorIterator !== undefined && this.cursorIterator.EndReached && this.currentTimestamp.gte(this.cursorIterator.CurrentEnrolledTimestamp)) {
  321. console.log("End reached, resetting");
  322. this.reset();
  323. }
  324. this.isPlaying = true;
  325. this.RunningState = PlaybackState.Running;
  326. await this.timingSource.start();
  327. this.loop();
  328. }
  329. public async pause(): Promise<void> {
  330. // lock(this.playbackThreadSyncObject) {
  331. this.isPlaying = false;
  332. // stop all active midi notes:
  333. this.stopAllCurrentlyPlayingNotes();
  334. // inform sample player to e.g. dispose used samples:
  335. if (this.audioPlayer !== undefined) {
  336. this.audioPlayer.playbackHasStopped();
  337. }
  338. // notify delegates (coreContainer) that the playing has finished:
  339. this.RunningState = PlaybackState.Stopped;
  340. await this.timingSource.pause();
  341. try {
  342. //bool endReached = this.iterator !== undefined && this.iterator.EndReached;
  343. for (const listener of this.listeners) {
  344. listener?.pauseOccurred(undefined);
  345. }
  346. } catch (ex) {
  347. log.debug("PlaybackManager.pause: ", ex);
  348. }
  349. // }
  350. }
  351. /** Effectively resets the playback to osmd.Sheet.SelectionStart.
  352. * If you want to reset to the start of the sheet,
  353. * call osmd.Sheet.SelectionStart = osmd.Sheet.sourceMeasures[0].AbsoluteTimestamp beforehand.
  354. */
  355. public reset(): void {
  356. // lock(this.playbackThreadSyncObject) {
  357. //this.resetRequested = true;
  358. this.doReset(this.DoPreCount, this.PreCountMeasures);
  359. if (this.musicPartManager === undefined) {
  360. return;
  361. }
  362. if (this.RunningState === PlaybackState.Stopped) {
  363. //this.isPlaying = true;
  364. }
  365. for (const listener of this.listeners) {
  366. listener?.resetOccurred(undefined);
  367. }
  368. // }
  369. }
  370. /** Starts play() from a time in milliseconds.*/
  371. public async playFromMs(timeInMs: number): Promise<void> {
  372. await this.pause();
  373. const timestamp: Fraction = this.getTimestampFromMs(timeInMs);
  374. const previousSelectionStart: Fraction = this.musicPartManager.MusicSheet.SelectionStart.clone();
  375. this.musicPartManager.MusicSheet.SelectionStart = timestamp;
  376. this.reset();
  377. this.play();
  378. this.musicPartManager.MusicSheet.SelectionStart = previousSelectionStart; // restore previous start point
  379. }
  380. public getTimestampFromMs(timeInMs: number): Fraction {
  381. return this.timingSource.Settings.getDurationAsNoteDuration(timeInMs);
  382. }
  383. /** Sets the cursor and MusicSheet.SelectionStart to the target timestamp (fraction),
  384. * but does not start playback if it was not started already.
  385. * To get the timestamp from milliseconds, use getTimestampFromMs().
  386. * To reset the playback start to the beginning of the sheet, call the function without arguments.
  387. */
  388. public setPlaybackStart(timestamp?: Fraction): void {
  389. if (!timestamp) {
  390. timestamp = this.musicPartManager.MusicSheet.SourceMeasures[0].AbsoluteTimestamp; // start of sheet
  391. }
  392. this.musicPartManager.MusicSheet.SelectionStart = timestamp;
  393. this.reset();
  394. }
  395. public Dispose(): void {
  396. // lock(this.playbackThreadSyncObject) {
  397. this.listeners = [];
  398. this.isRunning = false;
  399. // stop all active midi notes:
  400. if (this.isInitialized) {
  401. this.stopAllCurrentlyPlayingNotes();
  402. if (this.audioPlayer !== undefined) {
  403. this.audioPlayer.close();
  404. }
  405. }
  406. // this.musicPartManager = undefined;
  407. // // }
  408. }
  409. public setSound(instrumentId: number, newSoundId: MidiInstrument): boolean {
  410. if (newSoundId <= MidiInstrument.None || newSoundId > MidiInstrument.Percussion) {
  411. return false;
  412. }
  413. // lock(this.playbackThreadSyncObject) {
  414. try {
  415. const isPercussionNow: boolean = newSoundId === MidiInstrument.Percussion;
  416. if (instrumentId === -1) { // Metronome
  417. if (this.audioPlayer !== undefined && !isPercussionNow) {
  418. this.audioPlayer.setSound(0, newSoundId);
  419. }
  420. } else {
  421. let neededLastChannel: boolean = false;
  422. const musicSheet: MusicSheet = this.musicPartManager.MusicSheet;
  423. let instrument: Instrument;
  424. if (instrumentId === -2) {
  425. instrument = musicSheet.Instruments.find(x => x.Id === instrumentId);
  426. } else {
  427. instrument = musicSheet.Instruments[instrumentId];
  428. }
  429. this.instrumentIdMapping.setValue(instrument.Id, instrument);
  430. for (const staff of instrument.Staves) {
  431. //int staffIndex = musicSheet.getIndexFromStaff(staff);
  432. //int channel = this.staffIndexToMidiChannelMapping[staffIndex];
  433. let channel: number = this.instrumentToStaffToMidiChannelDict.getValue(staff);
  434. const wasPercussion: boolean = channel === this.percussionChannel;
  435. if (isPercussionNow) { // if is now a percussion
  436. const oldChannel: number = channel;
  437. channel = this.percussionChannel;
  438. // check if this instrument has been initialized and was no percussion instrument:
  439. if (oldChannel > 0 && !wasPercussion) {
  440. this.freeMidiChannels.push(oldChannel);
  441. this.freeMidiChannels.sort((a, b) => a - b); //TODO MB: Does this .sort do the same thing as C# .Sort()?
  442. }
  443. } else {
  444. if (channel < 0 || wasPercussion) { // if is not initialized or was a percussion:
  445. if (this.freeMidiChannels.length > 0) { // if still a free channel exists
  446. // get the channel and remove in from the free channels list
  447. channel = this.freeMidiChannels[0];
  448. this.freeMidiChannels.shift();
  449. } else { // if no channel is free any more:
  450. this.tooManyInstruments = true;
  451. // use last channel
  452. channel = 15;
  453. this.instrumentToStaffToMidiChannelDict.setValue(staff, channel);
  454. //// use piano sound
  455. //newSoundId = 0;
  456. neededLastChannel = true;
  457. }
  458. }
  459. }
  460. this.instrumentToStaffToMidiChannelDict.setValue(staff, channel);
  461. if (this.audioPlayer !== undefined && !isPercussionNow) {
  462. // TODO: Uncomment when panaroma is supported in audio player
  463. // this.audioPlayer.setPanorama(channel, instrument.SubInstruments[0].pan);
  464. // TODO: Commented because AvailableComponents not defined
  465. // if (AvailableComponents.PLAYBACK_INSTRUMENTS_AVAILABLE) {
  466. // // only set instrument sounds in pro version:
  467. this.audioPlayer.setSound(channel, newSoundId);
  468. // } else {
  469. // play all as piano in free version:
  470. // this.audioPlayer.setSound(channel, 0);
  471. // }
  472. }
  473. }
  474. if (neededLastChannel) {
  475. return false;
  476. }
  477. }
  478. return true;
  479. } catch (ex) {
  480. log.info("PlaybackManager.setSound: ", ex);
  481. return false;
  482. }
  483. // }
  484. }
  485. // public mainParameterChanged(client: IPhonicScoreClient, settingType: ProgramParameters, currentValue, previousValue): void {
  486. // switch (settingType) {
  487. // case ProgramParameters.DynamicInstructionsImpact:
  488. // this.dynamicImpactFactor = Convert.ToSingle(currentValue);
  489. // break;
  490. // case ProgramParameters.TempoInstructionsImpact: {
  491. // this.tempoImpactFactor = Convert.ToSingle(currentValue);
  492. // this.setTempo();
  493. // break;
  494. // }
  495. // }
  496. // }
  497. // TODO MB: Check if function setBpm() is sufficient for doing what commented function below does.
  498. // protected musicSheetParameterChanged(client: IPhonicScoreClient, parameter: MusicSheetParameters, currentValue, previousValue): void {
  499. // switch (parameter) {
  500. // case MusicSheetParameters.StartTempoInBPM: {
  501. // this.tempoUserFactor = Convert.ToSingle(currentValue) / this.sheetStartBPM;
  502. // this.setTempo();
  503. // break;
  504. // }
  505. // }
  506. // }
  507. protected setBpm(bpm: number): void {
  508. this.tempoUserFactor = bpm / this.sheetStartBPM;
  509. this.setTempo();
  510. }
  511. public handlePlaybackEvent(): void {
  512. // lock(this.playbackThreadSyncObject) {
  513. // initialize flags:
  514. const resetOccurred: boolean = this.resetRequested;
  515. this.resetRequested = false;
  516. // const resetMetronomeBeatCounter: boolean = resetOccurred;
  517. // @ts-ignore
  518. const resetMetronomeBeatCounter: boolean = resetOccurred;
  519. let updateCursorPosition: boolean = resetOccurred;
  520. let endHasBeenReached: boolean = false;
  521. if (resetOccurred) {
  522. const shallPrecount: boolean = this.DoPreCount;
  523. this.doReset(shallPrecount);
  524. }
  525. if (this.musicPartManager === undefined) {
  526. return;
  527. }
  528. /**********************************************/
  529. // set the current values:
  530. this.currentTimestamp = this.timingSource.getCurrentTimestamp();
  531. // console.log("TS ms: " + this.timingSource.getCurrentTimeInMs());
  532. // console.log("TS ts: " + this.currentTimestamp);
  533. endHasBeenReached = this.cursorIterator.EndReached;
  534. // TODO cursorIterator.CurrentMeasure can be undefined (at the end of the piece?)
  535. const currentMeasure: SourceMeasure = this.cursorIterator.CurrentMeasure;
  536. if (currentMeasure && !currentMeasure.WasRendered && !currentMeasure.isReducedToMultiRest) {
  537. // stop if current measure is not rendered, but not if it's part of a multi-measure rest
  538. endHasBeenReached = true;
  539. }
  540. /**********************************************/
  541. // handle the currently pending instructions:
  542. // stop the notes that are already over now:
  543. this.stopFinishedNotes();
  544. this.processTempoInstructions();
  545. if (this.RunningState === PlaybackState.Running) { // needed when resetting when in pause
  546. const newCursorTimestampReached: boolean = this.currentTimestamp.gte(this.cursorIterator.CurrentEnrolledTimestamp)
  547. && !endHasBeenReached;
  548. if (newCursorTimestampReached) {
  549. this.isPreCounting = false;
  550. /***** Metronome Beat Calculations *****/
  551. // check if the measure has changed:
  552. if (this.currentMeasure !== this.cursorIterator.CurrentMeasure &&
  553. this.cursorIterator.CurrentMeasure !== undefined) {
  554. // set current measure to the new measure
  555. this.currentMeasure = this.cursorIterator.CurrentMeasure;
  556. this.startRhythmBeats = this.currentMeasure.ActiveTimeSignature.Numerator;
  557. this.startRhythmDenominator = this.currentMeasure.ActiveTimeSignature.Denominator;
  558. // get the enrolled timestamp of this measure start:
  559. const relativeToMeasureTimestamp: Fraction = this.cursorIterator.CurrentRelativeInMeasureTimestamp;
  560. this.currentMetronomeBaseTimestamp = Fraction.minus(this.cursorIterator.CurrentEnrolledTimestamp, relativeToMeasureTimestamp);
  561. // calculate the new beat duration
  562. this.currentBeatDuration = new Fraction(1, this.currentMeasure.Duration.Denominator);
  563. const currentMeasureBPM: number = this.currentMeasure.TempoInBPM;
  564. if (currentMeasureBPM !== this.currentBPM && currentMeasureBPM > 0) {
  565. // TODO the default value for measure.TempoInBPM is 0, probably not a good default setup. But we also catch it in bpmChanged().
  566. this.bpmChanged(currentMeasureBPM, false); // update playback speed/bpm
  567. }
  568. // calculate which beat is next:
  569. const relativeNextMetronomeBeatTimestamp: Fraction = new Fraction();
  570. this.beatCounter = 0;
  571. while (relativeNextMetronomeBeatTimestamp.lt(relativeToMeasureTimestamp)) {
  572. relativeNextMetronomeBeatTimestamp.Add(this.currentBeatDuration);
  573. this.beatCounter++;
  574. }
  575. this.playNextMetronomeAt = Fraction.plus(
  576. this.currentMetronomeBaseTimestamp,
  577. new Fraction(this.beatCounter, this.currentMeasure.Duration.Denominator)
  578. );
  579. }
  580. /***** process dynamic instructions: *****/
  581. const dynamicEntries: DynamicsContainer[] = this.cursorIterator.getCurrentDynamicChangingExpressions();
  582. for (const dynamicEntry of dynamicEntries) {
  583. const staff: Staff = this.musicPartManager.MusicSheet.getStaffFromIndex(dynamicEntry.staffNumber);
  584. const channel: number =
  585. this.instrumentToStaffToMidiChannelDict.getValue(staff);
  586. //int channel = this.staffIndexToMidiChannelMapping[dynamicEntry.StaffNumber];
  587. let volume: number = this.currentVolume;
  588. if (dynamicEntry.parMultiExpression().StartingContinuousDynamic !== undefined) {
  589. // dynamic expression is continuous:
  590. const currentDynamicValue: number =
  591. dynamicEntry.parMultiExpression().StartingContinuousDynamic.getInterpolatedDynamic(
  592. this.cursorIterator.CurrentSourceTimestamp);
  593. if (currentDynamicValue >= 0) {
  594. volume = this.calculateFinalVolume(currentDynamicValue);
  595. }
  596. } else { // dynamic Expression is instantanious - immediately set the volume:
  597. volume = this.calculateFinalVolume(dynamicEntry.parMultiExpression().InstantaneousDynamic.Volume);
  598. }
  599. try {
  600. if (this.audioPlayer !== undefined) {
  601. this.audioPlayer.setVolume(channel, volume);
  602. }
  603. } catch (ex) {
  604. log.info("PlaybackManager.handlePlaybackEvent: ", ex);
  605. }
  606. }
  607. dynamicEntries.clear();
  608. }
  609. // check if the time has come to process the pending instructions:
  610. const dueEntries: { enrolledTimestamp: Fraction, playbackEntry: PlaybackEntry }[] = this.playbackIterator.Update(this.currentTimestamp);
  611. if (dueEntries.length > 0) {
  612. // play new notes
  613. if (this.DoPlayback) {
  614. const playbackedNotes: PlaybackNote[] = [];
  615. for (const entry of dueEntries) {
  616. if (!entry.playbackEntry.ParentVoiceEntry.ParentSourceStaffEntry.VerticalContainerParent.ParentMeasure.WasRendered) {
  617. continue; // don't play back entry that isn't visible. (e.g. first note in measure after maxMeasureToDraw)
  618. }
  619. const playbackEntry: PlaybackEntry = entry.playbackEntry;
  620. const voiceEntry: VoiceEntry = playbackEntry.ParentVoiceEntry;
  621. if (playbackEntry.Notes.length === 0) {
  622. continue;
  623. }
  624. const instrument: Instrument = voiceEntry.ParentVoice.Parent;
  625. const staff: Staff = voiceEntry.Notes[0].ParentStaff;
  626. const staffIndex: number =
  627. MusicSheet.getIndexFromStaff(staff);
  628. let channel: number = this.instrumentToStaffToMidiChannelDict.getValue(staff);
  629. const isPercussion: boolean = instrument.MidiInstrumentId === MidiInstrument.Percussion;
  630. // choose percussion channel if Selected
  631. if (isPercussion) {
  632. channel = this.percussionChannel;
  633. }
  634. const currentlyActiveExpression: AbstractExpression = this.cursorIterator.ActiveDynamicExpressions[staffIndex];
  635. // adapt volume level for continuous expressions
  636. if (currentlyActiveExpression instanceof ContinuousDynamicExpression) {
  637. const currentDynamicValue: number =
  638. currentlyActiveExpression.getInterpolatedDynamic(
  639. this.cursorIterator.CurrentSourceTimestamp);
  640. if (currentDynamicValue >= 0) {
  641. const channelVolume: number = this.calculateFinalVolume(currentDynamicValue);
  642. try {
  643. if (this.audioPlayer !== undefined) {
  644. this.audioPlayer.setVolume(channel, channelVolume);
  645. }
  646. } catch (ex) {
  647. log.info("PlaybackManager.handlePlaybackEvent: ", ex);
  648. }
  649. }
  650. }
  651. // calculate volume from instrument volume, staff volume and voice volume:
  652. let volume: number = instrument.Volume * staff.Volume * voiceEntry.ParentVoice.Volume;
  653. // attenuate if in Solo mode an this voice is not soloed:
  654. const soloAttenuate: boolean = this.SoloActive &&
  655. !(instrument.Solo || voiceEntry.ParentVoice.Solo || staff.Solo);
  656. if (soloAttenuate) {
  657. volume *= this.SoloAttenuationValue;
  658. }
  659. // increase volume if this is an accent:
  660. const entryIsAccent: boolean = voiceEntry.VolumeModifier !== undefined;
  661. if (entryIsAccent) {
  662. volume *= 1.3;
  663. volume = Math.min(1, volume);
  664. }
  665. const transpose: number = this.musicPartManager.MusicSheet.Transpose;
  666. const instrumentPlaybackTranspose: number = instrument.PlaybackTranspose ?? 0;
  667. for (const note of playbackEntry.Notes.filter(n => n.MidiKey !== 0)) {
  668. // play the note
  669. let key: number = note.MidiKey;
  670. if (!isPercussion) {
  671. key += instrumentPlaybackTranspose + transpose;
  672. }
  673. // if note has another explicitly given playback instrument:
  674. if (note.ParentNote.PlaybackInstrumentId !== undefined) {
  675. // playback with other instrument:
  676. const notePlaybackInstrument: SubInstrument =
  677. instrument.getSubInstrument(note.ParentNote.PlaybackInstrumentId);
  678. if (notePlaybackInstrument !== undefined) {
  679. if (notePlaybackInstrument.fixedKey >= 0 && !(isPercussion && this.IgnoreFixedInstrumentKeysForPercussion)) {
  680. key = notePlaybackInstrument.fixedKey;
  681. }
  682. }
  683. // recalculate Volume for this instrument:
  684. const instrumentVolume: number = notePlaybackInstrument?.volume ?? 1;
  685. // TODO notePlaybackInstrument can be undefined, e.g. issue 42
  686. volume = instrumentVolume * staff.Volume * voiceEntry.ParentVoice.Volume;
  687. // attenuate if in Solo mode an this voice is not soloed:
  688. if (soloAttenuate) {
  689. volume *= this.SoloAttenuationValue;
  690. }
  691. if (entryIsAccent) {
  692. volume *= 1.3;
  693. volume = Math.min(1, volume);
  694. }
  695. }
  696. // calculate stop time and remember it
  697. let noteLength: Fraction = Fraction.createFromFraction(note.Length);
  698. let stopAt: Fraction;
  699. // ToDo MU: move this to PlaybackEntry
  700. const entryIsStaccato: boolean = voiceEntry.DurationModifier !== undefined;
  701. if (entryIsStaccato) {
  702. // Reduce length and stopAt time:
  703. noteLength = new Fraction(noteLength.Numerator * 2, noteLength.Denominator * 3);
  704. stopAt = Fraction.plus(entry.enrolledTimestamp, noteLength);
  705. } else {
  706. stopAt = Fraction.plus(entry.enrolledTimestamp, noteLength);
  707. }
  708. try {
  709. if (this.audioPlayer !== undefined) {
  710. this.audioPlayer.playSound( channel,
  711. key,
  712. volume,
  713. this.timingSource.getDurationInMs(noteLength));
  714. }
  715. } catch (ex) {
  716. log.info("PlaybackManager.handlePlaybackEvent. Failed playing sound: ", ex);
  717. }
  718. if (!this.notesToStop.containsKey(stopAt)) {
  719. // this.notesToStop.Add(stopAt, new List<ChannelNote>());
  720. this.notesToStop.setValue(stopAt, []);
  721. }
  722. this.notesToStop.getValue(stopAt).push(new ChannelNote(key, channel, note));
  723. if (this.highlightPlayedNotes) {
  724. note.ParentNote.state = NoteState.Selected;
  725. }
  726. playbackedNotes.push(note);
  727. }
  728. }
  729. /*** Inform about which notes are now played ***
  730. * e.g. for updating graphics
  731. */
  732. // TODO: Replace with generic event system
  733. // if (this.highlightPlayedNotes && this.NotesPlaybackEventOccurred !== undefined) {
  734. this.NotesPlaybackEventOccurred(playbackedNotes);
  735. }
  736. }
  737. if (newCursorTimestampReached) {
  738. // store current iterator parameters:
  739. this.currentIteratorSourceTimeStamp = this.cursorIterator.CurrentSourceTimestamp;
  740. this.currentMeasureIndex = this.cursorIterator.CurrentMeasureIndex;
  741. // this.currentRepetition = this.cursorIterator.CurrentRepetition;
  742. /************ Move to next sheet position ************/
  743. // move iterator already to next position, to find out how long to wait or if the End has been reached:
  744. this.cursorIterator.moveToNext();
  745. this.nextIteratorTimestamp = this.cursorIterator.CurrentEnrolledTimestamp;
  746. updateCursorPosition = true;
  747. }
  748. // Stop the sound of the last played metronome
  749. this.stopMetronomeSound();
  750. // Check for "end has been reached"
  751. if (endHasBeenReached && this.currentTimestamp.gte(this.cursorIterator.CurrentEnrolledTimestamp)) {
  752. // notify possible listeners:
  753. for (const listener of this.listeners) {
  754. listener?.selectionEndReached(undefined);
  755. }
  756. this.handleEndReached();
  757. } else {
  758. /******
  759. * Play Metronome if needed
  760. */
  761. if (this.currentTimestamp.gte(this.playNextMetronomeAt)) {
  762. updateCursorPosition = true;
  763. const playFirstBeatSample: boolean = this.beatCounter % this.startRhythmBeats === 0;
  764. this.playMetronomeSound(playFirstBeatSample);
  765. this.beatCounter++;
  766. }
  767. // calculate the next metronome beat timestamp
  768. if (this.currentMetronomeBaseTimestamp !== undefined) {
  769. this.playNextMetronomeAt = Fraction.plus(
  770. this.currentMetronomeBaseTimestamp,
  771. new Fraction(this.beatCounter, this.startRhythmDenominator)
  772. );
  773. }
  774. /*************************************/
  775. this.calculateClosestNextTimestamp();
  776. }
  777. } else {
  778. // needed when a reset was requested: reset parameters, fire score position changed and finally stop again
  779. this.isPlaying = false;
  780. }
  781. // Check for "updating the display"
  782. if (updateCursorPosition ||
  783. endHasBeenReached && !this.loopTriggeredReset) {
  784. // set the play cursor in the display
  785. this.updateScoreCursorPosition(resetOccurred);
  786. }
  787. // }
  788. }
  789. private NotesPlaybackEventOccurred(notes: PlaybackNote[]): void {
  790. for (const listener of this.listeners) {
  791. listener?.notesPlaybackEventOccurred(notes);
  792. }
  793. }
  794. public calculateFinalVolume(volume: number): number {
  795. return ((volume - this.defaultVolume) * this.dynamicImpactFactor + this.defaultVolume);
  796. }
  797. /** Main playback loop. Not a sheet music loop, but checks for new notes to be played. */
  798. private loop(): void {
  799. // start playing:
  800. try {
  801. this.isRunning = true;
  802. // @ts-ignore
  803. const reset: boolean = false;
  804. if (this.isPlaying) {
  805. try {
  806. if (this.isRunning && this.isInitialized) {
  807. //console.log(`handlePlayback, timing deviation: ${Math.round(this.timingSource.getCurrentTimeInMs()) - Math.round(this.wantedNextIteratorTimestampMs)}`);
  808. this.handlePlaybackEvent();
  809. if (this.closestNextTimestamp !== undefined) {
  810. const wantedNextElapsedMs: number = this.timingSource.getWaitingTimeForTimestampInMs(this.closestNextTimestamp);
  811. //this.wantedNextIteratorTimestampMs = this.timingSource.getCurrentTimeInMs() + wantedNextElapsedMs;
  812. window.setTimeout(() => { this.loop(); }, Math.max(0, wantedNextElapsedMs));
  813. //this.interruptWaiting.WaitOne(Math.max(0, wantedNextElapsedMs));
  814. }
  815. }
  816. } catch (ex) {
  817. this.pause();
  818. this.reset();
  819. const errorMsg: string = TextTranslation.translateText(
  820. "MidiPlaybackError",
  821. "An error occurred at the Midi Playback."
  822. );
  823. log.info("PlaybackManager.loop: " + errorMsg + " ", ex);
  824. if (this.messageViewer !== undefined && this.messageViewer.MessageOccurred !== undefined) {
  825. this.messageViewer.MessageOccurred(MessageBoxType.Error, errorMsg);
  826. }
  827. }
  828. }
  829. } catch (ex) {
  830. const errorMsg: string = TextTranslation.translateText(
  831. "MidiPlaybackLoopError",
  832. "An error occurred at the Midi Playback. Please restart the program in order for the Playback to be availiable again."
  833. );
  834. log.info("PlaybackManager.loop: " + errorMsg + " ", ex);
  835. if (this.messageViewer !== undefined && this.messageViewer.MessageOccurred !== undefined) {
  836. this.messageViewer.MessageOccurred(MessageBoxType.Error, errorMsg);
  837. }
  838. }
  839. this.isRunning = false;
  840. }
  841. private stopAllCurrentlyPlayingNotes(): void {
  842. try {
  843. // lock(this.playbackThreadSyncObject) {
  844. if (this.audioPlayer !== undefined) {
  845. // stop active metronome sound
  846. this.audioPlayer.stopSound(this.metronomeNoteFirstBeat.channel, this.metronomeNoteFirstBeat.key);
  847. this.audioPlayer.stopSound(this.metronomeNote.channel, this.metronomeNote.key);
  848. // stop active notes
  849. // TODO MB: check if port of following for..of is correct
  850. // check same in for..of below
  851. // for (const entry of this.notesToStop) {
  852. // for (const note of entry.Value) {
  853. // this.audioPlayer.stopSound(note.channel, note.key);
  854. // }
  855. // }
  856. for (const entry of this.notesToStop.values()) {
  857. for (const note of entry) {
  858. this.audioPlayer.stopSound(note.channel, note.key);
  859. }
  860. }
  861. }
  862. /*** Inform about which notes are now stopped ***
  863. * e.g. for updating graphics
  864. */
  865. const notes: Note[] = [];
  866. for (const entry of this.notesToStop.values()) {
  867. for (const note of entry) {
  868. note.note.ParentNote.state = NoteState.Normal;
  869. notes.push(note.note.ParentNote);
  870. }
  871. }
  872. if (this.highlightPlayedNotes) {
  873. // TODO: Replace with generic even system
  874. // if (this.NotesPlaybackEventOccurred !== undefined) {
  875. // this.NotesPlaybackEventOccurred(notes);
  876. // }
  877. }
  878. this.notesToStop.clear();
  879. // }
  880. } catch (ex) {
  881. log.info("PlaybackManager.stoppAllCurrentlyPlayingNotes: ", ex);
  882. }
  883. }
  884. protected doReset(shallPrecount: boolean, preCountMeasures: number = 1): void {
  885. this.nextIteratorTimestamp = undefined;
  886. this.playNextMetronomeAt = undefined;
  887. this.closestNextTimestamp = undefined;
  888. this.currentMeasure = undefined;
  889. this.beatCounter = 0;
  890. this.fermataActive = false;
  891. this.stopAllCurrentlyPlayingNotes();
  892. if (this.musicPartManager !== undefined) {
  893. this.cursorIterator = this.musicPartManager.getIterator();
  894. }
  895. if (this.cursorIterator === undefined) {
  896. return;
  897. }
  898. this.playbackIterator.Reset();
  899. this.currentIteratorSourceTimeStamp = this.cursorIterator.CurrentSourceTimestamp;
  900. this.nextIteratorTimestamp = this.cursorIterator.CurrentEnrolledTimestamp;
  901. this.currentMeasure = this.cursorIterator.CurrentMeasure;
  902. this.currentMeasureIndex = this.cursorIterator.CurrentMeasureIndex;
  903. // this.currentRepetition = this.cursorIterator.CurrentRepetition;
  904. this.startRhythmBeats = this.cursorIterator.currentPlaybackSettings().Rhythm.Numerator;
  905. this.startRhythmDenominator = this.cursorIterator.currentPlaybackSettings().Rhythm.Denominator;
  906. let preCountDuration: Fraction = new Fraction();
  907. if (shallPrecount) {
  908. this.isPreCounting = true;
  909. const rhythmDuration: Fraction = new Fraction(this.startRhythmBeats * preCountMeasures, this.startRhythmDenominator);
  910. const sourceMeasureDuration: Fraction = this.musicPartManager.MusicSheet.SourceMeasures[this.currentMeasureIndex].Duration;
  911. const measureDuration: Fraction = new Fraction(sourceMeasureDuration.Numerator * preCountMeasures, sourceMeasureDuration.Denominator);
  912. const duration: Fraction =
  913. Fraction.plus(this.musicPartManager.MusicSheet.SourceMeasures[this.currentMeasureIndex].AbsoluteTimestamp,
  914. measureDuration).Sub(this.currentIteratorSourceTimeStamp);
  915. preCountDuration = rhythmDuration;
  916. if (rhythmDuration.gte(duration)) { // make sure that missing duration can't get negative (e.g. if measure is longer that given rhythm.
  917. const missingDuration: Fraction = Fraction.minus(rhythmDuration, duration);
  918. if (missingDuration.RealValue / rhythmDuration.RealValue < 0.5) {
  919. preCountDuration.Add(missingDuration);
  920. } else {
  921. preCountDuration = missingDuration;
  922. }
  923. }
  924. }
  925. this.currentMetronomeBaseTimestamp = this.playNextMetronomeAt = Fraction.minus(this.cursorIterator.CurrentEnrolledTimestamp, preCountDuration);
  926. //this.timingSource.reset();
  927. this.timingSource.setTimeAndBpm(this.currentMetronomeBaseTimestamp,
  928. this.cursorIterator.currentPlaybackSettings().BeatsPerMinute);
  929. this.calculateClosestNextTimestamp();
  930. }
  931. /// <summary>
  932. /// Calculate the closest next timestamp at which the next instruction has to be processed
  933. /// </summary>
  934. private calculateClosestNextTimestamp(): void {
  935. const timestamps: Fraction[] = [];
  936. // add next timestamp for stopping notes
  937. if (this.notesToStop.size() > 0) {
  938. // timestamps.push(this.notesToStop.keys().Min());
  939. // TODO MB: Check if line below does what line above is supposed to do
  940. timestamps.push(this.notesToStop.keys().reduce( (a, b) => a.lt(b) ? a : b));
  941. }
  942. // add next timestamp for next notes or other sheet instruction
  943. if (this.playbackIterator.NextEntryTimestamp !== undefined) {
  944. timestamps.push(this.playbackIterator.NextEntryTimestamp);
  945. }
  946. if (this.nextIteratorTimestamp !== undefined) {
  947. timestamps.push(this.nextIteratorTimestamp);
  948. }
  949. // add next timestamp for metronome tick
  950. if (this.playNextMetronomeAt !== undefined) {
  951. timestamps.push(this.playNextMetronomeAt);
  952. }
  953. // get the closest next timestamp
  954. if (timestamps.length > 0) {
  955. // this.closestNextTimestamp = timestamps.Min();
  956. // TODO MB: Check if line below does what line above is supposed to do
  957. this.closestNextTimestamp = timestamps.reduce( (a, b) => a.lt(b) ? a : b);
  958. } else {
  959. this.closestNextTimestamp = undefined;
  960. }
  961. }
  962. /// <summary>
  963. /// Called when the end of the sheet or the selection has been reached
  964. /// </summary>
  965. protected handleEndReached(): void {
  966. this.pause();
  967. }
  968. /// <summary>
  969. /// Fire a delegate to inform the display, that the cursor position has changed
  970. /// </summary>
  971. /// <param name="resetOccurred"></param>
  972. private updateScoreCursorPosition(resetOccurred: boolean): void {
  973. if (this.isPreCounting && !this.MoveCursorDuringPrecount) {
  974. return;
  975. }
  976. this.scorePositionChangedData.CurrentMeasureIndex = this.currentMeasureIndex;
  977. this.scorePositionChangedData.CurrentRepetition = this.currentRepetition;
  978. this.scorePositionChangedData.PredictedPosition = this.currentTimestamp;
  979. this.scorePositionChangedData.CurrentBpm = this.musicPartManager.MusicSheet.SheetPlaybackSetting.BeatsPerMinute;
  980. this.scorePositionChangedData.ResetOccurred = resetOccurred;
  981. for (const listener of this.listeners) {
  982. listener?.cursorPositionChanged(this.currentIteratorSourceTimeStamp, this.scorePositionChangedData);
  983. }
  984. }
  985. private stopMetronomeSound(): void {
  986. if (this.metronomeSoundPlayed) {
  987. if (this.audioPlayer !== undefined) {
  988. this.audioPlayer.stopSound(this.metronomeNoteFirstBeat.channel, this.metronomeNoteFirstBeat.key);
  989. this.audioPlayer.stopSound(this.metronomeNote.channel, this.metronomeNote.key);
  990. }
  991. this.metronomeSoundPlayed = false;
  992. }
  993. }
  994. private playMetronomeSound(playFirstBeatSample: boolean): void {
  995. // play the metronome if needed:
  996. if (this.metronome.Audible ||
  997. this.metronome.Solo ||
  998. this.isPreCounting) {
  999. let volume: number = this.isPreCounting ? this.metronome.PreCountVolume : this.metronome.Volume;
  1000. if (!this.isPreCounting && this.SoloActive && !this.metronome.Solo) {
  1001. volume *= this.SoloAttenuationValue;
  1002. }
  1003. if (volume > 0) {
  1004. if (playFirstBeatSample) {
  1005. try {
  1006. if (this.audioPlayer !== undefined) {
  1007. this.audioPlayer.playSound(this.metronomeNoteFirstBeat.channel, this.metronomeNoteFirstBeat.key, volume, 1000);
  1008. }
  1009. if (this.audioMetronomePlayer !== undefined) {
  1010. this.audioMetronomePlayer.playFirstBeatSample(volume);
  1011. }
  1012. } catch (ex) {
  1013. log.info("PlaybackManager.playMetronomeSound: ", ex);
  1014. }
  1015. } else {
  1016. try {
  1017. if (this.audioPlayer !== undefined) {
  1018. this.audioPlayer.playSound(this.metronomeNote.channel, this.metronomeNote.key, volume, 1000);
  1019. }
  1020. if (this.audioMetronomePlayer !== undefined) {
  1021. this.audioMetronomePlayer.playBeatSample(volume);
  1022. }
  1023. } catch (ex) {
  1024. log.info("PlaybackManager.playMetronomeSound: ", ex);
  1025. }
  1026. }
  1027. this.metronomeSoundPlayed = true;
  1028. }
  1029. }
  1030. }
  1031. private stopFinishedNotes(): void {
  1032. // do the pending note stops:
  1033. let expiredKeys: Fraction[];
  1034. if (this.currentTimestamp !== undefined) {
  1035. expiredKeys = this.notesToStop.keys().filter(ts => ts.lte(this.currentTimestamp));
  1036. } else {
  1037. expiredKeys = this.notesToStop.keys();
  1038. }
  1039. for (const timestamp of expiredKeys) {
  1040. const notesToStop: ChannelNote[] = this.notesToStop.getValue(timestamp);
  1041. if (this.audioPlayer !== undefined) {
  1042. for (const note of notesToStop) {
  1043. this.audioPlayer.stopSound(note.channel, note.key);
  1044. }
  1045. }
  1046. /*** Inform about which notes are now stopped ***
  1047. * e.g. for updating graphics
  1048. */
  1049. const notes: Note[] = [];
  1050. for (const note of notesToStop) {
  1051. note.note.ParentNote.state = NoteState.Normal;
  1052. notes.push(note.note.ParentNote);
  1053. }
  1054. if (this.highlightPlayedNotes) {
  1055. // TODO: Replace with generic event system
  1056. // if (this.NotesPlaybackEventOccurred !== undefined) {
  1057. // this.NotesPlaybackEventOccurred(notes);
  1058. // }
  1059. }
  1060. this.notesToStop.remove(timestamp);
  1061. }
  1062. }
  1063. private processTempoInstructions(): void {
  1064. // 1. check if the current bpm of the iterator have changed (significantly):
  1065. if (Math.abs(this.currentReferenceBPM - this.cursorIterator.CurrentBpm) > 0.001) {
  1066. this.changeTempo(this.cursorIterator.CurrentBpm);
  1067. }
  1068. // 2. check for possible fermatas and slow down for that entry:
  1069. this.handleFermata();
  1070. }
  1071. private handleFermata(): void {
  1072. // check for fermatas:
  1073. let fermataFound: boolean = false;
  1074. if (!this.cursorIterator.EndReached) {
  1075. if (this.currentTimestamp.gte(this.cursorIterator.CurrentEnrolledTimestamp)) {
  1076. for (const ve of this.cursorIterator.CurrentVoiceEntries) {
  1077. fermataFound = ve.Fermata !== undefined;
  1078. }
  1079. }
  1080. }
  1081. if (fermataFound) {
  1082. if (!this.fermataActive) {
  1083. this.fermataActive = true;
  1084. this.changeTempo(this.cursorIterator.CurrentBpm / 3);
  1085. }
  1086. } else {
  1087. if (this.fermataActive) {
  1088. this.fermataActive = false;
  1089. this.changeTempo(this.cursorIterator.CurrentBpm);
  1090. }
  1091. }
  1092. }
  1093. public bpmChanged(newBpm: number, sheetOverride: boolean): void {
  1094. if (!(newBpm > 0)) {
  1095. // only accept valid bpm (> 0)
  1096. // TODO one sample has bpm 0 for measure 5: love again - dua lipa
  1097. log.info("invalid bpm set: " + newBpm + ". Ignoring.");
  1098. return;
  1099. }
  1100. if(sheetOverride && this.musicPartManager?.MusicSheet.SourceMeasures?.length > 0){
  1101. this.overrideBPM = newBpm;
  1102. this.musicPartManager.MusicSheet.SourceMeasures.forEach(measure => {
  1103. measure.TempoInBPM = newBpm;
  1104. });
  1105. }
  1106. this.currentBPM = newBpm;
  1107. this.timingSource.setBpm(newBpm);
  1108. }
  1109. public volumeChanged(instrument: number, newVolume: number): void {
  1110. this.currentVolume = newVolume / 100;
  1111. if (instrument === -1) {
  1112. this.metronome.Volume = this.currentVolume;
  1113. } else {
  1114. this.instrumentIdMapping.getValue(instrument).Volume = this.currentVolume;
  1115. }
  1116. }
  1117. public volumeMute(instrument: number): void {
  1118. if (instrument === -1) {
  1119. this.metronome.Mute = true;
  1120. } else {
  1121. this.instrumentIdMapping.getValue(instrument).Audible = false;
  1122. }
  1123. }
  1124. public volumeUnmute(instrument: number): void {
  1125. if (instrument === -1) {
  1126. this.metronome.Mute = false;
  1127. } else {
  1128. this.instrumentIdMapping.getValue(instrument).Audible = true;
  1129. }
  1130. }
  1131. private changeTempo(newTempoInBPM: number): void {
  1132. log.debug("PlaybackManager.changeTempo", `current tempo in BPM: ${newTempoInBPM}`);
  1133. //Console.WriteLine(currTempoInBPM.ToString());
  1134. if (newTempoInBPM > 0) {
  1135. this.currentReferenceBPM = newTempoInBPM;
  1136. this.setTempo();
  1137. }
  1138. }
  1139. protected setTempo(): void {
  1140. this.currentBPM = this.tempoUserFactor * this.getCurrentReferenceBPM();
  1141. this.timingSource.setBpm(this.currentBPM);
  1142. }
  1143. protected getCurrentReferenceBPM(): number {
  1144. return ((this.currentReferenceBPM - this.sheetStartBPM) * this.tempoImpactFactor + this.sheetStartBPM);
  1145. }
  1146. public checkForSoloDeactivated(): void {
  1147. if (this.musicPartManager.MusicSheet === undefined) {
  1148. this.SoloActive = false;
  1149. return;
  1150. }
  1151. let state: boolean = false;
  1152. for (const instrument of this.musicPartManager.MusicSheet.Instruments) {
  1153. for (const staff of instrument.Staves) {
  1154. state = state || staff.Solo;
  1155. }
  1156. for (const voice of instrument.Voices) {
  1157. state = state || voice.Solo;
  1158. }
  1159. }
  1160. state = state || this.Metronome.Solo;
  1161. if (!state) {
  1162. this.SoloActive = false;
  1163. }
  1164. }
  1165. /** Returns the duration of the piece in ms (by each measure's bpm, without repeats).
  1166. * The result may be inaccurate if you haven't set the bpm to the first measure's bpm before playback (or the other way round).
  1167. * In that case, getSheetDurationInMsEvenBpm() can be more accurate (previous version of this method)
  1168. */
  1169. public getSheetDurationInMs(): number {
  1170. let totalDuration: number = 0;
  1171. // code similar to PlaybackSettings.getDurationInMilliseconds()
  1172. const beatRealValue: number = 1.0 / 4.0;
  1173. for (const measure of this.musicPartManager.MusicSheet.SourceMeasures) {
  1174. const beatLengthInMs: number = 60000.0 / measure.TempoInBPM;
  1175. totalDuration += measure.Duration.RealValue * beatLengthInMs / beatRealValue;
  1176. }
  1177. return totalDuration;
  1178. }
  1179. /** Returns the sheet duration of the piece in ms given the tempo set via setBpm() doesn't change. */
  1180. public getSheetDurationInMsEvenBpm(): number {
  1181. return this.timingSource.getDurationInMs(this.musicPartManager.MusicSheet.SheetEndTimestamp);
  1182. }
  1183. //private class MidiChannelInfo
  1184. //{
  1185. // public List<IInstrument> subscribers = new List<IInstrument>();
  1186. // public int channel;
  1187. //}
  1188. }