bartman081523 commited on
Commit
416096f
1 Parent(s): ad6d586

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import soundfile as sf
3
+ from scipy import signal
4
+ import gradio as gr
5
+
6
+ def generate_vinyl_sound(noise_ratio, lowcut, highcut):
7
+ # Parameters
8
+ duration = 10.0 # duration of the sound in seconds
9
+ sample_rate = 44100 # sample rate in Hz
10
+ num_samples = int(duration * sample_rate)
11
+
12
+ # Generate pink noise
13
+ pink_noise = np.random.normal(0, 1, num_samples)
14
+ b, a = signal.butter(2, [0.002, 0.4], btype='band')
15
+ pink_noise = signal.lfilter(b, a, pink_noise)
16
+
17
+ # Apply band-pass filter to pink noise
18
+ nyquist_rate = 0.5 * sample_rate
19
+ low = lowcut / nyquist_rate
20
+ high = highcut / nyquist_rate
21
+ b, a = signal.butter(1, [low, high], btype='band')
22
+ pink_noise = signal.lfilter(b, a, pink_noise)
23
+
24
+ # Generate low-frequency rumble
25
+ rumble_noise = np.random.normal(0, 1, num_samples)
26
+ b, a = signal.butter(2, 0.002, btype='low')
27
+ rumble_noise = signal.lfilter(b, a, rumble_noise)
28
+
29
+ # Generate high-frequency hiss
30
+ hiss_noise = np.random.normal(0, 1, num_samples)
31
+ b, a = signal.butter(2, 0.4, btype='high')
32
+ hiss_noise = signal.lfilter(b, a, hiss_noise)
33
+
34
+ # Generate pops
35
+ pop_rate = 10 # average number of pops per second
36
+ num_pops = int(duration * pop_rate)
37
+ pop_times = np.random.randint(0, num_samples, num_pops)
38
+ pop_data = np.zeros(num_samples)
39
+ pop_data[pop_times] = np.random.normal(0, 1, num_pops) * 0.2 # random loudness
40
+
41
+ # Create a simple low-pass filter to make the pops sound more like clicks
42
+ b, a = signal.butter(4, 0.1)
43
+ pop_data = signal.lfilter(b, a, pop_data)
44
+
45
+ # Combine the noises and pops
46
+ vinyl_sound = noise_ratio * (pink_noise + 0.05 * rumble_noise + 0.05 * hiss_noise) + (1 - noise_ratio) * pop_data
47
+
48
+ # Normalize to between -1 and 1
49
+ vinyl_sound /= np.max(np.abs(vinyl_sound))
50
+
51
+ return vinyl_sound.astype(np.float32), sample_rate
52
+
53
+ iface = gr.Interface(
54
+ generate_vinyl_sound,
55
+ [
56
+ gr.inputs.Slider(minimum=0, maximum=1, default=0.7, step=0.1, label="Noise Ratio"),
57
+ gr.inputs.Slider(minimum=20, maximum=20000, default=300, step=10, label="Lowcut Frequency (Hz)"),
58
+ gr.inputs.Slider(minimum=20, maximum=20000, default=5000, step=10, label="Highcut Frequency (Hz)")
59
+ ],
60
+ gr.outputs.Audio(label="Generated Vinyl Sound"),
61
+ title="Vinyl Sound Generator",
62
+ description="Generate a synthetic vinyl sound using pink noise, rumble, hiss, and pops. Adjust the noise ratio and bandpass frequencies to modify the sound.",
63
+ examples=[
64
+ [0.7, 300, 5000],
65
+ [0.5, 500, 4000],
66
+ [0.8, 200, 6000]
67
+ ]
68
+ )
69
+
70
+ iface.launch()