Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python3
  2. # Render image to Oscilloscope XY vector audio
  3. #
  4. # https://pypi.org/project/svgpathtools/
  5. # https://dood.al/oscilloscope/
  6. #
  7. # ----------------------------------------------------------------------------
  8. # Copyright (c) 2024 Thomas Buck (thomas@xythobuz.de)
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, either version 3 of the License, or
  13. # (at your option) any later version.
  14. #
  15. # This program is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # See <http://www.gnu.org/licenses/>.
  21. # ----------------------------------------------------------------------------
  22. import sys
  23. import wave
  24. from svgpathtools import svg2paths
  25. samplerate = 44100 #192000
  26. volume_percent = 70
  27. path_steps = 10
  28. default_duration = 5.0
  29. default_outfile = "out.wav"
  30. def read_image(filename):
  31. paths, attributes = svg2paths(filename)
  32. path = paths[0]
  33. if len(paths) > 1:
  34. print("WARNING: multiple paths in file. will just draw first one.")
  35. print("paths={} segments={}".format(len(paths), len(path)))
  36. points = [[path[0].start.real, path[0].start.imag]]
  37. p_min = [points[0][0], points[0][1]]
  38. p_max = [points[0][0], points[0][1]]
  39. for segment in path:
  40. p = [segment.end.real, segment.end.imag]
  41. for i in range(0, 2):
  42. if p[i] < p_min[i]:
  43. p_min[i] = p[i]
  44. if p[i] > p_max[i]:
  45. p_max[i] = p[i]
  46. points.append(p)
  47. print("min={} max={}".format(p_min, p_max))
  48. data = bytearray()
  49. def add_point(p):
  50. for i in range(0, 2):
  51. v = p[i]
  52. v -= p_min[i]
  53. v /= p_max[i] - p_min[i]
  54. if i == 1:
  55. v = 1 - v
  56. c = int((v * 2 - 1) * (32767 / 100 * volume_percent))
  57. data.extend(c.to_bytes(2, byteorder="little", signed=True))
  58. def interpolate(p1, p2, step):
  59. p = []
  60. for i in range(0, 2):
  61. diff = p2[i] - p1[i]
  62. v = p1[i] + diff * step
  63. p.append(v)
  64. return p
  65. for n in range(0, len(points) - 1):
  66. p1 = points[n]
  67. p2 = points[n + 1]
  68. for step in range(0, path_steps):
  69. p = interpolate(p1, p2, step / path_steps)
  70. add_point(p)
  71. add_point(points[len(points) - 1])
  72. return data
  73. def write_waveform(data, filename):
  74. with wave.open(filename, "w") as f:
  75. f.setnchannels(2)
  76. f.setsampwidth(2)
  77. f.setframerate(samplerate)
  78. f.writeframes(data)
  79. def main():
  80. if len(sys.argv) <= 1:
  81. print("Usage:")
  82. print("\t" + sys.argv[0] + " image.png [out.wav] [seconds]")
  83. sys.exit(1)
  84. if len(sys.argv) == 3:
  85. duration = float(sys.argv[2])
  86. outfile = default_outfile
  87. if len(sys.argv) >= 4:
  88. duration = float(sys.argv[2])
  89. outfile = sys.argv[3]
  90. else:
  91. duration = default_duration
  92. outfile = default_outfile
  93. wave = read_image(sys.argv[1])
  94. samplecount = int(len(wave) / 2 / 2) # stereo, int16
  95. drawrate = samplerate / samplecount
  96. drawcount = drawrate * duration
  97. print("len={} samples={} drawrate={:.2f} count={:.2f}".format(len(wave), samplecount, drawrate, drawcount))
  98. data = bytearray()
  99. for n in range(0, int(drawcount)):
  100. data.extend(wave)
  101. print("len={}".format(len(data)))
  102. write_waveform(bytes(data), outfile)
  103. if __name__ == "__main__":
  104. main()