test_st.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. class TestSt:
  7. def __init__(self, tid, test_file):
  8. self.__th = threading.Thread(target=self.__test_run)
  9. self.__id = tid
  10. self.__test_file = test_file
  11. def loadfile(self, filename):
  12. with open(filename, "rb") as f:
  13. self.__data = f.read()
  14. def start(self):
  15. self.loadfile(self.__test_file)
  16. self.__th.start()
  17. def test_on_sentence_begin(self, message, *args):
  18. print("test_on_sentence_begin:{}".format(message))
  19. def test_on_sentence_end(self, message, *args):
  20. print("test_on_sentence_end:{}".format(message))
  21. def test_on_start(self, message, *args):
  22. print("test_on_start:{}".format(message))
  23. def test_on_error(self, message, *args):
  24. print("on_error args=>{}".format(args))
  25. def test_on_close(self, *args):
  26. print("on_close: args=>{}".format(args))
  27. def test_on_result_chg(self, message, *args):
  28. print("test_on_chg:{}".format(message))
  29. def test_on_completed(self, message, *args):
  30. print("on_completed:args=>{} message=>{}".format(args, message))
  31. def __test_run(self):
  32. print("thread:{} start..".format(self.__id))
  33. sr = nls.NlsSpeechTranscriber(
  34. token=TEST_ACCESS_TOKEN,
  35. appkey=TEST_ACCESS_APPKEY,
  36. on_sentence_begin=self.test_on_sentence_begin,
  37. on_sentence_end=self.test_on_sentence_end,
  38. on_start=self.test_on_start,
  39. on_result_changed=self.test_on_result_chg,
  40. on_completed=self.test_on_completed,
  41. on_error=self.test_on_error,
  42. on_close=self.test_on_close,
  43. callback_args=[self.__id]
  44. )
  45. while True:
  46. print("{}: session start".format(self.__id))
  47. r = sr.start(aformat="pcm",
  48. enable_intermediate_result=True,
  49. enable_punctuation_prediction=True,
  50. enable_inverse_text_normalization=True)
  51. self.__slices = zip(*(iter(self.__data),) * 640)
  52. for i in self.__slices:
  53. sr.send_audio(bytes(i))
  54. time.sleep(0.01)
  55. sr.ctrl(ex={"test":"tttt"})
  56. time.sleep(1)
  57. r = sr.stop()
  58. print("{}: sr stopped:{}".format(self.__id, r))
  59. time.sleep(5)
  60. def multiruntest(num=500):
  61. for i in range(0, num):
  62. name = "thread" + str(i)
  63. t = TestSt(name, "tests/test1.pcm")
  64. t.start()
  65. nls.enableTrace(True)
  66. multiruntest(1)