bartman081523 commited on
Commit
1bb307a
1 Parent(s): 49a534f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -68
app.py CHANGED
@@ -2,88 +2,85 @@ import numpy as np
2
  import soundfile as sf
3
  from scipy import signal
4
  import gradio as gr
5
- import shutil
6
  import tempfile
7
- from IPython.display import Audio, display
8
 
9
  def generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
10
- # Parameters
11
- sample_rate = 44100 # sample rate in Hz
12
- num_samples = int(duration * sample_rate)
13
-
14
- # Generate pink noise
15
- pink_noise = np.random.normal(0, 1, num_samples)
16
- b, a = signal.butter(2, [0.002, 0.4], btype='band')
17
- pink_noise = signal.lfilter(b, a, pink_noise)
18
-
19
- # Apply band-pass filter to pink noise
20
- nyquist_rate = 0.5 * sample_rate
21
- low = lowcut / nyquist_rate
22
- high = highcut / nyquist_rate
23
- b, a = signal.butter(1, [low, high], btype='band')
24
- pink_noise = signal.lfilter(b, a, pink_noise)
25
-
26
- # Generate low-frequency rumble
27
- rumble_noise = np.random.normal(0, 1, num_samples)
28
- b, a = signal.butter(2, 0.002, btype='low')
29
- rumble_noise = signal.lfilter(b, a, rumble_noise)
30
-
31
- # Generate high-frequency hiss
32
- hiss_noise = np.random.normal(0, 1, num_samples)
33
- b, a = signal.butter(2, 0.4, btype='high')
34
- hiss_noise = signal.lfilter(b, a, hiss_noise)
35
-
36
- # Generate pops
37
- num_pops = int(duration * pop_rate)
38
- pop_times = np.random.randint(0, num_samples, num_pops)
39
- pop_data = np.zeros(num_samples)
40
- pop_data[pop_times] = np.random.normal(0, 1, num_pops) * 0.2 # random loudness
41
-
42
- # Create a simple low-pass filter to make the pops sound more like clicks
43
- b, a = signal.butter(4, 0.1)
44
- pop_data = signal.lfilter(b, a, pop_data)
45
-
46
- # Combine the noises and pops
47
- vinyl_sound = noise_ratio * (pink_noise + 0.05 * rumble_noise + 0.05 * hiss_noise) + (1 - noise_ratio) * pop_data
48
-
49
- # Normalize to between -1 and 1
50
- vinyl_sound /= np.max(np.abs(vinyl_sound))
51
-
52
- return vinyl_sound.astype(np.float32), sample_rate
53
 
54
  def convert_to_wav(data, sample_rate):
55
- # Normalize to between -1 and 1
56
- data /= np.max(np.abs(data))
57
-
58
- # Save to a temporary .wav file
59
- temp_file = tempfile.mktemp(suffix=".wav")
60
- sf.write(temp_file, data, sample_rate)
61
-
62
- return temp_file
63
 
64
  def play_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
65
  data, sample_rate = generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate)
66
- display(Audio(data, rate=sample_rate))
 
 
67
 
68
  def download_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
69
- data, sample_rate = generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate)
70
  temp_file = convert_to_wav(data, sample_rate)
71
  shutil.move(temp_file, "download.wav")
72
  return "download.wav"
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  iface = gr.Interface(
75
- fn=None,
76
- inputs=[
77
- gr.inputs.Slider(minimum=0, maximum=1, default=0.001, step=0.001, label="Noise Ratio"),
78
- gr.inputs.Slider(minimum=20, maximum=20000, default=300, step=10, label="Lowcut Frequency (Hz)"),
79
- gr.inputs.Slider(minimum=20, maximum=20000, default=5000, step=10, label="Highcut Frequency (Hz)"),
80
- gr.inputs.Slider(minimum=1, maximum=60, default=10, step=1, label="Duration (seconds)"),
81
- gr.inputs.Slider(minimum=1, maximum=100, default=10, step=1, label="Pop Rate (pops per second)")
82
- ],
83
- outputs=[
84
- gr.outputs.Button(label="Play Vinyl Sound", type="button", callback=play_sound),
85
- gr.outputs.Button(label="Download Vinyl Sound", type="button", callback=download_sound)
86
- ],
87
  title="Vinyl Sound Generator",
88
  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.",
89
  examples=[
@@ -93,4 +90,5 @@ iface = gr.Interface(
93
  ]
94
  )
95
 
96
- iface.launch()
 
 
2
  import soundfile as sf
3
  from scipy import signal
4
  import gradio as gr
 
5
  import tempfile
6
+ import shutil
7
 
8
  def generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
9
+ # Code to generate vinyl sound
10
+ # ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  def convert_to_wav(data, sample_rate):
13
+ # Code to convert audio data to WAV file
14
+ # ...
 
 
 
 
 
 
15
 
16
  def play_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
17
  data, sample_rate = generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate)
18
+ temp_file = convert_to_wav(data, sample_rate)
19
+ audio = gr.outputs.Audio(temp_file, label="Generated Vinyl Sound")
20
+ return audio
21
 
22
  def download_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
23
+ data, sample_rate = generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate)
24
  temp_file = convert_to_wav(data, sample_rate)
25
  shutil.move(temp_file, "download.wav")
26
  return "download.wav"
27
 
28
+ # Custom HTML template for the UI
29
+ html_template = """
30
+ <div style="padding: 20px;">
31
+ <h2>Vinyl Sound Generator</h2>
32
+ <p>Adjust the parameters to generate a synthetic vinyl sound:</p>
33
+ <form method="POST" action="/" target="_blank">
34
+ <label for="noise_ratio">Noise Ratio:</label>
35
+ <input type="range" id="noise_ratio" name="noise_ratio" min="0" max="1" step="0.001" value="0.001">
36
+ <br><br>
37
+ <label for="lowcut">Lowcut Frequency (Hz):</label>
38
+ <input type="range" id="lowcut" name="lowcut" min="20" max="20000" step="10" value="300">
39
+ <br><br>
40
+ <label for="highcut">Highcut Frequency (Hz):</label>
41
+ <input type="range" id="highcut" name="highcut" min="20" max="20000" step="10" value="5000">
42
+ <br><br>
43
+ <label for="duration">Duration (seconds):</label>
44
+ <input type="range" id="duration" name="duration" min="1" max="60" step="1" value="10">
45
+ <br><br>
46
+ <label for="pop_rate">Pop Rate (pops per second):</label>
47
+ <input type="range" id="pop_rate" name="pop_rate" min="1" max="100" step="1" value="10">
48
+ <br><br>
49
+ <input type="submit" value="Generate Vinyl Sound">
50
+ </form>
51
+ <br>
52
+ <audio id="audio" controls style="display: none;"></audio>
53
+ <button onclick="playAudio()">Play Vinyl Sound</button>
54
+ <a href="download.wav" download><button>Download Vinyl Sound</button></a>
55
+ <script>
56
+ function playAudio() {
57
+ var audio = document.getElementById("audio");
58
+ audio.src = "{sound_url}";
59
+ audio.play();
60
+ }
61
+ </script>
62
+ </div>
63
+ """
64
+
65
+ def vinyl_sound_app(noise_ratio, lowcut, highcut, duration, pop_rate):
66
+
67
+ # Generate the vinyl sound
68
+ data, sample_rate = generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate)
69
+
70
+ # Convert audio data to WAV file
71
+ temp_file = convert_to_wav(data, sample_rate)
72
+
73
+ # Update the HTML template with the sound URL
74
+ html_template_with_sound_url = html_template.replace("{sound_url}", temp_file)
75
+
76
+ # Return the HTML template
77
+ return html_template_with_sound_url
78
+
79
+ # Define the Gradio interface
80
  iface = gr.Interface(
81
+ fn=vinyl_sound_app,
82
+ inputs=["slider", "slider", "slider", "slider", "slider"],
83
+ outputs="html",
 
 
 
 
 
 
 
 
 
84
  title="Vinyl Sound Generator",
85
  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.",
86
  examples=[
 
90
  ]
91
  )
92
 
93
+ # Launch the interface
94
+ iface.launch(inbrowser=True)