snowboydecoder.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/env python
  2. import collections
  3. import pyaudio
  4. import snowboydetect
  5. import time
  6. import wave
  7. import os
  8. import logging
  9. logging.basicConfig()
  10. logger = logging.getLogger("snowboy")
  11. logger.setLevel(logging.INFO)
  12. TOP_DIR = os.path.dirname(os.path.abspath(__file__))
  13. RESOURCE_FILE = os.path.join(TOP_DIR, "resources/common.res")
  14. DETECT_DING = os.path.join(TOP_DIR, "resources/ding.wav")
  15. DETECT_DONG = os.path.join(TOP_DIR, "resources/dong.wav")
  16. class RingBuffer(object):
  17. """Ring buffer to hold audio from PortAudio"""
  18. def __init__(self, size = 4096):
  19. self._buf = collections.deque(maxlen=size)
  20. def extend(self, data):
  21. """Adds data to the end of buffer"""
  22. self._buf.extend(data)
  23. def get(self):
  24. """Retrieves data from the beginning of buffer and clears it"""
  25. tmp = bytes(bytearray(self._buf))
  26. self._buf.clear()
  27. return tmp
  28. def play_audio_file(fname=DETECT_DING):
  29. """Simple callback function to play a wave file. By default it plays
  30. a Ding sound.
  31. :param str fname: wave file name
  32. :return: None
  33. """
  34. ding_wav = wave.open(fname, 'rb')
  35. ding_data = ding_wav.readframes(ding_wav.getnframes())
  36. audio = pyaudio.PyAudio()
  37. stream_out = audio.open(
  38. format=audio.get_format_from_width(ding_wav.getsampwidth()),
  39. channels=ding_wav.getnchannels(),
  40. rate=ding_wav.getframerate(), input=False, output=True)
  41. stream_out.start_stream()
  42. stream_out.write(ding_data)
  43. time.sleep(0.2)
  44. stream_out.stop_stream()
  45. stream_out.close()
  46. audio.terminate()
  47. class HotwordDetector(object):
  48. """
  49. Snowboy decoder to detect whether a keyword specified by `decoder_model`
  50. exists in a microphone input stream.
  51. :param decoder_model: decoder model file path, a string or a list of strings
  52. :param resource: resource file path.
  53. :param sensitivity: decoder sensitivity, a float of a list of floats.
  54. The bigger the value, the more senstive the
  55. decoder. If an empty list is provided, then the
  56. default sensitivity in the model will be used.
  57. :param audio_gain: multiply input volume by this factor.
  58. """
  59. def __init__(self, decoder_model,
  60. resource=RESOURCE_FILE,
  61. sensitivity=[],
  62. audio_gain=1):
  63. def audio_callback(in_data, frame_count, time_info, status):
  64. self.ring_buffer.extend(in_data)
  65. play_data = chr(0) * len(in_data)
  66. return play_data, pyaudio.paContinue
  67. tm = type(decoder_model)
  68. ts = type(sensitivity)
  69. if tm is not list:
  70. decoder_model = [decoder_model]
  71. if ts is not list:
  72. sensitivity = [sensitivity]
  73. model_str = ",".join(decoder_model)
  74. self.detector = snowboydetect.SnowboyDetect(
  75. resource_filename=resource.encode(), model_str=model_str.encode())
  76. self.detector.SetAudioGain(audio_gain)
  77. self.num_hotwords = self.detector.NumHotwords()
  78. if len(decoder_model) > 1 and len(sensitivity) == 1:
  79. sensitivity = sensitivity*self.num_hotwords
  80. if len(sensitivity) != 0:
  81. assert self.num_hotwords == len(sensitivity), \
  82. "number of hotwords in decoder_model (%d) and sensitivity " \
  83. "(%d) does not match" % (self.num_hotwords, len(sensitivity))
  84. sensitivity_str = ",".join([str(t) for t in sensitivity])
  85. if len(sensitivity) != 0:
  86. self.detector.SetSensitivity(sensitivity_str.encode())
  87. self.ring_buffer = RingBuffer(
  88. self.detector.NumChannels() * self.detector.SampleRate() * 5)
  89. self.audio = pyaudio.PyAudio()
  90. self.stream_in = self.audio.open(
  91. input=True, output=False,
  92. format=self.audio.get_format_from_width(
  93. self.detector.BitsPerSample() / 8),
  94. channels=self.detector.NumChannels(),
  95. rate=self.detector.SampleRate(),
  96. frames_per_buffer=2048,
  97. stream_callback=audio_callback)
  98. def start(self, detected_callback=play_audio_file,
  99. interrupt_check=lambda: False,
  100. sleep_time=0.03):
  101. """
  102. Start the voice detector. For every `sleep_time` second it checks the
  103. audio buffer for triggering keywords. If detected, then call
  104. corresponding function in `detected_callback`, which can be a single
  105. function (single model) or a list of callback functions (multiple
  106. models). Every loop it also calls `interrupt_check` -- if it returns
  107. True, then breaks from the loop and return.
  108. :param detected_callback: a function or list of functions. The number of
  109. items must match the number of models in
  110. `decoder_model`.
  111. :param interrupt_check: a function that returns True if the main loop
  112. needs to stop.
  113. :param float sleep_time: how much time in second every loop waits.
  114. :return: None
  115. """
  116. if interrupt_check():
  117. logger.debug("detect voice return")
  118. return
  119. tc = type(detected_callback)
  120. if tc is not list:
  121. detected_callback = [detected_callback]
  122. if len(detected_callback) == 1 and self.num_hotwords > 1:
  123. detected_callback *= self.num_hotwords
  124. assert self.num_hotwords == len(detected_callback), \
  125. "Error: hotwords in your models (%d) do not match the number of " \
  126. "callbacks (%d)" % (self.num_hotwords, len(detected_callback))
  127. logger.debug("detecting...")
  128. while True:
  129. if interrupt_check():
  130. logger.debug("detect voice break")
  131. break
  132. data = self.ring_buffer.get()
  133. if len(data) == 0:
  134. time.sleep(sleep_time)
  135. continue
  136. ans = self.detector.RunDetection(data)
  137. if ans == -1:
  138. logger.warning("Error initializing streams or reading audio data")
  139. elif ans > 0:
  140. message = "Keyword " + str(ans) + " detected at time: "
  141. message += time.strftime("%Y-%m-%d %H:%M:%S",
  142. time.localtime(time.time()))
  143. logger.info(message)
  144. callback = detected_callback[ans-1]
  145. if callback is not None:
  146. callback()
  147. logger.debug("finished.")
  148. def terminate(self):
  149. """
  150. Terminate audio stream. Users cannot call start() again to detect.
  151. :return: None
  152. """
  153. self.stream_in.stop_stream()
  154. self.stream_in.close()
  155. self.audio.terminate()