diff --git a/test/test_mzzsfy_tts.py b/test/test_mzzsfy_tts.py index a244b2a..f62c643 100644 --- a/test/test_mzzsfy_tts.py +++ b/test/test_mzzsfy_tts.py @@ -6,6 +6,7 @@ import numpy as np import requests import resampy import soundfile as sf +from io import BytesIO def download_tts(url): @@ -38,17 +39,15 @@ def __create_bytes_stream(byte_stream): return stream -def main(): +def test_async_tts(url, content): import aiohttp import asyncio - from io import BytesIO async def fetch_audio(): - # url = "http://localhost:8082/v1/audio/speech" - url = "https://tts.mzzsfy.eu.org/v1/audio/speech" + data = { "model": "tts-1", - "input": "写了一个高性能tts(文本转声音)工具,5千字仅需5秒,免费使用", + "input": content, "voice": "alloy", "speed": 1.0 } @@ -69,6 +68,33 @@ def main(): asyncio.run(fetch_audio()) +def test_sync_tts(url, content): + data = { + "model": "tts-1", + "input": content, + "voice": "alloy", + "speed": 1.0 + } + response = requests.post(url, json=data) + if response.status_code == 200: + audio_data = BytesIO(response.content) + audio_stream = __create_bytes_stream(audio_data) + + # 保存为新的音频文件 + sf.write("output_audio.wav", audio_stream, 16000) + print("Audio data received and saved to output_audio.wav") + else: + print("Error:", response.status_code, response.text) + + +def main(): + # url = "http://localhost:8082/v1/audio/speech" + url = "https://tts.mzzsfy.eu.org/v1/audio/speech" + content = "写了一个高性能tts(文本转声音)工具,5千字仅需5秒,免费使用" + # test_async_tts(url, content) + test_sync_tts(url, content) + + if __name__ == "__main__": try: t = time.time() diff --git a/tts/tts_edge_http.py b/tts/tts_edge_http.py index 1410e1f..11ec055 100644 --- a/tts/tts_edge_http.py +++ b/tts/tts_edge_http.py @@ -4,6 +4,7 @@ from io import BytesIO import aiohttp import numpy as np +import requests import soundfile as sf import edge_tts import resampy @@ -21,15 +22,7 @@ class TTSEdgeHttp(TTSBase): self._url = 'https://tts.mzzsfy.eu.org/v1/audio/speech' logger.info(f"TTSEdge init, {voice}") - async def _on_request(self, txt: str): - logger.info(f'TTSEdgeHttp, _on_request, txt:{txt}') - data = { - "model": "tts-1", - "input": txt, - "voice": "alloy", - "speed": 1.0, - "thread": 10 - } + async def _on_async_request(self, data): async with aiohttp.ClientSession() as session: async with session.post(self._url, json=data) as response: print('TTSEdgeHttp, _on_request, response:', response) @@ -40,6 +33,27 @@ class TTSEdgeHttp(TTSBase): byte_stream = None return byte_stream, None + def _on_sync_request(self, data): + response = requests.post(self._url, json=data) + if response.status_code == 200: + stream = BytesIO(response.content) + return stream + else: + return None + + async def _on_request(self, txt: str): + logger.info(f'TTSEdgeHttp, _on_request, txt:{txt}') + data = { + "model": "tts-1", + "input": txt, + "voice": "alloy", + "speed": 1.0, + "thread": 10 + } + + # return self._on_async_request(data) + return self._on_sync_request(data) + async def _on_handle(self, stream, index): print('-------tts _on_handle') try: