Java convert uLaw encoded audio file to PCM ecoded
-
Hi, I want to convert a ULAW encoded .wav file into a PCM encoded one. The ULAW encoded .wav file is of 8 bit, mono with 8Khz sample rate. How can I achieve this. I have written a code below but it gives error that the conversion is not supported. I am using javax.sound.sampled class. below is the code.
try{
File temp = new File("C:/temp.wav"); File fileout = new File("C:/1java.wav"); AudioInputStream sourceaudio = AudioSystem.getAudioInputStream(temp); AudioFormat targetformat = new AudioFormat(new AudioFormat.Encoding("PCM\_UNSIGNED"),8000,16,0,8,8000,true); AudioFileFormat.Type targettype = AudioFileFormat.Type.WAVE; AudioInputStream targetaudiostream = AudioSystem.getAudioInputStream(targetformat,sourceaudio); AudioSystem.write(targetaudiostream, targettype, fileout); } catch(Exception e){ out.println("Error in audio format conversion"); e.printStackTrace(); }
-
Hi, I want to convert a ULAW encoded .wav file into a PCM encoded one. The ULAW encoded .wav file is of 8 bit, mono with 8Khz sample rate. How can I achieve this. I have written a code below but it gives error that the conversion is not supported. I am using javax.sound.sampled class. below is the code.
try{
File temp = new File("C:/temp.wav"); File fileout = new File("C:/1java.wav"); AudioInputStream sourceaudio = AudioSystem.getAudioInputStream(temp); AudioFormat targetformat = new AudioFormat(new AudioFormat.Encoding("PCM\_UNSIGNED"),8000,16,0,8,8000,true); AudioFileFormat.Type targettype = AudioFileFormat.Type.WAVE; AudioInputStream targetaudiostream = AudioSystem.getAudioInputStream(targetformat,sourceaudio); AudioSystem.write(targetaudiostream, targettype, fileout); } catch(Exception e){ out.println("Error in audio format conversion"); e.printStackTrace(); }
I personally did not try this but I found this link that will solve your problem http://www.mms-computing.co.uk/uk/co/mmscomputing/sound/ Do tell if you figure it out
-
I personally did not try this but I found this link that will solve your problem http://www.mms-computing.co.uk/uk/co/mmscomputing/sound/ Do tell if you figure it out
Hi, I was able to solve it using the inherent java audio class javax.sound.sampled. I convert an input audio file encoded in uLaw to PCM and then write the PCM file into an Audiostream. Code is given below.
try{
File filein = new File("C:/sourcefile.wav");
File fileout = new File("C:/PCMfile.wav");
AudioInputStream sourceaudio = AudioSystem.getAudioInputStream(filein);
AudioFormat format = sourceaudio.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits() * 2,
format.getChannels(),
format.getFrameSize() * 2,
format.getFrameRate(),
true);
}
AudioFileFormat.Type targettype = AudioFileFormat.Type.WAVE;
AudioInputStream targetaudiostream = AudioSystem.getAudioInputStream(format, sourceaudio);
AudioSystem.write(targetaudiostream, targettype, fileout);
System.out.println("PCM encoded file generated successfully.");
sourceaudio.close();
targetaudiostream.close();
} catch (Exception e) {
out.println("Error in audio format conversion");
e.printStackTrace();
}