-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwav_decompressor.py
More file actions
59 lines (46 loc) · 1.58 KB
/
wav_decompressor.py
File metadata and controls
59 lines (46 loc) · 1.58 KB
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
# Imports ---------------------------------------------------------------------------------------- #
from wav import WAV
import sys
# Functions -------------------------------------------------------------------------------------- #
# Source: http://rosettacode.org/wiki/LZW_compression#Python
def decompress(compressed):
"""Decompress a list of output ks to a string."""
from io import BytesIO
# Build the dictionary.
dict_size = 256
dictionary = {i: i.to_bytes(1, 'little') for i in range(dict_size)}
# use BytesIO, otherwise this becomes O(N^2)
# due to string concatenation in a loop
result = BytesIO()
w: bytes = b'' + (compressed.pop(0) % 256).to_bytes(1, 'little')
result.write(w)
for k in compressed:
if k in dictionary:
entry = dictionary[k]
elif k == dict_size:
entry = w + w[0].to_bytes(1, 'little')
else:
continue
result.write(entry)
# Add w+entry[0] to the dictionary.
dictionary[dict_size] = w + entry[0].to_bytes(1, 'little')
dict_size += 1
w = entry
return result.getvalue()
# Main ------------------------------------------------------------------------------------------- #
if __name__ == '__main__':
# Check args
if (len(sys.argv) != 2):
print('USAGE: python3 wav_decompressor.py file_to_decompress')
exit(1)
# Import
print('READING...')
cwav = WAV(sys.argv[1])
cvals = [int.from_bytes(cwav.data_b[i:i+4], 'little') for i in range(0, cwav.data_sz_b, 4)]
# Decompress
print('DECODING...')
dec = decompress(cvals)
# Export
print('SAVING...')
cwav.save('output.wav', dec)
exit(0)