#!/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) # # 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 wave from svgpathtools import svg2paths samplerate = 44100 #192000 volume_percent = 70 path_steps = 10 default_duration = 5.0 default_outfile = "out.wav" def read_image(filename): 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]] 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] points.append(p) print("min={} max={}".format(p_min, p_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 for n in range(0, len(points) - 1): p1 = points[n] p2 = points[n + 1] for step in range(0, path_steps): p = interpolate(p1, p2, step / path_steps) add_point(p) add_point(points[len(points) - 1]) return data def write_waveform(data, filename): with wave.open(filename, "w") as f: f.setnchannels(2) f.setsampwidth(2) f.setframerate(samplerate) f.writeframes(data) def main(): if len(sys.argv) <= 1: print("Usage:") print("\t" + sys.argv[0] + " image.png [out.wav] [seconds]") sys.exit(1) if len(sys.argv) == 3: duration = float(sys.argv[2]) outfile = default_outfile if len(sys.argv) >= 4: duration = float(sys.argv[2]) outfile = sys.argv[3] else: duration = default_duration outfile = default_outfile wave = read_image(sys.argv[1]) samplecount = int(len(wave) / 2 / 2) # stereo, int16 drawrate = samplerate / samplecount drawcount = drawrate * duration 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), outfile) if __name__ == "__main__": main()