test_tts.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import time
  2. import threading
  3. import sys
  4. import nls
  5. from tests.test_utils import (TEST_ACCESS_TOKEN, TEST_ACCESS_APPKEY)
  6. TEXT='大壮正想去摘取花瓣,谁知阿丽和阿强突然内讧,阿丽拿去手枪向树干边的阿强射击,两声枪响,阿强直接倒入水中'
  7. class TestTts:
  8. def __init__(self, tid, test_file):
  9. self.__th = threading.Thread(target=self.__test_run)
  10. self.__id = tid
  11. self.__test_file = test_file
  12. def start(self, text):
  13. self.__text = text
  14. self.__f = open(self.__test_file, "wb")
  15. self.__th.start()
  16. def test_on_metainfo(self, message, *args):
  17. print("on_metainfo message=>{}".format(message))
  18. def test_on_error(self, message, *args):
  19. print("on_error args=>{}".format(args))
  20. def test_on_close(self, *args):
  21. print("on_close: args=>{}".format(args))
  22. try:
  23. self.__f.close()
  24. except Exception as e:
  25. print("close file failed since:", e)
  26. def test_on_data(self, data, *args):
  27. try:
  28. self.__f.write(data)
  29. except Exception as e:
  30. print("write data failed:", e)
  31. def test_on_completed(self, message, *args):
  32. print("on_completed:args=>{} message=>{}".format(args, message))
  33. def __test_run(self):
  34. print("thread:{} start..".format(self.__id))
  35. tts = nls.NlsSpeechSynthesizer(
  36. token=TEST_ACCESS_TOKEN,
  37. appkey=TEST_ACCESS_APPKEY,
  38. long_tts=True,
  39. on_metainfo=self.test_on_metainfo,
  40. on_data=self.test_on_data,
  41. on_completed=self.test_on_completed,
  42. on_error=self.test_on_error,
  43. on_close=self.test_on_close,
  44. callback_args=[self.__id]
  45. )
  46. print("{}: session start".format(self.__id))
  47. r = tts.start(self.__text, voice="ailun", ex={'enable_subtitle':True})
  48. print("{}: tts done with result:{}".format(self.__id, r))
  49. def multiruntest(num=500):
  50. for i in range(0, num):
  51. name = "thread" + str(i)
  52. t = TestTts(name, "tests/test_tts.pcm")
  53. t.start(TEXT)
  54. nls.enableTrace(True)
  55. multiruntest(1)