Ingen beskrivning
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

render.py 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/usr/bin/env python3
  2. # Render image to Oscilloscope XY vector audio
  3. #
  4. # https://pypi.org/project/svgpathtools/
  5. # https://dood.al/oscilloscope/
  6. #
  7. # ----------------------------------------------------------------------------
  8. # Copyright (c) 2024 Thomas Buck (thomas@xythobuz.de)
  9. # Copyright (c) 2024 Philipp Schönberger (mail@phschoen.de)
  10. #
  11. # This program is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # See <http://www.gnu.org/licenses/>.
  22. # ----------------------------------------------------------------------------
  23. import sys
  24. import math
  25. import wave
  26. import argparse
  27. from svgpathtools import svg2paths
  28. def rot_p(p_center, p, angle_d):
  29. angle = math.radians(angle_d)
  30. ox = p_center[0]
  31. oy = p_center[1]
  32. qx = ox + math.cos(angle) * (p[0] - ox) - math.sin(angle) * (p[1] - oy)
  33. qy = oy + math.sin(angle) * (p[0] - ox) + math.cos(angle) * (p[1] - oy)
  34. p = [qx, qy]
  35. return p
  36. def interpolate(p1, p2, step):
  37. p = []
  38. for i in range(0, 2):
  39. diff = p2[i] - p1[i]
  40. v = p1[i] + diff * step
  41. p.append(v)
  42. return p
  43. def read_image(filename, path_steps, volume_percent, angle_d):
  44. paths, attributes = svg2paths(filename)
  45. print("paths={}".format(len(paths)))
  46. for i, path in enumerate(paths):
  47. print("path={} segments={}".format(i, len(path)))
  48. # find center point to rotate around
  49. p0 = [paths[0][0].start.real, paths[0][0].start.imag]
  50. p_min = [p0[0], p0[1]]
  51. p_max = [p0[0], p0[1]]
  52. for path in paths:
  53. for segment in path:
  54. p = [segment.end.real, segment.end.imag]
  55. for i in range(0, 2):
  56. if p[i] < p_min[i]:
  57. p_min[i] = p[i]
  58. if p[i] > p_max[i]:
  59. p_max[i] = p[i]
  60. p_center = [ p_min[0] + (p_max[0] - p_min[0]) / 2, p_min[1] + (p_max[1] - p_min[1]) / 2 ]
  61. # find bounding box for rotated object
  62. for path in paths:
  63. # TODO this is not nice.
  64. # Doing both range(0, 360, 5) and angle_d is redundant.
  65. # But also, it bakes in the assumption of the animation
  66. # that's specified in the Makefile.
  67. # Ideally the animation should be handled here in the
  68. # script instead, with proper parameters.
  69. # find min max for all rotations
  70. for segment in path:
  71. p_org = [segment.end.real, segment.end.imag]
  72. for a in range(0, 360, 5):
  73. p = rot_p(p_center, p_org , a)
  74. for i in range(0, 2):
  75. if p[i] < p_min[i]:
  76. p_min[i] = p[i]
  77. if p[i] > p_max[i]:
  78. p_max[i] = p[i]
  79. # find min max for this specific rotation
  80. for segment in path:
  81. p = [segment.end.real, segment.end.imag]
  82. p = rot_p(p_center, p, angle_d)
  83. for i in range(0, 2):
  84. if p[i] < p_min[i]:
  85. p_min[i] = p[i]
  86. if p[i] > p_max[i]:
  87. p_max[i] = p[i]
  88. print("min={} max={}".format(p_min, p_max))
  89. print("center={} ".format(p_center))
  90. data = bytearray()
  91. def add_point(p):
  92. for i in range(0, 2):
  93. v = p[i]
  94. v -= p_min[i]
  95. v /= p_max[i] - p_min[i]
  96. if i == 1:
  97. v = 1 - v
  98. c = int((v * 2 - 1) * (32767 / 100 * volume_percent))
  99. data.extend(c.to_bytes(2, byteorder="little", signed=True))
  100. def add_segment(p1, p2):
  101. l = math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
  102. ps = max(1, int(path_steps * l))
  103. for step in range(0, ps):
  104. p = interpolate(p1, p2, step / ps)
  105. add_point(p)
  106. for path in paths:
  107. p0 = [path[0].start.real, path[0].start.imag]
  108. points = [rot_p(p_center, p0, angle_d)]
  109. for segment in path:
  110. p = [segment.end.real, segment.end.imag]
  111. p = rot_p(p_center, p, angle_d)
  112. points.append(p)
  113. # walk path forwards
  114. for n in range(0, len(points) - 1):
  115. add_segment(points[n], points[n + 1])
  116. # walk path backwards
  117. add_point(points[len(points) - 1])
  118. for n in range(len(points) - 2, -1, -1):
  119. add_segment(points[n + 1], points[n])
  120. add_point(points[0])
  121. # improve start/end of path. TODO only in virtual scope?
  122. add_point(points[0])
  123. return data
  124. def write_waveform(data, filename, samplerate):
  125. with wave.open(filename, "w") as f:
  126. f.setnchannels(2)
  127. f.setsampwidth(2)
  128. f.setframerate(samplerate)
  129. f.writeframes(data)
  130. def main():
  131. parser = argparse.ArgumentParser(
  132. prog=sys.argv[0],
  133. description='Render SVG path to vector XY audio file',
  134. epilog='Made by Thomas Buck (thomas@xythobuz.de) and Philipp Schönberger (mail@phschoen.de). Licensed as GPLv3.')
  135. parser.add_argument("input", help="Input SVG image file path.")
  136. parser.add_argument("-o", "--output", dest="output", default="out.wav",
  137. help="Output wav sound file path. Defaults to 'out.wav'.")
  138. parser.add_argument("-t", "--time", dest="time", default=5.0, type=float,
  139. help="Length of sound file in seconds. Defaults to 5s.")
  140. parser.add_argument("-s", "--samplerate", dest="samplerate", default=44100, type=int,
  141. help="Samplerate of output file in Hz. Defaults to 44.1kHz.")
  142. parser.add_argument("-v", "--volume", dest="volume", default=100.0, type=float,
  143. help="Volume of output file in percent. Defaults to 100%%.")
  144. parser.add_argument("-i", "--interpolate", dest="interpolate", default=3, type=int,
  145. help="Steps on interpolated paths. Defaults to 3.")
  146. parser.add_argument("-r", "--rotate", dest="angle_d", default=0.0, type=float,
  147. help="Angle to rotate image, in degrees. Defaults to 0 deg.")
  148. args = parser.parse_args()
  149. print(args)
  150. wave = read_image(args.input, args.interpolate, args.volume, args.angle_d)
  151. samplecount = int(len(wave) / 2 / 2) # stereo, int16
  152. drawrate = args.samplerate / samplecount
  153. drawcount = drawrate * args.time
  154. print("len={} samples={} drawrate={:.2f} count={:.2f}".format(len(wave), samplecount, drawrate, drawcount))
  155. data = bytearray()
  156. for n in range(0, int(drawcount)):
  157. data.extend(wave)
  158. print("len={}".format(len(data)))
  159. write_waveform(bytes(data), args.output, args.samplerate)
  160. if __name__ == "__main__":
  161. main()