설명 없음
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.6KB

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