Spaces:
Running
on
T4
Running
on
T4
Update spectro.py
Browse files- spectro.py +25 -1
spectro.py
CHANGED
@@ -182,4 +182,28 @@ def mp3_bytes_from_wav_bytes(wav_bytes: io.BytesIO) -> io.BytesIO:
|
|
182 |
sound = pydub.AudioSegment.from_wav(wav_bytes)
|
183 |
sound.export(mp3_bytes, format="mp3")
|
184 |
mp3_bytes.seek(0)
|
185 |
-
return mp3_bytes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
sound = pydub.AudioSegment.from_wav(wav_bytes)
|
183 |
sound.export(mp3_bytes, format="mp3")
|
184 |
mp3_bytes.seek(0)
|
185 |
+
return mp3_bytes
|
186 |
+
|
187 |
+
def image_from_spectrogram(spectrogram: np.ndarray, max_volume: float = 50, power_for_image: float = 0.25) -> Image.Image:
|
188 |
+
"""
|
189 |
+
Compute a spectrogram image from a spectrogram magnitude array.
|
190 |
+
"""
|
191 |
+
# Apply the power curve
|
192 |
+
data = np.power(spectrogram, power_for_image)
|
193 |
+
|
194 |
+
# Rescale to 0-255
|
195 |
+
data = data * 255 / max_volume
|
196 |
+
|
197 |
+
# Invert
|
198 |
+
data = 255 - data
|
199 |
+
|
200 |
+
# Convert to a PIL image
|
201 |
+
image = Image.fromarray(data.astype(np.uint8))
|
202 |
+
|
203 |
+
# Flip Y
|
204 |
+
image = image.transpose(Image.FLIP_TOP_BOTTOM)
|
205 |
+
|
206 |
+
# Convert to RGB
|
207 |
+
image = image.convert("RGB")
|
208 |
+
|
209 |
+
return image
|