Spaces:
Sleeping
Sleeping
| """Spectrogram helper for wav files.""" | |
| import tempfile | |
| import numpy as np | |
| import librosa | |
| import librosa.display | |
| import matplotlib | |
| matplotlib.use("Agg") | |
| import matplotlib.pyplot as plt | |
| def wav_to_spectrogram_image(wav_path: str | None, title: str = "") -> str | None: | |
| """Convert wav to mel spectrogram png; return temp file path.""" | |
| if not wav_path: | |
| return None | |
| try: | |
| y, sr = librosa.load(wav_path, sr=16000, mono=True) | |
| mel = librosa.feature.melspectrogram(y=y, sr=sr, n_fft=1024, hop_length=256, n_mels=80) | |
| mel_db = librosa.power_to_db(mel, ref=np.max) | |
| fig, ax = plt.subplots(figsize=(3, 2)) | |
| librosa.display.specshow(mel_db, sr=sr, hop_length=256, x_axis="time", y_axis="mel", ax=ax, cmap="magma") | |
| if title: | |
| ax.set_title(title[:40]) | |
| plt.tight_layout() | |
| path = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name | |
| plt.savefig(path, dpi=80, bbox_inches="tight") | |
| plt.close() | |
| return path | |
| except Exception: | |
| return None | |