#!/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