Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python3
  2. # ----------------------------------------------------------------------------
  3. # "THE BEER-WARE LICENSE" (Revision 42):
  4. # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  5. # you can do whatever you want with this stuff. If we meet some day, and you
  6. # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  7. # ----------------------------------------------------------------------------
  8. import time
  9. import pyaudio
  10. import numpy as np
  11. class AudioVisualizer:
  12. def __init__(self, g, f = 20, t = 60.0, fft = True):
  13. self.gui = g
  14. self.interval = 1.0 / f
  15. self.timeout = t
  16. self.do_fft = fft
  17. self.chunk = 1024 * 2 # stereo
  18. self.p = pyaudio.PyAudio()
  19. self.s = self.p.open(format=pyaudio.paInt16, channels=2, rate=44100, input=True)
  20. print(self.s)
  21. def restart(self):
  22. self.data = self.init()
  23. self.start = time.time()
  24. def finished(self):
  25. if (time.time() - self.start) >= self.timeout:
  26. return True
  27. return FALSE
  28. def draw_line(self, x, val, y_off):
  29. h = int(val) / 32768.0 * self.gui.height / 2
  30. self.gui.set_pixel(x, y_off + (16 - h), (255, 255, 255))
  31. def draw_bar(self, x, val, y_off, max_val):
  32. if max_val <= 0.0:
  33. max_val = 1.0
  34. h = int(val) / float(max_val) * self.gui.height / 2
  35. for y in range(0, int(h)):
  36. self.gui.set_pixel(x, y_off + (32 - y), (255, 255, 255))
  37. # stolen from https://github.com/aiXander/Realtime_PyAudio_FFT/blob/master/src/fft.py
  38. def calc_fft(self, data):
  39. data = data * np.hamming(len(data))
  40. try:
  41. fft = np.abs(np.fft.rfft(data)[1:])
  42. except:
  43. fft = np.fft.fft(data)
  44. left, right = np.split(np.abs(fft), 2)
  45. fft = np.add(left, right[::-1])
  46. #try:
  47. # fft = np.multiply(20, np.log10(fft))
  48. #except:
  49. # pass
  50. return fft
  51. def draw(self):
  52. frames = self.s.read(self.chunk)
  53. left = frames[::4] # start from first left sample (index 0), skip 4 bytes (2 * 2, int16 and stereo)
  54. left = np.frombuffer(left, dtype=np.int16)
  55. right = frames[2::4] # start from first right sample (index 2), skip 4 bytes
  56. right = np.frombuffer(right, dtype=np.int16)
  57. if self.do_fft:
  58. # FFT
  59. left = self.calc_fft(left)
  60. right = self.calc_fft(right)
  61. # average down to fit screen
  62. m = self.chunk / 2 / self.gui.width # stereo
  63. if self.do_fft:
  64. m = m / 2
  65. left = left.reshape(-1, int(m)).mean(axis=1)
  66. right = right.reshape(-1, int(m)).mean(axis=1)
  67. l_max = max(left)
  68. r_max = max(right)
  69. for x in range(0, self.gui.width):
  70. if self.do_fft:
  71. self.draw_bar(x, left[x], 0, l_max)
  72. self.draw_bar(x, right[x], self.gui.height / 2, r_max)
  73. else:
  74. self.draw_line(x, left[x], 0)
  75. self.draw_line(x, right[x], self.gui.height / 2)
  76. if __name__ == "__main__":
  77. import util
  78. i = util.getInput()
  79. t = util.getTarget(i)
  80. a = AudioVisualizer(t)
  81. util.loop(t, a.draw)