| 12 | |
| 13 | |
| 14 | class Mic: |
| 15 | |
| 16 | speechRec = None |
| 17 | speechRec_persona = None |
| 18 | |
| 19 | def __init__(self, speaker, passive_stt_engine, active_stt_engine): |
| 20 | """ |
| 21 | Initiates the pocketsphinx instance. |
| 22 | |
| 23 | Arguments: |
| 24 | speaker -- handles platform-independent audio output |
| 25 | passive_stt_engine -- performs STT while Jasper is in passive listen |
| 26 | mode |
| 27 | acive_stt_engine -- performs STT while Jasper is in active listen mode |
| 28 | """ |
| 29 | self._logger = logging.getLogger(__name__) |
| 30 | self.speaker = speaker |
| 31 | self.passive_stt_engine = passive_stt_engine |
| 32 | self.active_stt_engine = active_stt_engine |
| 33 | self._logger.info("Initializing PyAudio. ALSA/Jack error messages " + |
| 34 | "that pop up during this process are normal and " + |
| 35 | "can usually be safely ignored.") |
| 36 | self._audio = pyaudio.PyAudio() |
| 37 | self._logger.info("Initialization of PyAudio completed.") |
| 38 | |
| 39 | def __del__(self): |
| 40 | self._audio.terminate() |
| 41 | |
| 42 | def getScore(self, data): |
| 43 | rms = audioop.rms(data, 2) |
| 44 | score = rms / 3 |
| 45 | return score |
| 46 | |
| 47 | def fetchThreshold(self): |
| 48 | |
| 49 | # TODO: Consolidate variables from the next three functions |
| 50 | THRESHOLD_MULTIPLIER = 1.8 |
| 51 | RATE = 16000 |
| 52 | CHUNK = 1024 |
| 53 | |
| 54 | # number of seconds to allow to establish threshold |
| 55 | THRESHOLD_TIME = 1 |
| 56 | |
| 57 | # prepare recording stream |
| 58 | stream = self._audio.open(format=pyaudio.paInt16, |
| 59 | channels=1, |
| 60 | rate=RATE, |
| 61 | input=True, |
| 62 | frames_per_buffer=CHUNK) |
| 63 | |
| 64 | # stores the audio data |
| 65 | frames = [] |
| 66 | |
| 67 | # stores the lastN score values |
| 68 | lastN = [i for i in range(20)] |
| 69 | |
| 70 | # calculate the long run average, and thereby the proper threshold |
| 71 | for i in range(0, RATE / CHUNK * THRESHOLD_TIME): |