neuralworm commited on
Commit
1766824
·
verified ·
1 Parent(s): 0af716b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -26
app.py CHANGED
@@ -7,32 +7,38 @@ import tempfile
7
  from IPython.display import Audio, display
8
  from typing import Tuple
9
 
10
- def generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
11
  # Parameters
12
  sample_rate = 44100 # sample rate in Hz
13
  num_samples = int(duration * sample_rate)
14
 
15
- # Generate pink noise
 
 
 
 
 
 
16
  pink_noise = np.random.normal(0, 1, num_samples)
17
  b, a = signal.butter(2, [0.002, 0.4], btype='band')
18
- pink_noise = signal.lfilter(b, a, pink_noise)
19
 
20
- # Apply band-pass filter to pink noise
21
  nyquist_rate = 0.5 * sample_rate
22
  low = lowcut / nyquist_rate
23
  high = highcut / nyquist_rate
24
- b, a = signal.butter(1, [low, high], btype='band')
25
- pink_noise = signal.lfilter(b, a, pink_noise)
26
 
27
- # Generate low-frequency rumble
28
  rumble_noise = np.random.normal(0, 1, num_samples)
29
- b, a = signal.butter(2, 0.002, btype='low')
30
- rumble_noise = signal.lfilter(b, a, rumble_noise)
31
 
32
- # Generate high-frequency hiss
33
  hiss_noise = np.random.normal(0, 1, num_samples)
34
- b, a = signal.butter(2, 0.4, btype='high')
35
- hiss_noise = signal.lfilter(b, a, hiss_noise)
36
 
37
  # Generate pops with varying loudness and frequency
38
  num_pops = int(duration * pop_rate)
@@ -41,22 +47,23 @@ def generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
41
  pop_amplitudes = np.random.uniform(0.05, 0.2, num_pops) # Vary pop amplitudes
42
  pop_data[pop_times] = pop_amplitudes # Apply random loudness
43
 
44
- # Create a low-pass filter for clicks, with varying filter frequencies
45
- for i in range(num_pops):
46
- pop_filter_freq = np.random.uniform(0.05, 0.2) # Vary filter frequency
47
- b, a = signal.butter(4, pop_filter_freq)
48
-
49
- # Apply the filter to the entire pop_data array
50
- pop_data = signal.lfilter(b, a, pop_data)
51
 
52
  # Combine the noises and pops
53
  vinyl_sound = noise_ratio * (pink_noise + 0.05 * rumble_noise + 0.05 * hiss_noise) + (1 - noise_ratio) * pop_data
54
 
 
 
 
55
  # Normalize to between -1 and 1
56
  vinyl_sound /= np.max(np.abs(vinyl_sound))
57
 
58
  return vinyl_sound.astype(np.float32), sample_rate
59
 
 
60
  def convert_to_wav(data, sample_rate):
61
  # Normalize to between -1 and 1
62
  data /= np.max(np.abs(data))
@@ -67,11 +74,13 @@ def convert_to_wav(data, sample_rate):
67
 
68
  return temp_file
69
 
70
- def play_and_download_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
71
- data, sample_rate = generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate)
 
72
  temp_file = convert_to_wav(data, sample_rate)
73
  return temp_file, temp_file
74
 
 
75
  iface = gr.Interface(
76
  fn=play_and_download_sound,
77
  inputs=[
@@ -79,7 +88,8 @@ iface = gr.Interface(
79
  gr.inputs.Slider(minimum=20, maximum=20000, default=300, step=10, label="Lowcut Frequency (Hz)"),
80
  gr.inputs.Slider(minimum=20, maximum=20000, default=5000, step=10, label="Highcut Frequency (Hz)"),
81
  gr.inputs.Slider(minimum=1, maximum=600, default=30, step=1, label="Duration (seconds)"),
82
- gr.inputs.Slider(minimum=1, maximum=180, default=10, step=1, label="Pop Rate (pops per second)")
 
83
  ],
84
  outputs=[
85
  gr.outputs.Audio(type="numpy", label="Vinyl Sound"),
@@ -91,11 +101,10 @@ iface = gr.Interface(
91
  "link.click();}</script>")
92
  ],
93
  title="Vinyl Sound Generator",
94
- description="Generate a synthetic vinyl sound using pink noise, rumble, hiss, and pops. Adjust the noise ratio, bandpass frequencies, duration, and pop rate to modify the sound.",
95
  examples=[
96
- [0.0005, 300, 5000, 30, 10],
97
- [0.001, 500, 4000, 30, 50],
98
- [0.002, 200, 6000, 30, 100]
99
  ]
100
  )
101
 
 
7
  from IPython.display import Audio, display
8
  from typing import Tuple
9
 
10
+ def generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate, rpm):
11
  # Parameters
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) + \
19
+ 0.0005 * np.sin(2 * np.pi * flutter_freq * np.arange(num_samples) / sample_rate)
20
+
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
28
  low = lowcut / nyquist_rate
29
  high = highcut / nyquist_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)
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)
40
+ sos = signal.butter(2, 0.4, btype='high', output='sos')
41
+ hiss_noise = signal.sosfilt(sos, hiss_noise)
42
 
43
  # Generate pops with varying loudness and frequency
44
  num_pops = int(duration * pop_rate)
 
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)
60
+
61
  # Normalize to between -1 and 1
62
  vinyl_sound /= np.max(np.abs(vinyl_sound))
63
 
64
  return vinyl_sound.astype(np.float32), sample_rate
65
 
66
+
67
  def convert_to_wav(data, sample_rate):
68
  # Normalize to between -1 and 1
69
  data /= np.max(np.abs(data))
 
74
 
75
  return temp_file
76
 
77
+
78
+ def play_and_download_sound(noise_ratio, lowcut, highcut, duration, pop_rate, rpm):
79
+ data, sample_rate = generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate, rpm)
80
  temp_file = convert_to_wav(data, sample_rate)
81
  return temp_file, temp_file
82
 
83
+
84
  iface = gr.Interface(
85
  fn=play_and_download_sound,
86
  inputs=[
 
88
  gr.inputs.Slider(minimum=20, maximum=20000, default=300, step=10, label="Lowcut Frequency (Hz)"),
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"),
 
101
  "link.click();}</script>")
102
  ],
103
  title="Vinyl Sound Generator",
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