Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,48 +21,113 @@ sample_rate = 44100
|
|
21 |
amplitude_scaling_factor = 10.0
|
22 |
|
23 |
|
24 |
-
# -----------------
|
25 |
|
26 |
-
def
|
27 |
-
|
28 |
-
|
29 |
-
wavio.write("recorded.wav", data, sr)
|
30 |
-
main()
|
31 |
-
return f"Audio receive correctly"
|
32 |
-
except Exception as e:
|
33 |
-
return f"Error: {e}"
|
34 |
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
|
|
39 |
nyquist = 0.5 * sr
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
return b, a
|
46 |
|
47 |
|
48 |
-
def butter_bandpass_filter(data,
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
y = lfilter(b, a, data)
|
|
|
51 |
return y
|
52 |
|
53 |
|
54 |
-
def
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
highcut = 19500
|
59 |
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
return "Filtered Audio Generated"
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
# -----------------Frame----------------- #
|
68 |
|
|
|
21 |
amplitude_scaling_factor = 10.0
|
22 |
|
23 |
|
24 |
+
# -----------------Filter----------------- #
|
25 |
|
26 |
+
def butter_bandpass(sr, order=5):
|
27 |
+
"""
|
28 |
+
This function designs a Butterworth bandpass filter.
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
Parameters:
|
31 |
+
sr (int): The sample rate of the audio.
|
32 |
+
order (int): The order of the filter.
|
33 |
|
34 |
+
Returns:
|
35 |
+
tuple: The filter coefficients `b` and `a`.
|
36 |
+
"""
|
37 |
+
# Calculate the Nyquist frequency
|
38 |
nyquist = 0.5 * sr
|
39 |
+
|
40 |
+
# Normalize the cutoff frequencies
|
41 |
+
low = low_frequency / nyquist
|
42 |
+
high = high_frequency / nyquist
|
43 |
+
|
44 |
+
# Design the Butterworth bandpass filter
|
45 |
+
coefficient = butter(order, [low, high], btype='band')
|
46 |
+
|
47 |
+
# Extract the filter coefficients
|
48 |
+
b = coefficient[0]
|
49 |
+
a = coefficient[1]
|
50 |
+
|
51 |
return b, a
|
52 |
|
53 |
|
54 |
+
def butter_bandpass_filter(data, sr, order=5):
|
55 |
+
"""
|
56 |
+
This function applies the Butterworth bandpass filter to a given data.
|
57 |
+
|
58 |
+
Parameters:
|
59 |
+
data (array): The audio data to be filtered.
|
60 |
+
sr (int): The sample rate of the audio.
|
61 |
+
order (int): The order of the filter.
|
62 |
+
|
63 |
+
Returns:
|
64 |
+
array: The filtered audio data.
|
65 |
+
"""
|
66 |
+
# Get the filter coefficients
|
67 |
+
b, a = butter_bandpass(sr, order=order)
|
68 |
+
|
69 |
+
# Apply the filter to the data
|
70 |
y = lfilter(b, a, data)
|
71 |
+
|
72 |
return y
|
73 |
|
74 |
|
75 |
+
def filtered():
|
76 |
+
"""
|
77 |
+
This function reads an audio file, applies the bandpass filter to the audio data,
|
78 |
+
and then writes the filtered data to an output file.
|
|
|
79 |
|
80 |
+
Returns:
|
81 |
+
str: A success message if the audio is filtered correctly, otherwise an error message.
|
82 |
+
"""
|
83 |
+
try:
|
84 |
+
# Read the audio data from the input file
|
85 |
+
sr, data = read(input_file)
|
86 |
+
|
87 |
+
# Apply the bandpass filter to the audio data
|
88 |
+
filtered_data = butter_bandpass_filter(data, sr)
|
89 |
+
|
90 |
+
# Write the filtered data to the output file
|
91 |
+
write(output_file, sr, np.int16(filtered_data))
|
92 |
+
|
93 |
+
return "Filtered Audio Generated"
|
94 |
+
except Exception as e:
|
95 |
+
# If an error occurs, return an error message
|
96 |
+
return f"Error: {str(e)}"
|
97 |
+
|
98 |
+
|
99 |
+
# -----------------Record----------------- #
|
100 |
+
|
101 |
+
def record(audio):
|
102 |
+
"""
|
103 |
+
This function records audio and writes it to a .wav file.
|
104 |
|
105 |
+
Parameters:
|
106 |
+
audio (tuple): A tuple containing the sample rate and the audio data.
|
|
|
107 |
|
108 |
+
Returns:
|
109 |
+
str: A success message if the audio is recorded correctly, otherwise an error message.
|
110 |
+
"""
|
111 |
+
try:
|
112 |
+
# Check if the audio tuple contains exactly two elements
|
113 |
+
if len(audio) != 2:
|
114 |
+
return f"Error: Expected a tuple with 2 elements, but got {len(audio)}"
|
115 |
+
|
116 |
+
# Unpack the sample rate and data from the audio tuple
|
117 |
+
sr, data = audio
|
118 |
+
|
119 |
+
# Write the audio data to a .wav file
|
120 |
+
wavio.write("recorded.wav", data, sr)
|
121 |
+
|
122 |
+
# Call the filtered function to apply the bandpass filter to the audio data
|
123 |
+
filtered()
|
124 |
+
|
125 |
+
# Return a success message
|
126 |
+
return f"Audio receive correctly"
|
127 |
+
except Exception as e:
|
128 |
+
# If an error occurs, return an error message
|
129 |
+
return f"Error: {str(e)}"
|
130 |
+
|
131 |
|
132 |
# -----------------Frame----------------- #
|
133 |
|