Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Converting wav file formats Using java Sound API

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]);
}
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic