Skip to content

Instantly share code, notes, and snippets.

@miloh
Created July 4, 2015 22:18
Show Gist options
  • Save miloh/3a53223b8e79812a0bb1 to your computer and use it in GitHub Desktop.
Save miloh/3a53223b8e79812a0bb1 to your computer and use it in GitHub Desktop.
sayingthings was a script for irc TTS with festival, used by noisebridgers from 2010-2012
#!/bin/sh
ssh -p 9595 user@persistentserver "tail -1f irc.log.Window1" | perl -lne 'BEGIN{$|++} print "$1 says $2" if m/^[^<]+<.(\S+)> ([^[]+)$/; print "$1 $2" if m/^[^<]+ \* ((\S+) ([^[].+))$/' | while read f; do echo "(SayText \"$f\")"; done | festival
@hzeller
Copy link

hzeller commented Jul 5, 2015

#!/bin/bash

SPEECH_ENGINE=google

usage() {
    echo "Usage: $0 [-e{espeak,google,festival}] <channel-name>"
    exit 1
}

while getopts "e:" OPT ; do
    case $OPT in
    e)
        SPEECH_ENGINE=$OPTARG ;;
    [?])
        usage ;;
    esac
done
shift $(($OPTIND-1))


case "$SPEECH_ENGINE" in
    google) SAY=say_google ;;
    espeak) SAY=say_espeak ;;
    festival) SAY=say_festival ;;
    *) usage ;;
esac

if [ $# -ne 1 ] ; then
    usage
fi

CHANNEL=$1
IRC_LOG=~/logs/freenode_#${CHANNEL}.log

if [ ! -r "$IRC_LOG" ] ; then
    echo "No logfile ${IRC_LOG}"
    exit 2
fi

# Various experimental ways to say something.
# Common 'API' is to get lines from stdin and speak them.

say_google() {
    while read line ; do
    p=$(echo "$line" | sed 's/ /%20/g')
    wget -q -U Mozilla -O- "http://translate.google.com/translate_tts?tl=en&q=$p" \
          | mpg123 -q /dev/stdin
    done
}

say_espeak() {
    espeak > /dev/null 2>&1
}

say_festival() {
    (while read line ; do
     p=$(echo "$line" | sed 's/"/\\"/g')
    echo "(SayText \"$p\")"
    done) | festival
}

# The lines in the logfile look like this
# [Sunday, July 05, 2015] [01:52:08] <hzeller>  hello world
# Extract user and message.
extract() {
    awk -f <(cat - <<'EOF'
{ user=gensub(/<(.*)>/, "\\1", "", $6);  # Field 6 is user, get rid of <> brackets
  message=substr($0, index($0, $7));     # Rest of line after that.
  now=systime();
  # don't repeat the user if we just said the name
  if (last_user == user && now < last_time + 10) {
      print message;
  } else {
      print user " says " message;
  }
  last_time=now;
  last_user=user;
  fflush();
}
EOF
)
}

# Alright, let's do it
tail -f "${IRC_LOG}" | extract | $SAY

@hzeller
Copy link

hzeller commented Jul 5, 2015

(my thing where I experiment with different speak engines)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment