forked from marcan/blitzloop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay.py
More file actions
147 lines (127 loc) · 4.71 KB
/
play.py
File metadata and controls
147 lines (127 loc) · 4.71 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
#!/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
import time
from blitzloop import graphics, layout, mpvplayer, song, util
def entry():
parser = util.get_argparser()
parser.add_argument(
'songpath', metavar='SONGPATH', help='path to the song file')
parser.add_argument(
'--show-timings', dest='st', action='store_true',
help='show mpv timings')
parser.add_argument(
'--offset', type=float, default=0.0, help='song offset')
parser.add_argument(
'--variant', type=int, default=0, help='song variant')
parser.add_argument(
'--headstart', type=float, default=0.3, help='time offset')
opts = util.get_opts()
fullscreen = opts.fullscreen
s = song.Song(opts.songpath)
if fullscreen:
display = graphics.Display(1920, 1200, fullscreen, None)
else:
display = graphics.Display(1280, 720, fullscreen, None)
print(display.width, display.height)
mpv = mpvplayer.Player(display)
mpv.load_song(s)
display.set_aspect(mpv.aspect)
renderer = graphics.get_renderer().KaraokeRenderer(display)
song_layout = layout.SongLayout(s, list(s.variants.keys())[opts.variant], renderer)
song_time = -10
speed_i = 0
pitch_i = 0
channels_i = s.channel_defaults
for idx, val in enumerate(channels_i):
mpv.set_channel(idx, val / 10.0)
if opts.offset:
mpv.seek_to(opts.offset)
def render():
t = time.time()
nonlocal song_time
while not mpv.eof_reached():
graphics.get_renderer().clear(0, 0, 0, 1)
t1 = time.time()
mpv.draw()
dt = time.time() - t1
mpv.poll()
song_time = mpv.get_song_time() or song_time
mpv.draw_fade(song_time)
renderer.draw(song_time + opts.headstart * 2**(speed_i/12.0), song_layout)
yield None
t2 = time.time()
if opts.st:
print("T:%7.3f/%7.3f B:%7.3f FPS:%.2f draw:%.3f" % (song_time, mpv.duration, s.timing.time2beat(song_time), (1.0/(t2-t)), dt))
t = t2
mpv.flip()
mpv.shutdown()
os._exit(0)
pause = False
CH_UP = "+456"
CH_DOWN = "-123"
def key(k):
nonlocal speed_i, pitch_i, pause
if k == 'KEY_ESCAPE':
mpv.shutdown()
os._exit(0)
elif k == 'f':
display.toggle_fullscreen()
elif k == '[' and speed_i > -12:
speed_i -= 1
print("Speed: %d" % speed_i)
mpv.set_speed(2**(-speed_i/12.0))
elif k == ']' and speed_i < 12:
speed_i += 1
print("Speed: %d" % speed_i)
mpv.set_speed(2**(-speed_i/12.0))
elif k == 'KEY_UP' and pitch_i < 12:
pitch_i += 1
print("Pitch: %d" % pitch_i)
mpv.set_pitch(2**(pitch_i/12.0))
elif k == 'KEY_DOWN' and pitch_i > -12:
pitch_i -= 1
print("Pitch: %d" % pitch_i)
mpv.set_pitch(2**(pitch_i/12.0))
elif k in CH_UP:
idx = CH_UP.index(k)
if len(channels_i) > idx and channels_i[idx] < 30:
channels_i[idx] += 1
print("Channel %d: %d" % (idx, channels_i[idx]))
mpv.set_channel(idx, channels_i[idx]/10.0)
elif k in CH_DOWN:
idx = CH_DOWN.index(k)
if len(channels_i) > idx and channels_i[idx] > 0:
channels_i[idx] -= 1
print("Channel %d: %d" % (idx, channels_i[idx]))
mpv.set_channel(idx, channels_i[idx]/10.0)
elif k == 'KEY_LEFT':
mpv.seek(-10)
elif k == 'KEY_RIGHT':
mpv.seek(10)
elif k == ' ':
pause = not pause
t = time.time()
mpv.set_pause(pause)
print("P %.03f" % (time.time()-t))
mpv.play()
display.set_render_gen(render)
display.set_keyboard_handler(key)
display.main_loop()
mpv.shutdown()
if __name__ == '__main__':
entry()