コード例 #1
0
ファイル: backend.py プロジェクト: boradin/speechcontrol
class NativePocketSphinx(QObject):
    recognizedToFile = QtCore.pyqtSignal(str)

    def __init__(self):
        super().__init__()

        logger.info("Initializing PocketSphinx backend")
        self.ps = PocketSphinx()
        logger.debug("Initializing PocketSphinx decoder")
        self.ps.initializeDecoder()
        logger.debug("Initializing audio recording")
        self.ps.initializeAudio()

        self.contWorker = ContinuousAsrWorker()
        self.workerThread = QThread(self)
        self.workerThread.started.connect(self.contWorker.start)
        self.workerThread.finished.connect(self.contWorker.deleteLater)
        self.contWorker.moveToThread(self.workerThread)

    def recognizeFromMicrophone(self, sinkFileName):
        self.ps.recognizeFromMicrophone(sinkFileName)
        self.recognizedToFile.emit(sinkFileName)

    # This returns an iterator for n file names following some naming schema
    def fileNamesRange(self, n, stem="hyp"):
        for i in range(1, n+1):
            yield stem + str(i)

    #TODO: Write a solution for continuous recognition
    # Location of written files (and naming schema) should be governed by
    # a configuration manager
    # This will probably need a separate thread (QThread to be used)
    # FIXME: We should probably shut down the main PS instance to avoid conflicts
    # between it and the worker
    def startContinuousRecognition(self):
        logger.info("Starting continuous speech recognition")
        self.workerThread.start()

    def stopContinuousRecognition(self):
        logger.info("Stopping continuous speech recognition")
        self.workerThread.quit()

    @classmethod
    def supported():
        return True
コード例 #2
0
ファイル: backend.py プロジェクト: boradin/speechcontrol
class ContinuousAsrWorker(QObject):
    def __init__(self):
        super().__init__()
        self.ps = None
        self.finish = False

    @pyqtSlot()
    def start(self):
        logger.debug("Continuous ASR worker starts")

        logger.info("Initializing PocketSphinx backend")
        self.ps = PocketSphinx()
        logger.debug("Initializing PocketSphinx decoder")
        self.ps.initializeDecoder()
        logger.debug("Initializing audio recording")
        self.ps.initializeAudio()

        continuousSinkFileNameBase = SC_SHARE_PATH + '/contrecog/utts/hyp'
        i = 1
        while not self.finish:
            self.ps.recognizeFromMicrophone(continuousSinkFileNameBase + str(i))
            i += 1