-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaudioSaving.py
More file actions
40 lines (31 loc) · 1.08 KB
/
Copy pathaudioSaving.py
File metadata and controls
40 lines (31 loc) · 1.08 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
import wave
import struct
def encode(file_path, audio_path):
# read binary file and convert to bytes
with open(file_path, 'rb') as f:
data = f.read()
bytes_data = bytearray(data)
# create wave file and set parameters
wav_file = wave.open(audio_path, 'w')
wav_file.setsampwidth(2)
wav_file.setnchannels(1)
wav_file.setframerate(44100)
# convert bytes to audio samples and write to wave file
for byte in bytes_data:
sample = struct.pack('h', byte)
wav_file.writeframesraw(sample)
wav_file.close()
def decode(audio_path, file_path):
# read wave file and get audio samples
wav_file = wave.open(audio_path, 'r')
samples = wav_file.readframes(wav_file.getnframes())
# convert samples to bytes and write to binary file
bytes_data = bytearray()
for i in range(0, len(samples), 2):
byte = struct.unpack('h', samples[i:i+2])[0]
bytes_data.append(byte)
with open(file_path, 'wb') as f:
f.write(bytes_data)
wav_file.close()
encode("LICENSE", "audio.wav")
decode("audio.wav", "LICENSE2")