PlaybackManager.ts 63 KB

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