test_stream_input_tts.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import nls
  2. import pyaudio
  3. import time
  4. from tests.test_utils import TEST_ACCESS_TOKEN, TEST_ACCESS_APPKEY
  5. test_text = [
  6. "流式文本语音合成SDK,",
  7. "可以将输入的文本",
  8. "合成为语音二进制数据,",
  9. "相比于非流式语音合成,",
  10. "流式合成的优势在于实时性",
  11. "更强。用户在输入文本的同时",
  12. "可以听到接近同步的语音输出,",
  13. "极大地提升了交互体验,",
  14. "减少了用户等待时间。",
  15. "适用于调用大规模",
  16. "语言模型(LLM),以",
  17. "流式输入文本的方式",
  18. "进行语音合成的场景。",
  19. ]
  20. if __name__ == "__main__":
  21. player = pyaudio.PyAudio()
  22. stream = player.open(format=pyaudio.paInt16, channels=1, rate=24000, output=True)
  23. # 创建SDK实例
  24. # 配置回调函数
  25. def test_on_data(data, *args):
  26. stream.write(data)
  27. def test_on_message(message, *args):
  28. print('on message=>{}'.format(message))
  29. def test_on_close(*args):
  30. print('on_close: args=>{}'.format(args))
  31. def test_on_error(message, *args):
  32. print('on_error args=>{}, message=>{}'.format(args, message))
  33. sdk = nls.NlsStreamInputTtsSynthesizer(
  34. token=TEST_ACCESS_TOKEN,
  35. appkey=TEST_ACCESS_APPKEY,
  36. on_data=test_on_data,
  37. on_sentence_begin=test_on_message,
  38. on_sentence_synthesis=test_on_message,
  39. on_sentence_end=test_on_message,
  40. on_completed=test_on_message,
  41. on_error=test_on_error,
  42. on_close=test_on_close,
  43. callback_args=[],
  44. )
  45. # 发送文本消息
  46. sdk.startStreamInputTts()
  47. for text in test_text:
  48. sdk.sendStreamInputTts(text)
  49. time.sleep(0.05)
  50. sdk.stopStreamInputTts()
  51. stream.stop_stream()
  52. stream.close()
  53. player.terminate()