-
Notifications
You must be signed in to change notification settings - Fork 0
/
mode2reader.py
67 lines (56 loc) · 1.32 KB
/
mode2reader.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
import sys
class Reader:
def __init__(
self,
rate,
start_is_space,
):
self.rate = rate
if start_is_space:
self.bit = 0
else:
self.bit = 1
def read(
self,
wave,
):
for duration in wave:
units = round(duration/self.rate)
yield from [self.bit]*units
self.bit = 1-self.bit
@classmethod
def read_from_compact_file(
cls,
path,
rate,
start_is_space,
):
r = Reader(
rate=rate,
start_is_space=start_is_space,
)
with open(path, 'r') as f:
for line in f.readlines():
values = line.strip().split(' ')
yield from r.read(
wave=[
int(value.strip())
for value in values
if len(value.strip()) > 0
],
)
@classmethod
def read_from_verbose_file(
cls,
path,
rate,
start_is_space,
):
raise NotImplementedError()
if __name__ == '__main__':
wave = Reader.read_from_compact_file(
path=sys.argv[1],
rate=1000,
start_is_space=bool(sys.argv[2]),
)
print(list(wave))