-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualiser.py
226 lines (180 loc) · 7.66 KB
/
visualiser.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Audio Visualiser by Frank Fourlas is licensed under CC BY-NC-SA 4.0.
# To view a copy of this license, visit https://creativecommons.org/licenses/by-nc-sa/4.0
import pygame
from time import time
import os
import tkinter as tk
from tkinter import *
import json
from multiprocessing import Process, Queue
import AudioInput
from Spectrums import Spectrum, SpectrumBar, SpectrumLine
from Boxes import ResponsiveStar, ResponsiveBox, ResponsiveHelix
from VideoScreen import Video
from Sensors import BeatDetectorTCP
from ColourEngine import *
import Widgets as cw
from Menus import VisualiserMenubar
from Dialogs import NewScreenDialog
import ledScreenLayoutManager
with open('visualiser.info', 'r', encoding="utf8") as f:
info = json.load(f)
class Window:
def __init__(self, audioDevice, screens: tuple = (), fpsLimiter=1):
self.audioDevice = audioDevice
self.fpsLimiter = fpsLimiter
self.screens = list(screens)
self._running = True
self.width, self.height = (len(self.screens) - 1) * 10, 0
for i in self.screens:
self.width += i.resolution.x
self.height = max(self.height, i.resolution.y)
self.size = self.width, self.height
self.counter = 0
self.fps = 0
def init(self):
pygame.init()
pygame.display.set_caption("Visualizer v0.2b")
pygame.font.init()
self.screen = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE)
self._running = True
def event(self, event):
if event.type == pygame.QUIT:
self._running = False
def loop(self):
t = time()
self.audioDevice.getData()
self.counter += 1
if self.counter % self.fpsLimiter == 0:
self.fps = 0
try:
self.fps = 1 / ((time() - t) * self.fpsLimiter)
except ZeroDivisionError:
pass
except Exception as e:
print(e)
pygame.display.update()
def render(self, live: str):
self.screen.fill(BLACK)
liveData = None
tempX = 0
for i in self.screens:
display = i.render()
for nx, x in enumerate(display):
for ny, y in enumerate(x):
try:
pygame.draw.rect(self.screen, y,
pygame.Rect(tempX + nx * (i.pixel.x + i.pxDist),
i.resolution.y + i.pxDist - (ny + 1) * (i.pixel.y + i.pxDist),
i.pixel.x,
i.pixel.y))
except Exception as e:
print(e, y)
tempX += i.resolution.x + 10
if i.name == live: liveData = display
self.screen.blit(pygame.font.SysFont('Arial Bold', 30).render('FPS: %5.2f' % self.fps, False, RED), (10, 10))
return liveData
def execute(self, live: str):
if self._running:
for event in pygame.event.get():
self.event(event)
self.loop()
return self.render(live)
else:
self.cleanup()
def updateSize(self, parent):
self.width, self.height = (len(self.screens) - 1) * 10, 0
for i in self.screens:
self.width += i.resolution.x
self.height = max(self.height, i.resolution.y)
self.width, self.height = max(self.width, 125), max(self.height, 40)
self.size = self.width, self.height
self.screen = pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
parent.frame.config(width=self.width, height=self.height)
def cleanup(self):
for i in self.screens:
i.cleanup()
pygame.quit()
class Application(tk.Tk):
def __init__(self, window: Window, *args, **kwargs):
# Initiating tkinter application
tk.Tk.__init__(self, *args, **kwargs)
self.protocol("WM_DELETE_WINDOW", self.quit)
self.title(info['programName'] + ' v' + info['version'] + ' - by ' + info['author'])
self._running = True
# Define LED Screen Layout module
self.queue = Queue()
self.managerProc = Process(target=ledScreenLayoutManager.run, args=(self.queue,))
# Integrating pygame window
self.window = window
self.frame = tk.Frame(self, width=self.window.width, height=self.window.height)
self.frame.pack(side=LEFT)
os.environ['SDL_WINDOWID'] = str(self.frame.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
# Screen Settings Panel
self.settingSelector = cw.SettingsPanel(self)
self.settingSelector.pack(fill=X)
self.SettingsContainer = cw.FrameWithWin(self, self.window)
self.SettingsContainer.pack(fill=BOTH, expand=YES)
self.config(menu=VisualiserMenubar(self)) # Needs to be after declaration of self.settingSelector
self.settingsPanels = []
# Create all the Frames for the settings panels
for n, i in enumerate(self.window.screens):
self.settingsPanels.append(i.createSettings(self.SettingsContainer))
self.settingsPanels[-1].grid(row=0, column=0)
self.settingSelector.selectSetting()
def open(self):
self.window.init()
while self._running:
self.update()
data = self.window.execute(self.settingSelector.live.get())
try:
if not self.managerProc.is_alive():
self.settingSelector.liveBtn['state'] = DISABLED
else:
self.settingSelector.liveBtn['state'] = NORMAL
except tk.TclError:
pass
if data is not None: self.queue.put_nowait(data)
def quit(self):
if self.managerProc.is_alive(): self.managerProc.terminate()
self.window._running = False
self._running = False
super().quit()
self.destroy()
def addScreen(self):
cmd = NewScreenDialog(self).show()
if cmd == "Spectrum":
self.window.screens.append(Spectrum(self.window.audioDevice))
elif cmd == "SpectrumBar":
self.window.screens.append(SpectrumBar(self.window.audioDevice))
elif cmd == "ResponsiveStar":
self.window.screens.append(ResponsiveStar(self.window.audioDevice))
elif cmd == "ResponsiveBox":
self.window.screens.append(ResponsiveBox(self.window.audioDevice))
elif cmd == "ResponsiveHelix":
self.window.screens.append(ResponsiveHelix(self.window.audioDevice))
elif cmd == "Video":
self.window.screens.append(Video())
self.settingsPanels.append(self.window.screens[-1].createSettings(self.SettingsContainer))
self.settingsPanels[-1].grid(row=0, column=0)
self.settingSelector.setCombobox()
self.window.updateSize(self)
self.config(menu=VisualiserMenubar(self))
print("Created", self.window.screens[-1].name)
def rmvScreen(self, screen):
screen.cleanup()
t = self.window.screens.index(screen)
self.settingsPanels[t].destroy()
self.settingsPanels.pop(t)
self.window.screens.pop(t)
self.settingSelector.setCombobox()
self.window.updateSize(self)
self.config(menu=VisualiserMenubar(self))
def openLayoutManager(self):
if not self.managerProc.is_alive():
self.managerProc.start()
if __name__ == "__main__":
A = AudioInput.AudioInput(4096, 96000, 4096 * 3, 1)
app = Application(Window(A, screens=(SpectrumBar(A), BeatDetectorTCP(A),), fpsLimiter=1))
app.open()