class GraphicsCircleTest(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.radius = 0 self.direction = 1 self.color = RED def generate(self): # clear the drawing surface self.graphics.fill(BLACK) # put a circle on our surface self.graphics.drawCircle(matrix_width / 2, matrix_height / 2, self.radius, self.color) # circle grows and shrinks based on direction. if self.direction: self.radius += 1 else: self.radius -= 1 # if the circle is to big or to small inverse growth direction. if self.radius >= (matrix_height / 2) or self.radius <= 0: self.direction = not self.direction # get the surface drawn return self.graphics.getSurface()
class TestPattern(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) def generate(self): self.graphics.fill(WHITE) return self.graphics.getSurface()
class Test(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.x = 0 self.y = 0 self.color = GREEN self.phase = 1 self.timer = Timer(1 / 30.) self.wave_range = 1 self.wave_step = 1 self.amplitude = 4 self.offset = matrix_width / 2 self.freq = 1. / matrix_height * 8 self.controlled = True self.mc = MidiController() def generate(self): self.graphics.fill(BLACK) if self.timer.valid(): self.phase += 1 for i in range(0, self.wave_range, self.wave_step): for self.y in range(0, matrix_height): if self.controlled: self.freq = self.mc.getButton(0, 0) / 126. self.amplitude = self.mc.getButton(0, 1) self.timer.set_interval(self.mc.getButton(0, 2) / 126.) self.x = math.sin(self.y * self.freq + self.phase ) * self.amplitude + self.offset + i b = translate(i, 0, matrix_width, 0, 50) g = translate(self.y, 0, matrix_height, 0, 80) r = translate(self.x, 0, 12, 0, 24) self.color = (255, 0, 0) self.graphics.drawPixel(self.x, self.y, self.color) return self.graphics.getSurface()
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 Capture(object): def __init__(self): self.graphics = Graphics(matrix_height, matrix_width) def generate(self): self.graphics.fill(BLACK) return self.graphics.getSurface()
class DisplayPng(object): def __init__(self): # image = '/home/robert/py-artnet/hacked.png' # self.data = getPngPixelData(image) # self.pixeldata = self.data[0] self.graphics = Graphics(matrix_width, matrix_height) def generate(self): self.graphics.fill(BLACK) return self.graphics.getSurface()
class GraphicsDotTest(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.color = (123, 111, 222) def generate(self): self.graphics.fill(BLACK) for i in range(0, 5): self.graphics.drawPixel(i, i, self.color) return self.graphics.getSurface()
class GrayedLife(object): ''' take the gray scales over every point and add or subtract depening on if alive or not ''' def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.graphics.fill(BLUE) def generate(self): return self.graphics.getSurface()
class PatternDummy(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.color = BLACK self.graphics.fill(self.color) def generate(self): return self.graphics.getSurface() def __del__(self): del self
class SegmentClock(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.segmentdisp = SegmentDisplay(self.graphics) def generate(self): self.graphics.fill(BLACK) hour = time.localtime().tm_hour minutes = time.localtime().tm_min self.segmentdisp.drawnumbers(1, 0, hour, 2) self.segmentdisp.drawnumbers(1, matrix_height / 2 + 1, minutes, 2) return self.graphics.getSurface()
class PlasmaFirst(object): def __init__(self, speed=20): self.graphics = Graphics(matrix_width, matrix_height) self.plasma = Graphics(matrix_width, matrix_height) self.x_range = xrange(0, matrix_width, 1) self.y_range = xrange(0, matrix_height, 1) self.interval = .1/speed #interval/speed is how many ticks a second. self.time = 0 self.previousTick = 0 self.generatePalette() self.generatePlasmaSurface() def generatePalette(self): self.palette = [] for x in xrange(0, 256, 1): colorRGB = HSVtoRGB((x,255,255,)) self.palette.append(colorRGB) def generatePlasmaSurface(self): for y in self.y_range: for x in self.x_range: #c = int(abs(256*sin((x+y+self.time)/3.0))) c = int( 128.0 + (128.0*sin((x+6)/2.4)) +128.0 + (128.0*cos(y/3.4)) )/2 color = (c,)*3 self.plasma.drawPixel(x,y,color) return list(self.plasma.getSurface()) def process(self): if( (time.time()-self.previousTick) >= self.interval ): self.previousTick = time.time() self.time += 1 paletteShift = self.time for y in self.y_range: for x in self.x_range: plasma_color = self.plasma.readPixel(x,y) color_shift = self.palette[paletteShift%256] r = (plasma_color[0]+color_shift[0])%256 g = (plasma_color[1]+color_shift[1])%256 b = (plasma_color[2]+color_shift[2])%256 color = (r,g,b,) color = ColorRGBOps.darken(color, 50) self.graphics.drawPixel(x,y, color) def draw(self): pass def generate(self): self.graphics.fill(BLACK) self.process() self.draw() 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 SegmentClocked(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.segmentdisp = SegmentDisplay(self.graphics) self.previous = 1 def generate(self): self.graphics.fill(BLACK) current = time.time() fps = int(1. / (current - self.previous)) self.segmentdisp.drawnumbers(0, 0, fps, 4) self.previous = time.time() 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 SegmentCounter(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.timer = Timer(1 / 30.) self.number = 0 self.segmentdisp = SegmentDisplay(self.graphics) def generate(self): self.graphics.fill(BLACK) if self.timer.valid(): self.number += 1 self.segmentdisp.drawnumbers(0, 0, self.number, 4) 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 Sven(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.color = randColor() def generate(self): self.graphics.fill(BLACK) b = 0 i = 0 while(i <= 10): color2 = randColor() a = random.randint(0, matrix_width) b = random.randint(0, matrix_height) self.graphics.drawPixel(a, b, color2) i = i + 1 b = b + 1 return self.graphics.getSurface()
class GraphicsPixelTest(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.color = GREEN self.pos = (random.randint(1, matrix_width - 1), random.randint(1, matrix_height - 1)) self.speed = 1 self.deltax, self.deltay = self.speed, self.speed def generate(self): self.graphics.fill(BLACK) x, y = self.pos self.graphics.drawPixel(x, y, self.color) if x >= matrix_width - 1 or x <= 0: self.deltax *= -1 if y >= matrix_height - 1 or y <= 0: self.deltay *= -1 self.pos = x + self.deltax, y + self.deltay return self.graphics.getSurface()
class DrawSegmentNumber(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.letter_width = 4 self.letter_height = 7 self.timer = Timer(1 / 2.) self.number = 0 def generate(self): self.graphics.fill(BLACK) if self.timer.valid(): self.number += 1 if self.number > 9: self.number = 0 for x, row in enumerate(numbers[self.number]): for y, pixel in enumerate(row): color = (0, 0, 0xff * pixel) self.graphics.drawPixel(6 - x, y, color) return self.graphics.getSurface()
class MixedLife(object): def __init__(self): self.life = Life(matrix_width, matrix_height, 1, color=GREEN) self.graphics = Graphics(matrix_width, matrix_height) self.graphics.fill(BLACK) def draw(self): life_matrix = self.graphics.toMatrix(self.life.field, self.graphics.getSurfaceWidth()) for y in self.graphics.heightRange: for x in self.graphics.widthRange: if life_matrix[y][x]: color = GREEN else: color = BLACK self.graphics.drawPixel(x, y, color) def generate(self): self.life.process() self.draw() return self.graphics.getSurface()
class GraphicsRectTest(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.color = CYAN self.rect_size = matrix_width self.pos = 0, 0 def generate(self): # clear the drawing surface self.graphics.fill(BLACK) # put a rectangle on the surface x, y = self.pos if x >= matrix_width: x = 0 if y >= matrix_height: y = 0 self.graphics.drawRect(x, y, matrix_width - x, matrix_height - y, self.color) self.pos = x + 1, y + 1 # get te surface drawn return self.graphics.getSurface()
class SuperPixelBros(object): """ SuperPixelBros is a class that hanles function calling and processing. makes sure the level is generated. makes sure the player get the right data. """ def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.players = [] self.player = Player((9, 7), BLUE, self.graphics, self) self.level = level1 def handleInput(self): self.player.handleInput() def process(self): self.player.process() def draw(self): self.graphics.fill(BLACK) # draw the map. surfaceheight = self.graphics.getSurfaceHeight() level_matrix = self.graphics.toMatrix(self.level, surfaceheight) for y in self.graphics.heightRange: for x in self.graphics.widthRange: tile = level_matrix[x][y] # draw the map flipped self.graphics.drawPixel(self.graphics.width - x - 1, y, tile) # draw the player. self.player.draw() def generate(self): # self.handleInput() # self.process() # self.draw() return self.graphics.getSurface()
class Test(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.color = GREEN self.x = 0 self.y = 0 self.x_offset = matrix_width / 2 self.y_offset = matrix_height / 2 self.angle = 0 self.radius = 5 self.timer = Timer(0.1) def generate(self): self.graphics.fill(BLACK) for i in range(0, 360): self.x = math.sin(math.radians(i)) * self.radius + self.x_offset self.y = math.cos(math.radians(i)) * self.radius + self.y_offset self.graphics.drawPixel(self.x, self.y, self.color) #if self.timer.valid(): # self.angle += 1 # if self.angle > 360: # self.angle = 0 return self.graphics.getSurface()
class SineWave(object): def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.x = 0 self.y = 0 self.color = GREEN self.phase = 0 self.wave_range = 1 self.wave_step = 1 self.amplitude = 5 self.offset = matrix_width / 2 self.freq = 0.4 def generate(self): self.graphics.fill(BLACK) for i in range(0, self.wave_range, self.wave_step): for self.y in range(0, matrix_height): self.x = math.sin(self.y*self.freq+self.phase)*self.amplitude+self.offset + i b = translate(i, 0, matrix_width, 0, 50) g = translate(self.y, 0, matrix_height, 0, 80) r = translate(self.x, 0, 12, 0, 24) self.color = (int(r), int(g), int(b)) self.graphics.drawPixel(self.x, self.y, self.color) return self.graphics.getSurface()
class OldTron(object): # version before creating players that handle processing on thier own. def __init__(self): self.graphics = Graphics(matrix_width, matrix_height) self.color = BLUE x = 0 # random.randint(1, matrix_width - 1) y = 0 # 3random.randint(1, matrix_height - 1) self.pos = x, y self.speed = 1 self.deltax, self.deltay = 0, 0 self.body = [] # add our head to our body :) self.body.append(self.pos) self.player1 = TronPlayer(BLUE, self) self.player1.update(self) self.player1.draw() pygame.init() self.window = pygame.display.set_mode((80, 60)) self.debug = False pygame.key.set_repeat(1, 1) def inputHandling(self): keys_pressed = pygame.key.get_pressed() if keys_pressed[pygame.K_UP]: self.deltax = 0 self.deltay = 1 if keys_pressed[pygame.K_DOWN]: self.deltax = 0 self.deltay = -1 if keys_pressed[pygame.K_LEFT]: self.deltax = -1 self.deltay = 0 if keys_pressed[pygame.K_RIGHT]: self.deltax = 1 self.deltay = 0 def update(self): x, y = self.pos # update position x += self.deltax y += self.deltay # if the tail goes offscreen it appears on the other side. if x >= matrix_width: x = 0 self.pos = x, y elif x < 0: x = matrix_width - 1 self.pos = x, y elif y >= matrix_height: y = 0 self.pos = x, y elif y < 0: y = matrix_height - 1 self.pos = x, y else: self.pos = x, y if self.debug: print(self.deltax, self.deltay) print(x, y) # look if our "tail is in the way" and only if we have a tail. if len(self.body) > 2: if len(self.body) != len(set(self.body)): print("GameOver!") self.body = [self.pos] self.deltax = 0 self.deltay = 0 # add current point to tail # only if we moved though if self.deltax or self.deltay: self.body.append(self.pos) def draw(self): for x, y in self.body: self.graphics.drawPixel(x, y, self.color) def generate(self): self.graphics.fill(BLACK) self.inputHandling() self.update() self.draw() # self.player1.draw() return self.graphics.getSurface()
class Snake(object): def __init__(self, speed=8, plugged=0): self.graphics = Graphics(matrix_width, matrix_height) self.controller = SnakeController(plugged) self.body_color = GREEN self.head_color = BLUE x = random.randint(1, matrix_width - 1) y = random.randint(1, matrix_height - 1) self.pos = x, y self.original_speed = speed self.speed = speed self.previousTick = 0 self.deltax, self.deltay = 0, 0 self.body = [] self.tailLen = 0 self.food = Food((0, 0), WHITE, self.graphics) self.food.randPos() # add our head to our body :) self.body.append(self.pos) def inputHandling(self): if self.controller.getUp() and self.deltax != -1: self.deltax = 1 self.deltay = 0 if self.controller.getDown() and self.deltax != 1: self.deltax = -1 self.deltay = 0 if self.controller.getLeft() and self.deltay != 1: self.deltax = 0 self.deltay = -1 if self.controller.getRight() and self.deltay != -1: self.deltax = 0 self.deltay = 1 def update(self): x, y = self.pos # update position certain amount per second. if((time.time() - self.previousTick) >= 1. / self.speed): self.previousTick = time.time() x += self.deltax y += self.deltay # if the snake goes offscreen it appears on the other side. if x >= matrix_width: x = 0 self.pos = x, y elif x < 0: x = matrix_width - 1 self.pos = x, y elif y >= matrix_height: y = 0 self.pos = x, y elif y < 0: y = matrix_height - 1 self.pos = x, y else: self.pos = x, y if len(self.body) > self.tailLen: del self.body[0] self.body.append(self.pos) # and if we hit food increase tail length # also increase our speed if self.food.pos == self.pos: self.speed += 0.5 while self.food.pos in self.body: self.food.randPos() self.food.randColor() self.tailLen += 1 # look if our "tail is in the way" and only if we have a tail. if len(self.body) > 2: # check if head colides with body if len(self.body) != len(set(self.body)): self.body = [self.pos] self.tailLen = 0 self.speed = self.original_speed self.deltax = 0 self.deltay = 0 def draw(self): for i, (x, y) in enumerate(self.body): if i == self.tailLen: # draw our head a certain color self.graphics.drawPixel(x, y, self.head_color) else: # else just draw our body this color # self.graphics.drawPixel(x,y,Color.subtract(self.body_color, (int(255/(i+1)),)*3)) self.graphics.drawPixel(x, y, (255, 255, 255)) self.food.draw() def generate(self): self.graphics.fill(BLACK) self.inputHandling() self.update() self.draw() return self.graphics.getSurface()
class PlasmaThird(object): def __init__(self, speed=10): self.graphics = Graphics(matrix_width, matrix_height) self.plasma = Graphics(matrix_width, matrix_height) self.x_range = xrange(0, matrix_width, 1) self.y_range = xrange(0, matrix_height, 1) self.speed = speed self.interval = speed self.time = random.randint(0,100) self.previousTick = 0 self.angle = 0 self.generatePalette() self.generatePlasmaSurface() def generatePalette(self): self.palette = [] for x in xrange(0, (2**8), 1): r = int(128+256*sin(x)/20) g = int(128+256*sin(r)/100) b = int(128+256*sin(g)/50) colorRGB = (r,g,b) self.palette.append(colorRGB) def generatePlasmaSurface(self): self.angle = self.time x_offset = matrix_width*sin(radians(self.angle))+matrix_width y_offset = matrix_height*cos(radians(self.angle))+matrix_height for y in self.y_range: for x in self.x_range: c = int( 128+(128*sin((x+x_offset)/2.0)) +128+(128*sin((y+y_offset)/2.0)) +128+(128*sin(((x+x_offset)+(y+y_offset))/2.0)) +128+(128*sin(sqrt(float((x+x_offset)*(x+x_offset)+(y+y_offset)*(y+y_offset)))/2.0)) )/4 color = (c,)*3 self.plasma.drawPixel(x,y,color) return list(self.plasma.getSurface()) def process(self): if((time.time()-self.previousTick) >= 1./self.interval): self.previousTick = time.time() self.time+=1 def draw(self): paletteShift = self.time self.generatePlasmaSurface() for y in self.y_range: for x in self.x_range: plasma_color = self.plasma.readPixel(x,y) color_shift = self.palette[paletteShift%256] r = (plasma_color[0]+color_shift[0])%256 g = (plasma_color[1]+color_shift[1])%256 b = (plasma_color[2]+color_shift[2])%256 color = (r,g,b,) #darken the color to create a better contrast color = ColorRGBOps.brighten(color, 20) self.graphics.drawPixel(x,y, color) def generate(self): self.graphics.fill(BLACK) self.process() self.draw() return self.graphics.getSurface()
class VisualizerCircle(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, 1.0), 0.0) level = level**self.exponent level = int(level*0xff) self.graphics.drawCircle(matrix_width/2, matrix_height/2, level, 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()