/*
* Created on 2004/07/26
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.playdict;
import java.applet.AudioClip;
import java.awt.MediaTracker;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
/**
*
*
*/
public class SoundWord {
private File soundDir;
public File getSoundDir() {
return soundDir;
}
public void setSoundDir(File soundDir) {
this.soundDir = soundDir;
}
public static final int AUTO=0;
public static final int PLAIN=2;
public static final int ALPHABET=1;
private int type;
private File freettsZipFile;
private boolean allowFreeTTS=false;
private AudioClip audioClip;
public boolean isAllowFreeTTS() {
return allowFreeTTS;
}
public void setAllowFreeTTS(boolean allowFreeTTS) {
this.allowFreeTTS = allowFreeTTS;
}
public File getFreettsZipFile() {
return freettsZipFile;
}
public void setFreettsZipFile(File freettsZipFile) {
this.freettsZipFile = freettsZipFile;
}
public SoundWord(File soundDir,int type){
this.soundDir=soundDir;
this.type=type;
}
public boolean isExist(String word){
File file=getSoundFile(word);
if(file!=null && file.exists()){
return true;
}
if(allowFreeTTS){
return isEntry(word);
}
return false;
}
/**
* @param word
* @return
*/
private ZipEntry getZipEntry(ZipFile zipFile,String word){
ZipEntry entry=null;
if(freettsZipFile==null || !freettsZipFile.exists()){
return null;
}
String sub=""+word.charAt(0);
//System.out.println(sub.toLowerCase()+"/"+word.toLowerCase()+".wav");
entry=zipFile.getEntry(sub.toLowerCase()+"/"+word.toLowerCase()+".wav");
return entry;
}
private boolean isEntry(String word) {
//System.out.println("start "+word);
ZipFile zipFile;
ZipEntry zipEntry=null;
if(freettsZipFile!=null && freettsZipFile.exists()){
// System.out.println("entry check "+freettsZipFile.getAbsolutePath());
try {
zipFile = new ZipFile(freettsZipFile);
zipEntry=getZipEntry(zipFile,word);
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//System.out.println(zipEntry!=null);
return zipEntry!=null;
}
public File getSoundFile(String word){
if(word.length()<=0){
return null;
}
File result=null;
try{
if(type==AUTO){
result=new File(soundDir,word.toLowerCase()+".wav");
if(!result.exists()){
String sub=""+word.charAt(0);
result=new File(soundDir,sub.toLowerCase()+System.getProperty("file.separator")+word.toLowerCase()+".wav");
}
}
else if(type==PLAIN){
result=new File(soundDir,word.toLowerCase()+".wav");
}else if(type==ALPHABET){
String sub=""+word.charAt(0);
result=new File(soundDir,sub+System.getProperty("file.separator")+word.toLowerCase()+".wav");
}
}catch(Exception e){
e.printStackTrace();
}
return result;
}
public synchronized void playSound(String word){
File audioFile=getSoundFile(word);
//System.out.println(audioFile.getAbsolutePath());
if(audioFile!=null && audioFile.exists()){
// AudioInputStream inputStream=null;
/*
try {
inputStream = AudioSystem.getAudioInputStream(audioFile);
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(inputStream!=null){
playSound(inputStream);
}*/
try {
URL url;
url = new URL("file:///"+audioFile.getAbsolutePath().replaceAll("\\\\","/"));
if(audioClip!=null){
audioClip.stop();
audioClip=null;
}
audioClip = java.applet.Applet.newAudioClip(url);
audioClip.play();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
if(freettsZipFile!=null && allowFreeTTS){
ZipFile zipFile;
try {
// System.out.println("search "+word.toLowerCase()+".wav");
zipFile=new ZipFile(freettsZipFile);
ZipEntry entry=getZipEntry(zipFile,word);
if(entry!=null){
//System.out.println("entry");
InputStream input=zipFile.getInputStream(entry);
playSound(AudioSystem.getAudioInputStream(input));
}
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private void playSound(AudioInputStream inputStream){
try{
//AudioInputStream inputStream =
// AudioSystem.getAudioInputStream(audioFile);
AudioFormat format = inputStream.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class,format);
SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format);
line.start();
int readBytes = 0;
byte[] bytedata = new byte[1024];
while (readBytes != -1) {
readBytes = inputStream.read(bytedata, 0, bytedata.length);
if (readBytes >= 0) {
//int writeBytes =
line.write(bytedata, 0, readBytes);
}
}
line.drain();
line.close();
inputStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
|