Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
552ce5c
commit d528db4
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import pyaudio | ||
import sys | ||
import numpy as np | ||
import aubio | ||
|
||
import pygame | ||
import random | ||
|
||
from threading import Thread | ||
|
||
import queue | ||
import time | ||
|
||
import argparse | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-input", required=False, type=int, help="Audio Input Device") | ||
parser.add_argument("-f", action="store_true", help="Run in Fullscreen Mode") | ||
args = parser.parse_args() | ||
|
||
if not args.input: | ||
print("No input device specified. Printing list of input devices now: ") | ||
p = pyaudio.PyAudio() | ||
for i in range(p.get_device_count()): | ||
print("Device number (%i): %s" % (i, p.get_device_info_by_index(i).get('name'))) | ||
print("Run this program with -input 1, or the number of the input you'd like to use.") | ||
exit() | ||
|
||
pygame.init() | ||
|
||
if args.f: | ||
screenWidth, screenHeight = 1024, 768 | ||
screen = pygame.display.set_mode((screenWidth, screenHeight), pygame.FULLSCREEN | pygame.HWSURFACE | pygame.DOUBLEBUF) | ||
|
||
else: | ||
screenWidth, screenHeight = 800, 800 | ||
screen = pygame.display.set_mode((screenWidth, screenHeight)) | ||
|
||
white = (255, 255, 255) | ||
black = (0, 0, 0) | ||
|
||
class Circle(object): | ||
def __init__(self, x, y, color, size): | ||
self.x = x | ||
self.y = y | ||
self.color = color | ||
self.size = size | ||
|
||
def shrink(self): | ||
self.size -= 3 | ||
|
||
colors = [(229, 244, 227), (93, 169, 233), (0, 63, 145), (255, 255, 255), (109, 50, 109)] | ||
circleList = [] | ||
|
||
# initialise pyaudio | ||
p = pyaudio.PyAudio() | ||
|
||
clock = pygame.time.Clock() | ||
|
||
# open stream | ||
|
||
buffer_size = 4096 # needed to change this to get undistorted audio | ||
pyaudio_format = pyaudio.paFloat32 | ||
n_channels = 1 | ||
samplerate = 44100 | ||
stream = p.open(format=pyaudio_format, | ||
channels=n_channels, | ||
rate=samplerate, | ||
input=True, | ||
input_device_index=args.input, | ||
frames_per_buffer=buffer_size) | ||
|
||
time.sleep(1) | ||
|
||
# setup onset detector | ||
tolerance = 0.8 | ||
win_s = 4096 # fft size | ||
hop_s = buffer_size // 2 # hop size | ||
onset = aubio.onset("default", win_s, hop_s, samplerate) | ||
|
||
q = queue.Queue() | ||
|
||
def draw_pygame(): | ||
running = True | ||
while running: | ||
key = pygame.key.get_pressed() | ||
|
||
if key[pygame.K_q]: | ||
running = False | ||
for event in pygame.event.get(): | ||
if event.type == pygame.QUIT: | ||
running = False | ||
|
||
if not q.empty(): | ||
b = q.get() | ||
newCircle = Circle(random.randint(0, screenWidth), random.randint(0, screenHeight), | ||
random.choice(colors), 700) | ||
circleList.append(newCircle) | ||
|
||
screen.fill(black) | ||
for place, circle in enumerate(circleList): | ||
if circle.size < 1: | ||
circleList.pop(place) | ||
else: | ||
pygame.draw.circle(screen, circle.color, (circle.x, circle.y), circle.size) | ||
circle.shrink() | ||
|
||
pygame.display.flip() | ||
clock.tick(90) | ||
|
||
def get_onsets(): | ||
while True: | ||
try: | ||
buffer_size = 2048 # needed to change this to get undistorted audio | ||
audiobuffer = stream.read(buffer_size, exception_on_overflow=False) | ||
signal = np.fromstring(audiobuffer, dtype=np.float32) | ||
|
||
|
||
if onset(signal): | ||
q.put(True) | ||
|
||
except KeyboardInterrupt: | ||
print("*** Ctrl+C pressed, exiting") | ||
break | ||
|
||
|
||
t = Thread(target=get_onsets, args=()) | ||
t.daemon = True | ||
t.start() | ||
|
||
draw_pygame() | ||
stream.stop_stream() | ||
stream.close() | ||
pygame.display.quit() |