本文將展示如何建立一個跨平台的IRC機器人(一個自動化的用戶端),它使用FreeTTS Java語音合成庫來將IRC訊息轉換成聽得見的語音。 FreeTTS庫可以在freetts.sourceforge.net下載。為了串連到 IRC伺服器上,你需要在www.jibble.org/pircbot.php下載PircBot Java IRC API。 當你下載了所需的兩個庫之後,建立一個lib目錄,然後將下列.jar檔案拷貝到裡面。 ·cmu_time_awb.jar
·cmu_us_kal.jar
·cmulex.jar
·cmutimelex.jar
·en_us.jar
·freetts.jar
·pircbot.jar 現在,寫IRC機器人已經變成了一個簡單的任務,因為這些庫將為你完成多數的困難工作。建立一個名為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 {
private Voice voice;
public SpeechBot(String say) {
//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);
float wpm = 120f;//設定語速
voice.setRate(wpm);
voice.setPitch(85f);
voice.setPitchRange(10f);
//合成語音
voice.startBatch();
voice.speak(say);
voice.endBatch();
//在這裡可以獲得合成語音檔案
//釋放資源
voice.deallocate();
}
public void onMessage(String channel, String sender,
String login, String hostname, String message) {
// Send all IRC messages to the voice
synthesizer.
// 發送所有的IRC訊息到語音合成器
message = message.trim();
String input = sender + " on " + channel + "
says: " + message;
voice.speak(input);
}
public static void main(String[] args) throws Exception {
SpeechBot bot = new SpeechBot("SpeechBot");
}}