-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.py
34 lines (24 loc) · 998 Bytes
/
encode.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
import wave
import numpy as np
from num2txt import Convert
# Replace 'your_audio.wav' with the actual path to your WAV file
wav_file_path = 'full.wav'
# Open the WAV file in read mode
with wave.open(wav_file_path, 'rb') as wav_file:
num_frames = wav_file.getnframes() # Number of frames
sample_width = wav_file.getsampwidth() # Sample width in bytes
# Read all the audio frames as bytes
raw_audio_data = wav_file.readframes(num_frames)
print(num_frames)
print(sample_width)
# Convert the raw bytes to a NumPy array of int8
audio_array = np.frombuffer(raw_audio_data, dtype=np.int8)
pre_ascii_characters = [value + 128 for value in audio_array]
post_ascii_characters = []
for num in pre_ascii_characters:
converter = Convert(num)
converter.inttotxt()
post_ascii_characters.append(converter.txt)
# Print the first few ASCII characters for demonstration
with open("asciiaudio.txt", 'w', encoding='utf-8') as f:
f.write(''.join(post_ascii_characters))