screenshot.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env python3
  2. # SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. import json
  5. import os
  6. import subprocess
  7. import sys
  8. import logging
  9. import time
  10. from tornado.ioloop import IOLoop
  11. from mod.settings import HTML_DIR, DEV_ENVIRONMENT, DEVICE_KEY, CACHE_DIR, DESKTOP
  12. from modtools.utils import get_pedalboard_info, get_plugin_info, get_plugin_gui
  13. INGEN_GRAPH_PROTOTYPE = 'http://drobilla.net/ns/ingen#GraphPrototype'
  14. # Everything the renderer needs, taken from this process's LV2 world (already loaded, plugin info cached):
  15. # the child then renders straight away instead of scanning the whole LV2 world first (several seconds
  16. # of lilv parsing on a device, during which the audio threads lose their caches).
  17. def build_screenshot_job(bundle_path):
  18. pb = get_pedalboard_info(bundle_path)
  19. plugins = {}
  20. for p in pb['plugins']:
  21. uri = p['uri']
  22. if uri == INGEN_GRAPH_PROTOTYPE or uri in plugins:
  23. continue
  24. plugins[uri] = {
  25. 'info': get_plugin_info(uri),
  26. 'gui': get_plugin_gui(uri),
  27. }
  28. return {
  29. 'pedalboard': pb,
  30. 'plugins': plugins,
  31. }
  32. def generate_screenshot(bundle_path, callback):
  33. screenshot = os.path.join(bundle_path, 'screenshot.png')
  34. thumbnail = os.path.join(bundle_path, 'thumbnail.png')
  35. try:
  36. os.remove(screenshot)
  37. os.remove(thumbnail)
  38. except OSError:
  39. pass
  40. cwd = os.path.abspath(os.path.join(os.path.dirname(__file__), '../'))
  41. # one job at a time (ScreenshotGenerator), so a single file is enough
  42. jobfile = os.path.join(CACHE_DIR, 'screenshot-job.json')
  43. try:
  44. job = build_screenshot_job(bundle_path)
  45. os.makedirs(CACHE_DIR, exist_ok=True)
  46. with open(jobfile, 'w') as fh:
  47. json.dump(job, fh)
  48. jobargs = ['--job', jobfile]
  49. except Exception as ex:
  50. logging.error('[screenshot] cannot prepare the render job, the renderer will scan the LV2 world itself: %s', ex)
  51. jobargs = []
  52. # running packaged through cxfreeze
  53. if DESKTOP and os.path.isfile(sys.argv[0]):
  54. cmd = [os.path.join(cwd, 'mod-pedalboard'), 'take_screenshot', bundle_path, HTML_DIR, CACHE_DIR] + jobargs
  55. if sys.platform == 'win32':
  56. cmd[0] += ".exe"
  57. logging.debug('[screenshot] now running: %s', ' '.join(cmd))
  58. # regular run
  59. else:
  60. cmd = ['python3', '-m', 'modtools.pedalboard', 'take_screenshot', bundle_path, HTML_DIR, CACHE_DIR] + jobargs
  61. if not DEV_ENVIRONMENT and DEVICE_KEY: # if using a real MOD, setup niceness
  62. cmd = ['/usr/bin/nice', '-n', '+34'] + cmd
  63. started = time.time()
  64. proc = subprocess.Popen(cmd, cwd=cwd)
  65. loop = IOLoop.instance()
  66. def proc_callback():
  67. if proc.poll() is None:
  68. loop.call_later(0.5, proc_callback)
  69. return
  70. if not os.path.exists(screenshot) or not os.path.exists(thumbnail):
  71. logging.warn('[screenshot] process finished but image files do not exist')
  72. callback()
  73. return
  74. logging.info('[screenshot] %s done in %.1f s', os.path.basename(bundle_path), time.time() - started)
  75. callback(thumbnail)
  76. loop.call_later(0.5, proc_callback)
  77. class ScreenshotGenerator(object):
  78. def __init__(self):
  79. self.queue = []
  80. self.callbacks = {}
  81. self.processing = None
  82. def schedule_screenshot(self, bundlepath, callback=None):
  83. bundlepath = os.path.abspath(bundlepath)
  84. if bundlepath not in self.queue and self.processing != bundlepath:
  85. self.queue.append(bundlepath)
  86. if callback is not None:
  87. self.add_callback(bundlepath, callback)
  88. if self.processing is None:
  89. self.process_next()
  90. def process_next(self):
  91. if len(self.queue) == 0:
  92. self.processing = None
  93. return
  94. self.processing = self.queue.pop(0)
  95. def img_callback(thumbnail=None):
  96. if not thumbnail:
  97. for callback in self.callbacks.pop(self.processing, []):
  98. callback((False, 0.0))
  99. self.process_next()
  100. return
  101. ctime = os.path.getctime(thumbnail)
  102. for callback in self.callbacks.pop(self.processing, []):
  103. callback((True, ctime))
  104. self.process_next()
  105. try:
  106. generate_screenshot(self.processing, img_callback)
  107. except Exception as ex:
  108. logging.error('[screenshot] %s', ex)
  109. img_callback()
  110. def check_screenshot(self, bundlepath):
  111. bundlepath = os.path.abspath(bundlepath)
  112. if bundlepath in self.queue or self.processing == bundlepath:
  113. return 0, 0.0
  114. thumbnail = os.path.join(bundlepath, "thumbnail.png")
  115. if not os.path.exists(thumbnail):
  116. return -1, 0.0
  117. ctime = os.path.getctime(thumbnail)
  118. return 1, ctime
  119. def wait_for_pending_jobs(self, bundlepath, callback):
  120. bundlepath = os.path.abspath(bundlepath)
  121. if bundlepath not in self.queue and self.processing != bundlepath:
  122. # all ok
  123. thumbnail = os.path.join(bundlepath, "thumbnail.png")
  124. if os.path.exists(thumbnail):
  125. ctime = os.path.getctime(thumbnail)
  126. callback((True, ctime))
  127. else:
  128. callback((False, 0.0))
  129. return
  130. # report back later
  131. self.add_callback(bundlepath, callback)
  132. def add_callback(self, bundlepath, callback):
  133. if bundlepath not in self.callbacks.keys():
  134. self.callbacks[bundlepath] = [callback]
  135. else:
  136. self.callbacks[bundlepath].append(callback)