|
|
|
@ -12,10 +12,10 @@
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
import json
|
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter
|
|
|
|
|
from fastapi import WebSocket
|
|
|
|
|
from fastapi import WebSocketDisconnect
|
|
|
|
|
from starlette.websockets import WebSocketState as WebSocketState
|
|
|
|
|
|
|
|
|
|
from paddlespeech.cli.log import logger
|
|
|
|
@ -26,36 +26,79 @@ router = APIRouter()
|
|
|
|
|
|
|
|
|
|
@router.websocket('/paddlespeech/tts/streaming')
|
|
|
|
|
async def websocket_endpoint(websocket: WebSocket):
|
|
|
|
|
"""PaddleSpeech Online TTS Server api
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
websocket (WebSocket): the websocket instance
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
#1. the interface wait to accept the websocket protocal header
|
|
|
|
|
# and only we receive the header, it establish the connection with specific thread
|
|
|
|
|
await websocket.accept()
|
|
|
|
|
|
|
|
|
|
#2. if we accept the websocket headers, we will get the online tts engine instance
|
|
|
|
|
engine_pool = get_engine_pool()
|
|
|
|
|
tts_engine = engine_pool['tts']
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
# careful here, changed the source code from starlette.websockets
|
|
|
|
|
assert websocket.application_state == WebSocketState.CONNECTED
|
|
|
|
|
message = await websocket.receive()
|
|
|
|
|
websocket._raise_on_disconnect(message)
|
|
|
|
|
while True:
|
|
|
|
|
# careful here, changed the source code from starlette.websockets
|
|
|
|
|
assert websocket.application_state == WebSocketState.CONNECTED
|
|
|
|
|
message = await websocket.receive()
|
|
|
|
|
websocket._raise_on_disconnect(message)
|
|
|
|
|
message = json.loads(message["text"])
|
|
|
|
|
|
|
|
|
|
# get engine
|
|
|
|
|
engine_pool = get_engine_pool()
|
|
|
|
|
tts_engine = engine_pool['tts']
|
|
|
|
|
if 'signal' in message:
|
|
|
|
|
# start request
|
|
|
|
|
if message['signal'] == 'start':
|
|
|
|
|
session = uuid.uuid1().hex
|
|
|
|
|
resp = {
|
|
|
|
|
"status": 0,
|
|
|
|
|
"signal": "server ready",
|
|
|
|
|
"session": session
|
|
|
|
|
}
|
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
|
|
|
|
|
|
# 获取 message 并转文本
|
|
|
|
|
message = json.loads(message["text"])
|
|
|
|
|
text_bese64 = message["text"]
|
|
|
|
|
sentence = tts_engine.preprocess(text_bese64=text_bese64)
|
|
|
|
|
# end request
|
|
|
|
|
elif message['signal'] == 'end':
|
|
|
|
|
resp = {
|
|
|
|
|
"status": 0,
|
|
|
|
|
"signal": "connection will be closed",
|
|
|
|
|
"session": session
|
|
|
|
|
}
|
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
resp = {"status": 0, "signal": "no valid json data"}
|
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
|
|
|
|
|
|
# run
|
|
|
|
|
wav_generator = tts_engine.run(sentence)
|
|
|
|
|
# speech synthesis request
|
|
|
|
|
elif 'text' in message:
|
|
|
|
|
text_bese64 = message["text"]
|
|
|
|
|
sentence = tts_engine.preprocess(text_bese64=text_bese64)
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
tts_results = next(wav_generator)
|
|
|
|
|
resp = {"status": 1, "audio": tts_results}
|
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
|
except StopIteration as e:
|
|
|
|
|
resp = {"status": 2, "audio": ''}
|
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
|
logger.info("Complete the transmission of audio streams")
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
except WebSocketDisconnect:
|
|
|
|
|
pass
|
|
|
|
|
# run
|
|
|
|
|
wav_generator = tts_engine.run(sentence)
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
try:
|
|
|
|
|
tts_results = next(wav_generator)
|
|
|
|
|
resp = {"status": 1, "audio": tts_results}
|
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
|
except StopIteration as e:
|
|
|
|
|
resp = {"status": 2, "audio": ''}
|
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
|
logger.info(
|
|
|
|
|
"Complete the synthesis of the audio streams")
|
|
|
|
|
break
|
|
|
|
|
except Exception as e:
|
|
|
|
|
resp = {"status": -1, "audio": ''}
|
|
|
|
|
await websocket.send_json(resp)
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
logger.error(
|
|
|
|
|
"Invalid request, please check if the request is correct.")
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(e)
|
|
|
|
|