vitorcalvi commited on
Commit
ffc9ee0
·
1 Parent(s): 99e7048

first commit

Browse files
Files changed (2) hide show
  1. app.py +157 -0
  2. conda_create.sh +12 -0
app.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from scipy import signal
4
+ import soundfile as sf
5
+ import matplotlib.pyplot as plt
6
+ import io
7
+
8
+ def generate_test_tone(frequency, duration=1.0, sample_rate=44100):
9
+ t = np.linspace(0, duration, int(sample_rate * duration))
10
+ tone = np.sin(2 * np.pi * frequency * t)
11
+ return tone * np.hanning(len(tone))
12
+
13
+ def create_audiogram(left_ear_results, right_ear_results):
14
+ frequencies = [250, 500, 1000, 2000, 4000, 8000]
15
+ plt.figure(figsize=(10, 8))
16
+
17
+ plt.fill_between([125, 8000], -10, 25, color='#e6f3ff', alpha=0.3, label='Normal Hearing')
18
+ plt.fill_between([125, 8000], 25, 40, color='#b3d9ff', alpha=0.3, label='Mild Loss')
19
+ plt.fill_between([125, 8000], 40, 55, color='#80bfff', alpha=0.3, label='Moderate Loss')
20
+ plt.fill_between([125, 8000], 55, 70, color='#4da6ff', alpha=0.3, label='Moderate-Severe Loss')
21
+
22
+ plt.plot(frequencies, left_ear_results, 'x-', color='blue', label='Left Ear')
23
+ plt.plot(frequencies, right_ear_results, 'o-', color='red', label='Right Ear')
24
+
25
+ plt.xscale('log')
26
+ plt.xlim(125, 8000)
27
+ plt.ylim(70, -10)
28
+ plt.grid(True)
29
+ plt.xlabel('Frequency (Hz)')
30
+ plt.ylabel('Hearing Level (dB)')
31
+ plt.title('Audiogram Results')
32
+ plt.legend()
33
+
34
+ buf = io.BytesIO()
35
+ plt.savefig(buf, format='png')
36
+ plt.close()
37
+ return buf
38
+
39
+ def hearing_test(frequency, volume, ear_selection):
40
+ sample_rate = 44100
41
+ tone = generate_test_tone(float(frequency), 1.0, sample_rate)
42
+ volume_adjusted = tone * (10 ** (volume / 20))
43
+
44
+ stereo_tone = np.zeros((2, len(tone)))
45
+ if ear_selection == "Left":
46
+ stereo_tone[0] = volume_adjusted
47
+ elif ear_selection == "Right":
48
+ stereo_tone[1] = volume_adjusted
49
+ else:
50
+ stereo_tone[0] = stereo_tone[1] = volume_adjusted
51
+
52
+ output_path = f"test_tone_{frequency}Hz.wav"
53
+ sf.write(output_path, stereo_tone.T, sample_rate)
54
+ return output_path
55
+
56
+ def generate_audio(duration, selected_frequencies):
57
+ sample_rate = 44100
58
+ num_samples = int(float(duration) * sample_rate)
59
+ noise = np.random.normal(0, 1, num_samples)
60
+
61
+ if selected_frequencies:
62
+ frequencies = [int(f) for f in selected_frequencies]
63
+ for freq in frequencies:
64
+ depth = -40 if freq == 4000 else -30
65
+ width = freq / 10
66
+ nyquist = sample_rate / 2
67
+ freq_normalized = freq / nyquist
68
+ quality_factor = freq / width
69
+ b, a = signal.iirnotch(freq_normalized, quality_factor)
70
+ noise = signal.filtfilt(b, a, noise)
71
+ noise *= 10 ** (depth / 20)
72
+
73
+ noise = noise / np.max(np.abs(noise))
74
+ output_path = "notched_noise.wav"
75
+ sf.write(output_path, noise, sample_rate)
76
+ return output_path
77
+
78
+ def create_interface():
79
+ with gr.Blocks(title="Hearing Test & White Noise Generator") as app:
80
+ with gr.Tabs():
81
+ with gr.Tab("Hearing Test"):
82
+ gr.Markdown("## Hearing Test")
83
+ with gr.Row():
84
+ with gr.Column():
85
+ frequency = gr.Dropdown(
86
+ choices=["250", "500", "1000", "2000", "4000", "8000"],
87
+ value="1000",
88
+ label="Test Frequency (Hz)"
89
+ )
90
+ volume = gr.Slider(
91
+ minimum=-60,
92
+ maximum=0,
93
+ value=-20,
94
+ step=5,
95
+ label="Volume (dB)"
96
+ )
97
+ ear_select = gr.Radio(
98
+ choices=["Both", "Left", "Right"],
99
+ value="Both",
100
+ label="Ear Selection"
101
+ )
102
+ test_btn = gr.Button("Play Test Tone")
103
+ with gr.Column():
104
+ audio_output = gr.Audio(label="Test Tone")
105
+
106
+ with gr.Row():
107
+ with gr.Column():
108
+ left_thresholds = [gr.Number(value=0, label=f"{freq}Hz Left") for freq in [250, 500, 1000, 2000, 4000, 8000]]
109
+ with gr.Column():
110
+ right_thresholds = [gr.Number(value=0, label=f"{freq}Hz Right") for freq in [250, 500, 1000, 2000, 4000, 8000]]
111
+
112
+ generate_audiogram_btn = gr.Button("Generate Audiogram")
113
+ audiogram_output = gr.Image(label="Audiogram")
114
+
115
+ with gr.Tab("White Noise Generator"):
116
+ gr.Markdown("## Notched White Noise Generator")
117
+ with gr.Row():
118
+ with gr.Column():
119
+ duration = gr.Slider(
120
+ minimum=1,
121
+ maximum=30,
122
+ value=5,
123
+ step=1,
124
+ label="Duration (seconds)"
125
+ )
126
+ frequencies = gr.CheckboxGroup(
127
+ choices=["250", "500", "1000", "2000", "4000", "8000"],
128
+ label="Frequencies to Notch (Hz)",
129
+ value=["4000", "2000"]
130
+ )
131
+ generate_noise_btn = gr.Button("Generate Noise")
132
+ with gr.Column():
133
+ noise_output = gr.Audio(label="Generated Noise")
134
+
135
+ test_btn.click(
136
+ fn=hearing_test,
137
+ inputs=[frequency, volume, ear_select],
138
+ outputs=audio_output
139
+ )
140
+
141
+ generate_audiogram_btn.click(
142
+ fn=lambda *args: create_audiogram(args[:6], args[6:]).getvalue(),
143
+ inputs=left_thresholds + right_thresholds,
144
+ outputs=audiogram_output
145
+ )
146
+
147
+ generate_noise_btn.click(
148
+ fn=generate_audio,
149
+ inputs=[duration, frequencies],
150
+ outputs=noise_output
151
+ )
152
+
153
+ return app
154
+
155
+ if __name__ == "__main__":
156
+ app = create_interface()
157
+ app.launch(share=False)
conda_create.sh ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Create a new conda environment named 'noise-gen'
2
+ conda create -n noise-gen python=3.10
3
+
4
+ # Activate the environment
5
+ conda activate noise-gen
6
+
7
+ # Install required packages
8
+ conda install numpy scipy pip
9
+ pip install gradio soundfile
10
+
11
+ # Verify installation
12
+ python -c "import gradio; import numpy; import scipy; import soundfile"