Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import librosa
|
| 3 |
+
import librosa.display
|
| 4 |
+
import numpy as np
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
|
| 7 |
+
def visualize_audio(file):
|
| 8 |
+
# Charger l'audio
|
| 9 |
+
y, sr = librosa.load(file, sr=None)
|
| 10 |
+
|
| 11 |
+
# Transformer le signal en spectre de fréquences
|
| 12 |
+
D = librosa.stft(y) # Transformée de Fourier
|
| 13 |
+
magnitude, phase = np.abs(D), np.angle(D)
|
| 14 |
+
|
| 15 |
+
# Créer les figures pour affichage
|
| 16 |
+
fig, ax = plt.subplots(3, 1, figsize=(10, 12))
|
| 17 |
+
|
| 18 |
+
# 1. Affichage de la forme d'onde
|
| 19 |
+
ax[0].plot(y, color="blue")
|
| 20 |
+
ax[0].set_title("Forme d'onde")
|
| 21 |
+
ax[0].set_xlabel("Temps (échantillons)")
|
| 22 |
+
ax[0].set_ylabel("Amplitude")
|
| 23 |
+
|
| 24 |
+
# 2. Affichage du spectrogramme
|
| 25 |
+
librosa.display.specshow(librosa.amplitude_to_db(magnitude, ref=np.max), sr=sr, y_axis='log', x_axis='time', ax=ax[1])
|
| 26 |
+
ax[1].set_title("Spectrogramme (log-magnitude)")
|
| 27 |
+
ax[1].set_xlabel("Temps (s)")
|
| 28 |
+
ax[1].set_ylabel("Fréquence (Hz)")
|
| 29 |
+
|
| 30 |
+
# 3. Affichage de la phase
|
| 31 |
+
librosa.display.specshow(phase, sr=sr, y_axis='log', x_axis='time', ax=ax[2], cmap='twilight')
|
| 32 |
+
ax[2].set_title("Phase (FFT)")
|
| 33 |
+
ax[2].set_xlabel("Temps (s)")
|
| 34 |
+
ax[2].set_ylabel("Fréquence (Hz)")
|
| 35 |
+
|
| 36 |
+
# Sauvegarde de l'image
|
| 37 |
+
plt.tight_layout()
|
| 38 |
+
image_path = "audio_visualization.png"
|
| 39 |
+
plt.savefig(image_path)
|
| 40 |
+
plt.close()
|
| 41 |
+
|
| 42 |
+
return image_path
|
| 43 |
+
|
| 44 |
+
# Interface Gradio pour tester
|
| 45 |
+
iface = gr.Interface(
|
| 46 |
+
fn=visualize_audio,
|
| 47 |
+
inputs=gr.Audio(type="filepath"),
|
| 48 |
+
outputs="image",
|
| 49 |
+
title="Visualisation Audio (Waveform, Spectrogramme, Phase)"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
iface.launch()
|