Brak opisu
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 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, either version 3 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # See <http://www.gnu.org/licenses/>.
  21. # ----------------------------------------------------------------------------
  22. import sys
  23. import wave
  24. import argparse
  25. from svgpathtools import svg2paths
  26. def read_image(filename, path_steps, volume_percent):
  27. paths, attributes = svg2paths(filename)
  28. path = paths[0]
  29. if len(paths) > 1:
  30. print("WARNING: multiple paths in file. will just draw first one.")
  31. print("paths={} segments={}".format(len(paths), len(path)))
  32. points = [[path[0].start.real, path[0].start.imag]]
  33. p_min = [points[0][0], points[0][1]]
  34. p_max = [points[0][0], points[0][1]]
  35. for segment in path:
  36. p = [segment.end.real, segment.end.imag]
  37. for i in range(0, 2):
  38. if p[i] < p_min[i]:
  39. p_min[i] = p[i]
  40. if p[i] > p_max[i]:
  41. p_max[i] = p[i]
  42. points.append(p)
  43. print("min={} max={}".format(p_min, p_max))
  44. data = bytearray()
  45. def add_point(p):
  46. for i in range(0, 2):
  47. v = p[i]
  48. v -= p_min[i]
  49. v /= p_max[i] - p_min[i]
  50. if i == 1:
  51. v = 1 - v
  52. c = int((v * 2 - 1) * (32767 / 100 * volume_percent))
  53. data.extend(c.to_bytes(2, byteorder="little", signed=True))
  54. def interpolate(p1, p2, step):
  55. p = []
  56. for i in range(0, 2):
  57. diff = p2[i] - p1[i]
  58. v = p1[i] + diff * step
  59. p.append(v)
  60. return p
  61. def add_segment(p1, p2, f):
  62. p = interpolate(p1, p2, f)
  63. add_point(p)
  64. for n in range(0, len(points) - 1):
  65. for step in range(0, path_steps):
  66. add_segment(points[n], points[n + 1], step / path_steps)
  67. #add_point(points[len(points) - 1])
  68. for n in range(len(points) - 2, -1, -1):
  69. for step in range(0, path_steps):
  70. add_segment(points[n + 1], points[n], step / path_steps)
  71. add_point(points[0])
  72. return data
  73. def write_waveform(data, filename, samplerate):
  74. with wave.open(filename, "w") as f:
  75. f.setnchannels(2)
  76. f.setsampwidth(2)
  77. f.setframerate(samplerate)
  78. f.writeframes(data)
  79. def main():
  80. parser = argparse.ArgumentParser(
  81. prog=sys.argv[0],
  82. description='Render SVG path to vector XY audio file',
  83. epilog='Made by Thomas Buck <thomas@xythobuz.de>. Licensed as GPLv3.')
  84. parser.add_argument("input", help="Input SVG image file path.")
  85. parser.add_argument("-o", "--output", dest="output", default="out.wav",
  86. help="Output wav sound file path. Defaults to 'out.wav'.")
  87. parser.add_argument("-t", "--time", dest="time", default=5.0, type=float,
  88. help="Length of sound file in seconds. Defaults to 5s.")
  89. parser.add_argument("-s", "--samplerate", dest="samplerate", default=44100, type=int,
  90. help="Samplerate of output file in Hz. Defaults to 44.1kHz.")
  91. parser.add_argument("-v", "--volume", dest="volume", default=100.0, type=float,
  92. help="Volume of output file in percent. Defaults to 100%%.")
  93. parser.add_argument("-i", "--interpolate", dest="interpolate", default=10, type=int,
  94. help="Steps on interpolated paths. Defaults to 10.")
  95. args = parser.parse_args()
  96. print(args)
  97. wave = read_image(args.input, args.interpolate, args.volume)
  98. samplecount = int(len(wave) / 2 / 2) # stereo, int16
  99. drawrate = args.samplerate / samplecount
  100. drawcount = drawrate * args.time
  101. print("len={} samples={} drawrate={:.2f} count={:.2f}".format(len(wave), samplecount, drawrate, drawcount))
  102. data = bytearray()
  103. for n in range(0, int(drawcount)):
  104. data.extend(wave)
  105. print("len={}".format(len(data)))
  106. write_waveform(bytes(data), args.output, args.samplerate)
  107. if __name__ == "__main__":
  108. main()