bcci commited on
Commit
e334297
·
verified ·
1 Parent(s): b452b41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -13
app.py CHANGED
@@ -249,20 +249,14 @@ def tts_full(text: str, voice: str = "af_heart", speed: float = 1.0, format: str
249
  speed=np.ones(1, dtype=np.float32),
250
  ))[0]
251
 
252
- print(audio)
253
-
254
  # Write the concatenated audio to an in-memory WAV or Opus file.
255
  sample_rate = 24000
256
 
257
- # Normalize audio data to the range [-1.0, 1.0]
258
- # audio_normalized = audio / np.max(np.abs(audio))
259
-
260
- # Scale to 16-bit integer range
261
- audio_scaled = np.int16(audio * 32767)
262
- print(audio_scaled)
263
-
264
- if np.any(audio_scaled < -32768) or np.any(audio_scaled > 32767):
265
- raise ValueError("Scaled audio data is outside the valid range for 16-bit WAV files.")
266
 
267
  if format.lower() == "wav":
268
 
@@ -270,14 +264,14 @@ def tts_full(text: str, voice: str = "af_heart", speed: float = 1.0, format: str
270
  wav_io = io.BytesIO()
271
 
272
  # Write the audio data to the buffer in WAV format
273
- write_wav(wav_io, sample_rate, audio_scaled)
274
 
275
  # Seek to the beginning of the buffer
276
  wav_io.seek(0)
277
 
278
  return Response(content=wav_io.read(), media_type="audio/wav")
279
  elif format.lower() == "opus":
280
- opus_data = audio_tensor_to_opus_bytes(torch.from_numpy(audio_scaled), sample_rate=sample_rate)
281
  return Response(content=opus_data, media_type="audio/opus")
282
  else:
283
  raise HTTPException(status_code=400, detail=f"Unsupported audio format: {format}")
 
249
  speed=np.ones(1, dtype=np.float32),
250
  ))[0]
251
 
 
 
252
  # Write the concatenated audio to an in-memory WAV or Opus file.
253
  sample_rate = 24000
254
 
255
+ audio = np.array(audio, dtype=np.float32) # Ensure it's float32 first
256
+ audio = (audio * 32767).astype(np.int16) # Scale to int16 range
257
+
258
+ # Flatten the array if it's 2D
259
+ audio = audio.flatten()
 
 
 
 
260
 
261
  if format.lower() == "wav":
262
 
 
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)
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), 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}")