-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstreamer.py
executable file
·54 lines (44 loc) · 1.41 KB
/
streamer.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
#!/usr/bin/env python
import sys
import time
from ymreader import YmReader
def to_minsec(frames, frames_rate):
secs = frames / frames_rate
mins = secs / 60
secs = secs % 60
return (mins, secs)
def main():
header = None
data = None
if len(sys.argv) != 3:
print("Syntax is: {} <output_device> <ym_filepath>".format(sys.argv[0]))
exit(0)
with open(sys.argv[2]) as fd:
ym = YmReader(fd)
ym.dump_header()
header = ym.get_header()
data = ym.get_data()
song_min, song_sec = to_minsec(header['nb_frames'], header['frames_rate'])
print("")
with open(sys.argv[1], 'w') as fd:
time.sleep(2) # Wait for Arduino reset
frame_t = time.time()
for i in range(header['nb_frames']):
# Substract time spent in code
time.sleep(1./header['frames_rate'] - (time.time() - frame_t))
frame_t = time.time()
fd.write(data[i])
fd.flush()
i+= 1
# Additionnal processing
cur_min, cur_sec = to_minsec(i, header['frames_rate'])
sys.stdout.write(
"\x1b[2K\rPlaying {0:02}:{1:02} / {2:02}:{3:02}".format(
cur_min, cur_sec, song_min, song_sec))
sys.stdout.flush()
# Clear YM2149 registers
fd.write('\x00'*16)
fd.flush()
print("")
if __name__ == '__main__':
main()