I have this code that converts audio to different file formats.
import java.io.File;
import it.sauronsoftware.jave.AudioAttributes;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.EncoderException;
import it.sauronsoftware.jave.EncodingAttributes;
import it.sauronsoftware.jave.InputFormatException;
/* required jars(jave-1.0.2.jar)
* Handles Encoding and Decoding
* Audio to different file formats
*/
public class AudioEncoderDecoder {
private static final Integer bitrate = 256000;//Minimal bitrate only
private static final Integer channels = 2; //2 for stereo, 1 for mono
private static final Integer samplingRate = 44100;//For good quality.
/* Data structures for the audio
* and Encoding attributes
*/
private AudioAttributes audioAttr = new AudioAttributes();
private EncodingAttributes encoAttrs = new EncodingAttributes();
private Encoder encoder = new Encoder();
/*
* File formats that will be converted
* Please Don't change!
*/
private String oggFormat = "ogg";
private String mp3Format = "mp3";
private String wavFormat = "wav";
/*
* Codecs to be used
*/
private String oggCodec = "vorbis";
/* Set the default attributes
* for encoding
*/
public AudioEncoderDecoder(){
audioAttr.setBitRate(bitrate);
audioAttr.setChannels(channels);
audioAttr.setSamplingRate(samplingRate);
}
public void encodeAudio(File source, File target, String mimeType){
//Change the hardcoded mime type later on
if(mimeType.equals("audio/mp3")){
this.mp3ToOgg(source, target);
}
}
private void mp3ToOgg(File source, File target){
//ADD CODE FOR CHANGING THE EXTENSION OF THE FILE
encoAttrs.setFormat(oggFormat);
audioAttr.setCodec(oggCodec);
encoAttrs.setAudioAttributes(audioAttr);
try{
encoder.encode(source, target, encoAttrs);
}catch(Exception e){
System.out.println("Encoding Failed");
}
}
private void oggToMp3(){
//ADD CODE FOR CHANGING THE EXTENSION OF THE FILE
encoAttrs.setFormat(mp3Format);
audioAttr.setCodec(mp3Codec);
encoAttrs.setAudioAttributes(audioAttr);
try{
encoder.encode(source, target, encoAttrs);
}catch(Exception e){
System.out.println("Encoding Failed");
}
}
public static void main(String[] args){
AudioEncoderDecoder aed = new AudioEncoderDecoder();
File source = new File("singelementsmp3.mp3");
File target = new File("test.ogg");
//Test Mp3 To Ogg Convertion
String mimeType = "audio/mp3";
aed.encodeAudio(source, target, mimeType);
//Test Ogg To Mp3 Convertion
}
}
Now, the xToX conversion basically the same throughout the whole code implementation, the only thing will change is the codec. what is another way to make this code cleaner ? from my point of view it repeats itself, what should I change? or are there any design patterns I should implement?
