Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
from scipy.io.wavfile import
|
4 |
import tempfile
|
5 |
|
6 |
-
def
|
7 |
-
#
|
8 |
-
sample_rate
|
9 |
-
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
t = np.linspace(0, duration_per_burst, int(sample_rate * duration_per_burst), endpoint=False)
|
30 |
-
low_freq_wave = []
|
31 |
-
|
32 |
-
for freq in vibration_pattern:
|
33 |
-
if freq > 0:
|
34 |
-
wave = 0.5 * np.sin(2 * np.pi * freq * t) # Generate sine wave
|
35 |
-
else:
|
36 |
-
wave = np.zeros_like(t) # Silence
|
37 |
-
low_freq_wave.extend(wave)
|
38 |
-
|
39 |
-
# Convert to 16-bit PCM
|
40 |
-
low_freq_wave = (np.array(low_freq_wave) * 32767).astype(np.int16)
|
41 |
-
|
42 |
-
# Save the new simulated vibration audio to a temporary file
|
43 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
44 |
-
write(temp_file.name, sample_rate,
|
45 |
|
46 |
return temp_file.name
|
47 |
|
48 |
# Gradio Interface
|
49 |
interface = gr.Interface(
|
50 |
-
fn=
|
51 |
-
inputs=gr.Audio(label="Upload a WAV file"),
|
52 |
-
outputs=gr.Audio(label="Simulated Vibration
|
53 |
-
title="
|
54 |
-
description="
|
55 |
)
|
56 |
|
57 |
interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
+
from scipy.io.wavfile import write
|
4 |
import tempfile
|
5 |
|
6 |
+
def simulate_amber_alert_vibration(file):
|
7 |
+
# Sampling parameters
|
8 |
+
sample_rate = 44100 # Standard audio sample rate (44.1 kHz)
|
9 |
+
burst_frequency = 50 # Low frequency (50 Hz) for vibration-like sound
|
10 |
+
burst_duration = 0.5 # Duration of each burst in seconds
|
11 |
+
silence_duration = 0.5 # Silence between bursts in seconds
|
12 |
+
num_bursts = 5 # Number of vibration bursts
|
13 |
+
|
14 |
+
# Generate a single burst of low-frequency sound
|
15 |
+
t_burst = np.linspace(0, burst_duration, int(sample_rate * burst_duration), endpoint=False)
|
16 |
+
burst_wave = 0.5 * np.sin(2 * np.pi * burst_frequency * t_burst) # 50 Hz sine wave
|
17 |
+
|
18 |
+
# Generate silence
|
19 |
+
t_silence = np.linspace(0, silence_duration, int(sample_rate * silence_duration), endpoint=False)
|
20 |
+
silence_wave = np.zeros_like(t_silence)
|
21 |
+
|
22 |
+
# Combine bursts and silence to simulate vibration pattern
|
23 |
+
vibration_wave = np.concatenate([np.concatenate([burst_wave, silence_wave]) for _ in range(num_bursts)])
|
24 |
+
|
25 |
+
# Normalize to 16-bit PCM format
|
26 |
+
vibration_wave = (vibration_wave * 32767).astype(np.int16)
|
27 |
+
|
28 |
+
# Save to a temporary WAV file
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
|
30 |
+
write(temp_file.name, sample_rate, vibration_wave)
|
31 |
|
32 |
return temp_file.name
|
33 |
|
34 |
# Gradio Interface
|
35 |
interface = gr.Interface(
|
36 |
+
fn=simulate_amber_alert_vibration,
|
37 |
+
inputs=gr.Audio(label="Upload a WAV file (optional)", type="filepath"),
|
38 |
+
outputs=gr.Audio(label="Simulated Amber Alert Vibration"),
|
39 |
+
title="Amber Alert Vibration Simulation",
|
40 |
+
description="Simulate an Amber Alert-style vibration using low-frequency audio. Play the generated audio to feel the effect."
|
41 |
)
|
42 |
|
43 |
interface.launch()
|