Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -48,13 +48,18 @@ In this section, we'll look into the waveforms of multiple audios.
|
|
| 48 |
# https://github.com/gradio-app/gradio/issues/5469
|
| 49 |
@gr.on(inputs=[freq, freq2, amplitude], outputs=[audio, plots])
|
| 50 |
def plot_sine(freq, freq2, a):
|
| 51 |
-
sr =
|
| 52 |
ts = 1.0/sr # sampling interval
|
| 53 |
t = np.arange(0, 1, ts) # time vector
|
| 54 |
data = a * np.sin(2 * np.pi * freq * t) + a * np.sin(2 * np.pi * freq2 * t)
|
| 55 |
-
audio_path = "test.wav"
|
| 56 |
-
write(audio_path, sr, data)
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
fig, axes = plt.subplots(nrows=2, ncols=1, sharex=False)
|
| 59 |
ax_waveform = axes[0]
|
| 60 |
ax_spectrum = axes[1]
|
|
@@ -78,7 +83,7 @@ In this section, we'll look into the waveforms of multiple audios.
|
|
| 78 |
|
| 79 |
fig.tight_layout()
|
| 80 |
fig.savefig('foo.png')
|
| 81 |
-
return
|
| 82 |
button.click(plot_sine, inputs=[freq, freq2, amplitude], outputs=[audio, plots])
|
| 83 |
with gr.Tab("Spectrograms and Mel Spectrograms"):
|
| 84 |
gr.Markdown("""## Waveforms
|
|
|
|
| 48 |
# https://github.com/gradio-app/gradio/issues/5469
|
| 49 |
@gr.on(inputs=[freq, freq2, amplitude], outputs=[audio, plots])
|
| 50 |
def plot_sine(freq, freq2, a):
|
| 51 |
+
sr = 44100 # samples per second
|
| 52 |
ts = 1.0/sr # sampling interval
|
| 53 |
t = np.arange(0, 1, ts) # time vector
|
| 54 |
data = a * np.sin(2 * np.pi * freq * t) + a * np.sin(2 * np.pi * freq2 * t)
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# Normalize to [-1, 1]
|
| 57 |
+
data = data / np.max(np.abs(data))
|
| 58 |
+
|
| 59 |
+
# Convert to 16-bit integer PCM
|
| 60 |
+
data = (data * 32767).astype(np.int16)
|
| 61 |
+
audio_data = (sr, data)
|
| 62 |
+
|
| 63 |
fig, axes = plt.subplots(nrows=2, ncols=1, sharex=False)
|
| 64 |
ax_waveform = axes[0]
|
| 65 |
ax_spectrum = axes[1]
|
|
|
|
| 83 |
|
| 84 |
fig.tight_layout()
|
| 85 |
fig.savefig('foo.png')
|
| 86 |
+
return audio_data, fig
|
| 87 |
button.click(plot_sine, inputs=[freq, freq2, amplitude], outputs=[audio, plots])
|
| 88 |
with gr.Tab("Spectrograms and Mel Spectrograms"):
|
| 89 |
gr.Markdown("""## Waveforms
|