-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
150 lines (128 loc) · 5.28 KB
/
app.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 pygame
from time import sleep
from random import randint
from sound import Sound
from pixel import Pixel, Color
from consts import *
pygame.init()
class App:
def __init__(self):
pygame.display.set_caption("Visualizer")
self.window = pygame.display.set_mode(size=(WIDTH, HEIGHT))
self.window.fill([0, 0, 0])
self.running = True
self.top_pixels, self.bot_pixels = [], []
# Top pixel
self.top_color = Color(rgb=WHITE)
self.top_pixel = Pixel(CENTER[0], CENTER[1], RADIUS, self.top_color, facing='RIGHT', win=self.window)
# Bottom pixel
self.bot_color = Color(rgb=BLUE)
self.bot_pixel = Pixel(CENTER[0], CENTER[1], RADIUS, self.bot_color, facing='LEFT', win=self.window)
self.sound = Sound()
def run(self):
self.scenario3()
def scenario1(self):
self.sound.open_stream()
while self.running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
sound_bit = self.sound.update_stream()[0]
if sound_bit.any():
sound_block = sum(sound_bit)
rad_mod, dis_mod = AMP * (sound_block)
else:
rad_mod, dis_mod = (AMP, AMP)
self.top_pixel.mod_radius(rad_mod//AMP)
self.bot_pixel.mod_radius(rad_mod//AMP)
self.run_pixel(self.top_pixel)
self.run_pixel(self.bot_pixel)
self.top_pixel.mod_displacement(dis_mod//AMP)
self.bot_pixel.mod_displacement(dis_mod//AMP)
pygame.display.update()
pygame.time.delay(DELAY)
def scenario2(self):
self.sound.open_stream()
rad_mod, dis_mod = (1, 1)
top_coord, bot_coord = CENTER, CENTER
while self.running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
sound_bit = self.sound.update_stream()[0]
if sound_bit.any():
sound_block = sum(sound_bit)
rad_mod, dis_mod = AMP * (sound_block)
top_coord = (CENTER[0] + int(rad_mod), CENTER[1] + int(dis_mod))
bot_coord = (CENTER[0] - int(rad_mod), CENTER[1] + int(dis_mod))
if abs(rad_mod) >= (RADIUS//2):
self.top_pixel.coord = top_coord
self.top_pixel.mod_radius(rad_mod/AMP)
self.bot_pixel.coord = bot_coord
self.bot_pixel.mod_radius(rad_mod/AMP)
if abs(rad_mod) > (HEIGHT//4):
self.bot_pixel.color.randomize()
self.top_pixel.color.randomize()
self.bot_pixel.draw()
self.top_pixel.draw()
pygame.display.update()
pygame.time.delay(DELAY)
def scenario3(self):
self.sound.open_raw_stream()
top_coord, bot_coord = CENTER, CENTER
while self.running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
sound_bit = self.sound.update_raw_stream()
if sound_bit:
third = len(sound_bit) // 3
groups = [sound_bit[x:x+third] for x in range(0, len(sound_bit), third)]
sound_block = [sum(group)/len(group) for group in groups]
# sound_block = sum(sound_bit) // len(sound_bit)
top_mod = int(sound_block[0] ** 1.2)
bot_mod = int(sound_block[0] ** 0.7)
top_coord = (top_coord[0], CENTER[1] - int(top_mod) + 200)
bot_coord = top_coord
# print(rad_mod)
if abs(top_mod) >= (RADIUS*21):
self.bot_pixel.color.color = list(sound_block)
self.bot_pixel.color.darken()
self.top_pixel.color.color = sound_block[-2:] + [255-sound_block[0],]
# [randint(int(sound_block[0]), 255),]
if abs(bot_mod) >= (RADIUS//2):
self.top_pixel.coord = top_coord
self.top_pixel.radius = top_mod
self.bot_pixel.coord = bot_coord
self.bot_pixel.radius = bot_mod
self.top_pixel.draw()
self.bot_pixel.draw()
pygame.display.update()
pygame.time.delay(DELAY*10)
def run_pixel(self, pixel):
hit, direction = self.check_hit_change_direction(pixel, WIDTH, HEIGHT - (RADIUS/2))
if hit:
pixel.facing = direction
pixel.color.randomize()
else:
if pixel.facing == 'LEFT':
pixel.move_backwards()
else:
pixel.move_forwards()
pixel.draw()
return pixel.color, direction
def check_hit_change_direction(self, pixel, hit_x, hit_y):
cur_direction = pixel.facing
x, y = pixel.coord
if (x >= hit_x) and (y >= hit_y):
pixel.move_backwards()
return True, 'LEFT'
elif (x <= (0 + RADIUS//2)) and (y <= RADIUS//2):
pixel.move_forwards()
return True, 'RIGHT'
else:
return False, cur_direction
if __name__ == "__main__":
app = App()
app.run()
pygame.display.quit()