Java AudioSystem:读取32位Wav文件 [英] Java AudioSystem: Reading 32 Bit Wav Files

查看:831
本文介绍了Java AudioSystem:读取32位Wav文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取16位和24位采样位深度的音频文件并解析它们以确定它们的长度而没有任何困难。但是,当读取32位文件时,我得到

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)
...

32位测试文件的手动编码方式与其他文件相同(线性PCM)。我想知道AudioSystem是不支持32位Wav,还是可能有解决方法。作为参考,这是我的班级:

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();
        }
    }
}

感谢您提供任何见解。

推荐答案

老问题,但这是我自己正在研究的问题。我的测试可以确认16位48kHz PCM工作,32位不工作。

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.

然而,我的测试也暗示24位不起作用:

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.

这是一个以96Khz,32位创建的.wav文件,从Audacity导出,在那里呈现为24位48Khz wav。

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

这似乎反映在doc: https://docs.oracle.com/javase/8/docs/technotes/guides/sound/

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

声音格式:8位和16位音频数据,单声道和立体声,采样率从8 kHz到48 kHz

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

所以,没有32位浮动可用,我很害怕,我无法重现24位浮动的结果。

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

这篇关于Java AudioSystem:读取32位Wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆