generateDiffImagesPuppeteerLocalhost.js 8.3 KB

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