Need help writing non-gui JMF program
I need to write a program that will convert a WAV file from a ULAW format to a GSM WAV file format. I first tried this with the Java Sound API and the WAV file format of GSM was not supported so I stumbled across JMF.
I can use the JMStudio and Export GUI programs that come with JMF to convert the wav file as needed. But now I'm trying to write a batch program (non-gui) to convert the file on the fly.
First, is this possible. Second, does anyone have any code samples of a batch java program using JMF in batch.
Thanks,
Scott
# 2
Not exactly what I was looking for. After 2 days of beating my head against the wall this is what I got to work. Include the jmf.jar in my project. Here is my code.
import java.io.IOException;
import javax.media.DataSink;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoDataSourceException;
import javax.media.Processor;
import javax.media.control.TrackControl;
import javax.media.format.AudioFormat;
import javax.media.protocol.DataSource;
import javax.media.protocol.FileTypeDescriptor;
import jmapps.util.StateHelper;
public class TestAudio5 {
public static void main(String[] args) {
try {
Processor p = null;
StateHelper sh = null;
DataSource inSource = Manager.createDataSource(
new MediaLocator("file:c:\\ASSURANCE_THREE_MINUTE.wav"));
p = Manager.createProcessor(inSource);
sh = new StateHelper(p);
//Configure the processor
if (!sh.configure(10000)) {
System.out.println("can't configure");
System.exit(-1);
}
p.setContentDescriptor(new
FileTypeDescriptor(FileTypeDescriptor.WAVE));
//AudioFormat(java.lang.String encoding, double sampleRate, int sampleSizeInBits, int channels)
//AudioFormat(
//java.lang.String encoding,
//double sampleRate,
//int sampleSizeInBits,
//int channels,
//int endian,
//int signed,
//int frameSizeInBits,
//double frameRate,
//java.lang.Class dataType)
AudioFormat outputFormat = new javax.media.format.AudioFormat(
AudioFormat.GSM_MS,
8000.0,
0,
1,
0,
1,
520,
1625.0,
null);
System.out.println("outputFormat: " + outputFormat.toString());
TrackControl tc[] = p.getTrackControls();
for ( int i = 0; i < tc.length; i++ ) {
tc.setEnabled(true);
System.out.println("before format: " + tc.getFormat().toString());
tc.setFormat(outputFormat);
System.out.println("after format: " + tc.getFormat().toString());
}
if (!sh.realize(10000)) {
System.out.println("can't realize");
System.exit(-1);
}
// get the output of the processor
DataSource source = p.getDataOutput();
// create a File protocol MediaLocator with the location
// of the file to which bits are to be written
MediaLocator dest = new MediaLocator("file://c:\\test.wav");
// create a datasink to do the file writing & open the
// sink to make sure we can write to it.
DataSink filewriter = null;
filewriter = Manager.createDataSink(source, dest);
filewriter.open();
// now start the filewriter and processor
filewriter.start();
sh.playToEndOfMedia(5000);
sh.close();
filewriter.close();
} catch (NoDataSourceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("done!");
System.exit(-1);
}
}
# 3
Try this
AudioFormat outputFormat = new javax.media.format.AudioFormat(
AudioFormat.GSM_MS, 8000.0, 0, 1, 0, 1, 520, 1625.0, null);
//System.out.println("outputFormat: " + outputFormat.toString());
TrackControl tc[] = p.getTrackControls();
//ONLY expecting one format to be available
if(tc.length == 1){
System.out.println("TrackControl[" + 0 + "] - " + tc[0].getFormat().toString());
tc[0].setFormat(outputFormat);
}
else{
for (int i = 0; i < tc.length; i++) {
System.out.println("TrackControl[" + i + "]" + tc[i].getFormat().toString());
//
//This code fails to compile
//tc.setEnabled(true);
//System.out.println("before format: " + tc.getFormat().toString());
//tc.setFormat(outputFormat);
//System.out.println("after format: " + tc.getFormat().toString());
//This code fails to compile
//
//I think you want something like
//find a track that is an AudioFormat & set it to the format you want as the
//output of your processor
if(tc[i].getFormat() instanceof AudioFormat){
tc[i].setFormat(outputFormat);
break;//have set the output format, so break out of loop
}
}
}
if (!sh.realize(10000)) {
System.out.println("can't realize");
System.exit(-1);
}
// get the output of the processor
DataSource source = p.getDataOutput();
//Now that the processor has been realized - check what were going to produce
System.out.println(p.getContentDescriptor().toString());
TrackControl tcOut[] = p.getTrackControls();
//ONLY expecting one format to be available
for (int i = 0; i < tcOut.length; i++) {
System.out.println("TrackControl_Out[" + i + "] - " + tcOut[0].getFormat().toString());
}
// create a File protocol MediaLocator with the location
// of the file to which bits are to be written
MediaLocator dest = new MediaLocator("file://c:\\test.wav");
// create a datasink to do the file writing & open the
// sink to make sure we can write to it.
DataSink filewriter = null;
it produces
Open log file: c:\Logs\jmf.log
TrackControl[0] - LINEAR, 22050.0 Hz, 16-bit, Stereo, LittleEndian, Signed, 88200.0 frame rate, FrameSize=32 bits
WAV
TrackControl_Out[0] - gsm/ms, 8000.0 Hz, 0-bit, Mono, Signed, 1625.0 frame rate, FrameSize=520 bits
done!