bartman081523 commited on
Commit
463cfd6
1 Parent(s): a4d4ae8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -16
app.py CHANGED
@@ -1,6 +1,9 @@
1
  import numpy as np
 
2
  from scipy import signal
3
  import gradio as gr
 
 
4
 
5
  def generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
6
  # Parameters
@@ -47,25 +50,40 @@ def generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
47
 
48
  return vinyl_sound.astype(np.float32), sample_rate
49
 
50
- def vinyl_sound_app(noise_ratio, lowcut, highcut, duration, pop_rate):
 
 
 
 
 
 
 
 
 
 
51
  data, sample_rate = generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate)
52
- return data, sample_rate
 
 
53
 
54
  iface = gr.Interface(
55
- fn=vinyl_sound_app,
56
- inputs=["slider", "slider", "slider", "slider", "slider"],
57
- outputs=["audio", "number"],
58
- title="Vinyl Sound Generator",
59
- 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.",
60
- examples=[
61
- [0.001, 300, 5000, 10, 10],
62
- [0.002, 500, 4000, 15, 20],
63
- [0.005, 200, 6000, 20, 30]
64
- ],
65
- allow_flagging=False,
66
- layout="horizontal",
67
- capture_session=True,
68
- live=False
 
 
69
  )
70
 
71
  iface.launch(inbrowser=True)
 
 
1
  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
 
8
  def generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
9
  # Parameters
 
50
 
51
  return vinyl_sound.astype(np.float32), sample_rate
52
 
53
+ def convert_to_wav(data, sample_rate):
54
+ # Normalize to between -1 and 1
55
+ data /= np.max(np.abs(data))
56
+
57
+ # Save to a temporary .wav file
58
+ temp_file = tempfile.mktemp(suffix=".wav")
59
+ sf.write(temp_file, data, sample_rate)
60
+
61
+ return temp_file
62
+
63
+ def download_wav(noise_ratio, lowcut, highcut, duration, pop_rate):
64
  data, sample_rate = generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate)
65
+ temp_file = convert_to_wav(data, sample_rate)
66
+ shutil.move(temp_file, "download.wav")
67
+ return "download.wav"
68
 
69
  iface = gr.Interface(
70
+ fn=download_wav,
71
+ inputs=[
72
+ gr.inputs.Slider(minimum=0, maximum=1, default=0.001, step=0.001, label="Noise Ratio"),
73
+ gr.inputs.Slider(minimum=20, maximum=20000, default=300, step=10, label="Lowcut Frequency (Hz)"),
74
+ gr.inputs.Slider(minimum=20, maximum=20000, default=5000, step=10, label="Highcut Frequency (Hz)"),
75
+ gr.inputs.Slider(minimum=1, maximum=60, default=10, step=1, label="Duration (seconds)"),
76
+ gr.inputs.Slider(minimum=1, maximum=100, default=10, step=1, label="Pop Rate (pops per second)")
77
+ ],
78
+ outputs="file",
79
+ title="Vinyl Sound Generator",
80
+ 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.",
81
+ examples=[
82
+ [0.001, 300, 5000, 10, 10],
83
+ [0.002, 500, 4000, 15, 20],
84
+ [0.005, 200, 6000, 20, 30]
85
+ ]
86
  )
87
 
88
  iface.launch(inbrowser=True)
89
+