1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
/**
*
* FreeTTS
* requiere en el CLASSPATH: cmu_time_awb.jar, cmu_us_kal.jar, cmudict04.jar, cmulex.jar, cmutimelex.jar,
* en_us.jar, freets.jar, jsapi.jar
*
* @author jose
* @version 0.0.0.1
* @since JDK 1.5 / Eclipse Callisto
*/
import com.sun.speech.freetts.audio.AudioPlayer;
//import com.sun.speech.freetts.audio.JavaClipAudioPlayer;
//import com.sun.speech.freetts.audio.MultiFileAudioPlayer;
import com.sun.speech.freetts.audio.NullAudioPlayer;
//import com.sun.speech.freetts.audio.RawFileAudioPlayer;
import com.sun.speech.freetts.audio.SingleFileAudioPlayer;
//import java.io.*;
//import java.net.URL;
//import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioSystem;
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
//import com.sun.speech.freetts.audio.JavaClipAudioPlayer;
public class SimpleTTS
{
Voice voice=null;
public SimpleTTS(String voiceName) throws Exception
{
VoiceManager voiceManager = VoiceManager.getInstance();
this.voice = voiceManager.getVoice(voiceName);
if (this.voice == null)
{
System.out.println("La lista de voces disponibles es:");
listAllVoices();
throw new Exception("No se encuentra la voz llamada: "+voiceName+". Por favor selecciona una voz diferente.");
}
this.voice.allocate();
}
//-
public void speak(String text) throws Exception
{
this.voice.speak(text);
}
//-
public void toFile(String filename,String text) throws Exception
{
javax.sound.sampled.AudioFileFormat.Type type = getAudioType(filename);
AudioPlayer audioPlayer = null;
if(audioPlayer == null)
audioPlayer = new NullAudioPlayer();
audioPlayer = new SingleFileAudioPlayer(getBasename(filename), type);
System.out.println("audioPlayer "+audioPlayer);
this.voice.setAudioPlayer(audioPlayer);
this.voice.speak(text);
audioPlayer.close();
}
//-
public void close() throws Exception
{
this.voice.deallocate();
}
//-
public static void listAllVoices()
{
System.out.println();
System.out.println("All voices available:");
VoiceManager voiceManager = VoiceManager.getInstance();
System.out.println("voiceManager:"+voiceManager);
Voice[] voices = voiceManager.getVoices();
for (int i = 0; i < voices.length; i++) {
System.out.println(" " + voices[i].getName()
+ " (" + voices[i].getDomain() + " domain)");
}
}
//-
public static javax.sound.sampled.AudioFileFormat.Type getAudioType(String file)
{
javax.sound.sampled.AudioFileFormat.Type types[] = AudioSystem.getAudioFileTypes();
String extension = getExtension(file);
for(int i = 0; i < types.length; i++)
if(types[i].getExtension().equals(extension))
return types[i];
return null;
}
//-
public static String getExtension(String path)
{
int index = path.lastIndexOf(".");
if(index == -1)
return null;
else
return path.substring(index + 1);
}
//-
public static String getBasename(String path)
{
int index = path.lastIndexOf(".");
if(index == -1)
return path;
else
return path.substring(0, index);
}
//-
static public void main(String[] args) throws Exception
{
try
{
// Instanciamos para usar la voz "kevin16?
SimpleTTS voz = new SimpleTTS("kevin16");
// Reproduce por el altavoz
//while (true)
//{
//voz.speak("auan babuluba balan bambu");
voz.speak("Hello World!");
//}
voz.speak("test text to speech ");
// Graba un fichero de audio con el contenido en el directorio
voz.toFile("ttsdemo.wav", "Ubuntu Life is the best site of Linux");
// Podriamos reproducir este fichero desde consola: $ mplayer ttsdemo.wav
// Cerramos
voz.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//-
}
//end of class SimpleTTS |
Partager