Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- TODOLIST
- 1.instanciate classes outside of loop then run a function to call on classes to move/extend{}
- 2. Add movement for x,y,z,w
- """
- import wave
- import pygame
- import pyaudio
- import numpy as np
- from scipy.signal import find_peaks
- class Rectangle:
- def __init__(self, x, y, z, w, color):
- self.x = x
- self.y = y
- self.z = z
- self.w = w
- self.color = color
- self.dimentions = (x, y, z, w)
- self.new_dimentions = None
- def extend_w(self, move):
- self.w = self.w + move
- self.new_dimentions = (self.x, self.y, self.z, self.w)
- return self.new_dimentions
- def extend_z(self, move):
- self.w = self.w + move
- self.new_dimentions = (self.x, self.y, self.z, self.w)
- return self.new_dimentions
- def extend_y(self, move):
- self.w = self.w + move
- self.new_dimentions = (self.x, self.y, self.z, self.w)
- return self.new_dimentions
- def extend_x(self, move):
- self.w = self.w + move
- self.new_dimentions = (self.x, self.y, self.z, self.w)
- return self.new_dimentions
- def reset_dimentions_w(self, move):
- self.new_dimentions = None
- self.w = self.w - move
- return self.new_dimentions
- class GiveWaveInfo:
- def __init__(self, wavefile):
- self.wavefile = wavefile
- def get_info(self):
- with wave.open(self.wavefile) as wave_file:
- framerate = wave_file.getframerate()
- frames = wave_file.getnframes()
- channels = wave_file.getnchannels()
- width = wave_file.getsampwidth()
- print('sampling rate:', framerate, 'Hz')
- print('length:', frames, 'samples')
- print('channels:', channels)
- print('sample width:', width, 'bytes')
- return frames, channels
- def manipulate_info(self, data, channels):
- sig = np.frombuffer(data, dtype='<i2').reshape(-1, channels)
- #sig = np.fft.fft(sig)
- sig_x = []
- sig_y = []
- #seperate channels
- for number, array in enumerate(sig):
- sig_x.append(array[0])
- sig_y.append(array[1])
- #find peaks of channels
- sig_peaks_x = find_peaks(sig_x)
- sig_peaks_y = find_peaks(sig_y)
- sig_peaks_x = sig_peaks_x[0].tolist()
- sig_peaks_y = sig_peaks_y[0].tolist()
- #find value associated with peaks of channel
- peak_value_x = []
- #peak_value_y = []
- # compares value from both arrays
- for index, peak_value in enumerate(sig_x):
- for peak_index in sig_peaks_x:
- #The values are then used and if they match they are added to an array
- if index == peak_index:
- if peak_value >= 0:
- peak_value_x.append(peak_value)
- all_peak_values = peak_value_x
- #movement of squares according to array
- ## Ranges for equiliz
- array_one = []
- array_two = []
- array_three = []
- array_four = []
- array_five = []
- ### puts the arrays according to values
- for value in all_peak_values:
- if value <= 50:
- array_one.append(value)
- if value > 50 and value < 100:
- array_two.append(value)
- if value > 100 and value < 200:
- array_three.append(value)
- if value > 200 and value < 1000:
- array_four.append(value)
- if value >= 1000:
- array_five.append(value)
- one_move = 0
- two_move = 0
- three_move = 0
- four_move = 0
- five_move = 0
- if len(array_one) > 0:
- one_move = int((max(array_one)/50)*400)
- if len(array_two) > 0:
- two_move = int((max(array_two)/100)*400)
- if len(array_three) > 0:
- three_move = int((max(array_three)/200)*400)
- if len(array_four) > 0:
- four_move = int((max(array_four)/1000)*400)
- if len(array_five) > 0:
- five_move = int((max(array_five)/35_000)*400)
- return [one_move, two_move, three_move, four_move, five_move]
- def play_file(fname):
- # Prepare the pygame module for use
- pygame.init()
- # Desired physical surface size, in pixels.
- surface_sz = 510
- #Create surface of (width, height), and its window.
- main_surface = pygame.display.set_mode((surface_sz, surface_sz))
- # create an audio object "instantiate"
- wf = wave.open(fname, 'rb')
- p = pyaudio.PyAudio()
- #Normal Chunksize = 1024
- chunk = 1024
- # open stream based on the wave object which has been input.
- stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
- channels=wf.getnchannels(),
- rate=wf.getframerate(),
- output=True)
- # read data (based on the chunk size)
- data = wf.readframes(chunk)
- info = GiveWaveInfo(fname)
- frames, channels = info.get_info()
- #instantiate recangle classes
- rect_one = Rectangle(10, 0, 90, 90, (255, 0, 0))
- rect_two = Rectangle(110, 0, 90, 90, (0, 125, 125))
- rect_three = Rectangle(210, 0, 90, 90, (0, 255, 255))
- rect_four = Rectangle(310, 0, 90, 90, (255, 255, 255))
- rect_five = Rectangle(410, 0, 90, 90, (0, 0, 0))
- #play stream (looping from beginning of file to the end) it also indexes
- for index, num in enumerate(range(0, int(frames/chunk))):
- #creates movement from class with the frame data and channels
- one_move, two_move, three_move, four_move,five_move = info.manipulate_info(data, channels)
- #instantiates rectangle classes and changes them every frame based on movement
- ev = pygame.event.poll()
- # Window close button clicked?
- if ev.type == pygame.QUIT:
- break
- #fills the screen with this color
- main_surface.fill((0, 0, 255))
- stream.write(data)
- num += 1
- data = wf.readframes(chunk)
- #move objects here
- ##Rectangle movement up and down extend
- rect_one.extend_z(one_move)
- rect_two.extend_z(two_move)
- rect_three.extend_z(three_move)
- rect_four.extend_z(four_move)
- rect_five.extend_z(five_move)
- #movement on screen every nth indexes
- if index % 10 == 0:
- main_surface.fill(rect_one.color, rect_one.new_dimentions)
- main_surface.fill(rect_two.color, rect_two.new_dimentions)
- main_surface.fill(rect_three.color, rect_three.new_dimentions)
- main_surface.fill(rect_four.color, rect_four.new_dimentions)
- main_surface.fill(rect_five.color, rect_five.new_dimentions)
- pygame.display.flip()
- #reset movement to orginal
- rect_one.new_dimentions = rect_one.reset_dimentions_w(one_move)
- rect_two.new_dimentions = rect_two.reset_dimentions_w(two_move)
- rect_three.new_dimentions = rect_three.reset_dimentions_w(three_move)
- rect_four.new_dimentions = rect_four.reset_dimentions_w(four_move)
- rect_five.new_dimentions = rect_five.reset_dimentions_w(five_move)
- # cleanup stuff
- stream.close()
- p.terminate()
- def main(args):
- info = GiveWaveInfo('edm.wav')
- play_file(info.wavefile)
- return 0
- if __name__ == '__main__':
- import sys
- sys.exit(main(sys.argv))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement