Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

converting PCM format to ULAW format with Sample rate 8000

807589Sep 2 2008 — edited Sep 2 2008
*Hi All,
Im able to convert wav file of PCM audio format to ULAW audio format using the below code,*
import java.io.IOException;
import java.io.File;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class UlawEncoder{
	public static void main(String[] args){
		File	pcmFile = new File(args[0]);
		File	ulawFile = new File(args[1]);
		AudioInputStream	ais = null;
		
		try{
			ais = AudioSystem.getAudioInputStream(pcmFile);
		}catch (Exception e){
			e.printStackTrace();
		}
		
		AudioFormat.Encoding	targetEncoding = AudioFormat.Encoding.ULAW;
		AudioInputStream	ulawAudioInputStreamAIS = AudioSystem.getAudioInputStream(targetEncoding, ais);
		AudioFileFormat.Type	fileType = AudioFileFormat.Type.WAVE;
		int	nWrittenFrames = 0;
		try{
			nWrittenFrames = AudioSystem.write(ulawAudioInputStreamAIS, fileType, ulawFile);
		}catch (IOException e){
			e.printStackTrace();
		}
	}
}
And now i want to make the Audio Sample Rate to 8000 and i have done like the code below,

		
		try{
			ais = AudioSystem.getAudioInputStream(pcmFile);
			format = ais.getFormat();
			
			format = new AudioFormat(
                    AudioFormat.Encoding.ULAW,
                    8000.0f,
                    format.getSampleSizeInBits(),
					format.getChannels(),
                    format.getFrameSize(),
                    format.getFrameRate(),
                    true);			
				ais = AudioSystem.getAudioInputStream(format, ais);
			
		}catch (Exception e){
			e.printStackTrace();
		}
		
		AudioInputStream	AudioInputStreamAIS = AudioSystem.getAudioInputStream(format, ais);
		AudioFileFormat.Type	fileType = AudioFileFormat.Type.WAVE;
		int	nWrittenFrames = 0;
		try{
			nWrittenFrames = AudioSystem.write(AudioInputStreamAIS, fileType, ulawFile);
		}catch (IOException e){
			e.printStackTrace();
		}
im getting Exception
java.lang.IllegalArgumentException: Unsupported conversion: ULAW 8000.0 Hz, 16 bit, mono, 2 bytes/fr
ame, 11025.0 frames/second,  from PCM_SIGNED 11025.0 Hz, 16 bit, mono, 2 bytes/frame, little-endian
        	
	
How can i make the audio sample rate to 8000. please help.
Thanks in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 30 2008
Added on Sep 2 2008
4 comments
505 views