#!/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 read_image(filename, path_steps, volume_percent, angle_d): paths, attributes = svg2paths(filename) path = paths[0] if len(paths) > 1: print("WARNING: multiple paths in file. will just draw first one.") print("paths={} segments={}".format(len(paths), len(path))) points = [[path[0].start.real, path[0].start.imag]] p_min = [points[0][0], points[0][1]] p_max = [points[0][0], points[0][1]] # find center dist_min = float('inf') dist_max = 0 p_prev = p_min 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] dist_curr = (p[0] - p_prev[0]) * (p[0] - p_prev[0]) dist_curr += (p[1] - p_prev[1]) * (p[1] - p_prev[1]) dist_curr = math.sqrt(dist_curr) p_prev = p if dist_curr > dist_max: dist_max = dist_curr if dist_curr < dist_min: dist_min = dist_curr p_center = [ p_min[0] + (p_max[0] - p_min[0]) / 2, p_min[1] + (p_max[1] - p_min[1]) / 2 ] # find min max for all rotatations 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] p = [path[0].start.real, path[0].start.imag] p = rot_p(p_center, p , angle_d) points = [p] # p_min = [points[0][0], points[0][1]] # p_max = [points[0][0], points[0][1]] 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] points.append(p) print("min={} max={}".format(p_min, p_max)) print("center={} ".format(p_center)) print("dist min={} max={} ".format(dist_min, dist_max)) 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 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 add_segment(p1, p2, f): p = interpolate(p1, p2, f) add_point(p) def add_path(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): add_segment(p1, p2, step / ps) # walk path forwards for n in range(0, len(points) - 1): add_path(points[n], points[n + 1]) add_point(points[len(points) - 1]) # walk path backwards for n in range(len(points) - 2, -1, -1): add_path(points[n + 1], points[n]) 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 . 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()