jump to navigation

FreeTTS, convertir textos a voz… Agosto 21, 2007

Posted by superpiwi in Eclipse, Java, Programacion.
trackback

Hoy algo divertido, vamos a convertir un texto en voz. Para ello hacemos uso del siguiente API: FreeTTS

Descargate el paquete desde su sitio web y lo descomprimes, Tienes que añadir a tu CLASSPATH los siguientes ficheros:

cmu_time_awb.jar, cmu_us_kal.jar, cmudict04.jar, cmulex.jar, cmutimelex.jar, en_us.jar, freets.jar, jsapi.jar

Aqui te dejo un ejemplo sencillito. He escrito una clase SimpleTTS. Con un constructor donde le pasas la voz que quieres usar (”En el ejemplo Kevin16) y 2 metodos: speak (que te reproduce por el altavoz el texto especificado) y toFile (que te graba un fichero de audio).

Ejemplo:

SimpleTTS voz = new SimpleTTS(”kevin16″);
voz.speak(”Hello World!”);
voz.close();


/**
*
* 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("Text to Speech demo");
// 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

Comentarios»

1. Ubuntu Life » Blog Archive » Festival, Convertir un texto a voz - Agosto 29, 2007

[...] Si te interesa para Java, hace algunos dias escribi algo parecido, pero empleando un API llamado FreeTTS. Aqui el enlace. [...]

2. dd - Octubre 17, 2007

hola

3. dd - Octubre 17, 2007

hola hola

4. superpiwi - Octubre 19, 2007

for (int i=0;i<3;i++)
{
System.out.println(”Hola”);
}
//—

;-)

5. lorena - Octubre 22, 2007

hola necesito q me ayuden a convertir el texto a voz sobre un aplicacion q tengo hecha en java..gracias

6. superpiwi - Octubre 25, 2007

¿Que necesitas hacer?. Echale de todas formas un vistazo a este API tal vez te sirva.

7. lorena - Noviembre 8, 2007

Y para Hablar en español ?

8. superpiwi - Noviembre 13, 2007

ah, en eso estoy yo tambien ;-) , hace falta un engine TTS en español. pero los que conozco no son libres. asi que no los uso. Pero si encuentro alguno, lo comentare aca.

9. AlboradaJose - Noviembre 28, 2007

Hola me pueden ayudar lo que pasa es que quiero convertir un texto en voz pero para el idioma Espñol.

10. Tuska - Enero 6, 2008

Hola, yo también necesitaba convertir texto en voz en Español. Si alguien ha encontrado algo, por favor, que me avise. Un saludo

11. aap - Abril 9, 2008

hola necesito ayuda!!!
Como puedo trabajar con TTS Festival y FreeTTS, por favor pueden ayudarme con información…
Saludos
annabell447@hotmail.com

12. Roger - Septiembre 9, 2008

Hola quisiera saber si alguien encontro el Engine TTS en español. porfa estoy desarrollando una aplicacion y lo necesito

13. Dav!d - Febrero 27, 2009

RESPONDAN por favor se necesita la voz en español, como se lo importo a mi aplicacion usando freetts, si podrian responder al email dmcoding@hotmail.com GRACIAS

14. Noel Pimentel - Marzo 12, 2009

Hola…. ta weno el programita…. si junciona… nomás ke pos toy chkando en español… gracias… me sirve… y mucho…

15. Francisco Nicolás - Abril 14, 2009

seeeee el programa esta chido y como importo la voz en español de festival al freetts , es para pasar la materia.
Si alguien sabe contesten a mi correro se los agradecere.

16. Francisco Nicolás - Abril 14, 2009

aqui esta mi correro jajaja fnicolas_guillen@hotmail.com

17. karina - Junio 7, 2009

hola yo tambien estoy necesitando la voz en español para mi programita con fretts , si me la pueden mandar a mi mail se los agradezzo . gracias