~fabrixxm/tradfri-voice

04144403bdc8a37fd0c78c1c0bedf2f20a84158b — fabrixxm 3 years ago master
First commit
5 files changed, 79 insertions(+), 0 deletions(-)

A listen.sh
A sentences.ini
A sounds/alarm.wav
A sounds/beep_hi.wav
A sounds/beep_lo.wav
A  => listen.sh +74 -0
@@ 1,74 @@
#!/usr/bin/env bash
#
# Adattato dall'esempio 'recipes/launch_program/' nel tree di voice2json

VOICE2JSON="/home/fabio/progetti/voice2json/bin/voice2json"
TRADFRI="/home/fabio/progetti/tradfri/tradfri-cli/tradfri"

# ----------

this_dir="$( cd "$( dirname "$0" )" && pwd )"

# Train profile first
$VOICE2JSON train-profile

# Use a temporary WAV file for recorded command.
# Clean up when this script exits.
temp_wav="$(mktemp)"
function finish {
    rm -rf "${temp_wav}"
    exit 0
}

trap finish SIGTERM SIGINT

# ----------

while true;
do
    # Wait until the wake word has been spoken, then exit
    echo 'Waiting for wake word...'
    $VOICE2JSON wait-wake --exit-count 1

    # Play a sound to tell the user we're recording
    aplay "${this_dir}/sounds/beep_hi.wav"

    # Record voice command until silence
    echo 'Recording voice command...'
    $VOICE2JSON record-command > "${temp_wav}"

    # Play a sound to tell the user we're done recording
    aplay "${this_dir}/sounds/beep_lo.wav" &

    # 1. Transcribe the WAV file.
    # 2. Recognize the intent from the transcription.
    # 3. Extract the name of the program to launch
    echo 'Recognizing intent...'
    $VOICE2JSON transcribe-wav "${temp_wav}" | \
        $VOICE2JSON recognize-intent | \
        while read -r intent_json;
        do
            echo "${intent_json}"

            # Verify intent is ChangeLightState
            intent_name="$(echo "${intent_json}" | jq -r .intent.name)"
            if [[ "${intent_name}" == 'ChangeLightState' ]]; then

                # Extract slots
                state="$(echo "${intent_json}" | jq -r .slots.state)"
                name="$(echo "${intent_json}" | jq -r .slots.name)"
                echo "##### $state $name"
                if [[ "${state}" == "accendi"  ]]; then
                    $TRADFRI light $name on
                elif [[ "${state}" == "spegni" ]]; then
                    $TRADFRI light $name off
                else
                  aplay "${this_dir}/sounds/alarm.wav" &
                    echo 'No state was found in intent'
                fi
            else
                aplay "${this_dir}/sounds/alarm.wav" &
                echo "Unexpected intent, got ${intent_name}"
            fi
        done
done

A  => sentences.ini +5 -0
@@ 1,5 @@
[ChangeLightState]
light_name = (soggiorno | garage){name}
light_state = (accendi | spegni){state}

<light_state> [la] luce (in|del) <light_name>

A  => sounds/alarm.wav +0 -0
A  => sounds/beep_hi.wav +0 -0
A  => sounds/beep_lo.wav +0 -0