bartman081523
commited on
Commit
·
c0b1f61
1
Parent(s):
1bb307a
Update app.py
Browse files
app.py
CHANGED
@@ -2,85 +2,86 @@ import numpy as np
|
|
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 |
-
#
|
10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def convert_to_wav(data, sample_rate):
|
13 |
-
#
|
14 |
-
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
temp_file
|
19 |
-
audio = gr.outputs.Audio(temp_file, label="Generated Vinyl Sound")
|
20 |
-
return audio
|
21 |
|
22 |
-
|
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 |
-
|
|
|
71 |
temp_file = convert_to_wav(data, sample_rate)
|
|
|
|
|
72 |
|
73 |
-
|
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=
|
82 |
-
inputs=[
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,5 +91,4 @@ iface = gr.Interface(
|
|
90 |
]
|
91 |
)
|
92 |
|
93 |
-
|
94 |
-
iface.launch(inbrowser=True)
|
|
|
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_and_download_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
|
65 |
+
data, sample_rate = generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate)
|
66 |
temp_file = convert_to_wav(data, sample_rate)
|
67 |
+
audio = Audio(temp_file)
|
68 |
+
display(audio)
|
69 |
|
70 |
+
return audio
|
|
|
|
|
|
|
|
|
71 |
|
|
|
72 |
iface = gr.Interface(
|
73 |
+
fn=play_and_download_sound,
|
74 |
+
inputs=[
|
75 |
+
gr.inputs.Slider(minimum=0, maximum=1, default=0.001, step=0.001, label="Noise Ratio"),
|
76 |
+
gr.inputs.Slider(minimum=20, maximum=20000, default=300, step=10, label="Lowcut Frequency (Hz)"),
|
77 |
+
gr.inputs.Slider(minimum=20, maximum=20000, default=5000, step=10, label="Highcut Frequency (Hz)"),
|
78 |
+
gr.inputs.Slider(minimum=1, maximum=60, default=10, step=1, label="Duration (seconds)"),
|
79 |
+
gr.inputs.Slider(minimum=1, maximum=100, default=10, step=1, label="Pop Rate (pops per second)")
|
80 |
+
],
|
81 |
+
outputs=[
|
82 |
+
gr.outputs.Audio(label="Vinyl Sound"),
|
83 |
+
gr.outputs.Button(label="Download Vinyl Sound", type="button", callback=download_sound)
|
84 |
+
],
|
85 |
title="Vinyl Sound Generator",
|
86 |
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.",
|
87 |
examples=[
|
|
|
91 |
]
|
92 |
)
|
93 |
|
94 |
+
iface.launch()
|
|