RPi Zero and HiFiBerry DAC+ based Oscilloscope Music Player https://www.xythobuz.de/osci_music_player.html
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

render.py 3.0KB

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