示例#1
0
    def say(self, text, locale=None, gender=None):
        '''Converts the provided text to speech and plays it over the
        user's default audio device.

        text:
            The text to say.
        locale:
            The locale to use. If omitted, uses the default for this
            client.
        gender:
            The gender to use. If omitted, uses the default for this
            client.
        '''
        if text.strip():
            audio.play(self.say_to_wav(text, locale, gender))
示例#2
0
    def say(self, text, locale=None, gender=None):
        '''Converts the provided text to speech and plays it over the
        user's default audio device.

        text:
            The text to say.
        locale:
            The locale to use. If omitted, uses the default for this
            client.
        gender:
            The gender to use. If omitted, uses the default for this
            client.
        '''
        if text.strip():
            audio.play(self.say_to_wav(text, locale, gender))
示例#3
0
    def recognize(self, wav=None, locale=None, require_high_confidence=True):
        '''Converts a wave file to text. If no file is provided, the
        user's default microphone will record up to 30 seconds of
        audio. Returns a string containing the recognized text.

        wav:
            An open `wave.Wave_read` object, a `bytes` object
            containing a wave file, or a valid argument to
            `wave.open`. If omitted, a beep will be played and the
            user's default microphone will record up to 30 seconds of
            audio.
        locale:
            The locale to use. If omitted, uses the default for this
            client.
        require_high_confidence:
            If True, raises `LowConfidenceError` when the result is
            not of high confidence. The first argument of the
            exception contains the text that was heard. Otherwise,
            low confidence results will be returned as normal.
        '''
        if not wav:
            if self.quiet_threshold is None:
                self.calibrate_audio_recording()
            audio.play(_BEEP_ON_WAV)
            wav = audio.record(seconds=30,
                               quiet_seconds=1,
                               quiet_threshold=self.quiet_threshold)
            audio.play(_BEEP_OFF_WAV)
        res = self.recognize_raw(wav, locale)
        try:
            best = res['results'][0]
            if best['properties'].get('HIGHCONF'):
                return best['name']
            if best['properties'].get('MIDCONF') or best['properties'].get(
                    'LOWCONF'):
                if require_high_confidence:
                    raise LowConfidenceError(best['name'])
                return best['name']
        except LookupError:
            pass
        raise ValueError('unable to recognize speech')
示例#4
0
    def recognize(self, wav=None, locale=None, require_high_confidence=True):
        '''Converts a wave file to text. If no file is provided, the
        user's default microphone will record up to 30 seconds of
        audio. Returns a string containing the recognized text.

        wav:
            An open `wave.Wave_read` object, a `bytes` object
            containing a wave file, or a valid argument to
            `wave.open`. If omitted, a beep will be played and the
            user's default microphone will record up to 30 seconds of
            audio.
        locale:
            The locale to use. If omitted, uses the default for this
            client.
        require_high_confidence:
            If True, raises `LowConfidenceError` when the result is
            not of high confidence. The first argument of the
            exception contains the text that was heard. Otherwise,
            low confidence results will be returned as normal.
        '''
        if not wav:
            if self.quiet_threshold is None:
                self.calibrate_audio_recording()
            audio.play(_BEEP_ON_WAV)
            wav = audio.record(seconds=30, quiet_seconds=1, quiet_threshold=self.quiet_threshold)
            audio.play(_BEEP_OFF_WAV)
        res = self.recognize_raw(wav, locale)
        try:
            best = res['results'][0]
            if best['properties'].get('HIGHCONF'):
                return best['name']
            if best['properties'].get('MIDCONF') or best['properties'].get('LOWCONF'):
                if require_high_confidence:
                    raise LowConfidenceError(best['name'])
                return best['name']
        except LookupError:
            pass
        raise ValueError('unable to recognize speech')