forked from marcan/blitzloop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
221 lines (185 loc) · 7.17 KB
/
main.py
File metadata and controls
221 lines (185 loc) · 7.17 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2013 Hector Martin "marcan" <hector@marcansoft.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 or version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import os, threading
from blitzloop import graphics, idlescreen, layout, mpvplayer, songlist, util, web
def entry():
data_home = os.getenv('XDG_DATA_HOME', '~/.local/share')
songs_dir = os.path.join(data_home, 'blitzloop', 'songs')
def csv_list(s):
return s.split(",")
parser = util.get_argparser()
parser.add_argument(
'--songdir', default=os.path.expanduser(songs_dir),
help='directory with songs')
parser.add_argument('--host', default='0.0.0.0', help='IP to listen on')
parser.add_argument(
'--port', default=10111, type=int,
help='port for the UI')
parser.add_argument(
'--width', type=int, default=1024,
help='width of blitzloop window (ignored in fs)')
parser.add_argument(
'--height', type=int, default=768,
help='height of blitzloop window (ignored in fs)')
parser.add_argument(
'--no-audioengine', action="store_true",
help='Disable JACK-based audio engine (mic echo effect)')
parser.add_argument(
'--mics', type=csv_list, default=["system:capture_1"],
help='Mic input connections (list of JACK ports)')
opts = util.get_opts()
songs_dir = os.path.expanduser(opts.songdir)
print("Loading song DB...")
song_database = songlist.SongDatabase(songs_dir)
print("Done.")
display = graphics.Display(opts.width, opts.height, opts.fullscreen)
renderer = graphics.get_renderer().KaraokeRenderer(display)
mpv = mpvplayer.Player(display)
if not opts.no_audioengine and opts.mics:
from blitzloop._audio import AudioEngine
print(repr(opts.mics))
audio = AudioEngine([s.encode("ascii") for s in opts.mics])
print("Engine sample rate: %dHz" % audio.sample_rate)
queue = songlist.SongQueue()
class AudioConfig(object):
def __init__(self):
self.nmics = len(opts.mics) if not opts.no_audioengine else 0
self.volume = 80
if opts.no_audioengine:
self.mic_channels = []
else:
self.mic_channels = [{"volume": 80} for i in range(self.nmics)]
self.mic_feedback = 20
self.mic_delay = 12
self.headstart = 30
def update(self, song=None):
mpv.set_volume(((self.volume / 100.0) ** 2) * 0.5)
if not opts.no_audioengine:
for i, j in enumerate(self.mic_channels):
audio.set_mic_volume(i, ((j["volume"] / 100.0) ** 2) * 2.0)
audio.set_mic_feedback(self.mic_feedback / 100.0)
audio.set_mic_delay(self.mic_delay / 100.0)
audio_config = AudioConfig()
web.database = song_database
web.queue = queue
web.audio_config = audio_config
server = web.ServerThread(host=opts.host, port=opts.port)
server.start()
idle_screen = idlescreen.IdleScreen(display)
def main_render():
# Wait for element in queue
print("Waiting for song to appear in queue...")
qe = None
with queue.lock:
if len(queue) != 0:
qe = queue[0]
if not qe:
idle_screen.reset()
graphics.get_renderer().clear(0, 0, 0, 1)
for f in idle_screen:
audio_config.update()
yield None
graphics.get_renderer().clear(0, 0, 0, 1)
if not qe:
with queue.lock:
if len(queue) != 0:
qe = queue[0]
idle_screen.close()
for i in range(2):
yield None
graphics.get_renderer().clear(0, 0, 0, 1)
print("Loading audio/video...")
mpv.load_song(qe.song)
display.set_aspect(mpv.aspect)
def update_params(defer=False):
mpv.set_speed(1.0 / (2**(qe.speed / 12.0)))
mpv.set_pitch(2**(qe.pitch / 12.0))
for i, j in enumerate(qe.channels):
mpv.set_channel(i, j["volume"] / 10., defer=defer)
mpv.set_pause(qe.pause)
update_params(True)
mpv.update_mixer(force=True)
print("Laying out song...")
renderer.reset()
variant_key = list(qe.song.variants.keys())[qe.variant]
song_layout = layout.SongLayout(qe.song, variant_key, renderer)
print("Loaded.")
song_time = -10
stopping = False
while not (mpv.eof_reached() or (stopping and qe.pause)):
while qe.commands:
cmd, arg = qe.commands.pop(0)
if cmd == "seek":
mpv.seek(arg)
elif cmd == "seekto":
mpv.seek_to(arg)
mpv.draw()
mpv.poll()
song_time = mpv.get_song_time() or song_time
if qe.stop and not stopping:
stopping = True
mpv.fade_out = 2
mpv.duration = min(mpv.duration, song_time + 2)
if not stopping:
mpv.draw_fade(song_time)
speed = 2**(qe.speed / 12.0)
renderer.draw(song_time + audio_config.headstart / 100.0 * speed,
song_layout)
if stopping:
fade = mpv.draw_fade(song_time)
mpv.set_fadevol(max(fade * 1.3 - 0.3, 0))
update_params()
audio_config.update(qe.song)
yield None
mpv.flip()
graphics.get_renderer().clear(0, 0, 0, 1)
for i in range(2):
yield None
graphics.get_renderer().clear(0, 0, 0, 1)
print("Song complete.")
del song_layout
try:
queue.pop(qe.qid)
except (IndexError, KeyError):
pass
mpv.stop()
display.set_aspect(None)
def main():
while True:
for i in main_render():
yield i
def exit():
print("Exit handler called")
mpv.shutdown()
if not opts.no_audioengine:
audio.shutdown()
server.stop()
print("Exit handler done")
def key(k):
if k == 'KEY_ESCAPE':
display.queue_exit()
elif k == 'f':
display.toggle_fullscreen()
display.set_render_gen(main)
display.set_keyboard_handler(key)
display.set_exit_handler(exit)
display.main_loop()
threads = threading.enumerate()
if len(threads) > 1:
print("Loose threads: %r" % threads)
if __name__ == '__main__':
entry()