-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuef2wave.py
205 lines (159 loc) · 6.24 KB
/
uef2wave.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python3
from argparse import ArgumentParser
from functools import reduce
from math import pi, radians, sin
from operator import xor
from struct import pack, unpack
import gzip
import io
import os
import sys
import zipfile
args = None
def parse_args():
parser = ArgumentParser()
parser.add_argument('-s', '--stretch', metavar='FACTOR', type=int,
help='stretch carrier tone duration (default: 1)',
default=1)
args = parser.parse_args()
args.stretch = max(1, args.stretch)
return args
def open_zip(file):
if not zipfile.is_zipfile(file):
file.seek(0)
return file
zip = zipfile.ZipFile(file, 'r')
uefs = [filename for filename in zip.namelist()
if os.path.splitext(filename)[1].lower() == '.uef']
assert uefs
return zip.open(uefs[0], 'r')
def open_gzip(file):
try:
f = gzip.open(file)
f.peek(6)
return f
except OSError:
file.seek(0)
return file
def open_uef(stream):
return open_gzip(open_zip(io.BytesIO(stream.read())))
def as_bits(byte):
return [1 if byte & (1 << i) else 0 for i in range(8)]
def read_chunks(stream):
frequency = 1200
phase = radians(180)
def sample(freq, ph0, ph1, amp=32767):
n = int(round(44100 * (ph1 - ph0) / (2 * pi) // freq))
points = [amp * sin(phase + ph0 + 2 * pi * freq * t / 44100)
for t in range(n)]
fmt = '<' + 'h' * n
return pack(fmt, *[int(p) for p in points])
def wave(x):
if x == 'SL': # Slow Low pulse.
return sample(frequency, 0, pi)
if x == 'SH': # Slow High pulse.
return sample(frequency, pi, 2 * pi)
if x == 'FL': # Fast Low pulse.
return sample(2 * frequency, 0, pi)
if x == 'FH': # Fast High pulse.
return sample(2 * frequency, pi, 2 * pi)
if x == 'SC': # Slow Cycle.
return sample(frequency, 0, 2 * pi)
if x == 'FC': # Fast Cycle.
return sample(2 * frequency, 0, 2 * pi)
if x == 1: # 1-bit
return sample(2 * frequency, 0, 4 * pi)
if x == 0: # 0-bit
return sample(frequency, 0, 2 * pi)
if x == '.': # Silence.
return sample(frequency, 0, 2 * pi, 0)
data = io.BytesIO()
with open_uef(stream) as uef:
assert uef.read(10) == b'UEF File!\x00'
uef.read(2)
while True:
header = uef.read(6)
if len(header) == 0:
break
identifier, length = unpack('<HI', header)
chunk = uef.read(length)
if identifier == 0x100: # Implicit start/stop bit tape data block.
for byte in chunk:
data.write(wave(0))
for bit in as_bits(byte):
data.write(wave(bit))
data.write(wave(1))
elif identifier == 0x104: # Defined tape format data block.
data_bits, parity, stop_bits = unpack('<Bcb', chunk[:3])
for byte in chunk[3:]:
data.write(wave(0))
bits = as_bits(byte)[:data_bits]
for bit in bits:
data.write(wave(bit))
if parity == b'E':
data.write(wave(reduce(xor, bits, 0)))
elif parity == b'O':
data.write(wave(1 - reduce(xor, bits, 0)))
data.write(wave(1) * abs(stop_bits))
if stop_bits < 0:
data.write(wave('FC'))
elif identifier == 0x110: # Carrier tone.
cycles = unpack('<H', chunk)[0]
data.write(wave('FC') * cycles * args.stretch)
elif identifier == 0x111: # Carrier tone with dummy byte.
n, m = unpack('<HH', chunk)
data.write(wave(1) * n)
for b in [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]:
data.write(wave(b))
data.write(wave(1) * m)
elif identifier == 0x112: # Integer gap.
cycles = unpack('<H', chunk)[0]
data.write(wave('.') * cycles)
elif identifier == 0x113: # Change of base frequency.
frequency = unpack('<f', chunk)[0]
elif identifier == 0x114: # Security cycles.
lower, upper = unpack('<BH', chunk[:3])
cycles = upper << 8 + lower
bits = reduce(lambda bs, b: bs + as_bits(b), chunk[5:], [])
index = 0
if chunk[3] == b'P':
data.write(wave('FH' if data[0] else 'SH'))
index += 1
cycles -= 1
for _ in range(cycles, 1, -1):
data.write(wave('FC' if bits[index] else 'SC'))
index += 1
cycles -= 1
if cycles == 1:
if chunk[4] == b'P':
data.write(wave('FL' if bits[index] else 'SL'))
else:
data.write(wave('FC' if bits[index] else 'SC'))
elif identifier == 0x115: # Phase change.
phase = radians(unpack('<H', chunk)[0])
elif identifier == 0x116: # Floating point gap.
secs = unpack('<f', chunk)[0]
data.write(wave('.') * int(round(frequency * secs)))
data.seek(0)
return data.read()
def write_wav(data, stream):
stream.write(b'RIFF')
stream.write(pack('<I', 4 + 8 + 16 + 8 + len(data)))
stream.write(b'WAVE')
stream.write(b'fmt ')
stream.write(pack('<I', 16)) # PCM
stream.write(pack('<h', 1)) # PCM
stream.write(pack('<h', 1)) # Channels
stream.write(pack('<I', 44100)) # Sample rate
stream.write(pack('<I', 44100 * 2)) # Byte rate
stream.write(pack('<h', 2)) # Block align
stream.write(pack('<h', 16)) # Bits per sample
stream.write(b'data')
stream.write(pack('<I', len(data)))
stream.write(data)
def main():
global args
args = parse_args()
write_wav(read_chunks(sys.stdin.buffer), sys.stdout.buffer)
if __name__ == '__main__':
main()