123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- #!/usr/bin/env python3
-
- # Render image to Oscilloscope XY vector audio
- #
- # https://pypi.org/project/svgpathtools/
- # https://dood.al/oscilloscope/
- #
- # ----------------------------------------------------------------------------
- # Copyright (c) 2024 Thomas Buck (thomas@xythobuz.de)
- # Copyright (c) 2024 Philipp Schönberger (mail@phschoen.de)
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # See <http://www.gnu.org/licenses/>.
- # ----------------------------------------------------------------------------
-
- import sys
- import math
- import wave
- import argparse
- from svgpathtools import svg2paths
-
- def rot_p(p_center, p, angle_d):
- angle = math.radians(angle_d)
- ox = p_center[0]
- oy = p_center[1]
- qx = ox + math.cos(angle) * (p[0] - ox) - math.sin(angle) * (p[1] - oy)
- qy = oy + math.sin(angle) * (p[0] - ox) + math.cos(angle) * (p[1] - oy)
- p = [qx, qy]
- return p
-
- def interpolate(p1, p2, step):
- p = []
- for i in range(0, 2):
- diff = p2[i] - p1[i]
- v = p1[i] + diff * step
- p.append(v)
- return p
-
- def read_image(filename, path_steps, volume_percent,
- samplerate, time,
- angle_d, rot_steps, rot_size):
- paths, attributes = svg2paths(filename)
- print("paths={}".format(len(paths)))
- for i, path in enumerate(paths):
- print("path={} segments={}".format(i, len(path)))
-
- # find center point to rotate around
- p0 = [ paths[0][0].start.real, paths[0][0].start.imag ]
- p_min = [p0[0], p0[1]]
- p_max = [p0[0], p0[1]]
- for path in paths:
- for segment in path:
- p = [ segment.end.real, segment.end.imag ]
- for i in range(0, 2):
- if p[i] < p_min[i]:
- p_min[i] = p[i]
- if p[i] > p_max[i]:
- p_max[i] = p[i]
- p_center = [ p_min[0] + (p_max[0] - p_min[0]) / 2, p_min[1] + (p_max[1] - p_min[1]) / 2 ]
-
- # find bounding box for rotated object
- for path in paths:
- for segment in path:
- a = angle_d
- while (a < (angle_d + (rot_steps * rot_size))) or ((rot_steps <= 0) and (a == angle_d)):
- p = rot_p(p_center, [ segment.end.real, segment.end.imag ], a)
- for i in range(0, 2):
- if p[i] < p_min[i]:
- p_min[i] = p[i]
- if p[i] > p_max[i]:
- p_max[i] = p[i]
- a += rot_size
-
- print("min={} max={}".format(p_min, p_max))
- print("center={} ".format(p_center))
-
- def add_point(p, data):
- for i in range(0, 2):
- v = p[i]
- v -= p_min[i]
- v /= p_max[i] - p_min[i]
- if i == 1:
- v = 1 - v
- c = int((v * 2 - 1) * (32767 / 100 * volume_percent))
- data.extend(c.to_bytes(2, byteorder="little", signed=True))
-
- def add_segment(p1, p2, data):
- l = math.sqrt((p2[0] - p1[0]) ** 2 + (p2[1] - p1[1]) ** 2)
- ps = max(1, int(path_steps * l))
- for step in range(0, ps):
- p = interpolate(p1, p2, step / ps)
- add_point(p, data)
-
- wave = bytearray()
-
- a = angle_d
- while (a < (angle_d + (rot_steps * rot_size))) or ((rot_steps <= 0) and (a == angle_d)):
- data = bytearray()
-
- for path in paths:
- p0 = [ path[0].start.real, path[0].start.imag ]
- points = [ rot_p(p_center, p0, a) ]
- for segment in path:
- p = [ segment.end.real, segment.end.imag ]
- p = rot_p(p_center, p, a)
- points.append(p)
-
- # walk path forwards
- for n in range(0, len(points) - 1):
- add_segment(points[n], points[n + 1], data)
-
- # walk path backwards
- add_point(points[len(points) - 1], data)
- for n in range(len(points) - 2, -1, -1):
- add_segment(points[n + 1], points[n], data)
- add_point(points[0], data)
-
- # improve start/end of path. TODO only in virtual scope?
- add_point(points[0], data)
-
- # extend data to required length / runtime
- samplecount = int(len(data) / 2 / 2) # stereo, int16
- drawrate = samplerate / samplecount
- drawcount = drawrate * (time / max(1, rot_steps))
-
- for n in range(0, max(1, int(drawcount))):
- wave.extend(data)
-
- a += rot_size
-
- return wave
-
- def write_waveform(data, filename, samplerate):
- with wave.open(filename, "w") as f:
- f.setnchannels(2)
- f.setsampwidth(2)
- f.setframerate(samplerate)
- f.writeframes(data)
-
- def main():
- parser = argparse.ArgumentParser(
- prog=sys.argv[0],
- description='Render SVG path to vector XY audio file',
- epilog='Made by Thomas Buck (thomas@xythobuz.de) and Philipp Schönberger (mail@phschoen.de). Licensed as GPLv3.')
-
- parser.add_argument("input", help="Input SVG image file path.")
- parser.add_argument("-o", "--output", dest="output", default="out.wav",
- help="Output wav sound file path. Defaults to 'out.wav'.")
- parser.add_argument("-t", "--time", dest="time", default=5.0, type=float,
- help="Length of sound file in seconds. Defaults to 5s.")
- parser.add_argument("-s", "--samplerate", dest="samplerate", default=44100, type=int,
- help="Samplerate of output file in Hz. Defaults to 44.1kHz.")
- parser.add_argument("-v", "--volume", dest="volume", default=100.0, type=float,
- help="Volume of output file in percent. Defaults to 100%%.")
- parser.add_argument("-i", "--interpolate", dest="interpolate", default=3, type=int,
- help="Steps on interpolated paths. Defaults to 3.")
- parser.add_argument("-r", "--rotate", dest="angle_d", default=0.0, type=float,
- help="Angle to rotate image, in degrees. Defaults to 0 deg.")
- parser.add_argument("-p", "--rot-steps", dest="rot_steps", default=0, type=int,
- help="Number of steps to animate rotation. Defaults to 0.")
- parser.add_argument("-z", "--rot-size", dest="rot_size", default=5.0, type=float,
- help="Step size for rotation animation. Defaults to 5 deg.")
-
- args = parser.parse_args()
- print(args)
-
- data = read_image(args.input, args.interpolate, args.volume,
- args.samplerate, args.time,
- args.angle_d, args.rot_steps, args.rot_size)
- write_waveform(bytes(data), args.output, args.samplerate)
-
- if __name__ == "__main__":
- main()
|