class VuBarVertiPir(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.graphics.fill(BLUE) self.p = pyaudio.PyAudio() self.stream = self.p.open(format=audio_params[0], channels=audio_params[1], rate=audio_params[2], input=audio_params[3], output=audio_params[4], frames_per_buffer=audio_params[5]) self.color = BLUE self.max = 0 def getaudio(self): try: raw = self.stream.read(audio_params[5]) except IOError as e: if e[1] != pyaudio.paInputOverflowed: raise else: print("Warning: audio input buffer overflow") raw = '\x00' * self.audio_params[5] return np.array(np.frombuffer(raw, np.int16), dtype=np.float64) def generate(self): self.graphics.fill(BLACK) audio = self.getaudio() self.color = interp_color(rms(audio/10000)) self.color = color_convert(self.color) for i in range(0, matrix_height): rmsed = translate(rms(audio), 0, ((2**16)/2), 0, matrix_width*i) self.graphics.drawLine(0, i, rmsed, i, self.color) return self.graphics.getSurface()
class VUmetertwo(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.controller = AudioController(channel=1, rate=256000, period=64) self.color = BLUE self.inputlength = matrix_height self.inputs = [] self.offset = 13 self.max = 1250 self.lim = self.max self.rate = 10 def getaverageof(self, number): averages = [] for i in range(0, number): data = self.controller.getinput() while(data is None): data = self.controller.getinput() averages.append(data) sums = 0 for num in averages: sums += num return int(sums / len(averages)) def getinputs(self): self.inputs = [] sums = 0 for input in range(0, self.inputlength): data = self.getaverageof(3) self.inputs.append(data) sums += data return sums / len(self.inputs) def generate(self): average = self.getinputs() self.graphics.fill(BLACK) for i, line in enumerate(self.inputs): if line > self.max: self.max = line else: if self.max <= self.lim: self.max = self.lim else: self.max -= int((self.max - self.lim) / self.rate) length = int(translate(average, 0, self.max, 0, matrix_height)) data = int(translate(line, 0, average, 0, length)) if data > 0xff: self.data = 0xff if data > self.max: self.max = data self.graphics.drawLine(0, i, data - self.offset, i, self.color) return self.graphics.getSurface()
class VUmeterThree(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.controller = AudioController(channel=1, rate=16000, period=64) self.max = 1 def generate(self): self.graphics.fill(BLACK) data = getaverageof(10, self.controller) data = int(translate(data, 0, 700, 0, 8)) for i in range(0, 17): self.graphics.drawLine(0, i, data - 10, 0, BLUE) return self.graphics.getSurface()
class VUmeterone(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.controller = AudioController(channel=1, rate=8000, period=128) self.average = [] self.averaged = 1 self.averagelength = 10 def generate(self): data = getaverageof(10, self.controller) if data: data = int(translate(data, 0, 700, 0, 8)) self.graphics.fill(BLACK) self.graphics.drawLine(0, 0, data - 10, 0, BLUE) return self.graphics.getSurface()
class GraphicsLineTest(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.color = YELLOW self.pos = 0, 0 def generate(self): self.graphics.fill(BLACK) x, y = self.pos self.graphics.drawLine(matrix_width - x, matrix_height - y, x, y, self.color) if x >= matrix_height: x = 0 y = 0 self.pos = x + 1, y return self.graphics.getSurface()
class VisualizerTwo(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.graphics.fill(BLUE) self.color = BLUE self.timer = Timer(0) self.chunk = 1024 self.scale = 55 self.exponent = 2 self.samplerate = 44100 self.audio_params = (pyaudio.paInt16, 1, self.samplerate, True, self.chunk) self.p = pyaudio.PyAudio() self.stream = self.p.open(format=self.audio_params[0], channels=self.audio_params[1], rate=self.audio_params[2], input=self.audio_params[3], frames_per_buffer=self.audio_params[4]) self.data = self.stream.read(self.chunk) def calculate_levels(self, data, chunk, samplerate, points=6, maxi=0): # Use FFT to calculate volume for each frequency MAX=maxi # Convert raw sound data to Numpy array fmt = "%dH"%(len(data)/2) data2 = struct.unpack(fmt, data) data2 = numpy.array(data2, dtype='h') # Apply FFT fourier = numpy.fft.fft(data2) ffty = numpy.abs(fourier[0:len(fourier)/2])/1000 ffty1=ffty[:len(ffty)/2] ffty2=ffty[len(ffty)/2::]+2 ffty2=ffty2[::-1] ffty=ffty1+ffty2 ffty=numpy.log(ffty)-2 fourier = list(ffty)[4:-4] fourier = fourier[:len(fourier)/2] size = len(fourier) # Add up for 6 lights levels = [sum(fourier[i:(i+size/points)]) for i in xrange(0, size, size/points)][:points] return levels def getaudio(self): try: raw = self.stream.read(self.audio_params[5]) except IOError as e: if e[1] != pyaudio.paInputOverflowed: raise else: print("Warning: audio input buffer overflow") raw = '\x00' * self.audio_params[5] return np.array(np.frombuffer(raw, np.int16), dtype=np.float64) def generate(self): self.graphics.fill(BLACK) if self.stream.get_read_available(): self.data = self.stream.read(self.chunk) levels = self.calculate_levels(self.data, self.chunk, self.samplerate, matrix_height) for i, level in enumerate(levels): self.color = color_convert(interp_color(level/max(levels))) level = max(min(level/self.scale, i), 0.0) level = level**self.exponent level = int(level*0xff) self.graphics.drawLine(0, i, level, i, self.color) return self.graphics.getSurface() def __del__(self): print("Closing Stream") self.stream.close() self.p.terminate()
class Visualizer(object): """ testing with this is cool: https://www.youtube.com/watch?v=82Q6DRqf9H4 https://youtu.be/XzjmPo6qr_0?list=RD05IZxpCWSao https://youtu.be/UUIQox072QA?list=RD05IZxpCWSao https://youtu.be/JTNXgzSpiTU?list=RD05IZxpCWSao https://youtu.be/S5xOj3JGU0c?list=RD05IZxpCWSao https://youtu.be/7Ul7uBoewdM?list=RD05IZxpCWSao https://youtu.be/B1DDpyt8qyg?list=RD05IZxpCWSao https://youtu.be/VwME67reIYk?list=RD05IZxpCWSao https://youtu.be/hn_T2rMPL4c?list=RD05IZxpCWSao https://youtu.be/U20HZoCnRGA https://youtu.be/eRE0dfOfVYE """ def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.graphics.fill(BLUE) self.color = BLUE self.timer = Timer(0) self.chunk = 1024 self.scale = 55 self.exponent = 2 self.samplerate = 44100 self.audio_params = (pyaudio.paInt16, 1, self.samplerate, True, self.chunk) self.p = pyaudio.PyAudio() self.stream = self.p.open(format=self.audio_params[0], channels=self.audio_params[1], rate=self.audio_params[2], input=self.audio_params[3], frames_per_buffer=self.audio_params[4]) self.data = self.stream.read(self.chunk) def calculate_levels(self, data, chunk, samplerate, points=6, maxi=0): # Use FFT to calculate volume for each frequency MAX=maxi # Convert raw sound data to Numpy array fmt = "%dH"%(len(data)/2) data2 = struct.unpack(fmt, data) data2 = numpy.array(data2, dtype='h') # Apply FFT fourier = numpy.fft.fft(data2) ffty = numpy.abs(fourier[0:len(fourier)/2])/1000 ffty1=ffty[:len(ffty)/2] ffty2=ffty[len(ffty)/2::]+2 ffty2=ffty2[::-1] ffty=ffty1+ffty2 ffty=numpy.log(ffty)-2 fourier = list(ffty)[4:-4] fourier = fourier[:len(fourier)/2] size = len(fourier) # Add up for 6 lights levels = [sum(fourier[i:(i+size/points)]) for i in xrange(0, size, size/points)][:points] return levels def getaudio(self): try: raw = self.stream.read(self.audio_params[5]) except IOError as e: if e[1] != pyaudio.paInputOverflowed: raise else: print("Warning: audio input buffer overflow") raw = '\x00' * self.audio_params[5] return np.array(np.frombuffer(raw, np.int16), dtype=np.float64) def generate(self): self.graphics.fill(BLACK) self.data = self.stream.read(self.chunk) levels = self.calculate_levels(self.data, self.chunk, self.samplerate, matrix_height) for i, level in enumerate(levels): self.color = color_convert(interp_color(level/max(levels))) level = max(min(level/self.scale, 1.0), 0.0) level = level**self.exponent level = int(level*0xff) self.graphics.drawLine(0, i, level, i, self.color) return self.graphics.getSurface() def __del__(self): print("Closing Stream") self.stream.close() self.p.terminate()