Sunday, 21 August 2016

IRC Text to Speech with Java

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.

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.

Once you have downloaded both of the required libraries, create a lib directory and copy the following .jar files into it:
  • cmu_time_awb.jar
  • cmu_us_kal.jar
  • cmulex.jar
  • cmutimelex.jar
  • en_us.jar
  • freetts.jar
  • pircbot.jar
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

import org.jibble.pircbot.*;
import com.sun.speech.freetts.*;
import javax.sound.sampled.*;

public class SpeechBot extends PircBot {

    private Voice voice;
    public SpeechBot(String name) {

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

        if (voice == null) {
            System.out.println("Voice not found.");
        // Set up the output format.
        AudioPlayer voicePlayer = new JavaClipAudioPlayer();
        voicePlayer.setAudioFormat(new AudioFormat(8000,
16, 1, false, true));
    public void onMessage(String channel, String sender,
String login, String hostname, String message) {
        // Send all IRC messages to the voice
        message = message.trim();
        String input = sender + " on " + channel + "
says: " + message;
    public static void main(String[] args) throws
Exception {
        if (args.length < 2) {
            System.out.println("Usage: java SpeechBot
<server> <channel>");
        SpeechBot bot = new SpeechBot("SpeechBot");

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

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