Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -251,20 +251,27 @@ def tts_full(text: str, voice: str = "af_heart", speed: float = 1.0, format: str
|
|
251 |
|
252 |
# Write the concatenated audio to an in-memory WAV or Opus file.
|
253 |
sample_rate = 24000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
if format.lower() == "wav":
|
255 |
|
256 |
# Create an in-memory buffer
|
257 |
wav_io = io.BytesIO()
|
258 |
|
259 |
# Write the audio data to the buffer in WAV format
|
260 |
-
write_wav(wav_io, sample_rate,
|
261 |
|
262 |
# Seek to the beginning of the buffer
|
263 |
wav_io.seek(0)
|
264 |
|
265 |
return Response(content=wav_io.read(), media_type="audio/wav")
|
266 |
elif format.lower() == "opus":
|
267 |
-
opus_data = audio_tensor_to_opus_bytes(torch.from_numpy(
|
268 |
return Response(content=opus_data, media_type="audio/opus")
|
269 |
else:
|
270 |
raise HTTPException(status_code=400, detail=f"Unsupported audio format: {format}")
|
|
|
251 |
|
252 |
# Write the concatenated audio to an in-memory WAV or Opus file.
|
253 |
sample_rate = 24000
|
254 |
+
|
255 |
+
# Normalize audio data to the range [-1.0, 1.0]
|
256 |
+
audio_normalized = audio / np.max(np.abs(audio))
|
257 |
+
|
258 |
+
# Scale to 16-bit integer range
|
259 |
+
audio_scaled = np.int16(audio_normalized * 32767)
|
260 |
+
|
261 |
if format.lower() == "wav":
|
262 |
|
263 |
# Create an in-memory buffer
|
264 |
wav_io = io.BytesIO()
|
265 |
|
266 |
# Write the audio data to the buffer in WAV format
|
267 |
+
write_wav(wav_io, sample_rate, audio_scaled)
|
268 |
|
269 |
# Seek to the beginning of the buffer
|
270 |
wav_io.seek(0)
|
271 |
|
272 |
return Response(content=wav_io.read(), media_type="audio/wav")
|
273 |
elif format.lower() == "opus":
|
274 |
+
opus_data = audio_tensor_to_opus_bytes(torch.from_numpy(audio_scaled), sample_rate=sample_rate)
|
275 |
return Response(content=opus_data, media_type="audio/opus")
|
276 |
else:
|
277 |
raise HTTPException(status_code=400, detail=f"Unsupported audio format: {format}")
|