Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +171 -0
- packages.txt +1 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
from scipy.io.wavfile import write, read
|
3 |
+
from tqdm import tqdm
|
4 |
+
import sounddevice as sd
|
5 |
+
import gradio as gr
|
6 |
+
from scipy.signal import butter, lfilter
|
7 |
+
import reedsolo
|
8 |
+
import random
|
9 |
+
from IPython.display import Audio, display
|
10 |
+
import os
|
11 |
+
|
12 |
+
input_file = 'input_text.wav'
|
13 |
+
output_file = 'output_filtered_sender.wav'
|
14 |
+
|
15 |
+
low_frequency = 18000
|
16 |
+
high_frequency = 19000
|
17 |
+
bit_duration = 0.007
|
18 |
+
sample_rate = 44100
|
19 |
+
amplitude_scaling_factor = 15.0
|
20 |
+
|
21 |
+
def delete_file(file_path):
|
22 |
+
try:
|
23 |
+
os.remove(file_path)
|
24 |
+
print(f"File '{file_path}' deleted successfully.")
|
25 |
+
except OSError as e:
|
26 |
+
print(f"Error deleting file '{file_path}': {e}")
|
27 |
+
|
28 |
+
#-----------------Sender-----------------#
|
29 |
+
|
30 |
+
def text_to_binary(text):
|
31 |
+
binary_string = ''.join(format(ord(char), '08b') for char in text)
|
32 |
+
return binary_string
|
33 |
+
|
34 |
+
def signal_function(frequency, time):
|
35 |
+
return np.sin(2 * np.pi * frequency * time)
|
36 |
+
|
37 |
+
def generate_silence(duration, sample_rate=44100):
|
38 |
+
return np.zeros(int(sample_rate * duration))
|
39 |
+
|
40 |
+
def binary_signal(binary_string):
|
41 |
+
t = np.linspace(0, bit_duration, int(sample_rate * bit_duration), False)
|
42 |
+
signal = []
|
43 |
+
|
44 |
+
for bit in tqdm(binary_string, desc="Generating Signal"):
|
45 |
+
if bit == '0':
|
46 |
+
signal.append(amplitude_scaling_factor * np.sign(signal_function(low_frequency, t)))
|
47 |
+
else:
|
48 |
+
signal.append(amplitude_scaling_factor * np.sign(signal_function(high_frequency, t)))
|
49 |
+
|
50 |
+
return np.concatenate(signal)
|
51 |
+
|
52 |
+
def flag_encoding(bit_value):
|
53 |
+
flag_duration = 6 * 0.0014
|
54 |
+
t_flag = np.linspace(0, flag_duration, int(sample_rate * flag_duration), False)
|
55 |
+
signal = []
|
56 |
+
|
57 |
+
if bit_value == 0:
|
58 |
+
binary_flag = "100001"
|
59 |
+
for bit in binary_flag:
|
60 |
+
if bit == '0':
|
61 |
+
signal.append(amplitude_scaling_factor * np.sign(signal_function(low_frequency, t_flag)))
|
62 |
+
else:
|
63 |
+
signal.append(amplitude_scaling_factor * np.sign(signal_function(high_frequency, t_flag)))
|
64 |
+
|
65 |
+
return np.concatenate(signal)
|
66 |
+
else:
|
67 |
+
binary_flag = "011110"
|
68 |
+
for bit in tqdm(binary_flag, desc="Generating Signal"):
|
69 |
+
if bit == '0':
|
70 |
+
signal.append(amplitude_scaling_factor * np.sign(signal_function(low_frequency, t_flag)))
|
71 |
+
else:
|
72 |
+
signal.append(amplitude_scaling_factor * np.sign(signal_function(high_frequency, t_flag)))
|
73 |
+
|
74 |
+
return np.concatenate(signal)
|
75 |
+
|
76 |
+
def encode_rs(binary_string, ecc_bytes):
|
77 |
+
byte_data = bytearray(int(binary_string[i:i + 8], 2) for i in range(0, len(binary_string), 8))
|
78 |
+
rs = reedsolo.RSCodec(ecc_bytes)
|
79 |
+
encoded_data = rs.encode(byte_data)
|
80 |
+
encoded_binary_string = ''.join(format(byte, '08b') for byte in encoded_data)
|
81 |
+
return encoded_binary_string
|
82 |
+
|
83 |
+
def manchester_encoding(binary_string):
|
84 |
+
encode_binary_string = encode_rs(binary_string, 20)
|
85 |
+
|
86 |
+
t = np.linspace(0, bit_duration, int(sample_rate * bit_duration), False)
|
87 |
+
signal = []
|
88 |
+
|
89 |
+
for bit in tqdm(encode_binary_string, desc="Generating Signal"):
|
90 |
+
if bit == '0':
|
91 |
+
signal.append(amplitude_scaling_factor * np.sign(signal_function(low_frequency, t)))
|
92 |
+
signal.append(amplitude_scaling_factor * np.sign(signal_function(high_frequency, t)))
|
93 |
+
else:
|
94 |
+
signal.append(amplitude_scaling_factor * np.sign(signal_function(high_frequency, t)))
|
95 |
+
signal.append(amplitude_scaling_factor * np.sign(signal_function(low_frequency, t)))
|
96 |
+
|
97 |
+
return np.concatenate(signal)
|
98 |
+
|
99 |
+
def binary_to_signal(binary_string):
|
100 |
+
flag_start = flag_encoding(0)
|
101 |
+
flag_end = flag_encoding(1)
|
102 |
+
silence_duration = 0.1
|
103 |
+
silence_before = generate_silence(silence_duration)
|
104 |
+
silence_after = generate_silence(silence_duration)
|
105 |
+
|
106 |
+
signal = np.concatenate([silence_before, flag_start, manchester_encoding(binary_string), flag_end, silence_after])
|
107 |
+
|
108 |
+
return signal
|
109 |
+
|
110 |
+
def encode_and_generate_audio(text):
|
111 |
+
delete_file("output_text.wav")
|
112 |
+
delete_file("output_filtered.wav")
|
113 |
+
binary_string_to_send = text_to_binary(text)
|
114 |
+
signal = binary_to_signal(binary_string_to_send)
|
115 |
+
write('output_text.wav', 44100, signal.astype(np.int16))
|
116 |
+
main()
|
117 |
+
return "WAV file generated and ready to be sent."
|
118 |
+
|
119 |
+
#-----------------Filter-----------------#
|
120 |
+
|
121 |
+
def butter_bandpass(lowcut, highcut, fs, order=5):
|
122 |
+
nyquist = 0.5 * fs
|
123 |
+
low = lowcut / nyquist
|
124 |
+
high = highcut / nyquist
|
125 |
+
coef = butter(order, [low, high], btype='band')
|
126 |
+
b = coef[0]
|
127 |
+
a = coef[1]
|
128 |
+
return b, a
|
129 |
+
|
130 |
+
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
|
131 |
+
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
|
132 |
+
y = lfilter(b, a, data)
|
133 |
+
return y
|
134 |
+
|
135 |
+
def main():
|
136 |
+
input_file = 'output_text.wav'
|
137 |
+
output_file = 'output_filtered_sender.wav'
|
138 |
+
lowcut = 18000
|
139 |
+
highcut = 19000
|
140 |
+
|
141 |
+
try:
|
142 |
+
fs, data = read(input_file)
|
143 |
+
|
144 |
+
filtered_data = butter_bandpass_filter(data, lowcut, highcut, fs)
|
145 |
+
write(output_file, fs, np.int16(filtered_data))
|
146 |
+
return "Filtered Audio Generated"
|
147 |
+
except Exception as e:
|
148 |
+
return f"Error: {str(e)}"
|
149 |
+
|
150 |
+
|
151 |
+
#-----------------Player-----------------#
|
152 |
+
|
153 |
+
wav_file_path = "output_filtered_sender.wav"
|
154 |
+
|
155 |
+
def play_sound():
|
156 |
+
# You can replace 'your_sound_file.mp3' with the path to your sound file
|
157 |
+
return gr.Audio(wav_file_path, autoplay=True)
|
158 |
+
|
159 |
+
|
160 |
+
#-----------------Interface-----------------#
|
161 |
+
|
162 |
+
# Define the Gradio interface
|
163 |
+
with gr.Blocks() as demo:
|
164 |
+
name = gr.Textbox(label="Your Text")
|
165 |
+
output = gr.Textbox(label="Output")
|
166 |
+
submit = gr.Button("Generate Audio")
|
167 |
+
submit.click(fn=encode_and_generate_audio, inputs=name, outputs=output)
|
168 |
+
|
169 |
+
gr.Interface(fn=play_sound, inputs=[], outputs=gr.Audio(), live=False)
|
170 |
+
|
171 |
+
demo.launch()
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
libportaudio2
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy~=1.24.3
|
2 |
+
sounddevice~=0.4.6
|
3 |
+
gradio~=4.8.0
|
4 |
+
reedsolo~=1.7.0
|
5 |
+
scipy~=1.11.4
|
6 |
+
tqdm~=4.66.1
|
7 |
+
ipython~=8.12.2
|