/*
* Created on 2004/08/13
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.wavconvert;
import java.io.File;
import java.util.List;
import java.util.Vector;
import org.jpn.xucker.commons.swt.series.ResultData;
import org.jpn.xucker.commons.swt.series.wfs.WrapperExecuter;
import org.jpn.xucker.html.URLPathNameUtil;
/**
*
*
*/
public class SoxExecuter extends WrapperExecuter{
private SoxWrapper wrapper;
public SoxWrapper getWrapper() {
return wrapper;
}
public void setWrapper(SoxWrapper wrapper) {
this.wrapper = wrapper;
}
private List audioFormats=new Vector();
private boolean overwrite;
public boolean isOverwrite() {
return overwrite;
}
public void setOverwrite(boolean overwrite) {
this.overwrite = overwrite;
}
public void addAudioFormat(AudioFormat format){
audioFormats.add(format);
}
public AudioFormat[] getFormats(){
return (AudioFormat[])audioFormats.toArray(new AudioFormat[audioFormats.size()]);
}
/* (non-Javadoc)
* @see org.jpn.xucker.commons.swt.series.wfs.WrapperExecuter#setResultValue(org.jpn.xucker.commons.swt.series.ResultData, java.lang.String, java.io.File, java.io.File, java.io.File)
*/
List createdFiles=new Vector();
public void inCallCommand(File inputFile,File outputFile,AudioFormat format){
Runtime runtime = Runtime.getRuntime();
String args[]=createSoxArgs(inputFile,outputFile,format);
File tmpOutput=new File(args[args.length-1]);
if(tmpOutput.exists() && overwrite==false){
if(! wrapper.doOverwriteDialog(tmpOutput)){
return; //don't execute.
}
}
createdFiles.add(tmpOutput);
try {
if(isPrint){
printArgs(args);
}
if(!isShowOnly){
Process process = runtime.exec(args);
if(isDebug){
System.out.println("[log]");
System.out.println(readAll(process.getInputStream()));
}
process.waitFor();
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void callCommand(File inputFile, File outputFile) {
if(!outputFile.exists()){
outputFile.getParentFile().mkdirs();
}
AudioFormat[] formats=getFormats();
for(int j=0;j<formats.length;j++){
inCallCommand(inputFile,outputFile,formats[j]);
}
}
/**
* @param inputFile
* @param outputFile
* @param format
* @return
*/
private String[] createSoxArgs(File inputFile, File outputFile, AudioFormat format) {
Vector argVector=new Vector();
int argIndex=0;
argVector.add(commandPath);
//input
argVector.add(inputFile.getAbsolutePath());
//type;
if(!format.getType().equals("au")){
//
argVector.add("-s");
}
//wav
if(format.getBit()==8){
argVector.add("-b");
argVector.add("-u");
}else{
//16bit
argVector.add("-w");
}
argVector.add("-r");
argVector.add(""+format.getRate());
argVector.add("-c");
argVector.add(""+format.getChannel());
argVector.add(toOutputFile(outputFile,format));
return (String[])argVector.toArray(new String[argVector.size()]);
}
public String toOutputFile(File outputFile,AudioFormat format){
String fName=URLPathNameUtil.removeExtension(outputFile.getName());
fName+="_"+format.getBit()+"bit_"+format.getRate()+"hz_";
if(format.getChannel()==1){
fName+="mono";
}else{
fName+="stereo";
}
fName+="."+format.getType();
return new File(outputFile.getParentFile(),fName).getAbsolutePath();
}
/**
* @param rootFolder
* @param output2
* @param file
*/
protected void setResultValue(ResultData tmp, String rootPath, File outputDir, File input, File output) {
// TODO Auto-generated method stub
File[] outputFiles=(File[])createdFiles.toArray(new File[createdFiles.size()]);
for (int i = 0; i < outputFiles.length; i++) {
ResultData result=new ResultData();
if(outputFiles[i].exists()){
result.setValue(resultLabel[0],"");
}else{
result.setValue(resultLabel[0],"s");
}
result.setValue(resultLabel[1],""+toKByte(outputFiles[i].length())+"k");
result.setValue(resultLabel[2],outputFiles[i].getName());
result.setValue(resultLabel[3],outputFiles[i].getParent());
resultVector.add(result);
createdFiles.remove(outputFiles[i]);
}
tmp.setValid(false);
}
/**
* @param l
* @return
*/
private int toKByte(long bytes) {
// TODO Auto-generated method stub
int result=(int)(bytes/1024);
return result;
}
protected String[] createArgs(File inputFile, File outputFile) {
String args[]=null;
//don't use this class.
return args;
}
}
|