Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from kokoro import KPipeline
|
|
| 4 |
import soundfile as sf
|
| 5 |
import os
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
|
@@ -34,11 +35,15 @@ async def generate_audio(text: str, voice: str = "af_heart", speed: float = 1.0)
|
|
| 34 |
|
| 35 |
# Process only the first segment for demo
|
| 36 |
for i, (gs, ps, audio) in enumerate(generator):
|
|
|
|
|
|
|
|
|
|
| 37 |
# Convert to 16-bit PCM
|
|
|
|
| 38 |
# Ensure the audio is in the range [-1, 1]
|
| 39 |
-
|
| 40 |
# Convert to 16-bit signed integers
|
| 41 |
-
pcm_data = (
|
| 42 |
|
| 43 |
# Convert to bytes (automatically uses row-major order)
|
| 44 |
raw_audio = pcm_data.tobytes()
|
|
|
|
| 4 |
import soundfile as sf
|
| 5 |
import os
|
| 6 |
import numpy as np
|
| 7 |
+
import torch
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
|
|
|
| 35 |
|
| 36 |
# Process only the first segment for demo
|
| 37 |
for i, (gs, ps, audio) in enumerate(generator):
|
| 38 |
+
|
| 39 |
+
# Convert PyTorch tensor to NumPy array
|
| 40 |
+
audio_numpy = audio.cpu().numpy()
|
| 41 |
# Convert to 16-bit PCM
|
| 42 |
+
|
| 43 |
# Ensure the audio is in the range [-1, 1]
|
| 44 |
+
audio_numpy = np.clip(audio_numpy, -1, 1)
|
| 45 |
# Convert to 16-bit signed integers
|
| 46 |
+
pcm_data = (audio_numpy * 32767).astype(np.int16)
|
| 47 |
|
| 48 |
# Convert to bytes (automatically uses row-major order)
|
| 49 |
raw_audio = pcm_data.tobytes()
|