Rahatara commited on
Commit
7f89414
·
verified ·
1 Parent(s): ce2ccd8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -47
app.py CHANGED
@@ -4,61 +4,70 @@ from scipy.io.wavfile import read, write
4
  import tempfile
5
 
6
  def dynamic_vibration_simulation(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 to range [-1, 1]
15
- data = data / np.max(np.abs(data))
16
-
17
- # Parameters for vibration simulation
18
- segment_duration = 0.1 # Analyze 0.1-second segments
19
- segment_length = int(sample_rate * segment_duration)
20
 
21
- # Initialize the low-frequency waveform
22
- low_freq_wave = []
 
 
 
 
 
 
 
 
23
 
24
- for i in range(0, len(data), segment_length):
25
- segment = data[i:i + segment_length]
26
 
27
- if len(segment) < segment_length:
28
- break # Skip incomplete segment at the end
29
 
30
- # Calculate amplitude (RMS value) of the segment
31
- rms_amplitude = np.sqrt(np.mean(segment**2))
32
 
33
- # Map amplitude to vibration parameters
34
- if rms_amplitude > 0.5:
35
- freq = 50 # Strong vibration frequency (50 Hz)
36
- duration = segment_duration
37
- elif rms_amplitude > 0.2:
38
- freq = 30 # Medium vibration frequency (30 Hz)
39
- duration = segment_duration
40
- else:
41
- freq = 0 # Silence (no vibration)
42
- duration = segment_duration
43
 
44
- # Generate waveform for this segment
45
- t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
46
- if freq > 0:
47
- wave = 0.5 * np.sin(2 * np.pi * freq * t)
48
- else:
49
- wave = np.zeros_like(t) # Silence
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- # Append the generated wave
52
- low_freq_wave.extend(wave)
53
-
54
- # Convert the waveform to 16-bit PCM
55
- low_freq_wave = (np.array(low_freq_wave) * 32767).astype(np.int16)
56
-
57
- # Save the generated waveform to a temporary file
58
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
59
- write(temp_file.name, sample_rate, low_freq_wave)
60
 
61
- return temp_file.name
 
 
 
 
62
 
63
  # Gradio Interface
64
  interface = gr.Interface(
 
4
  import tempfile
5
 
6
  def dynamic_vibration_simulation(file):
7
+ try:
8
+ # Read the uploaded WAV file
9
+ sample_rate, data = read(file)
10
+
11
+ # Debug: Print sample rate and shape
12
+ print(f"Sample Rate: {sample_rate}, Data Shape: {data.shape}")
 
 
 
 
 
 
 
13
 
14
+ # Convert to mono if stereo
15
+ if len(data.shape) > 1:
16
+ data = data.mean(axis=1)
17
+
18
+ # Normalize the data to range [-1, 1]
19
+ data = data / np.max(np.abs(data))
20
+
21
+ # Parameters for vibration simulation
22
+ segment_duration = 0.1 # Analyze 0.1-second segments
23
+ segment_length = int(sample_rate * segment_duration)
24
 
25
+ # Initialize the low-frequency waveform
26
+ low_freq_wave = []
27
 
28
+ for i in range(0, len(data), segment_length):
29
+ segment = data[i:i + segment_length]
30
 
31
+ if len(segment) < segment_length:
32
+ break # Skip incomplete segment at the end
33
 
34
+ # Calculate amplitude (RMS value) of the segment
35
+ rms_amplitude = np.sqrt(np.mean(segment**2))
36
+ print(f"RMS Amplitude for segment {i // segment_length}: {rms_amplitude}")
 
 
 
 
 
 
 
37
 
38
+ # Map amplitude to vibration parameters
39
+ if rms_amplitude > 0.5:
40
+ freq = 50 # Strong vibration frequency (50 Hz)
41
+ duration = segment_duration
42
+ elif rms_amplitude > 0.2:
43
+ freq = 30 # Medium vibration frequency (30 Hz)
44
+ duration = segment_duration
45
+ else:
46
+ freq = 0 # Silence (no vibration)
47
+ duration = segment_duration
48
+
49
+ # Generate waveform for this segment
50
+ t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
51
+ if freq > 0:
52
+ wave = 0.5 * np.sin(2 * np.pi * freq * t)
53
+ else:
54
+ wave = np.zeros_like(t) # Silence
55
+
56
+ # Append the generated wave
57
+ low_freq_wave.extend(wave)
58
+
59
+ # Convert the waveform to 16-bit PCM
60
+ low_freq_wave = (np.array(low_freq_wave) * 32767).astype(np.int16)
61
 
62
+ # Save the generated waveform to a temporary file
63
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
64
+ write(temp_file.name, sample_rate, low_freq_wave)
 
 
 
 
 
 
65
 
66
+ print("Output WAV file generated successfully.")
67
+ return temp_file.name
68
+ except Exception as e:
69
+ print(f"Error: {e}")
70
+ return "Error in processing the uploaded WAV file."
71
 
72
  # Gradio Interface
73
  interface = gr.Interface(