generateDiffImagesPuppeteerLocalhost.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Render each OSMD sample, grab the generated images, and
  3. dump them into a local directory as PNG files.
  4. inspired by Vexflow's generate_png_images and vexflow-tests.js
  5. This is meant to be used with the visual regression test system in
  6. `tools/visual_regression.sh`. (TODO)
  7. */
  8. // function sleep(ms) {
  9. // return new Promise((resolve) => {
  10. // setTimeout(resolve, ms);
  11. // });
  12. // }
  13. const osmdPort = 8000 // OSMD webpack server port. OSMD has to be running (npm start) when this script runs.
  14. // main function
  15. async function init () {
  16. console.log('init')
  17. const fs = require('fs')
  18. const [sampleDir, imageDir] = process.argv.slice(2, 4)
  19. console.log('sampleDir: ' + sampleDir)
  20. console.log('imageDir: ' + imageDir)
  21. // Create the image directory if it doesn't exist.
  22. fs.mkdirSync(imageDir, { recursive: true })
  23. const samples = {
  24. 'Beethoven, L.v. - An die ferne Geliebte': 'Beethoven_AnDieFerneGeliebte.xml',
  25. 'Clementi, M. - Sonatina Op.36 No.1 Pt.1': 'MuzioClementi_SonatinaOpus36No1_Part1.xml',
  26. 'Clementi, M. - Sonatina Op.36 No.1 Pt.2': 'MuzioClementi_SonatinaOpus36No1_Part2.xml',
  27. 'Clementi, M. - Sonatina Op.36 No.3 Pt.1': 'MuzioClementi_SonatinaOpus36No3_Part1.xml',
  28. 'Clementi, M. - Sonatina Op.36 No.3 Pt.2': 'MuzioClementi_SonatinaOpus36No3_Part2.xml',
  29. 'Bach, J.S. - Praeludium in C-Dur BWV846 1': 'JohannSebastianBach_PraeludiumInCDur_BWV846_1.xml',
  30. 'Bach, J.S. - Air': 'JohannSebastianBach_Air.xml',
  31. // 'Gounod, C. - Méditation': 'CharlesGounod_Meditation.xml', // TODO this Gounod sample fails to generate a proper PNG image for some reason
  32. 'Haydn, J. - Concertante Cello': 'JosephHaydn_ConcertanteCello.xml',
  33. 'Joplin, S. - Elite Syncopations': 'ScottJoplin_EliteSyncopations.xml',
  34. 'Joplin, S. - The Entertainer': 'ScottJoplin_The_Entertainer.xml',
  35. 'Mozart, W.A. - An Chloe': 'Mozart_AnChloe.xml',
  36. 'Mozart, W.A. - Das Veilchen': 'Mozart_DasVeilchen.xml',
  37. 'Mozart, W.A. - Clarinet Quintet (Excerpt)': 'Mozart_Clarinet_Quintet_Excerpt.mxl',
  38. 'Mozart, W.A. - String Quartet in G, K. 387, 1st Mvmt Excerpt': 'Mozart_String_Quartet_in_G_K._387_1st_Mvmnt_excerpt.musicxml',
  39. 'Mozart/Holzer - Land der Berge (national anthem of Austria)': 'Land_der_Berge.musicxml',
  40. 'OSMD Function Test - All': 'OSMD_function_test_all.xml',
  41. 'OSMD Function Test - Accidentals': 'OSMD_function_test_accidentals.musicxml',
  42. 'OSMD Function Test - Autobeam': 'OSMD_function_test_autobeam.musicxml',
  43. 'OSMD Function Test - Auto-/Custom-Coloring': 'OSMD_function_test_auto-custom-coloring-entchen.musicxml',
  44. 'OSMD Function Test - Bar lines': 'OSMD_function_test_bar_lines.musicxml',
  45. 'OSMD Function Test - Color (from XML)': 'OSMD_function_test_color.musicxml',
  46. 'OSMD Function Test - Drumset': 'OSMD_function_test_drumset.musicxml',
  47. 'OSMD Function Test - Expressions': 'OSMD_function_test_expressions.musicxml',
  48. 'OSMD Function Test - Expressions Overlap': 'OSMD_function_test_expressions_overlap.musicxml',
  49. 'OSMD Function Test - Grace Notes': 'OSMD_function_test_GraceNotes.xml',
  50. 'OSMD Function Test - Invisible Notes': 'OSMD_function_test_invisible_notes.musicxml',
  51. 'OSMD Function Test - Selecting Measures To Draw': 'OSMD_function_test_measuresToDraw_Beethoven_AnDieFerneGeliebte.xml',
  52. 'OSMD Function Test - Notehead Shapes': 'OSMD_function_test_noteheadShapes.musicxml',
  53. 'OSMD Function Test - Ornaments': 'OSMD_function_test_Ornaments.xml',
  54. 'OSMD Function Test - Tremolo': 'OSMD_Function_Test_Tremolo_2bars.musicxml',
  55. 'Schubert, F. - An Die Musik': 'Schubert_An_die_Musik.xml',
  56. 'Anonymous - Saltarello': 'Saltarello.mxl',
  57. 'Debussy, C. - Mandoline': 'Debussy_Mandoline.xml',
  58. 'Levasseur, F. - Parlez Mois': 'Parlez-moi.mxl',
  59. 'Schumann, R. - Dichterliebe': 'Dichterliebe01.xml',
  60. 'Telemann, G.P. - Sonate-Nr.1.1-Dolce': 'TelemannWV40.102_Sonate-Nr.1.1-Dolce.xml',
  61. 'Telemann, G.P. - Sonate-Nr.1.2-Allegro': 'TelemannWV40.102_Sonate-Nr.1.2-Allegro-F-Dur.xml'
  62. // 'Hello World': 'HelloWorld.xml',
  63. // 'Clementi, M. - Sonatina Op.36 No.1 Pt.2': 'MuzioClementi_SonatinaOpus36No1_Part2.xml',
  64. }
  65. // const sampleKeys = Object.keys(samples)
  66. const sampleValues = Object.values(samples)
  67. const puppeteer = require('puppeteer')
  68. const browser = await puppeteer.launch({ headless: true })
  69. const page = await browser.newPage() // TODO set width/height
  70. // fix navigation error
  71. var responseEventOccurred = false
  72. var responseHandler = function (event) { responseEventOccurred = true }
  73. var responseWatcher = new Promise(function (resolve, reject) {
  74. setTimeout(function () {
  75. if (!responseEventOccurred) {
  76. resolve(true)
  77. } else {
  78. setTimeout(function () { resolve(true) }, 30000)
  79. }
  80. page.removeListener('response', responseHandler)
  81. }, 500)
  82. })
  83. page.on('response', responseHandler)
  84. // get image data
  85. const getDataUrl = async (page) => {
  86. return page.evaluate(async () => {
  87. return new Promise(resolve => {
  88. const canvasImage = document.getElementById('osmdCanvasVexFlowBackendCanvas')
  89. var imageData = canvasImage.toDataURL()
  90. // TODO fetch multiple pages from multiple OSMD backends
  91. resolve(imageData)
  92. })
  93. })
  94. }
  95. // generate png for all given samples
  96. for (let i = 0; i < sampleValues.length; i++) {
  97. // const sampleTitle = sampleValues[i].replace('/', '|')
  98. const sampleFileName = sampleValues[i].replace('/', '|') // TODO maybe take filenames from script arguments
  99. const sampleParameter = `&openUrl=${sampleFileName}&endUrl`
  100. const pageUrl = `http://localhost:${osmdPort}/?showHeader=0&debugControls=0&backendType=canvas&pageBackgroundColor=FFFFFF${sampleParameter}` +
  101. sampleParameter
  102. console.log('puppeteer: page.goto url: ' + pageUrl)
  103. try {
  104. await page.goto(pageUrl, { waitUntil: 'networkidle2' })
  105. } catch (error) {
  106. console.log(error)
  107. console.log('[OSMD.generateImages] Error generating images: could not reach local OSMD server. ' +
  108. 'Make sure to start OSMD local webpack server (npm start) before running this script.')
  109. process.exit(-1) // exit script with error. otherwise process will continue running
  110. }
  111. console.log('puppeteer.page.goto done')
  112. var navigationWatcher = page.waitForNavigation()
  113. await Promise.race([responseWatcher, navigationWatcher])
  114. console.log('navigation race done')
  115. const dataUrl = await getDataUrl(page)
  116. // console.log('dataUrl: ' + dataUrl);
  117. const imageData = dataUrl.split(';base64,').pop()
  118. const imageBuffer = Buffer.from(imageData, 'base64')
  119. var fileName = `${imageDir}/${sampleFileName}.png`
  120. console.log('got image data, saving to: ' + fileName)
  121. fs.writeFileSync(fileName, imageBuffer, { encoding: 'base64' })
  122. }
  123. // const html = await page.content();
  124. // console.log('page content: ' + html);
  125. browser.close()
  126. console.log('puppeteer browser closed. exiting.')
  127. }
  128. // function start() {
  129. // // await (async () => {
  130. // // init();
  131. // // });
  132. // (async function(){
  133. // await init();
  134. // // more code here or the await is useless
  135. // })();
  136. // }
  137. // function resizeCanvas (elementId, width, height) {
  138. // $('#' + elementId).width(width)
  139. // $('#' + elementId).attr('width', width)
  140. // $('#' + elementId).attr('height', height)
  141. // }
  142. init()