PlaybackManager_Test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {TestUtils} from "../../Util/TestUtils";
  2. import {OpenSheetMusicDisplay} from "../../../src/OpenSheetMusicDisplay/OpenSheetMusicDisplay";
  3. import { LinearTimingSource } from "../../../src/Playback/TimingSources/LinearTimingSource";
  4. import { PlaybackManager } from "../../../src/Playback/PlaybackManager";
  5. import { BasicAudioPlayer } from "../../../src/Playback/BasicAudioPlayer";
  6. describe("PlaybackManager", () => {
  7. const div: HTMLElement = document.createElement("div");
  8. const osmd: OpenSheetMusicDisplay =
  9. TestUtils.createOpenSheetMusicDisplay(div);
  10. // for (const score of TestUtils.XmlTestSet) {
  11. // testFile(score);
  12. // }
  13. // use it.only to test only this.
  14. // TODO if just using it() (all tests), this can still crash the browser somehow.
  15. it.skip("getSheetDuration total", (done: Mocha.Done) => {
  16. //let currentFileIndex: number = 0;
  17. loadNextFile(0, done);
  18. }).timeout(10000);
  19. function loadNextFile(fileIndex: number, done: Mocha.Done): void {
  20. if (fileIndex > TestUtils.XmlTestSet.length - 1) {
  21. done();
  22. }
  23. const score: Document = TestUtils.getScore(TestUtils.XmlTestSet[fileIndex]);
  24. try {
  25. osmd.load(score).then(
  26. (result) => {
  27. const timingSource: LinearTimingSource = new LinearTimingSource();
  28. timingSource.Settings = osmd.Sheet.SheetPlaybackSetting;
  29. timingSource.logEnabled = false;
  30. const playbackManager: PlaybackManager = new PlaybackManager(
  31. timingSource, undefined, new BasicAudioPlayer(), undefined);
  32. osmd.PlaybackManager = playbackManager;
  33. playbackManager.initialize(osmd.Sheet.MusicPartManager);
  34. const timeWithoutRepeats: number = playbackManager.getSheetDurationInMs(false);
  35. const timeWithRepeats: number = playbackManager.getSheetDurationInMsWithRepeats();
  36. console.log("time with repeats: " + timeWithRepeats.toFixed(2) + " for " + osmd.Sheet.Title.text);
  37. console.log("time without repeats: " + timeWithoutRepeats.toFixed(2));
  38. chai.expect(timeWithRepeats >= timeWithoutRepeats).to.equal(true);
  39. loadNextFile(fileIndex + 1, done);
  40. },
  41. (err) => {
  42. // couldn't read file
  43. if (fileIndex < TestUtils.XmlTestSet.length - 1) {
  44. loadNextFile(fileIndex + 1, done);
  45. } else {
  46. done();
  47. // not sure why this if/else block is necessary, but otherwise it infinite loops when a file couldn't be loaded.
  48. // somehow the catch block doesn't seem to get executed.
  49. }
  50. });
  51. } catch (err) {
  52. console.log("error: " + err);
  53. loadNextFile(fileIndex + 1, done);
  54. // we need to catch the error and call the next file,
  55. // otherwise done() never gets called and the test hangs
  56. }
  57. }
  58. // this variation of doing the test is similar to the loop in Xml_Test.ts,
  59. // but seems to crash the browser because the for loop calls too many tests at once that are too demanding.
  60. // (Xml_Test does the same, but it looks like the test is simple enough to not overload the browser)
  61. // function testFile(scoreName: string): void {
  62. // it("getSheetDuration: " + scoreName, (done: Mocha.Done) => {
  63. // //let timingSource: LinearTimingSource; // somehow this will be undefined in load().then()
  64. // //let playbackManager: PlaybackManager;
  65. // if (false) {
  66. // initialize(osmd, undefined, undefined);
  67. // }
  68. // const score: Document = TestUtils.getScore(scoreName);
  69. // osmd.load(score).then(() => {
  70. // const timingSource: LinearTimingSource = new LinearTimingSource();
  71. // timingSource.Settings = osmd.Sheet.SheetPlaybackSetting;
  72. // timingSource.logEnabled = false;
  73. // const playbackManager = new PlaybackManager(timingSource, undefined, new BasicAudioPlayer(), undefined);
  74. // osmd.PlaybackManager = playbackManager;
  75. // playbackManager.initialize(osmd.Sheet.MusicPartManager);
  76. // const timeWithoutRepeats: number = playbackManager.getSheetDurationInMs(false);
  77. // const timeWithRepeats: number = playbackManager.getSheetDurationInMsWithRepeats();
  78. // console.log("time with repeats: " + timeWithRepeats + " for " + osmd.Sheet.Title.text);
  79. // console.log("time without repeats: " + timeWithoutRepeats);
  80. // chai.expect(timeWithRepeats >= timeWithoutRepeats).to.equal(true);
  81. // done();
  82. // });
  83. // }).timeout(10000);
  84. // }
  85. // function initialize(osmd, timingSource, playbackManager) {
  86. // timingSource = new LinearTimingSource();
  87. // playbackManager = new PlaybackManager(timingSource, undefined, new BasicAudioPlayer(), undefined);
  88. // // timingSource.reset();
  89. // // timingSource.pause();
  90. // //playbackManager.removeListener(osmd.cursor); // only necessary if no duplicate checks in addListener
  91. // // playbackManager.addListener(osmd.cursor);
  92. // playbackManager.reset();
  93. // osmd.PlaybackManager = playbackManager;
  94. // }
  95. });