Rahatara commited on
Commit
e7c1a47
·
verified ·
1 Parent(s): 9e4d172

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -44
app.py CHANGED
@@ -1,57 +1,43 @@
1
  import gradio as gr
2
  import numpy as np
3
- from scipy.io.wavfile import read, write
4
  import tempfile
5
 
6
- def simulate_vibration(file):
7
- # Read the uploaded WAV file
8
- sample_rate, data = read(file)
9
-
10
- # Convert to mono if stereo
11
- if len(data.shape) > 1:
12
- data = data.mean(axis=1)
13
-
14
- # Normalize the data
15
- data = data / np.max(np.abs(data))
16
-
17
- # Extract vibration pattern based on amplitude
18
- vibration_pattern = []
19
- for amp in data[::sample_rate // 10]: # Take 10 samples per second
20
- if abs(amp) > 0.5:
21
- vibration_pattern.append(40) # Strong vibration frequency (40 Hz)
22
- elif abs(amp) > 0.2:
23
- vibration_pattern.append(20) # Weak vibration frequency (20 Hz)
24
- else:
25
- vibration_pattern.append(0) # No vibration
26
-
27
- # Generate low-frequency vibration-like sound
28
- duration_per_burst = 0.1 # 0.1 seconds per vibration burst
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, low_freq_wave)
45
 
46
  return temp_file.name
47
 
48
  # Gradio Interface
49
  interface = gr.Interface(
50
- fn=simulate_vibration,
51
- inputs=gr.Audio(label="Upload a WAV file"),
52
- outputs=gr.Audio(label="Simulated Vibration WAV"),
53
- title="Simulate Vibrations Using Low-Frequency Audio",
54
- description="Upload a WAV file, and a low-frequency audio file will be generated to simulate vibrations."
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()