DiffImages_Test_Experimental.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { OpenSheetMusicDisplay, CanvasVexFlowBackend } from "../../src";
  2. import { TestUtils } from "./TestUtils";
  3. //import fs from "fs";
  4. // experimental code, shouldn't be included in Karma test suite
  5. describe("GeneratePNGImages", () => {
  6. // Test all the following xml files:
  7. const sampleFilenames: string[] = [
  8. "Beethoven_AnDieFerneGeliebte.xml",
  9. // "CharlesGounod_Meditation.xml",
  10. // "Debussy_Mandoline.xml",
  11. // "Dichterliebe01.xml",
  12. // "JohannSebastianBach_Air.xml",
  13. // "JohannSebastianBach_PraeludiumInCDur_BWV846_1.xml",
  14. // "JosephHaydn_ConcertanteCello.xml",
  15. // "Mozart_AnChloe.xml",
  16. // "Mozart_DasVeilchen.xml",
  17. "MuzioClementi_SonatinaOpus36No1_Part1.xml",
  18. // "MuzioClementi_SonatinaOpus36No1_Part2.xml",
  19. // "MuzioClementi_SonatinaOpus36No3_Part1.xml",
  20. // "MuzioClementi_SonatinaOpus36No3_Part2.xml",
  21. // "Saltarello.xml",
  22. // "ScottJoplin_EliteSyncopations.xml",
  23. // "ScottJoplin_The_Entertainer.xml",
  24. // "TelemannWV40.102_Sonate-Nr.1.1-Dolce.xml",
  25. // "TelemannWV40.102_Sonate-Nr.1.2-Allegro-F-Dur.xml",
  26. ];
  27. for (const score of sampleFilenames) {
  28. generatePNG(score);
  29. }
  30. // TODO This is just example code for now.
  31. // generate PNG. TODO fs doesn't work with Karma. This is the big problem that needs to be worked around with ts/Karma.
  32. function generatePNG(sampleFilename: string): void {
  33. it(sampleFilename, (done: Mocha.Done) => {
  34. // Load the xml file content
  35. const score: Document = TestUtils.getScore(sampleFilename);
  36. const div: HTMLElement = document.createElement("div");
  37. const openSheetMusicDisplay: OpenSheetMusicDisplay =
  38. new OpenSheetMusicDisplay(div, { autoResize: false, backend: "canvas"});
  39. openSheetMusicDisplay.load(score);
  40. const testDir: string = "../data/images";
  41. //fs.mkdirSync(testDir, { recursive: true });
  42. const fileName: string = `${testDir}/${sampleFilename}.png`;
  43. console.log("fileName: " + fileName);
  44. console.log("before buffer");
  45. const canvasBackend: CanvasVexFlowBackend = openSheetMusicDisplay.Drawer.Backends[0] as CanvasVexFlowBackend;
  46. const imageData: string = (canvasBackend.getCanvas() as HTMLCanvasElement).toDataURL().split(";base64,").pop();
  47. const imageBuffer: Buffer = Buffer.from(imageData, "base64");
  48. console.log("imageBuffer.length: " + imageBuffer.length);
  49. //console.log("after buffer");
  50. //let arraybuffer = Uint8Array.from(imageBuffer, 'base64').buffer;
  51. //fs.writeFileSync(fileName, imageBuffer, { encoding: "base64" });
  52. done();
  53. }).timeout(30000);
  54. }
  55. });