Awell00 commited on
Commit
ab96498
·
1 Parent(s): 713f3f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -28
app.py CHANGED
@@ -21,48 +21,113 @@ sample_rate = 44100
21
  amplitude_scaling_factor = 10.0
22
 
23
 
24
- # -----------------Record----------------- #
25
 
26
- def record(audio):
27
- try:
28
- sr, data = audio
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
- # -----------------Filter----------------- #
37
-
38
- def butter_bandpass(lowcut, highcut, sr, order=5):
 
39
  nyquist = 0.5 * sr
40
- low = lowcut / nyquist
41
- high = highcut / nyquist
42
- coef = butter(order, [low, high], btype='band')
43
- b = coef[0]
44
- a = coef[1]
 
 
 
 
 
 
 
45
  return b, a
46
 
47
 
48
- def butter_bandpass_filter(data, lowcut, highcut, sr, order=5):
49
- b, a = butter_bandpass(lowcut, highcut, sr, order=order)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  y = lfilter(b, a, data)
 
51
  return y
52
 
53
 
54
- def main():
55
- input_file = 'recorded.wav'
56
- output_file = 'output_filtered_receiver.wav'
57
- lowcut = 17500
58
- highcut = 19500
59
 
60
- sr, data = read(input_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- filtered_data = butter_bandpass_filter(data, lowcut, highcut, sr)
63
- write(output_file, sr, np.int16(filtered_data))
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