Created
June 30, 2017 15:17
-
-
Save LandonPowell/fcdd0ceffa6478751d77ea5b457f657a to your computer and use it in GitHub Desktop.
Very simple, very shitty, Python Audio Visualizer PROOF OF CONCEPT.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame, sys, random, colorsys | |
from pygame import gfxdraw | |
from pygame.locals import * | |
pygame.init() | |
canvas = pygame.display.set_mode((750, 750)) | |
center = canvas.get_rect().center | |
pygame.display.set_caption("Visualization") | |
def hsv2rgb(h, s, v): | |
return tuple(int(i * 255) for i in colorsys.hsv_to_rgb(h, s, v)) | |
import pyaudio | |
import audioop | |
CHUNK = 1024 | |
FORMAT = pyaudio.paInt16 | |
CHANNELS = 2 | |
RATE = 44100 | |
p = pyaudio.PyAudio() | |
stream = p.open(format=FORMAT, | |
channels=CHANNELS, | |
rate=RATE, | |
input=True, | |
frames_per_buffer=CHUNK) | |
x = 0 | |
while True: | |
data = stream.read(CHUNK) | |
canvas.fill((0,0,0)) | |
if x > 1: x = 0 | |
else: x += 0.01 | |
amplitude = audioop.rms(data, 2) | |
color = hsv2rgb(amplitude / 255.0 / 5 + x, 1, 1) | |
pygame.gfxdraw.aacircle(canvas, center[0], center[1], 300, color) | |
pygame.gfxdraw.aacircle(canvas, center[0], center[1], amplitude, color) | |
pygame.gfxdraw.aacircle(canvas, center[0], center[1], amplitude / 3, color) | |
pygame.gfxdraw.filled_circle(canvas, center[0], center[1], amplitude / 3, color) | |
pygame.display.update() | |
stream.stop_stream() | |
stream.close() | |
p.terminate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment