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.
131 lines
4.5 KiB
131 lines
4.5 KiB
#!/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 <http://www.gnu.org/licenses/>.
|
|
# ----------------------------------------------------------------------------
|
|
|
|
import sys
|
|
import wave
|
|
import argparse
|
|
from svgpathtools import svg2paths
|
|
|
|
def read_image(filename, path_steps, volume_percent):
|
|
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
|
|
|
|
def add_segment(p1, p2, f):
|
|
p = interpolate(p1, p2, f)
|
|
add_point(p)
|
|
|
|
for n in range(0, len(points) - 1):
|
|
for step in range(0, path_steps):
|
|
add_segment(points[n], points[n + 1], step / path_steps)
|
|
|
|
#add_point(points[len(points) - 1])
|
|
|
|
for n in range(len(points) - 2, -1, -1):
|
|
for step in range(0, path_steps):
|
|
add_segment(points[n + 1], points[n], step / path_steps)
|
|
|
|
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>. 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=10, type=int,
|
|
help="Steps on interpolated paths. Defaults to 10.")
|
|
|
|
args = parser.parse_args()
|
|
print(args)
|
|
|
|
wave = read_image(args.input, args.interpolate, args.volume)
|
|
|
|
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()
|