from winsound import Beep
from time import sleep
from sys import argv
from os import system
from os import path as op_pa
my_dict = [{1:261, 2:294, 3:330, 4:349, 5:392, 6:440, 7:494}, \
{1:523, 2:587, 3:659, 4:699, 5:784, 6:880, 7:988}, \
{1:1047, 2:1175, 3:1319, 4:1397, 5:1568}]
class Player:
def __init__(self, line, speed = 0.2, last = 150):
self.line = line
self.speed = speed
self.last = last
def play(self):
state = 3
i = 0
while i < len(self.line):
if self.line[i] == '\\':
state = int(self.line[i + 1])
i += 2
if self.line[i] == '[':
tmp = int(self.line[i + 1])
for each in range(tmp):
i += 2
a, b = int(self.line[i]), int(self.line[i + 1])
if b == 0:
sleep(self.last // tmp / 1000)
continue
Beep(my_dict[a][b], self.last // tmp)
i += 2
sleep(self.speed)
continue
if state == 3:
if i + 2 < len(self.line):
if self.line[i + 2] == '-':
tmp = 1
while self.line[i + tmp + 1] == '-':
tmp += 1
if not(i + tmp + 1 < len(self.line)):
break
a, b = int(self.line[i]), int(self.line[i + 1])
Beep( \
my_dict[a][b], \
round(self.last * tmp + \
self.speed * (tmp - 1) * 1000))
i += 1 + tmp
sleep(self.speed)
continue
a, b = int(self.line[i]), int(self.line[i + 1])
i += 2
else:
a, b = state, int(self.line[i])
i += 1
if b == 0:
sleep(self.last / 1000)
sleep(self.speed)
continue
Beep(my_dict[a][b], self.last)
sleep(self.speed)
def main():
'''
try:
path = argv[1]
except IndexError:
system('_for_player_get_path.exe')
with open('tmp.txt', 'rt') as f:
path = f.readline()[0:-1]
system('del tmp.txt')
if path == 'exit':
exit()
with open(path, 'rt') as f:
tmp = '#'
while tmp[0] == '#':
tmp = f.readline()
speed, last = eval(tmp)
tmp = '#'
while tmp[0] == '#':
tmp = f.readline()
callable_char = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', \
'\\', '[', '-'}
line = ''
for each in tmp:
if each in callable_char:
line += each
'''
line = r'\15543[21111251[3050505\0555\1[21314543[211112\05[20006[20605\11[21314\315--\1[2141565[21414[213112\06[20011[207065\1[21311\312--[311111116-\14655431[21211[21213\060565\1[21112322431[20605[2121310\316-\146[21514[21314\315--00\314-\124[21514[21312\313--\101[20511316[21416530[2121431\0665\11[21311\312--00[21111[20511\1316[2141653[21212[2121431\0665\123\311--00\13[21314[2151504[21415[2161605543[211112[21314\315--00\13[21314[2151504[21415[2161605543[211112510'
speed, last = 0.1, 150
path = tmp = callable_char = None
player = Player(line, speed, last)
del path, line, speed, last, tmp, callable_char
print('关闭该窗口即可停止播放')
player.play()
if __name__ == '__main__':
main()