-
Notifications
You must be signed in to change notification settings - Fork 16
/
beatDetector.py
145 lines (119 loc) · 4.38 KB
/
beatDetector.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
import random
import osc
import ui
import sys
from PyQt5 import QtCore, QtWidgets
from bpm import SignalGenerator, AudioAnalyzer
from recorder import *
class BeatDetector:
ui: ui.UserInterface
osc_client: osc.OscClient
input_recorder: InputRecorder
timer_period = int(round(1000 / (180 / 60) / 16)) # 180bpm / 16
min_program_beats = 8
max_program_beats = 8 * 4
current_intensity = 0
current_program = 0
current_program_beats = 0
change_program = False
calm_programs = [
2, # Full Color Slow
5, # Retro
8, # Wipe Noise
]
normal_programs = [
1, # Full Color Moving
3, # Fill Up Once
6, # Kitt
]
intense_programs = [
4, # Fill Up Repeat
7, # Flash Noise
]
def __init__(self, window) -> None:
self.ui = ui.UserInterface(self.on_auto_prog_button_clicked, self.on_input_changed)
self.ui.setup_ui(window)
self.osc_client = osc.OscClient("localhost", 7701)
self.auto_prog = False
# Wire up beat detector and signal generation
self.input_recorder = InputRecorder(self)
self.audio_analyzer = AudioAnalyzer(self.input_recorder)
signal_generator = SignalGenerator(self.audio_analyzer)
# Wire up callbacks
signal_generator.on_beat(self.on_beat)
signal_generator.on_bar(self.on_bar)
signal_generator.on_new_song(self.on_new_song)
signal_generator.on_bpm_change(self.on_bpm_change)
signal_generator.on_intensity_change(self.on_intensity_change)
# Start beat detection
self.timer = QtCore.QTimer()
self.timer.start(self.timer_period)
self.timer.timeout.connect(self.audio_analyzer.analyze_audio)
self.input_recorder.start()
def change_program_if_needed(self):
if self.change_program and self.current_program_beats >= self.min_program_beats:
new_program = self.choose_program_by_intensity()
if new_program != self.current_program:
print("Change program to {:d} for intensity {:d}".format(new_program, self.current_intensity))
self.current_program = new_program
self.osc_client.send_prog_signal(new_program)
self.current_program_beats = 1
self.change_program = False
def choose_program_by_intensity(self):
if self.current_intensity == 1:
program_list = self.intense_programs
elif self.current_intensity == -1:
program_list = self.calm_programs
else:
program_list = self.normal_programs
return random.choice(program_list)
def on_auto_prog_button_clicked(self):
self.auto_prog = not self.auto_prog
self.current_program = 0
self.current_program_beats = 0
if self.auto_prog:
self.change_program = True
self.ui.change_auto_prog_state(self.auto_prog)
def on_input_changed(self, index):
self.input_recorder.change_input(index)
def on_beat(self, beat_index):
# print("beat")
self.osc_client.send_beat_signal()
self.ui.change_beat_button_color()
self.ui.display_beat_index(beat_index + 1) # Starts with 0
# Keep track how long current program is running
if self.auto_prog:
self.current_program_beats += 1
if self.current_program_beats > self.max_program_beats:
self.change_program = True
def on_bar(self):
# print("bar")
self.change_program_if_needed()
self.osc_client.send_bar_signal()
self.ui.change_bar_button_color()
def on_new_song(self):
# print("next song")
self.change_program = True
self.ui.display_new_song()
def on_bpm_change(self, bpm):
# print("bpm changed")
self.ui.display_bpm(bpm)
def on_intensity_change(self, intensity):
self.current_intensity = intensity
if self.auto_prog:
self.change_program = True
self.ui.display_intensity(intensity)
def close(self):
self.input_recorder.close()
if __name__ == "__main__":
# Setup UI
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QMainWindow()
# Start beat tracking
beat_detector = BeatDetector(window)
# Display window
window.show()
code = app.exec_()
# Clean up
beat_detector.close()
sys.exit(code)