ONJava.com    
 Published on ONJava.com (http://www.onjava.com/)
 See this if you're having trouble printing code examples


IRC Text to Speech with Java IRC Text to Speech with Java

by Paul Mutton, author of IRC Hacks
02/02/2005

Internet Relay Chat (IRC) is a network chat system used by millions of people all over the world. It has been around for several years and is used by groups of friends, programmers, universities, and even banks to facilitate discussions, the exchanging of ideas, and collaborative research.

Because IRC is very much a real-time chat system, you will rarely benefit from using it unless you are able to pay close attention to the sequence of dialog as it transpires. Unfortunately, this can lead to a lack of productivity in the workplace, which is why many employers naively frown upon the use of IRC. However, when used properly, IRC can let employees work effectively with remote colleagues, regardless of whether they are on the other side of the planet or just in the next building. Real-time group chat systems like IRC make it easier to organize meetings (and possibly even carry out virtual meetings), ask questions, and to negotiate the less-important things such as where to go for lunch.

Many bosses would argue that the most obvious way to boost productivity in the workplace is to avoid using IRC altogether. While this may be true to some extent, there really are cases where IRC can be useful. When you carry out a conversation on IRC--even if it's with someone in the same office--your colleagues elsewhere will be able to see what you are both saying. You will also be able to see what was discussed while you were away from your desk. These are both important aspects that cannot be achieved easily using a telephone or conventional peer-to-peer chat system.

Related Reading

IRC Hacks
100 Industrial-Strength Tips & Tools
By Paul Mutton

The ideal solution is to engineer a way to keep track of IRC dialog without having to constantly check to see if there are any new messages to read. As long as your IRC channel is not too busy, a text-to-speech system provides an excellent solution. By reading out messages as they arrive, you will be able to continue working and only divert your attention to IRC when absolutely necessary.

This article will show you how to create a multi-platform IRC bot (an automated client) that uses the FreeTTS Java speech synthesizer library to convert IRC messages into audible speech.

The FreeTTS library can be downloaded from freetts.sourceforge.net. To connect to an IRC server, you will need to download the PircBot Java IRC API from www.jibble.org/pircbot.php.

Once you have downloaded both of the required libraries, create a lib directory and copy the following .jar files into it:

Writing the IRC bot is now a simple task, as these libraries will do most of the hard work for you. Create a file called SpeechBot.java:


import org.jibble.pircbot.*;
import com.sun.speech.freetts.*;
import com.sun.speech.freetts.audio.*;
import javax.sound.sampled.*;
import java.io.File;

public class SpeechBot extends PircBot {

    private Voice voice;
    
    public SpeechBot(String name) {
        setName(name);

        // Choose the voice for the speech synthesizer.
        String voiceName = "kevin16";
        VoiceManager voiceManager = 
VoiceManager.getInstance();
        voice = voiceManager.getVoice(voiceName);

        if (voice == null) {
            System.out.println("Voice not found.");
            System.exit(1);
        }

        voice.allocate();

        // Set up the output format.
        AudioPlayer voicePlayer = new JavaClipAudioPlayer();
        voicePlayer.setAudioFormat(new AudioFormat(8000, 
16, 1, false, true));
        voice.setAudioPlayer(voicePlayer);
    }    
    
    public void onMessage(String channel, String sender, 
String login, String hostname, String message) {
        // Send all IRC messages to the voice 
synthesizer.
        message = message.trim();
        String input = sender + " on " + channel + " 
says: " + message;
        voice.speak(input);
    }

    public static void main(String[] args) throws 
Exception {
        if (args.length < 2) {
            System.out.println("Usage: java SpeechBot 
<server> <channel>");
            System.exit(1);
        }
        SpeechBot bot = new SpeechBot("SpeechBot");
        bot.connect(args[0]);
        bot.joinChannel(args[1]);
    }

}

You can now compile the IRC bot. Make sure to include the necessary .jar files in the classpath:


javac -classpath .;./lib/pircbot.jar;./lib/freetts.jar 
SpeechBot.java

Note that this way of specifying the classpath will only work on Windows systems. On Unix/Linux systems, you will need to use a colon (:) instead of a semicolon (;) to separate the entries. You will also need to specify the classpath when running the IRC bot.

When running the bot, you will need to provide a couple of command-line parameters to tell it which IRC server to connect to and which IRC channel to join:


java -classpath .;./lib/pircbot.jar;./lib/freetts.jar 
SpeechBot irc.freenode.net #irchacks

Note: Make sure your directory names do not contain any spaces, as the version of FreeTTS used here seems to have trouble reading itself when there is a space in the path.

After you have launched the bot, it will shortly join the specified IRC channel (in this case, #irchacks on the freenode IRC network). You can now test the bot by sending a message to the channel. If everything is set up properly, you will now hear the bot speak to you:


"Jibbler on #irchacks says: Hello, world!"

Now you can carry on working hard while you listen to IRC!

Paul Mutton is the author of the PircBot IRC framework and several other Java programs that can be found on his web site.


In July 2004, O'Reilly Media released IRC Hacks.


Return to ONJava.com

Copyright © 2009 O'Reilly Media, Inc.