#!/bin/sh # # speakd # # (C) 2003 Malte J. Wetz # # A simple shell script that starts speak in a loop reading # from a named pipe. # # Don't touch these two! BASENAME=`basename $0` VERSION="0.0.2" ############################################################################## # SETTINGS: Change the following parameters to meet your requirements. # If you don't know what you're doing, just don't do anything or at # least keep a backup! ############################################################################## # What is the name of the named pipe from which speak should get its # input data? DEVICE="/dev/speech" # Where is the speak script located? SPEAK="/usr/local/bin/speak" # With what parameters should speak be started? SPEAK_PARAMS="-a -n -p" # Set to 'yes', if you want speakd to die if speak produces an error # while synthesizing. Set to 'no', if speak should ignore such # situations. DIE_ON_ERROR="yes" # What action should be made upon usage of the named pipe? ACTION="" #ACTION="logger -t $BASENAME successfully synthesized text from $DEVICE" #ACTION="echo -n ." # Create a pid file for this process at the specified location PIDFILE="/var/run/$BASENAME.pid" ############################################################################## # END OF SETTINGS: There should be nothing to change below this point! ############################################################################## # sanity check: is there another speakd running? test -e "$PIDFILE" && { PID=`cat "$PIDFILE"` result=`ps -p "$PID" --no-headers` if [ -z "$result" ]; then echo "warning: pid file ($PID) found, appears to be stale, removing..." rm -f "$PIDFILE" else echo "error: another $BASENAME seems to be running with pid $PID, exit." exit 1 fi } # sanity check: does the device exist and is it readable? test -r "$DEVICE" || { echo "error: $DEVICE does not exists or is not readable. exit." exit 1 } # This function is called upon exit. It will remove the pid file. function finalize { test -e "$PIDFILE" && rm -f "$PIDFILE" } # Evaluate command line options while getopts hnep: switch; do case "$switch" in n) LINE_READ_MODE=yes ;; e) DIE_ON_ERROR=yes ;; p) PIDFILE=$OPTARG ;; h) cat< DESCRIPTION Shell-script daemon that listens on $DEVICE and uses the script 'speak' to synthesize any text written to it. $DEVICE must be a named-pipe. USAGE $BASENAME [-n] [-e] [-p ] OPTIONS -n Reads single lines from $DEVICE and synthesize them. Otherwise, $BASENAME will wait until the named pipe is closed on the remote end and then speak the complete text. Note: in line-reading mode, some speak-filters will not work (i.e. the filter that removes linefeeds from the pipe). -e Exit on error. If an error occurs during synthesis, $BASENAME will terminate until the issue is resolved. If not specified, $BASENAME will continue waiting for data and try again. -p Use the specified file as pid file. Default is $PIDFILE. EOF exit ;; esac done # Create the pid file test -z "$PIDFILE" || echo $$ > "$PIDFILE" # Set exit trap trap finalize 0 if [ "$LINE_READ_MODE" = "yes" ]; then while true; do while read line; do [ -z "$line" ] || { echo "$line" | $SPEAK $SPEAK_PARAMS || { [ "$DIE_ON_ERROR" = yes ] && exit 1 } [ -z "$ACTION" ] || $ACTION } done < $DEVICE done else while true; do $SPEAK $SPEAK_PARAMS < $DEVICE || { [ "$DIE_ON_ERROR" = yes ] && exit 1 } [ -z "$ACTION" ] || $ACTION done fi # We're done here exit 0