/*
* PlayerModelImpl.java file will use this class to Generate the Sound file.
*/
/**
*
* @author kpham
*/
package virtualnewsreader;
import java.io.File;
import javax.swing.JFileChooser;
import com.sun.speech.freetts.audio.SingleFileAudioPlayer;
import javax.sound.sampled.AudioFileFormat;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.swing.JOptionPane;
public class TextToSoundFile {
private PlayerModelImpl PlayerModel = null;
public TextToSoundFile(PlayerModelImpl PlayerM)
{
PlayerModel = PlayerM;
}
public void StartConvert(String text)
{
String audioFile = OpenFileToSave();
if(audioFile == null)
return;
SynthesizerModeDesc desc = (SynthesizerModeDesc) PlayerModel.getSynthesizer().getEngineModeDesc();
javax.speech.synthesis.Voice[] jsapiVoices = desc.getVoices();
javax.speech.synthesis.Voice jsapiVoice = null;
for(int i=0; i<jsapiVoices.length; i++)
{
if(jsapiVoices[i].getName().equals(PlayerModel.getVoice().getName()))
jsapiVoice = jsapiVoices[i];
}
/* Non-JSAPI modification of voice audio player
*/
if (jsapiVoice instanceof com.sun.speech.freetts.jsapi.FreeTTSVoice) {
AudioFileFormat.Type type = PlayerModel.getAudioType(audioFile);
com.sun.speech.freetts.Voice freettsVoice =
((com.sun.speech.freetts.jsapi.FreeTTSVoice) jsapiVoice).getVoice();
SingleFileAudioPlayer player = new SingleFileAudioPlayer(PlayerModel.getBasename(audioFile), type);
freettsVoice.setAudioPlayer(player);
freettsVoice.startBatch();
if (!freettsVoice.speak(text)) {
JOptionPane.showMessageDialog(null, "Sorry, the application unable to convert your text into sound file!", "Failed to convert to Sound", JOptionPane.ERROR_MESSAGE);
}
freettsVoice.endBatch();
player.close();
freettsVoice.setAudioPlayer(new com.sun.speech.freetts.audio.JavaStreamingAudioPlayer() );
}
String finishMsg = "Your sound file have saved to "+audioFile;
JOptionPane.showMessageDialog(null,finishMsg, "Done Export sound file", JOptionPane.ERROR_MESSAGE);
}
public String OpenFileToSave()
{
String filePath = null;
String osName = System.getProperty("os.name");
JFileChooser c = new JFileChooser();
c.setFileFilter(new myFileFilter(".aif","AIFF/Amiga/Mac audio files (*.aif)"));
c.setFileFilter(new myFileFilter(".wav","WAV audio files (*.wav)"));
c.setFileFilter(new myFileFilter(".au","ULAW (Sun) audio files (*.au)"));
int rVal = c.showSaveDialog(null);
if (rVal == JFileChooser.APPROVE_OPTION) {
String seperator = "";
if(osName.startsWith("Windows"))
seperator = "\\";
else
seperator = "/";
filePath = c.getCurrentDirectory().toString() + seperator +
c.getSelectedFile().getName() +
( (myFileFilter)c.getFileFilter()).getExtention();
}
if (rVal == JFileChooser.CANCEL_OPTION) {
filePath = null;
}
return filePath;
}
/**
* Inner Class for FileFilter within the file Chooser.
*/
class myFileFilter extends javax.swing.filechooser.FileFilter
{
private String extention = "";
private String description = "";
public myFileFilter(String ext, String desc){
extention = ext;
description = desc;
}
@Override
public boolean accept(File f) {
if (f.isDirectory() || (f.getName().toLowerCase().endsWith(extention) ))
return true;
else
return false;
}
@Override
public String getDescription() {
return description;
}
public String getExtention()
{
return extention;
}
}
}