File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting wav file formats Using java Sound API

 
murali chevuri
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic