neuralworm commited on
Commit
9e1277b
·
verified ·
1 Parent(s): 1766824

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -12,7 +12,7 @@ def generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate, rpm):
12
  sample_rate = 44100 # sample rate in Hz
13
  num_samples = int(duration * sample_rate)
14
 
15
- # Calculate wow and flutter based on RPM
16
  wow_freq = rpm / 60 * 0.1 # Example: 0.1 Hz variation for 33 1/3 RPM
17
  flutter_freq = np.random.uniform(1, 10) # Flutter frequency in Hz
18
  wow_flutter = 0.001 * np.sin(2 * np.pi * wow_freq * np.arange(num_samples) / sample_rate) + \
@@ -21,7 +21,7 @@ def generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate, rpm):
21
  # Generate pink noise (optimized using faster method)
22
  pink_noise = np.random.normal(0, 1, num_samples)
23
  b, a = signal.butter(2, [0.002, 0.4], btype='band')
24
- pink_noise = signal.filtfilt(b, a, pink_noise) # Use filtfilt for zero-phase filtering
25
 
26
  # Apply band-pass filter to pink noise (optimized using sosfilt)
27
  nyquist_rate = 0.5 * sample_rate
@@ -30,10 +30,10 @@ def generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate, rpm):
30
  sos = signal.butter(1, [low, high], btype='band', output='sos')
31
  pink_noise = signal.sosfilt(sos, pink_noise)
32
 
33
- # Generate low-frequency rumble (optimized using sosfilt)
34
  rumble_noise = np.random.normal(0, 1, num_samples)
35
- sos = signal.butter(2, 0.002, btype='low', output='sos')
36
- rumble_noise = signal.sosfilt(sos, rumble_noise)
37
 
38
  # Generate high-frequency hiss (optimized using sosfilt)
39
  hiss_noise = np.random.normal(0, 1, num_samples)
@@ -44,16 +44,16 @@ def generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate, rpm):
44
  num_pops = int(duration * pop_rate)
45
  pop_times = np.random.randint(0, num_samples, num_pops)
46
  pop_data = np.zeros(num_samples)
47
- pop_amplitudes = np.random.uniform(0.05, 0.2, num_pops) # Vary pop amplitudes
48
- pop_data[pop_times] = pop_amplitudes # Apply random loudness
49
 
50
  # Apply pop filter (optimized by applying to the entire signal at once)
51
- pop_filter_freq = np.random.uniform(0.05, 0.2) # Vary filter frequency
52
  b, a = signal.butter(4, pop_filter_freq)
53
  pop_data = signal.lfilter(b, a, pop_data)
54
 
55
  # Combine the noises and pops
56
- vinyl_sound = noise_ratio * (pink_noise + 0.05 * rumble_noise + 0.05 * hiss_noise) + (1 - noise_ratio) * pop_data
57
 
58
  # Apply wow and flutter
59
  vinyl_sound = vinyl_sound * (1 + wow_flutter)
@@ -89,7 +89,7 @@ iface = gr.Interface(
89
  gr.inputs.Slider(minimum=20, maximum=20000, default=5000, step=10, label="Highcut Frequency (Hz)"),
90
  gr.inputs.Slider(minimum=1, maximum=600, default=30, step=1, label="Duration (seconds)"),
91
  gr.inputs.Slider(minimum=1, maximum=180, default=10, step=1, label="Pop Rate (pops per second)"),
92
- gr.inputs.Dropdown([33.33, 45], default=33.33, label="RPM")
93
  ],
94
  outputs=[
95
  gr.outputs.Audio(type="numpy", label="Vinyl Sound"),
@@ -104,7 +104,8 @@ iface = gr.Interface(
104
  description="Generate a synthetic vinyl sound using pink noise, rumble, hiss, and pops. Adjust the noise ratio, bandpass frequencies, duration, pop rate, and RPM to modify the sound.",
105
  examples=[
106
  [0.0005, 300, 5000, 30, 10, 33.33], # Example for 33 1/3 RPM
107
- [0.001, 500, 4000, 30, 50, 45] # Example for 45 RPM
 
108
  ]
109
  )
110
 
 
12
  sample_rate = 44100 # sample rate in Hz
13
  num_samples = int(duration * sample_rate)
14
 
15
+ # Calculate wow and flutter based on RPM (more realistic variations)
16
  wow_freq = rpm / 60 * 0.1 # Example: 0.1 Hz variation for 33 1/3 RPM
17
  flutter_freq = np.random.uniform(1, 10) # Flutter frequency in Hz
18
  wow_flutter = 0.001 * np.sin(2 * np.pi * wow_freq * np.arange(num_samples) / sample_rate) + \
 
21
  # Generate pink noise (optimized using faster method)
22
  pink_noise = np.random.normal(0, 1, num_samples)
23
  b, a = signal.butter(2, [0.002, 0.4], btype='band')
24
+ pink_noise = signal.filtfilt(b, a, pink_noise)
25
 
26
  # Apply band-pass filter to pink noise (optimized using sosfilt)
27
  nyquist_rate = 0.5 * sample_rate
 
30
  sos = signal.butter(1, [low, high], btype='band', output='sos')
31
  pink_noise = signal.sosfilt(sos, pink_noise)
32
 
33
+ # Generate low-frequency rumble (optimized using sosfilt, more pronounced)
34
  rumble_noise = np.random.normal(0, 1, num_samples)
35
+ sos = signal.butter(2, 0.005, btype='low', output='sos') # Lower cutoff for more rumble
36
+ rumble_noise = signal.sosfilt(sos, rumble_noise) * 0.1 # Adjust amplitude
37
 
38
  # Generate high-frequency hiss (optimized using sosfilt)
39
  hiss_noise = np.random.normal(0, 1, num_samples)
 
44
  num_pops = int(duration * pop_rate)
45
  pop_times = np.random.randint(0, num_samples, num_pops)
46
  pop_data = np.zeros(num_samples)
47
+ pop_amplitudes = np.random.uniform(0.05, 0.2, num_pops)
48
+ pop_data[pop_times] = pop_amplitudes
49
 
50
  # Apply pop filter (optimized by applying to the entire signal at once)
51
+ pop_filter_freq = np.random.uniform(0.05, 0.2)
52
  b, a = signal.butter(4, pop_filter_freq)
53
  pop_data = signal.lfilter(b, a, pop_data)
54
 
55
  # Combine the noises and pops
56
+ vinyl_sound = noise_ratio * (pink_noise + 0.1 * rumble_noise + 0.05 * hiss_noise) + (1 - noise_ratio) * pop_data
57
 
58
  # Apply wow and flutter
59
  vinyl_sound = vinyl_sound * (1 + wow_flutter)
 
89
  gr.inputs.Slider(minimum=20, maximum=20000, default=5000, step=10, label="Highcut Frequency (Hz)"),
90
  gr.inputs.Slider(minimum=1, maximum=600, default=30, step=1, label="Duration (seconds)"),
91
  gr.inputs.Slider(minimum=1, maximum=180, default=10, step=1, label="Pop Rate (pops per second)"),
92
+ gr.inputs.Number(default=33.33, label="RPM")
93
  ],
94
  outputs=[
95
  gr.outputs.Audio(type="numpy", label="Vinyl Sound"),
 
104
  description="Generate a synthetic vinyl sound using pink noise, rumble, hiss, and pops. Adjust the noise ratio, bandpass frequencies, duration, pop rate, and RPM to modify the sound.",
105
  examples=[
106
  [0.0005, 300, 5000, 30, 10, 33.33], # Example for 33 1/3 RPM
107
+ [0.001, 500, 4000, 30, 50, 45.0], # Example for 45 RPM
108
+ [0.0008, 400, 6000, 20, 15, 78.0] # Example for 78 RPM
109
  ]
110
  )
111