【问题标题】:how do I get Mixer channels layout in java如何在 java 中获取混音器通道布局
【发布时间】:2012-10-03 12:08:41
【问题描述】:

我以为我可以在这个很棒的网站上找到任何东西,但现在我似乎遇到了没有答案的问题 :) 请帮忙!

问题是,我需要将最多 6 个不同的 wav 文件(每个 1 个通道)播放到系统混音器支持的 6 个通道中(左、右、左环绕等)。

使用 6 种不同的 SourceDataLines 看起来合乎逻辑,但据我所知,Mixer 无法为它们进行每个样本同步,因此我想出了将它们交错在单独的线程中并仅使用一个 SourceDataLine 来播放它。它适用于左右两个通道,但对于更多通道,我需要知道精确的通道布局(可能在差异系统和声卡上有所不同)。我认为系统混音器之一(我有两个:“线性输入”和“java 声音系统混音器”)可能有这个元数据,但我找不到。

有人知道如何获取这些信息吗?

谢谢。

【问题讨论】:

  • Java 声音只支持立体声,其他一切由原生声音软硬件产生。

标签: java audio javasound mixer channels


【解决方案1】:

是的,这是一个老问题,但由于我正在寻找所有通道、深度等,我决定编写一个小型调试方法,如果您想查看更多详细信息并想使用 java 8 流,该方法很有用:-)

享受:

//filter may be all if you want to include microphone 
 public void getDeviceInfos(String filter) {

            Stream
                    .of(AudioSystem.getMixerInfo())
                    .forEach(info -> {
                        System.out.println("Mixer Name: " + info.getName());
                        System.out.println("Mixer Description: " + info.getDescription());
                        System.out.println("Mixer Vendor: " + info.getVendor());

                        Mixer m = AudioSystem.getMixer(info);

                        Stream
                                .of(Arrays.asList(m.getSourceLineInfo()), filter.equals("record") ? Arrays.asList(m.getTargetLineInfo()) : null)
                                .flatMap(List::stream)
                                .collect(Collectors.toList())
                                .forEach(lineInfo -> {
                                    System.out.println("    info: " + lineInfo);
                                    try {
                                        Line line = AudioSystem.getLine(lineInfo);
                                        if (line instanceof SourceDataLine || line instanceof TargetDataLine) {

                                            Arrays.asList(((DataLine.Info) line.getLineInfo()).getFormats()).forEach(format -> {
                                                System.out.println("Channels: " + format.getChannels());
                                                System.out.println("Size in Bits: " + format.getSampleSizeInBits());
                                                System.out.println("Frame Rate: " + format.getFrameRate());
                                                System.out.println("Frame Size: " + format.getFrameSize());
                                                System.out.println("Encoding: " + format.getEncoding());
                                                System.out.println("Sample Rate: " + format.getSampleRate());

                                            });
                                        }
                                    } catch (Exception ex) {
                                        ex.printStackTrace();
                                    }
                                });
                    });

    }

【讨论】:

    【解决方案2】:

    我和我的朋友刚刚发现 java7 音响系统支持 6 个声道以及它们使用不同位的所有变体等。

    这是列出您的 java 可以做什么的代码:

            Mixer.Info[] mi = AudioSystem.getMixerInfo();
            for (Mixer.Info info : mi) {
                System.out.println("info: " + info);
                Mixer m = AudioSystem.getMixer(info);
                System.out.println("mixer " + m);
                Line.Info[] sl = m.getSourceLineInfo();
                for (Line.Info info2 : sl) {
                    System.out.println("    info: " + info2);
                    Line line = AudioSystem.getLine(info2);
                    if (line instanceof SourceDataLine) {
                        SourceDataLine source = (SourceDataLine) line;
    
                        DataLine.Info i = (DataLine.Info) source.getLineInfo();
                        for (AudioFormat format : i.getFormats()) {
                            System.out.println("    format: " + format);
                        }
                    }
                }
            }
    

    这是我的 macbook pro 的输出,带有 USB 连接的外部声卡:

    info: Default Audio Device, version Unknown Version
    mixer com.sun.media.sound.DirectAudioDevice@67310bf
        info: interface SourceDataLine supporting 14 audio formats, and buffers of at least 32 bytes
        format: PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
        format: PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
        format: PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
        format: PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
        format: PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian
        format: PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian
        format: PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
        format: PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
        format: PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
        format: PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
        format: PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian
        format: PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian
        format: PCM_SIGNED 48000.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
        format: PCM_SIGNED 48000.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian
        info: interface Clip supporting 14 audio formats, and buffers of at least 32 bytes
    info: Built-in Input, version Unknown Version
    mixer com.sun.media.sound.DirectAudioDevice@2edf98c4
    info: Built-in Output, version Unknown Version
    mixer com.sun.media.sound.DirectAudioDevice@59a683e6
        info: interface SourceDataLine supporting 14 audio formats, and buffers of at least 32 bytes
        format: PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
        format: PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
        format: PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
        format: PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
        format: PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian
        format: PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian
        format: PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
        format: PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
        format: PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
        format: PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
        format: PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian
        format: PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian
        format: PCM_SIGNED 48000.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
        format: PCM_SIGNED 48000.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian
        info: interface Clip supporting 14 audio formats, and buffers of at least 32 bytes
    info: SB X-Fi Surround 5.1 Pro, version Unknown Version
    mixer com.sun.media.sound.DirectAudioDevice@431d9f05
        info: interface SourceDataLine supporting 20 audio formats, and buffers of at least 32 bytes
        format: PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
        format: PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
        format: PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
        format: PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
        format: PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian
        format: PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian
        format: PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
        format: PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
        format: PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
        format: PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
        format: PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian
        format: PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian
        format: PCM_UNSIGNED unknown sample rate, 8 bit, 6 channels, 6 bytes/frame, 
        format: PCM_SIGNED unknown sample rate, 8 bit, 6 channels, 6 bytes/frame, 
        format: PCM_SIGNED unknown sample rate, 16 bit, 6 channels, 12 bytes/frame, little-endian
        format: PCM_SIGNED unknown sample rate, 16 bit, 6 channels, 12 bytes/frame, big-endian
        format: PCM_SIGNED unknown sample rate, 24 bit, 6 channels, 18 bytes/frame, little-endian
        format: PCM_SIGNED unknown sample rate, 24 bit, 6 channels, 18 bytes/frame, big-endian
        format: PCM_SIGNED 48000.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
        format: PCM_SIGNED 48000.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian
        info: interface Clip supporting 20 audio formats, and buffers of at least 32 bytes
    info: SB X-Fi Surround 5.1 Pro, version Unknown Version
    mixer com.sun.media.sound.DirectAudioDevice@4db323af
    info: Port Built-in Input, version Unknown Version
    mixer com.sun.media.sound.PortMixer@4b743fba
        info: Built-in Input source port
    info: Port Built-in Output, version Unknown Version
    mixer com.sun.media.sound.PortMixer@45d1edcc
    info: Port SB X-Fi Surround 5.1 Pro, version Unknown Version
    mixer com.sun.media.sound.PortMixer@59530fe3
    info: Port SB X-Fi Surround 5.1 Pro, version Unknown Version
    mixer com.sun.media.sound.PortMixer@5c382de5
        info: SB X-Fi Surround 5.1 Pro source port
    

    【讨论】:

    • 另请参阅this answer 上的MediaTypes 源到“Java 中有用的环境设置列表”。
    猜你喜欢
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 2015-06-01
    • 2011-06-01
    • 2011-08-31
    • 2018-07-10
    • 1970-01-01
    • 2019-05-17
    相关资源
    最近更新 更多