-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.py
177 lines (156 loc) · 6.47 KB
/
player.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
import re
import time
import threading
from classes import NormalSong, PreciseSong
from pynput import keyboard
from typing import Callable, Union
class Player:
TRANSFORM_CASES = {
'!': '1', '@': '2', '£': '3', '$': '4',
'%': '5', '^': '6', '&': '7', '*': '8',
'(': '9', ')': '0'
}
SPECIAL_CHARS = [
"!", "@", "#", "$", "%", "^", "&", "*", "(", ")",
"_", "+", "{", "}", "|", ":", "\\","\"","<",">","?"
]
WAIT_CASES = {
"|": (1, 'm'), "-": (2, 'm'),
"--": (4, 'm'), "----": (8, 'm'),
"~": (2, 'd'), "#": (4, 'd'),
"<": (8, 'd'), ">": (16, 'd')
}
def __init__(self, error_callback: Callable, progress_callback: Callable):
self.controller = keyboard.Controller()
self.error_callback = error_callback
self.progress_callback = progress_callback
self.is_playing = False
self.is_paused = False
self.play_thread = None
self.current_song = None
self.pause_time = 0
self.start_time = 0
def isShifted(self, key: str):
ascii_value = ord(key)
if 65 <= ascii_value <= 90:
return True
if key in self.SPECIAL_CHARS:
return True
return False
def pressKey(self, key: str):
try:
if key in self.TRANSFORM_CASES:
key = self.TRANSFORM_CASES[key]
if self.isShifted(key):
self.controller.press(keyboard.Key.shift)
self.controller.press(key.lower())
time.sleep(0.001)
self.controller.release(key.lower())
self.controller.release(keyboard.Key.shift)
else:
self.controller.press(key)
time.sleep(0.001)
self.controller.release(key)
except Exception as e:
self.error_callback(f"Error in pressKey: {e}")
def load(self, song_data: Union[NormalSong, PreciseSong]):
self.current_song = song_data
self.stop()
if self.play_thread:
self.play_thread.join()
def play(self, song_data: Union[NormalSong, PreciseSong] = None):
if song_data:
self.load(song_data)
if self.play_thread is not None and self.play_thread.is_alive():
self.stop()
self.play_thread.join()
self.is_playing = True
self.is_paused = False
self.start_time = time.time() - self.pause_time
self.play_thread = threading.Thread(target=self._play)
self.play_thread.start()
def _play(self):
try:
if isinstance(self.current_song, NormalSong):
beat_delay = 30.0 / self.current_song.tempo
total_notes = len(self.current_song.note_list)
for index, note in enumerate(self.current_song.note_list):
while self.is_paused:
time.sleep(0.1)
if not self.is_playing:
break
if isinstance(note, tuple): # Check if the note is a tuple (polyphonic)
for n in note:
self.pressKey(n)
time.sleep(beat_delay)
elif note in self.WAIT_CASES:
delay, unit = self.WAIT_CASES[note]
time.sleep(beat_delay * delay)
else:
self.pressKey(note)
time.sleep(beat_delay)
progress = (index + 1) / total_notes * 100
self.progress_callback(progress)
elif isinstance(self.current_song, PreciseSong):
total_time = self.current_song.note_list[-1][1] / 1000.0
for note, timestamp in self.current_song.note_list:
while self.is_paused:
time.sleep(0.1)
if not self.is_playing:
break
current_time = time.time() - self.start_time
wait_time = (timestamp / 1000.0) - current_time
if wait_time > 0:
time.sleep(wait_time)
if isinstance(note, tuple): # Check if the note is a tuple (polyphonic)
for n in note:
self.pressKey(n)
else:
self.pressKey(note)
progress = (timestamp / 1000.0) / total_time * 100
self.progress_callback(progress)
except Exception as e:
self.error_callback(f"Error in _play: {e}")
finally:
self.is_playing = False
def stop(self):
self.is_playing = False
self.is_paused = False
self.pause_time = 0
def pause(self):
if self.is_paused:
self.is_paused = False
self.start_time += time.time() - self.pause_time
else:
self.is_paused = True
self.pause_time = time.time()
def set_tempo(self, tempo: int):
if isinstance(self.current_song, NormalSong):
self.current_song.tempo = tempo
def translator(self, song_file: str, newline_delay: bool = True, polynote_delay: bool = False):
try:
if song_file.endswith('.sheet'):
with open(song_file, 'r') as f:
tempo = int(f.readline())
transpose = int(f.readline())
note_list = []
contents = f.read().replace(' ', ' | ')
if newline_delay:
contents = contents.replace('\n', ' | ')
char_list = re.split(r'(\[.*?\]|\s)', contents)
for note in char_list:
if note.startswith('[') and note.endswith(']'):
match = note[1:-1]
if polynote_delay:
replaced = '~'.join(match.split())
else:
replaced = ''.join(match.split())
note_list.append(tuple(replaced))
elif note and not note.isspace():
note_list.extend(note)
return NormalSong(tempo=tempo, transpose=transpose, note_list=note_list)
else:
raise Exception("Invalid file format")
except Exception as e:
self.error_callback(f"Error in translator: {e}")
return None