aspose file tools*
The moose likes I/O and Streams and the fly likes Converting wav file formats Using java Sound API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Converting wav file formats Using java Sound API" Watch "Converting wav file formats Using java Sound API" New topic
Author

Converting wav file formats Using java Sound API

murali chevuri
Greenhorn

Joined: Dec 24, 2004
Posts: 6
Hi,

My name is Murali. I am doing project on Sound Systems. My work is converting the differen wav file formats to a Standard format. For example 44000 sampled rate wav file to 8000 Sample rate. When I am trying to converting the file. I am getting exception like Unsupported Conversion. Please help me. Thanks in advance. I am sending my code. Please check and advise me.



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;
import javax.sound.sampled.UnsupportedAudioFileException;


public class Audio
{

int nDefaultSampleSizeInBits = 16;
float fDefaultSampleRate = 8000;
AudioFormat.Encoding defaultEncoding = AudioFormat.Encoding.PCM_SIGNED;
AudioFileFormat.Type defaultFileType = AudioFileFormat.Type.WAVE;
boolean bDefaultBigEndian = false;
int nDefaultChannels = 1;
float frameRate= 8000;
int frameSize = 2;
AudioFormatdefaultFormat = null;
File inputFile = null;
File outputFile = null;



public Audio(String sou,String tar)
{

try
{
//Standard Format
defaultFormat = new AudioFormat(defaultEncoding,fDefaultSampleRate,nDefaultSampleSizeInBits,nDefaultChannels,frameSize,frameRate,bDefaultBigEndian);
inputFile = new File(sou);
outputFile = new File(tar);
convert();
}
catch(Exception e)
{
e.printStackTrace();
}
}

protected void convert() throws UnsupportedAudioFileException, IOException
{

AudioFileFormatinputFileFormat = AudioSystem.getAudioFileFormat(inputFile);
AudioFormatinputFormat = inputFileFormat.getFormat();

if(inputFormat.matches(defaultFormat))
{
System.out.println("No Change");
return;
}
AudioInputStream sourceStream = AudioSystem.getAudioInputStream(inputFile);
intnWrittenBytes = 0;
AudioInputStream stream = AudioSystem.getAudioInputStream(defaultFormat,sourceStream);
nWrittenBytes = AudioSystem.write(stream, defaultFileType, outputFile);
System.out.println("Written bytes: " + nWrittenBytes);
}

public static void main(String args[])
{
new Audio(args[0],args[1]);
}
}
 
Consider Paul's rocket mass heater.
 
subject: Converting wav file formats Using java Sound API