bartman081523 commited on
Commit
42e062f
1 Parent(s): 9380a48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -58
app.py CHANGED
@@ -2,9 +2,7 @@ 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
- import simpleaudio as sa
8
 
9
  def generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
10
  # Parameters
@@ -61,73 +59,37 @@ def convert_to_wav(data, sample_rate):
61
 
62
  return temp_file
63
 
64
- def play_wav(noise_ratio, lowcut, highcut, duration, pop_rate):
65
- data, sample_rate = generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate)
66
- play_obj = sa.play_buffer((data * 32767).astype(np.int16), 1, 2, sample_rate)
67
- play_obj.wait_done()
68
-
69
- def download_wav(noise_ratio, lowcut, highcut, duration, pop_rate):
70
- data, sample_rate = generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate)
71
- temp_file = convert_to_wav(data, sample_rate)
72
- shutil.move(temp_file, "download.wav")
73
- return "download.wav"
74
-
75
- # HTML template for the custom UI
76
- html_template = """
77
- <div style="padding: 20px;">
78
- <h2>Vinyl Sound Generator</h2>
79
- <p>Adjust the parameters to generate a synthetic vinyl sound:</p>
80
- <form method="POST" action="/" target="_blank">
81
- <label for="noise_ratio">Noise Ratio:</label>
82
- <input type="range" id="noise_ratio" name="noise_ratio" min="0" max="1" step="0.001" value="0.001">
83
- <br><br>
84
- <label for="lowcut">Lowcut Frequency (Hz):</label>
85
- <input type="range" id="lowcut" name="lowcut" min="20" max="20000" step="10" value="300">
86
- <br><br>
87
- <label for="highcut">Highcut Frequency (Hz):</label>
88
- <input type="range" id="highcut" name="highcut" min="20" max="20000" step="10" value="5000">
89
- <br><br>
90
- <label for="duration">Duration (seconds):</label>
91
- <input type="range" id="duration" name="duration" min="1" max="60" step="1" value="10">
92
- <br><br>
93
- <label for="pop_rate">Pop Rate (pops per second):</label>
94
- <input type="range" id="pop_rate" name="pop_rate" min="1" max="100" step="1" value="10">
95
- <br><br>
96
- <input type="submit" value="Generate Vinyl Sound">
97
- </form>
98
- <br>
99
- <button onclick="playSound()">Play Vinyl Sound</button>
100
- </div>
101
-
102
- <script>
103
- function playSound() {
104
- var audio = new Audio("generated_vinyl_sound.wav");
105
- audio.play();
106
- }
107
- </script>
108
- """
109
-
110
  def vinyl_sound_app(noise_ratio, lowcut, highcut, duration, pop_rate):
111
  data, sample_rate = generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate)
112
  temp_file = convert_to_wav(data, sample_rate)
113
- shutil.move(temp_file, "generated_vinyl_sound.wav")
114
- return data
 
 
 
 
 
115
 
116
  iface = gr.Interface(
117
  fn=vinyl_sound_app,
118
- inputs=["slider", "slider", "slider", "slider", "slider"],
119
- outputs=[gr.outputs.Audio(label="Generated Vinyl Sound"), gr.outputs.Button(label="Download Vinyl Sound")],
 
 
 
 
 
 
 
 
 
120
  title="Vinyl Sound Generator",
121
  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.",
122
  examples=[
123
  [0.001, 300, 5000, 10, 10],
124
  [0.002, 500, 4000, 15, 20],
125
  [0.005, 200, 6000, 20, 30]
126
- ],
127
- allow_flagging=False,
128
- layout="horizontal",
129
- capture_session=True,
130
- template=html_template
131
  )
132
 
133
- iface.launch(inbrowser=True)
 
2
  import soundfile as sf
3
  from scipy import signal
4
  import gradio as gr
 
5
  import tempfile
 
6
 
7
  def generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate):
8
  # Parameters
 
59
 
60
  return temp_file
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  def vinyl_sound_app(noise_ratio, lowcut, highcut, duration, pop_rate):
63
  data, sample_rate = generate_vinyl_sound(noise_ratio, lowcut, highcut, duration, pop_rate)
64
  temp_file = convert_to_wav(data, sample_rate)
65
+ return temp_file
66
+
67
+ def play_sound(file_path):
68
+ with open(file_path, "rb") as f:
69
+ audio_data = f.read()
70
+ audio_html = f'<audio controls src="data:audio/wav;base64,{audio_data.encode("base64")}"></audio>'
71
+ return audio_html
72
 
73
  iface = gr.Interface(
74
  fn=vinyl_sound_app,
75
+ inputs=[
76
+ gr.inputs.Slider(minimum=0, maximum=1, default=0.001, step=0.001, label="Noise Ratio"),
77
+ gr.inputs.Slider(minimum=20, maximum=20000, default=300, step=10, label="Lowcut Frequency (Hz)"),
78
+ gr.inputs.Slider(minimum=20, maximum=20000, default=5000, step=10, label="Highcut Frequency (Hz)"),
79
+ gr.inputs.Slider(minimum=1, maximum=60, default=10, step=1, label="Duration (seconds)"),
80
+ gr.inputs.Slider(minimum=1, maximum=100, default=10, step=1, label="Pop Rate (pops per second)")
81
+ ],
82
+ outputs=[
83
+ gr.outputs.HTML(label="Generated Vinyl Sound", type="html", html=True),
84
+ gr.outputs.File(label="Download Vinyl Sound", type="auto", convert=convert_to_wav)
85
+ ],
86
  title="Vinyl Sound Generator",
87
  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.",
88
  examples=[
89
  [0.001, 300, 5000, 10, 10],
90
  [0.002, 500, 4000, 15, 20],
91
  [0.005, 200, 6000, 20, 30]
92
+ ]
 
 
 
 
93
  )
94
 
95
+ iface.launch()