如何在Java中获取所有有效系统AudiOoforMat对象的数组

发布于 2025-01-23 05:12:23 字数 1218 浏览 0 评论 0 原文

我正在创建一个可以在Java中播放合成音频的对象,但是我需要能够将其设置为具有操作系统可能播放的最高音频比特率的AudiooFormat。

(合成器生成64位浮动音频,可以将其划分为32位浮点或PCM,24位,16位和8位PCM音频。)

我将需要过滤所有操作系统的有效视听器并选择系统可以使用的比特率最高的格式。

如何获得系统可以播放的所有有声构架的批准数组?

public class AudioSettings {
    
    // instance variables
    private int sampleRate;
    private AudioFormat audioFormat;
    private SourceDataLine sourceDataLine;
    
    public AudioSettings(int sampleRate) {
        
        this.sampleRate = sampleRate;
        
        // get highest possible quality bitrate for system
        int highestBitRate = 16;
        
        AudioFormat currentFormat = new AudioFormat(new Encoding("PCM_SIGNED"), (float) sampleRate, highestBitRate,
                2, highestBitRate / 8 * 2, sampleRate, true);
        
        for (AudioFormat format : /* What goes here? */) {

            if (format.getSampleSizeInBits() > highestBitRate 
                    && format.isBigEndian()
                    && format.getChannels() == 2) {
                currentFormat = format;
                highestBitRate = format.getSampleSizeInBits();
            }
        }
        
        audioFormat = currentFormat;
    }
    
}

I am creating an object that can play synthesised audio in Java but I need to be able to set it to the AudioFormat with the Operating system's highest possible audio bitrate it can play.

(Synth generates 64-bit float audio and can bit-crush it to 32-bit float or PCM, 24-bit, 16-bit and 8-bit PCM audio.)

I will need to filter all the Operating system's valid AudioFormats and pick the format with the highest bitrate the system can use.

How can I get the approtriate array of all the AudioFormats that the system can play without error?

public class AudioSettings {
    
    // instance variables
    private int sampleRate;
    private AudioFormat audioFormat;
    private SourceDataLine sourceDataLine;
    
    public AudioSettings(int sampleRate) {
        
        this.sampleRate = sampleRate;
        
        // get highest possible quality bitrate for system
        int highestBitRate = 16;
        
        AudioFormat currentFormat = new AudioFormat(new Encoding("PCM_SIGNED"), (float) sampleRate, highestBitRate,
                2, highestBitRate / 8 * 2, sampleRate, true);
        
        for (AudioFormat format : /* What goes here? */) {

            if (format.getSampleSizeInBits() > highestBitRate 
                    && format.isBigEndian()
                    && format.getChannels() == 2) {
                currentFormat = format;
                highestBitRate = format.getSampleSizeInBits();
            }
        }
        
        audioFormat = currentFormat;
    }
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此刻的回忆 2025-01-30 05:12:23

根据本文档frpm java 8天,,Java支持最多16位编码,最高的样本率为48 kHz。

IDK如果从那以后有任何进步。例如,必须有Java 17的规范,例如,列出了规格。

至于向系统查询支持的文件类型,在教程中提到使用文件和格式转换器,在最后一节中:学习可用的转换

相关音频系统方法, getaudiofiletypes(audioInputstream)
返回给定的支持文件类型的完整列表
流,作为 audiofileformat.type 实例的数组。

According to this document frpm the Java 8 days, Java Sound Technology, Java supports a max of 16-bit encoding, and a highest sample rate of 48 kHz.

IDK if there's been any advancement since then. There must be a specification for Java 17, for example, where the specs are listed.

As far as querying the system for supported file types, there is a mention of in the tutorial Using File and Format Converters, in the last section: Learning What Conversions Are Available.

A related AudioSystem method, getAudioFileTypes(AudioInputStream),
returns the complete list of supported file types for the given
stream, as an array of AudioFileFormat.Type instances.

花间憩 2025-01-30 05:12:23

多亏了@gpasch,我从他的链接中找到了答案。尽管我认为您只需要读取line.info []数组的一个实例,因为它似乎打印出三个完全相同的组。

public static void main(String[] args) {
        
        Line.Info desired = new Line.Info(SourceDataLine.class);
        Line.Info[] infos = AudioSystem.getSourceLineInfo(desired);

        for (Line.Info info : infos) {
            if (info instanceof DataLine.Info) {
                AudioFormat[] forms = ((DataLine.Info) info).getFormats();

                for (AudioFormat format : forms) {
                    System.out.println(format);
                }
            }
        }

    }

Thanks to @gpasch I found my answer from his link. Although I think you only need to read one instance of the Line.Info[] array because it seems to print out three groups that are exactly the same.

public static void main(String[] args) {
        
        Line.Info desired = new Line.Info(SourceDataLine.class);
        Line.Info[] infos = AudioSystem.getSourceLineInfo(desired);

        for (Line.Info info : infos) {
            if (info instanceof DataLine.Info) {
                AudioFormat[] forms = ((DataLine.Info) info).getFormats();

                for (AudioFormat format : forms) {
                    System.out.println(format);
                }
            }
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文