Java AudioSystem: Reading 32 Bit Wav Files

2019-05-24 12:41发布

I'm reading in audio files in 16 and 24 bit sampling bit depths and parsing them to determine their lengths without difficulty. However when reading a 32 bit file, I get

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1170)
...

The 32 bit test file is manually encoded in the same manner as the others (linear PCM). I'm wondering if AudioSystem doesn't support 32 bit Wavs, or if there might be a workaround. For reference, here's my class:

import java.io.*;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class soundUtility {
    public static double getWavDuration(File filename)
    {   
        AudioInputStream stream = null;
        try 
        {
            stream = AudioSystem.getAudioInputStream(filename);
            AudioFormat format = stream.getFormat();
            return filename.length() / format.getSampleRate() / (format.getSampleSizeInBits() / 8.0) / format.getChannels();
        }
        catch (Exception e) 
        {
            e.printStackTrace();
            return -1;
        }
        finally
        {
            try { stream.close(); } catch (Exception ex) { }
        }
    }

    public static void main(String[] args) {

    try {
        // ===== TESTS: toggle these calls to test the included files =====
        // File soundFile = new File("16bit.mono.441k.30secs.wav");
        // File soundFile = new File("24bit.48k.11secs.stereo.wav");
        File soundFile = new File("32bit.Floating.Stereo.48k.wav");
        // ===========

        System.out.println(getWavDuration(soundFile));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Thanks for any insight.

1条回答
疯言疯语
2楼-- · 2019-05-24 12:49

Old question, but it's something I was just looking into for myself. My tests can confirm that 16bit 48kHz PCM works, and 32bit doesn't.

However my tests also imply that 24 bit doesn't work:

 No line matching interface Clip supporting format PCM_SIGNED 48000.0 Hz, 24 bit, stereo, 6 bytes/frame, little-endian is supported.

This is a .wav file created at 96Khz, 32bit and exported from Audacity where it was rendered as a 24bit 48Khz wav.

This would seem reflected in the doc: https://docs.oracle.com/javase/8/docs/technotes/guides/sound/

"Sound formats: 8-bit and 16-bit audio data, in mono and stereo, with sample rates from 8 kHz to 48 kHz"

So, no 32 bit floating available, I'm afraid, and I cannot reproduce your result that 24 bit floating works.

查看更多
登录 后发表回答