Sin descripción
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

render.py 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 read_image(filename, path_steps, volume_percent, angle_d):
  37. paths, attributes = svg2paths(filename)
  38. print("paths={}".format(len(paths)))
  39. p0 = [paths[0][0].start.real, paths[0][0].start.imag]
  40. p_min = [p0[0], p0[1]]
  41. p_max = [p0[0], p0[1]]
  42. for path in paths:
  43. print("segments={}".format(len(path)))
  44. # find center
  45. dist_min = float('inf')
  46. dist_max = 0
  47. p_prev = p_min
  48. for segment in path:
  49. p = [segment.end.real, segment.end.imag]
  50. for i in range(0, 2):
  51. if p[i] < p_min[i]:
  52. p_min[i] = p[i]
  53. if p[i] > p_max[i]:
  54. p_max[i] = p[i]
  55. dist_curr = math.sqrt((p[0] - p_prev[0]) ** 2 + (p[1] - p_prev[1]) ** 2)
  56. p_prev = p
  57. if dist_curr > dist_max:
  58. dist_max = dist_curr
  59. if dist_curr < dist_min:
  60. dist_min = dist_curr
  61. p_center = [ p_min[0] + (p_max[0] - p_min[0]) / 2, p_min[1] + (p_max[1] - p_min[1]) / 2 ]
  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. print("dist min={} max={} ".format(dist_min, dist_max))
  91. data = bytearray()
  92. def add_point(p):
  93. for i in range(0, 2):
  94. v = p[i]
  95. v -= p_min[i]
  96. v /= p_max[i] - p_min[i]
  97. if i == 1:
  98. v = 1 - v
  99. c = int((v * 2 - 1) * (32767 / 100 * volume_percent))
  100. data.extend(c.to_bytes(2, byteorder="little", signed=True))
  101. def interpolate(p1, p2, step):
  102. p = []
  103. for i in range(0, 2):
  104. diff = p2[i] - p1[i]
  105. v = p1[i] + diff * step
  106. p.append(v)
  107. return p
  108. def add_segment(p1, p2, f):
  109. p = interpolate(p1, p2, f)
  110. add_point(p)
  111. def add_path(p1, p2):
  112. l = math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
  113. ps = max(1, int(path_steps * l))
  114. for step in range(0, ps):
  115. add_segment(p1, p2, step / ps)
  116. for path in paths:
  117. p = [path[0].start.real, path[0].start.imag]
  118. p = rot_p(p_center, p , angle_d)
  119. points = [p]
  120. for segment in path:
  121. p = [segment.end.real, segment.end.imag]
  122. p = rot_p(p_center, p , angle_d)
  123. points.append(p)
  124. # walk path forwards
  125. for n in range(0, len(points) - 1):
  126. add_path(points[n], points[n + 1])
  127. add_point(points[len(points) - 1])
  128. # walk path backwards
  129. for n in range(len(points) - 2, -1, -1):
  130. add_path(points[n + 1], points[n])
  131. add_point(points[0])
  132. return data
  133. def write_waveform(data, filename, samplerate):
  134. with wave.open(filename, "w") as f:
  135. f.setnchannels(2)
  136. f.setsampwidth(2)
  137. f.setframerate(samplerate)
  138. f.writeframes(data)
  139. def main():
  140. parser = argparse.ArgumentParser(
  141. prog=sys.argv[0],
  142. description='Render SVG path to vector XY audio file',
  143. epilog='Made by Thomas Buck (thomas@xythobuz.de) and Philipp Schönberger (mail@phschoen.de). Licensed as GPLv3.')
  144. parser.add_argument("input", help="Input SVG image file path.")
  145. parser.add_argument("-o", "--output", dest="output", default="out.wav",
  146. help="Output wav sound file path. Defaults to 'out.wav'.")
  147. parser.add_argument("-t", "--time", dest="time", default=5.0, type=float,
  148. help="Length of sound file in seconds. Defaults to 5s.")
  149. parser.add_argument("-s", "--samplerate", dest="samplerate", default=44100, type=int,
  150. help="Samplerate of output file in Hz. Defaults to 44.1kHz.")
  151. parser.add_argument("-v", "--volume", dest="volume", default=100.0, type=float,
  152. help="Volume of output file in percent. Defaults to 100%%.")
  153. parser.add_argument("-i", "--interpolate", dest="interpolate", default=3, type=int,
  154. help="Steps on interpolated paths. Defaults to 3.")
  155. parser.add_argument("-r", "--rotate", dest="angle_d", default=0.0, type=float,
  156. help="Angle to rotate image, in degrees. Defaults to 0 deg.")
  157. args = parser.parse_args()
  158. print(args)
  159. wave = read_image(args.input, args.interpolate, args.volume, args.angle_d)
  160. samplecount = int(len(wave) / 2 / 2) # stereo, int16
  161. drawrate = args.samplerate / samplecount
  162. drawcount = drawrate * args.time
  163. print("len={} samples={} drawrate={:.2f} count={:.2f}".format(len(wave), samplecount, drawrate, drawcount))
  164. data = bytearray()
  165. for n in range(0, int(drawcount)):
  166. data.extend(wave)
  167. print("len={}".format(len(data)))
  168. write_waveform(bytes(data), args.output, args.samplerate)
  169. if __name__ == "__main__":
  170. main()