Update app.py
Browse files
app.py
CHANGED
|
@@ -11,9 +11,32 @@ import numpy as np
|
|
| 11 |
import soundfile as sf
|
| 12 |
|
| 13 |
def save_audio_mp3(audio_tuple, filename):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
sampling_rate, audio_data = audio_tuple
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
audio_bytes = np.array(audio_data, dtype=np.int16).tobytes()
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
audio_segment.export(filename, format="mp3")
|
| 18 |
|
| 19 |
return f"Audio saved successfully as {filename}"
|
|
|
|
| 11 |
import soundfile as sf
|
| 12 |
|
| 13 |
def save_audio_mp3(audio_tuple, filename):
|
| 14 |
+
#sampling_rate, audio_data = audio_tuple
|
| 15 |
+
#audio_bytes = np.array(audio_data, dtype=np.int16).tobytes()
|
| 16 |
+
#audio_segment = AudioSegment(audio_bytes, sample_width=2, frame_rate=sampling_rate, channels=1)
|
| 17 |
+
#audio_segment.export(filename, format="mp3")
|
| 18 |
sampling_rate, audio_data = audio_tuple
|
| 19 |
+
|
| 20 |
+
# Verificar la tasa de muestreo
|
| 21 |
+
if not isinstance(sampling_rate, int) or sampling_rate <= 0:
|
| 22 |
+
raise ValueError("La tasa de muestreo debe ser un entero positivo.")
|
| 23 |
+
|
| 24 |
+
# Verificar los datos de audio
|
| 25 |
+
if not isinstance(audio_data, np.ndarray):
|
| 26 |
+
raise ValueError("Los datos de audio deben ser un array de numpy.")
|
| 27 |
+
|
| 28 |
+
# Convertir los datos de audio a bytes
|
| 29 |
audio_bytes = np.array(audio_data, dtype=np.int16).tobytes()
|
| 30 |
+
|
| 31 |
+
# Crear el segmento de audio
|
| 32 |
+
audio_segment = AudioSegment(
|
| 33 |
+
data=audio_bytes,
|
| 34 |
+
sample_width=2,
|
| 35 |
+
frame_rate=sampling_rate,
|
| 36 |
+
channels=1
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
# Exportar el segmento de audio a un archivo MP3
|
| 40 |
audio_segment.export(filename, format="mp3")
|
| 41 |
|
| 42 |
return f"Audio saved successfully as {filename}"
|