Java-从22050降采样到8000可获得零字节 [英] Java - Downsampling from 22050 to 8000 gives zero bytes

查看:247
本文介绍了Java-从22050降采样到8000可获得零字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AudioInputStream将.wav音频从22050降采样为8000,但该转换返回0个数据字节.这是代码:

I'm trying to downsample a .wav audio from 22050 to 8000 using AudioInputStream but the conversion returns me 0 data bytes. Here is the code:

AudioInputStream ais;
AudioInputStream eightKhzInputStream = null;
ais = AudioSystem.getAudioInputStream(file);
if (ais.getFormat().getSampleRate() == 22050f) {
    AudioFileFormat sourceFileFormat = AudioSystem.getAudioFileFormat(file);
    AudioFileFormat.Type targetFileType = sourceFileFormat.getType();
    AudioFormat sourceFormat = ais.getFormat();
    AudioFormat targetFormat = new AudioFormat(
        sourceFormat.getEncoding(),
        8000f,
        sourceFormat.getSampleSizeInBits(),
        sourceFormat.getChannels(),
        sourceFormat.getFrameSize(),
        8000f,
        sourceFormat.isBigEndian());
    eightKhzInputStream = AudioSystem.getAudioInputStream(targetFormat, ais);
    int nWrittenBytes = 0;
    nWrittenBytes = AudioSystem.write(eightKhzInputStream, targetFileType, file);

我已经检查了AudioSystem.isConversionSupported(targetFormat, sourceFormat),它返回true.有什么主意吗?

I already checked AudioSystem.isConversionSupported(targetFormat, sourceFormat) and it returns true. Any idea?

推荐答案

我刚刚使用不同的音频文件测试了您的代码,并且一切似乎都正常.我只能猜测,您是使用空的音频文件(字节== 0)测试代码,还是Java音频系统不支持您尝试转换的文件.

I have just tested your code with different audio files and everything seems to work just fine. I can only guess, that you are either testing your code with an empty audio file (bytes == 0) or, that the file you try to convert is not supported by the Java Audio System.

尝试使用另一个输入文件和/或将输入文件转换为兼容文件,它应该可以工作.

Try using another input file and/or convert your input file to a compatible file, and it should work.

这是最适合我的主要方法:

Here is the main method, that worked for me:

public static void main(String[] args) throws InterruptedException, UnsupportedAudioFileException, IOException {
    File file = ...;
    File output = ...;

    AudioInputStream ais;
    AudioInputStream eightKhzInputStream = null;
    ais = AudioSystem.getAudioInputStream(file);
    AudioFormat sourceFormat = ais.getFormat();
    if (ais.getFormat().getSampleRate() == 22050f) {
        AudioFileFormat sourceFileFormat = AudioSystem.getAudioFileFormat(file);
        AudioFileFormat.Type targetFileType = sourceFileFormat.getType();

        AudioFormat targetFormat = new AudioFormat(
                sourceFormat.getEncoding(),
                8000f,
                sourceFormat.getSampleSizeInBits(),
                sourceFormat.getChannels(),
                sourceFormat.getFrameSize(),
                8000f,
                sourceFormat.isBigEndian());
        if (!AudioSystem.isFileTypeSupported(targetFileType) || ! AudioSystem.isConversionSupported(targetFormat, sourceFormat)) {
              throw new IllegalStateException("Conversion not supported!");
        }
        eightKhzInputStream = AudioSystem.getAudioInputStream(targetFormat, ais);
        int nWrittenBytes = 0;

        nWrittenBytes = AudioSystem.write(eightKhzInputStream, targetFileType, output);
        System.out.println("nWrittenBytes: " + nWrittenBytes);
    }
}

这篇关于Java-从22050降采样到8000可获得零字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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