-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin2audio.py
90 lines (67 loc) · 2.27 KB
/
bin2audio.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
import soundfile
import os
import struct
from utils import *
def read_file():
filename = input("File to probe: ")
file_found = False
try:
file = open(filename, "rb")
file_found = True
except:
for extension in common_extensions:
try:
file = open(filename + extension, "rb")
filename = filename + extension
file_found = True
break
except FileNotFoundError:
pass
if not file_found:
print("Can not find or open file.")
quit()
filesize = os.path.getsize(filename)
data = []
i = 0
while i < filesize:
try:
data.append(file.read(1))
i += 1
except:
break
file.close()
return data, filename
def write_audio(data, filename):
sample_rate = 21000
sample_rate_inp = input("Sample rate (default 21000):")
if sample_rate_inp:
try:
sample_rate = int(sample_rate_inp)
except:
print("Could not interpret sample rate input. Defaulting to 21000...")
pass
exp_data = []
max_float = 0
for element_idx in range(0, len(data), 4):
try:
e = data[element_idx] + data[element_idx+1] + data[element_idx+2] + data[element_idx+3]
exp_data.append(float(struct.unpack('f', e)[0]))
if float(struct.unpack('f', e)[0]) > max_float:
max_float = float(struct.unpack('f', e)[0])
except:
left_to_encode = len(data) - element_idx
while left_to_encode > 0:
e = data[element_idx]
left_to_encode -= 1
element_idx += 1
e += bytearray(4 - len(e))
exp_data.append(float(struct.unpack('f', e)[0]))
if float(struct.unpack('f', e)[0]) > max_float:
max_float = float(struct.unpack('f', e)[0])
normalized_exp_data = []
for element in exp_data:
normalized_exp_data.append(element / max_float)
filename = filename + ".wav"
soundfile.write(filename, normalized_exp_data, sample_rate)
rawdata, filename = read_file()
write_audio(rawdata, filename)