Exemple #1
0
def test_pronunciation():
    stt_client = stt_provider.stt_client

    # If they're not in study mode, use this as a shortcut to open settings
    if not mw.reviewer.card:
        settings_dialog()
        return

    # Make sure that the field on the card exists
    field_to_read = stt_client.get_field_to_read()
    if field_to_read not in mw.reviewer.card.note():
        show_error_dialog(
            f'This plugin needs to know which field you are reading. '
            f'It\'s looking for a field named: "{field_to_read}", '
            f'but there is no field named: "{field_to_read}" on the current card. '
            f'Please check the settings.', True)
        return

    # Validate anything else
    try:
        stt_client.pre_stt_validate()
    except STTError as e:
        show_error_dialog(str(e), e.show_settings)
        return

    to_read_text = mw.reviewer.card.note()[field_to_read]
    to_read_text = strip_all_punc(remove_html(to_read_text)).strip()

    def after_record(recorded_voice):
        # If the user canceled the recording, do nothing and return
        if not recorded_voice:
            return
        try:
            tts_result = stt_client.get_stt_results(recorded_voice)
            tts_result = strip_all_punc(tts_result).strip()
            diff_and_show_result(to_read_text, tts_result,
                                 stt_client.get_language_code())
        except STTError as e:
            show_error_dialog(str(e), e.show_settings)
            return

    # This stores the file as "rec.wav" in a tmp directory (for me it's /tmp/anki_tmp/rec.wav)
    # It will overwrite the file every time, so there's no need to delete it after.
    record_audio(mw, mw, False, after_record)
Exemple #2
0
    def onRecordVoice(self) -> None:
        def after_record(path: str) -> None:
            self._recordedAudio = path
            self.onReplayRecorded()

        record_audio(self.mw, self.mw, False, after_record)