#!/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 . # ---------------------------------------------------------------------------- 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, angle_d): 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: # TODO this is not nice. # Doing both range(0, 360, 5) and angle_d is redundant. # But also, it bakes in the assumption of the animation # that's specified in the Makefile. # Ideally the animation should be handled here in the # script instead, with proper parameters. # find min max for all rotations for segment in path: p_org = [segment.end.real, segment.end.imag] for a in range(0, 360, 5): p = rot_p(p_center, p_org , 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] # find min max for this specific rotation for segment in path: p = [segment.end.real, segment.end.imag] p = rot_p(p_center, p, angle_d) 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] print("min={} max={}".format(p_min, p_max)) print("center={} ".format(p_center)) data = bytearray() def add_point(p): 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): 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) for path in paths: p0 = [path[0].start.real, path[0].start.imag] points = [rot_p(p_center, p0, angle_d)] for segment in path: p = [segment.end.real, segment.end.imag] p = rot_p(p_center, p, angle_d) points.append(p) # walk path forwards for n in range(0, len(points) - 1): add_segment(points[n], points[n + 1]) # walk path backwards add_point(points[len(points) - 1]) for n in range(len(points) - 2, -1, -1): add_segment(points[n + 1], points[n]) add_point(points[0]) # improve start/end of path. TODO only in virtual scope? add_point(points[0]) return data 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.") args = parser.parse_args() print(args) wave = read_image(args.input, args.interpolate, args.volume, args.angle_d) samplecount = int(len(wave) / 2 / 2) # stereo, int16 drawrate = args.samplerate / samplecount drawcount = drawrate * args.time print("len={} samples={} drawrate={:.2f} count={:.2f}".format(len(wave), samplecount, drawrate, drawcount)) data = bytearray() for n in range(0, int(drawcount)): data.extend(wave) print("len={}".format(len(data))) write_waveform(bytes(data), args.output, args.samplerate) if __name__ == "__main__": main()