SHARE
TWEET

Untitled

vexedkiller0071 Dec 11th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """
  2. TODOLIST
  3. 1.instanciate classes outside of loop then run a function to call on classes to move/extend{}
  4. 2. Add movement for x,y,z,w
  5. """
  6. import wave
  7. import pygame
  8. import pyaudio
  9. import numpy as np
  10. from scipy.signal import find_peaks
  11.  
  12.  
  13.  
  14. class Rectangle:
  15.  
  16.     def __init__(self, x, y, z, w, color):
  17.         self.x = x
  18.         self.y = y
  19.         self.z = z
  20.         self.w = w
  21.         self.color = color
  22.         self.dimentions = (x, y, z, w)
  23.         self.new_dimentions = None
  24.  
  25.     def extend_w(self, move):
  26.         self.w = self.w + move
  27.         self.new_dimentions = (self.x, self.y, self.z, self.w)
  28.         return self.new_dimentions
  29.  
  30.     def extend_z(self, move):
  31.         self.w = self.w + move
  32.         self.new_dimentions = (self.x, self.y, self.z, self.w)
  33.         return self.new_dimentions
  34.  
  35.     def extend_y(self, move):
  36.         self.w = self.w + move
  37.         self.new_dimentions = (self.x, self.y, self.z, self.w)
  38.         return self.new_dimentions
  39.  
  40.     def extend_x(self, move):
  41.         self.w = self.w + move
  42.         self.new_dimentions = (self.x, self.y, self.z, self.w)
  43.         return self.new_dimentions
  44.  
  45.     def reset_dimentions_w(self, move):
  46.         self.new_dimentions = None
  47.         self.w = self.w - move
  48.         return self.new_dimentions
  49.  
  50. class GiveWaveInfo:
  51.     def __init__(self, wavefile):
  52.         self.wavefile = wavefile
  53.  
  54.     def get_info(self):
  55.  
  56.         with wave.open(self.wavefile) as wave_file:
  57.             framerate = wave_file.getframerate()
  58.             frames = wave_file.getnframes()
  59.             channels = wave_file.getnchannels()
  60.             width = wave_file.getsampwidth()
  61.             print('sampling rate:', framerate, 'Hz')
  62.             print('length:', frames, 'samples')
  63.             print('channels:', channels)
  64.             print('sample width:', width, 'bytes')
  65.         return frames, channels
  66.  
  67.     def manipulate_info(self, data, channels):
  68.         sig = np.frombuffer(data, dtype='<i2').reshape(-1, channels)
  69. #sig = np.fft.fft(sig)
  70.  
  71.         sig_x = []
  72.         sig_y = []
  73.  
  74. #seperate channels
  75.         for number, array in enumerate(sig):
  76.             sig_x.append(array[0])
  77.             sig_y.append(array[1])
  78.  
  79. #find peaks of channels
  80.         sig_peaks_x = find_peaks(sig_x)
  81.         sig_peaks_y = find_peaks(sig_y)
  82.  
  83.         sig_peaks_x = sig_peaks_x[0].tolist()
  84.         sig_peaks_y = sig_peaks_y[0].tolist()
  85.  
  86. #find value associated with peaks of channel
  87.         peak_value_x = []
  88.         #peak_value_y = []
  89. # compares value from both arrays
  90.         for index, peak_value in enumerate(sig_x):
  91.             for peak_index in sig_peaks_x:
  92. #The values are then used and if they match they are added to an array
  93.                 if index == peak_index:
  94.                     if peak_value >= 0:
  95.                         peak_value_x.append(peak_value)
  96.  
  97.         all_peak_values = peak_value_x
  98.  
  99. #movement of squares according to array
  100. ## Ranges for equiliz
  101.         array_one = []
  102.         array_two = []
  103.         array_three = []
  104.         array_four = []
  105.         array_five = []
  106.  
  107. ### puts the arrays according to values
  108.         for value in all_peak_values:
  109.  
  110.             if value <= 50:
  111.                 array_one.append(value)
  112.             if value > 50 and value < 100:
  113.                 array_two.append(value)
  114.             if value > 100 and value < 200:
  115.                 array_three.append(value)
  116.             if value > 200 and value < 1000:
  117.                 array_four.append(value)
  118.             if value >= 1000:
  119.                 array_five.append(value)
  120.  
  121.         one_move = 0
  122.         two_move = 0
  123.         three_move = 0
  124.         four_move = 0
  125.         five_move = 0
  126.  
  127.         if len(array_one) > 0:
  128.             one_move = int((max(array_one)/50)*400)
  129.         if len(array_two) > 0:
  130.             two_move = int((max(array_two)/100)*400)
  131.         if len(array_three) > 0:
  132.             three_move = int((max(array_three)/200)*400)
  133.         if len(array_four) > 0:
  134.             four_move = int((max(array_four)/1000)*400)
  135.         if len(array_five) > 0:
  136.             five_move = int((max(array_five)/35_000)*400)
  137.         return [one_move, two_move, three_move, four_move, five_move]
  138.  
  139.  
  140.  
  141.  
  142. def play_file(fname):
  143. # Prepare the pygame module for use
  144.     pygame.init()
  145.  
  146. # Desired physical surface size, in pixels.
  147.     surface_sz = 510
  148.  
  149. #Create surface of (width, height), and its window.
  150.     main_surface = pygame.display.set_mode((surface_sz, surface_sz))
  151.  
  152.  
  153. # create an audio object "instantiate"
  154.     wf = wave.open(fname, 'rb')
  155.     p = pyaudio.PyAudio()
  156.  
  157. #Normal Chunksize = 1024
  158.     chunk = 1024
  159.  
  160. # open stream based on the wave object which has been input.
  161.  
  162.     stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
  163.                     channels=wf.getnchannels(),
  164.                     rate=wf.getframerate(),
  165.                     output=True)
  166.  
  167.     # read data (based on the chunk size)
  168.     data = wf.readframes(chunk)
  169.  
  170.     info = GiveWaveInfo(fname)
  171.     frames, channels = info.get_info()
  172. #instantiate recangle classes
  173.     rect_one = Rectangle(10, 0, 90, 90, (255, 0, 0))
  174.     rect_two = Rectangle(110, 0, 90, 90, (0, 125, 125))
  175.     rect_three = Rectangle(210, 0, 90, 90, (0, 255, 255))
  176.     rect_four = Rectangle(310, 0, 90, 90, (255, 255, 255))
  177.     rect_five = Rectangle(410, 0, 90, 90, (0, 0, 0))
  178.  
  179. #play stream (looping from beginning of file to the end) it also indexes
  180.     for index, num in enumerate(range(0, int(frames/chunk))):
  181.  
  182. #creates movement from class with the frame data and channels
  183.         one_move, two_move, three_move, four_move,five_move = info.manipulate_info(data, channels)
  184. #instantiates rectangle classes and changes them every frame based on movement
  185.         ev = pygame.event.poll()
  186. # Window close button clicked?
  187.         if ev.type == pygame.QUIT:
  188.             break
  189. #fills the screen with this color
  190.         main_surface.fill((0, 0, 255))
  191.  
  192.         stream.write(data)
  193.         num += 1
  194.         data = wf.readframes(chunk)
  195. #move objects here
  196. ##Rectangle movement up and down extend
  197.         rect_one.extend_z(one_move)
  198.         rect_two.extend_z(two_move)
  199.         rect_three.extend_z(three_move)
  200.         rect_four.extend_z(four_move)
  201.         rect_five.extend_z(five_move)
  202.  
  203. #movement on screen every nth indexes
  204.         if index % 10 == 0:
  205.  
  206.             main_surface.fill(rect_one.color, rect_one.new_dimentions)
  207.             main_surface.fill(rect_two.color, rect_two.new_dimentions)
  208.             main_surface.fill(rect_three.color, rect_three.new_dimentions)
  209.             main_surface.fill(rect_four.color, rect_four.new_dimentions)
  210.             main_surface.fill(rect_five.color, rect_five.new_dimentions)
  211.             pygame.display.flip()
  212.  
  213.  
  214.  
  215. #reset movement to orginal
  216.         rect_one.new_dimentions = rect_one.reset_dimentions_w(one_move)
  217.         rect_two.new_dimentions = rect_two.reset_dimentions_w(two_move)
  218.         rect_three.new_dimentions = rect_three.reset_dimentions_w(three_move)
  219.         rect_four.new_dimentions = rect_four.reset_dimentions_w(four_move)
  220.         rect_five.new_dimentions = rect_five.reset_dimentions_w(five_move)
  221.  
  222. # cleanup stuff
  223.     stream.close()
  224.     p.terminate()
  225.  
  226.  
  227.  
  228. def main(args):
  229.     info = GiveWaveInfo('edm.wav')
  230.     play_file(info.wavefile)
  231.  
  232.     return 0
  233.  
  234. if __name__ == '__main__':
  235.     import sys
  236.     sys.exit(main(sys.argv))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top