暫無描述
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 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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,
  44. samplerate, time,
  45. angle_d, rot_steps, rot_size):
  46. paths, attributes = svg2paths(filename)
  47. print("paths={}".format(len(paths)))
  48. for i, path in enumerate(paths):
  49. print("path={} segments={}".format(i, len(path)))
  50. # find center point to rotate around
  51. p0 = [ paths[0][0].start.real, paths[0][0].start.imag ]
  52. p_min = [p0[0], p0[1]]
  53. p_max = [p0[0], p0[1]]
  54. for path in paths:
  55. for segment in path:
  56. p = [ segment.end.real, segment.end.imag ]
  57. for i in range(0, 2):
  58. if p[i] < p_min[i]:
  59. p_min[i] = p[i]
  60. if p[i] > p_max[i]:
  61. p_max[i] = p[i]
  62. p_center = [ p_min[0] + (p_max[0] - p_min[0]) / 2, p_min[1] + (p_max[1] - p_min[1]) / 2 ]
  63. # find bounding box for rotated object
  64. for path in paths:
  65. for segment in path:
  66. a = angle_d
  67. while (a < (angle_d + (rot_steps * rot_size))) or ((rot_steps <= 0) and (a == angle_d)):
  68. p = rot_p(p_center, [ segment.end.real, segment.end.imag ], a)
  69. for i in range(0, 2):
  70. if p[i] < p_min[i]:
  71. p_min[i] = p[i]
  72. if p[i] > p_max[i]:
  73. p_max[i] = p[i]
  74. a += rot_size
  75. print("min={} max={}".format(p_min, p_max))
  76. print("center={} ".format(p_center))
  77. def add_point(p, data):
  78. for i in range(0, 2):
  79. v = p[i]
  80. v -= p_min[i]
  81. v /= p_max[i] - p_min[i]
  82. if i == 1:
  83. v = 1 - v
  84. c = int((v * 2 - 1) * (32767 / 100 * volume_percent))
  85. data.extend(c.to_bytes(2, byteorder="little", signed=True))
  86. def add_segment(p1, p2, data):
  87. l = math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
  88. ps = max(1, int(path_steps * l))
  89. for step in range(0, ps):
  90. p = interpolate(p1, p2, step / ps)
  91. add_point(p, data)
  92. wave = bytearray()
  93. a = angle_d
  94. while (a < (angle_d + (rot_steps * rot_size))) or ((rot_steps <= 0) and (a == angle_d)):
  95. data = bytearray()
  96. for path in paths:
  97. p0 = [ path[0].start.real, path[0].start.imag ]
  98. points = [ rot_p(p_center, p0, a) ]
  99. for segment in path:
  100. p = [ segment.end.real, segment.end.imag ]
  101. p = rot_p(p_center, p, a)
  102. points.append(p)
  103. # walk path forwards
  104. for n in range(0, len(points) - 1):
  105. add_segment(points[n], points[n + 1], data)
  106. # walk path backwards
  107. add_point(points[len(points) - 1], data)
  108. for n in range(len(points) - 2, -1, -1):
  109. add_segment(points[n + 1], points[n], data)
  110. add_point(points[0], data)
  111. # improve start/end of path. TODO only in virtual scope?
  112. add_point(points[0], data)
  113. # extend data to required length / runtime
  114. samplecount = int(len(data) / 2 / 2) # stereo, int16
  115. drawrate = samplerate / samplecount
  116. drawcount = drawrate * (time / max(1, rot_steps))
  117. for n in range(0, max(1, int(drawcount))):
  118. wave.extend(data)
  119. a += rot_size
  120. return wave
  121. def write_waveform(data, filename, samplerate):
  122. with wave.open(filename, "w") as f:
  123. f.setnchannels(2)
  124. f.setsampwidth(2)
  125. f.setframerate(samplerate)
  126. f.writeframes(data)
  127. def main():
  128. parser = argparse.ArgumentParser(
  129. prog=sys.argv[0],
  130. description='Render SVG path to vector XY audio file',
  131. epilog='Made by Thomas Buck (thomas@xythobuz.de) and Philipp Schönberger (mail@phschoen.de). Licensed as GPLv3.')
  132. parser.add_argument("input", help="Input SVG image file path.")
  133. parser.add_argument("-o", "--output", dest="output", default="out.wav",
  134. help="Output wav sound file path. Defaults to 'out.wav'.")
  135. parser.add_argument("-t", "--time", dest="time", default=5.0, type=float,
  136. help="Length of sound file in seconds. Defaults to 5s.")
  137. parser.add_argument("-s", "--samplerate", dest="samplerate", default=44100, type=int,
  138. help="Samplerate of output file in Hz. Defaults to 44.1kHz.")
  139. parser.add_argument("-v", "--volume", dest="volume", default=100.0, type=float,
  140. help="Volume of output file in percent. Defaults to 100%%.")
  141. parser.add_argument("-i", "--interpolate", dest="interpolate", default=3, type=int,
  142. help="Steps on interpolated paths. Defaults to 3.")
  143. parser.add_argument("-r", "--rotate", dest="angle_d", default=0.0, type=float,
  144. help="Angle to rotate image, in degrees. Defaults to 0 deg.")
  145. parser.add_argument("-p", "--rot-steps", dest="rot_steps", default=0, type=int,
  146. help="Number of steps to animate rotation. Defaults to 0.")
  147. parser.add_argument("-z", "--rot-size", dest="rot_size", default=5.0, type=float,
  148. help="Step size for rotation animation. Defaults to 5 deg.")
  149. args = parser.parse_args()
  150. print(args)
  151. data = read_image(args.input, args.interpolate, args.volume,
  152. args.samplerate, args.time,
  153. args.angle_d, args.rot_steps, args.rot_size)
  154. write_waveform(bytes(data), args.output, args.samplerate)
  155. if __name__ == "__main__":
  156. main()