Image

Imagepaulttt wrote in Imagejava_dev

java sound sampled convert wav

i need to be able to take in a wav file (of any quality) and output to a this audio format
                     AudioFormat audioFormat = new AudioFormat (
                             AudioFormat.Encoding.PCM_SIGNED,
                             8000.0F, 8, 1, 1, 16000.0F, false);


the code i am trying to use:


import java.io.*;
import javax.sound.sampled.*;

public class Convert
{
   
    /** Creates a new instance of Convert */
    public Convert ()
    {
    }
   
    public static void convert (String inFile)
    {
       
        AudioFileFormat.Type outputType = AudioFileFormat.Type.WAVE;
       
        String outFile = "D:\\888 01 Girls Aloud - Jump.wav";
       
        try
        {            AudioInputStream inStream = AudioSystem.getAudioInputStream (
                             new BufferedInputStream (
                             new FileInputStream (inFile)));
                     
                     //        AudioFormat audioFormat = new AudioFormat (
//                AudioFormat.Encoding.PCM_SIGNED,
//                8000.0F, 8, 1, 1, 16000.0F, false);
                     
//AudioFormat audioFormat = inStream.getFormat();
                     
//        AudioFormat audioFormat = new AudioFormat (
//                AudioFormat.Encoding.PCM_SIGNED,
//                44100.0F, 16, 2, 4, 44100.0F, false);
                     
                     AudioFormat audioFormat = new AudioFormat (
                             AudioFormat.Encoding.PCM_SIGNED,
                             8000.0F, 8, 1, 1, 16000.0F, false);
                     
                     System.out.println (audioFormat.getFrameRate ());
                     
                     AudioInputStream  audioInputStream = AudioSystem.
                             getAudioInputStream (audioFormat, inStream);
                     
                     AudioSystem.write (audioInputStream,
                             outputType,
                             new File (outFile));
        }
        catch (Exception e)
        {
            e.printStackTrace ();
            System.exit (0);
        }
       
    }
   
   
    public static void main (String [] args)
    {
        Convert.convert ("D:\\01 Girls Aloud - Jump.wav");
       
    }
}





the code does not convert the wav file it just hands out a
java.lang.IllegalArgumentException:
Unsupported conversion: PCM_SIGNED 8000.0 Hz, 8 bit, mono, 1
bytes/frame, 16000.0 frames/second,  from PCM_SIGNED 44100.0 Hz, 16
bit, stereo, 4 bytes/frame, little-endian
        at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:955)
        at nametune.other.Convert.convert(Convert.java:60)
        at nametune.other.Convert.main(Convert.java:78)
BUILD SUCCESSFUL (total time: 0 seconds)

it
doesn't seem to make a difference what the file before is or what the
output audioformat parameters are the only time the program runs
successfully is when the output audioformat is set to be the same as
the input.