Created
October 9, 2021 18:56
-
-
Save rntz/febd5437f7eedfefc44e9be956b5ce3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mode: dictation | |
- | |
^command$: | |
mode.disable("dictation") | |
mode.enable("command") | |
^command [mode|mood|boat] <phrase>$: | |
mode.disable("dictation") | |
mode.enable("command") | |
user.rerun_phrase(phrase) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Quick switch to dictation mode. | |
dictation [<user.prose>]$: | |
mode.disable("command") | |
mode.enable("dictation") | |
user.dictation_insert(prose or "") | |
# dictation <phrase>$: | |
# mode.disable("command") | |
# mode.enable("dictation") | |
# user.rerun_phrase(phrase) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adapated from https://github.com/pokey/pokey_talon/blob/900b6ae2ab7b978efa7605371fd43ad353ae8d2e/code/parse_phrase.py | |
from talon import Module, speech_system, scope, actions | |
from talon.grammar import Phrase | |
from typing import Union | |
phrase_stack = [] | |
def on_pre_phrase(d): phrase_stack.append(d) | |
def on_post_phrase(d): phrase_stack.pop() | |
speech_system.register("pre:phrase", on_pre_phrase) | |
speech_system.register("post:phrase", on_post_phrase) | |
mod = Module() | |
@mod.action_class | |
class Actions: | |
def rerun_phrase(phrase: Union[str,Phrase]): | |
"""Rerun phrase""" | |
# this is a quite unstable API | |
if not phrase: return | |
current_phrase = phrase_stack[-1] | |
ts = current_phrase["_ts"] | |
start = phrase.words[0].start - ts | |
end = phrase.words[-1].end - ts | |
samples = current_phrase["samples"] | |
pstart = int(start * 16_000) | |
pend = int(end * 16_000) | |
samples = samples[pstart:pend] | |
speech_system._on_audio_frame(samples) | |
def momentary(phrase: Phrase): | |
"""Run the given phrase as if in command mode.""" | |
checked_modes = list(scope.get('mode').intersection({'sleep', 'dictation'})) | |
if 1 != len(checked_modes): return | |
mode = checked_modes[0] | |
actions.mode.disable(mode) | |
actions.mode.enable('command') | |
try: | |
actions.user.rerun_phrase(phrase) | |
finally: | |
actions.mode.disable('command') | |
actions.mode.enable(mode) | |
" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mode: all | |
- | |
^simon says <phrase>$: user.momentary(phrase) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment