forked from burningion/pygame-video-synthesizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_glitchy_circles.py
executable file
·150 lines (110 loc) · 3.94 KB
/
02_glitchy_circles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import pyaudio
import sys
import numpy as np
import matplotlib.pyplot as plt
import aubio
import pygame
pygame.font.init() # you have to call this at the start,
# if you want to use this module.
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 = 600, 600
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 -= 1
def drawColorFromCmap(rand_nummer):
rgb_tuple = plt.cm.plasma(rand_nummer)
return (np.int(rgb_tuple[0]*255), np.int(rgb_tuple[1]*255), np.int(rgb_tuple[2]*255))
colors = [(229, 244, 227), (93, 169, 233), (0, 63, 145), (255, 255, 255), (109, 50, 109)]
circleList = []
words = ["Frieder Carlo!", "Dr. Dr. Hyper"]
circle_poitions = [(0, 0), (0, screenHeight), (screenWidth, 0), (screenWidth, screenHeight)]
# 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),
drawColorFromCmap(random.randint(0,255)), 100)
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(10)
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()