1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #!/usr/bin/env python3
-
- # Render image to Oscilloscope XY vector audio
- #
- # ----------------------------------------------------------------------------
- # 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
- from svgpathtools import svg2paths
-
- samplerate = 44100 #192000
- 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()
- for n in range(0, len(points)):
- for i in range(0, 2):
- v = points[n][i]
- v -= p_min[i]
- v /= p_max[i] - p_min[i]
- c = int((v * 2 - 1) * (32767 / 100 * 70))
- data.extend(c.to_bytes(2, byteorder="little", signed=True))
- 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()
|