Example #1
0
    def _callback(self, in_data, frame_count, time_info, status):
        if self.status & self.recording_mask:
            pass

        if self.status & self.detecting_mask:
            self.detect_queue.put(in_data)

        if self.status & self.listening_mask:
            active = vad.is_speech(in_data)
            if active:
                if not self.active:
                    for d in self.listen_history:
                        self.listen_queue.put(d)
                        self.listen_countdown[0] -= 1
                        self.listen_countdown[1] = self.end_silence

                    self.listen_history.clear()

                self.listen_queue.put(in_data)
                self.listen_countdown[0] -= 1
                self.listen_countdown[1] = self.end_silence
            else:
                if self.active:
                    self.listen_queue.put(in_data)
                else:
                    self.listen_history.append(in_data)

                self.listen_countdown[1] -= 1
            if (self.listen_countdown[1] < -(self.end_silence) and self.listen_countdown[0] < (1/10)*self.end_silence) \
                or (self.listen_countdown[0] <= 0) \
                or (self.listen_countdown[1] <= 0):

                self.listen_queue.put('')
                self.status &= ~self.listening_mask
                logger.info('Stop listening')

            self.active = active

        if self.status & self.recording_mask:
            self.wav.writeframes(in_data)
            self.record_countdown -= 1
            if self.record_countdown <= 0:
                self.status &= ~self.recording_mask
                self.wav.close()

        return None, pyaudio.paContinue
    def _callback(self, in_data, frame_count, time_info, status):
        if self.status & self.recording_mask:
            pass

        if self.status & self.detecting_mask:
            self.detect_queue.put(in_data)

        if self.status & self.listening_mask:
            active = vad.is_speech(in_data)
            if active:
                if not self.active:
                    for d in self.listen_history:
                        self.listen_queue.put(d)
                        self.listen_countdown[0] -= 1

                    self.listen_history.clear()

                self.listen_queue.put(in_data)
                self.listen_countdown[0] -= 1
            else:
                if self.active:
                    self.listen_queue.put(in_data)
                else:
                    self.listen_history.append(in_data)

                self.listen_countdown[1] -= 1

            if self.listen_countdown[0] <= 0 or self.listen_countdown[1] <= 0:
                self.listen_queue.put('')
                self.status &= ~self.listening_mask
                pixel_ring.wait()
                logger.info('Stop listening')

            self.active = active

        return None, pyaudio.paContinue