up33
Browse files- __pycache__/app.cpython-312.pyc +0 -0
- app.py +28 -2
__pycache__/app.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-312.pyc and b/__pycache__/app.cpython-312.pyc differ
|
|
|
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import outetts
|
|
| 4 |
import io
|
| 5 |
import json
|
| 6 |
import base64
|
|
|
|
| 7 |
import os
|
| 8 |
# Initialize the interface
|
| 9 |
interface = outetts.Interface(
|
|
@@ -54,8 +55,32 @@ async def websocket_tts(websocket: WebSocket):
|
|
| 54 |
chunk_size = 4096
|
| 55 |
try:
|
| 56 |
with open(temp_path, "rb") as f:
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
if not chunk:
|
| 60 |
break
|
| 61 |
audio_b64 = base64.b64encode(chunk).decode("ascii")
|
|
@@ -66,6 +91,7 @@ async def websocket_tts(websocket: WebSocket):
|
|
| 66 |
"request_finished": False
|
| 67 |
}
|
| 68 |
}))
|
|
|
|
| 69 |
finally:
|
| 70 |
try:
|
| 71 |
os.remove(temp_path)
|
|
|
|
| 4 |
import io
|
| 5 |
import json
|
| 6 |
import base64
|
| 7 |
+
import struct
|
| 8 |
import os
|
| 9 |
# Initialize the interface
|
| 10 |
interface = outetts.Interface(
|
|
|
|
| 55 |
chunk_size = 4096
|
| 56 |
try:
|
| 57 |
with open(temp_path, "rb") as f:
|
| 58 |
+
wav_data = f.read()
|
| 59 |
+
# WAV header is typically 44 bytes, but let's detect it robustly
|
| 60 |
+
# Find the end of the header (data chunk)
|
| 61 |
+
if wav_data[:4] != b'RIFF' or wav_data[8:12] != b'WAVE':
|
| 62 |
+
raise ValueError("Not a valid WAV file")
|
| 63 |
+
# Find 'data' subchunk
|
| 64 |
+
data_offset = wav_data.find(b'data')
|
| 65 |
+
if data_offset == -1:
|
| 66 |
+
raise ValueError("No 'data' chunk found in WAV file")
|
| 67 |
+
header_end = data_offset + 8 # 'data' + size (4 bytes)
|
| 68 |
+
wav_header = wav_data[:header_end]
|
| 69 |
+
pcm_data = wav_data[header_end:]
|
| 70 |
+
# Send header + first PCM chunk
|
| 71 |
+
first_chunk = pcm_data[:chunk_size]
|
| 72 |
+
audio_b64 = base64.b64encode(wav_header + first_chunk).decode("ascii")
|
| 73 |
+
await websocket.send_text(json.dumps({
|
| 74 |
+
"data": {
|
| 75 |
+
"audio_bytes": audio_b64,
|
| 76 |
+
"duration": None,
|
| 77 |
+
"request_finished": False
|
| 78 |
+
}
|
| 79 |
+
}))
|
| 80 |
+
# Send rest of PCM data in chunks (without header)
|
| 81 |
+
idx = chunk_size
|
| 82 |
+
while idx < len(pcm_data):
|
| 83 |
+
chunk = pcm_data[idx:idx+chunk_size]
|
| 84 |
if not chunk:
|
| 85 |
break
|
| 86 |
audio_b64 = base64.b64encode(chunk).decode("ascii")
|
|
|
|
| 91 |
"request_finished": False
|
| 92 |
}
|
| 93 |
}))
|
| 94 |
+
idx += chunk_size
|
| 95 |
finally:
|
| 96 |
try:
|
| 97 |
os.remove(temp_path)
|