Como posso armazenar a voz de saída para um arquivo de áudio em Freetts
-
26-09-2019 - |
Pergunta
Estou tentando usar o Freetts para um aplicativo Java simples, mas estou enfrentando um problema, alguém pode me dizer como posso salvar a voz de saída que é convertida do texto em fala em um arquivo de onda no meu programa. Eu quero fazer isso via código.
Este é o aplicativo de helloworld de amostra que é fornecido com a amostra
/**
* Copyright 2003 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and
* redistribution of this file, and for a DISCLAIMER OF ALL
* WARRANTIES.
*/
import com.sun.speech.freetts.FreeTTS;
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import com.sun.speech.freetts.audio.JavaClipAudioPlayer;
/**
* Simple program to demonstrate the use of the FreeTTS speech
* synthesizer. This simple program shows how to use FreeTTS
* without requiring the Java Speech API (JSAPI).
*/
public class FreeTTSHelloWorld {
/**
* Example of how to list all the known voices.
*/
public static void main(String[] args) {
// listAllVoices();
FreeTTS freetts;
String voiceName = "kevin16";
System.out.println();
System.out.println("Using voice: " + voiceName);
/* The VoiceManager manages all the voices for FreeTTS.
*/
VoiceManager voiceManager = VoiceManager.getInstance();
Voice helloVoice = voiceManager.getVoice(voiceName);
if (helloVoice == null) {
System.err.println(
"Cannot find a voice named "
+ voiceName + ". Please specify a different voice.");
System.exit(1);
}
/* Allocates the resources for the voice.
*/
helloVoice.allocate();
/* Synthesize speech.
*/
helloVoice.speak("Thank you for giving me a voice. "
+ "I'm so glad to say hello to this world.");
/* Clean up and leave.
*/
helloVoice.deallocate();
System.exit(0);
}
}
Este código está funcionando bem, eu quero salvar a saída como um arquivo de áudio no meu disco.
Obrigado Pranay
Solução
Eu descobri como fazer isso que você tem que simplesmente usar SingleFileAudioPlayer
Passe o nome do arquivo e o tipo de arquivo que você deseja que a declaração de amostra seja como:
audioPlayer = new SingleFileAudioPlayer("output",Type.WAVE);
Agora você precisa anexar o SinglefileAudioplayer
objetar ao seu VoiceManager
Objeto: por exemplo
helloVoice.setAudioPlayer(audioPlayer);
Agora usa:
hellovoice.speak("zyxss");
Isso salvará o arquivo com o que quer que esteja lá. Lembre -se de fechar o Audioplayer, caso contrário, o arquivo não será salvo. Colocar audioPlayer.close();
antes de sair.
Aqui está o código de trabalho completo que despejará o arquivo no seu diretório C
/**
* Copyright 2003 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and
* redistribution of this file, and for a DISCLAIMER OF ALL
* WARRANTIES.
*/
import com.sun.speech.freetts.FreeTTS;
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import com.sun.speech.freetts.audio.AudioPlayer;
import com.sun.speech.freetts.audio.SingleFileAudioPlayer;
import javax.sound.sampled.AudioFileFormat.Type;
/**
* Simple program to demonstrate the use of the FreeTTS speech
* synthesizer. This simple program shows how to use FreeTTS
* without requiring the Java Speech API (JSAPI).
*/
public class FreeTTSHelloWorld {
/**
* Example of how to list all the known voices.
*/
public static void main(String[] args) {
// listAllVoices();
FreeTTS freetts;
AudioPlayer audioPlayer = null;
String voiceName = "kevin16";
System.out.println();
System.out.println("Using voice: " + voiceName);
/* The VoiceManager manages all the voices for FreeTTS.
*/
VoiceManager voiceManager = VoiceManager.getInstance();
Voice helloVoice = voiceManager.getVoice(voiceName);
if (helloVoice == null) {
System.err.println(
"Cannot find a voice named "
+ voiceName + ". Please specify a different voice.");
System.exit(1);
}
/* Allocates the resources for the voice.
*/
helloVoice.allocate();
/* Synthesize speech.
*/
//create a audioplayer to dump the output file
audioPlayer = new SingleFileAudioPlayer("C://output",Type.WAVE);
//attach the audioplayer
helloVoice.setAudioPlayer(audioPlayer);
helloVoice.speak("Thank you for giving me a voice. "
+ "I'm so glad to say hello to this world.");
/* Clean up and leave.
*/
helloVoice.deallocate();
//don't forget to close the audioplayer otherwise file will not be saved
audioPlayer.close();
System.exit(0);
}
}
Outras dicas
Eu nunca usei Freetts, mas uma rápida varredura dos javadocs revela Voice.setWaveDumpFile (String). Isso faz o que é necessário?