3

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

user1697850
  • 31
  • 1
  • 2

3 Answers3

2

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();
    }
}
11101101b
  • 7,679
  • 2
  • 42
  • 52
  • Here is the full source: Convert a RTP sequence payload in a .wav file: [http://stackoverflow.com/questions/11622397/convert-a-rtp-sequence-payload-in-a-wav-file/17732264#17732264](http://stackoverflow.com/questions/11622397/convert-a-rtp-sequence-payload-in-a-wav-file/17732264#17732264) – XP1 Apr 09 '16 at 22:54
1

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

Community
  • 1
  • 1
sheidaei
  • 9,842
  • 20
  • 63
  • 86
1

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.

Kneel-Before-ZOD
  • 4,141
  • 1
  • 24
  • 26