user1697850
user1697850

Reputation: 31

How can I convert a wav file in java

How can I convert a wav file in java

AudioFormat targetFormat = new AudioFormat(
                                    sourceFormat.getEncoding(),
                                    fTargetFrameRate,
                                    16,
                                    sourceFormat.getChannels(),
                                    sourceFormat.getFrameSize(),
                                    fTargetFrameRate,
                                    false);

in result Exception :

java.lang.IllegalArgumentException: Unsupported conversion: 
ULAW 8000.0 Hz, **16 bit**, mono, 1 bytes/frame, **from** ULAW 8000.0 Hz, **8 bit**, mono, 1 bytes/frame

it is possible in java?

I need get wav file 16 bit, from 8

Upvotes: 3

Views: 6634

Answers (3)

11101101b
11101101b

Reputation: 7779

Here is a method that will convert an 8-bit uLaw encoded binary file into a 16-bit WAV file using built-in Java methods.

public static void convertULawFileToWav(String filename) {
    File file = new File(filename);
    if (!file.exists())
        return;
    try {
        long fileSize = file.length();
        int frameSize = 160;
        long numFrames = fileSize / frameSize;
        AudioFormat audioFormat = new AudioFormat(Encoding.ULAW, 8000, 8, 1, frameSize, 50, true);
        AudioInputStream audioInputStream = new AudioInputStream(new FileInputStream(file), audioFormat, numFrames);
        AudioSystem.write(audioInputStream, Type.WAVE, new File("C:\\file.wav"));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Upvotes: 2

Kneel-Before-ZOD
Kneel-Before-ZOD

Reputation: 4221

You can always use FFMPEG, http://ffmpeg.org/, to do the conversion. Your Java program can call FFMPEG to do the conversion.
FFMPEG works on all OS.

Upvotes: 1

sheidaei
sheidaei

Reputation: 10332

Look at this one: Conversion of Audio Format it is similar to your issue suggesting looking at http://docs.oracle.com/javase/6/docs/api/javax/sound/sampled/AudioSystem.html

Upvotes: 1

Related Questions