Java中的多个混音器输出

时间:2016-01-22 08:11:52

标签: java audio mixer javax.sound.sampled

我试图让Java在两个输出(前后音频插孔)中播放不同的立体声音频。

我的声卡被配置为独立处理两个输出,并且在Windows调音台中我可以让它们分别发出测试声音,因此它不是卡片问题。

我在Change Mixer to output sound to in java上使用不同的输出尝试了这种方法,方法是使用AudioSystem.getClip(AudioSystem.getMixerInfo()[i]);获得两个具有不同混音器的剪辑。但是,这行代码仅适用于Java声音音频引擎(AudioSystem.getMixerInfo()[0]),它在Windows'中输出。默认音频输出。别的什么抛出

java.lang.IllegalArgumentException: Line unsupported: interface Clip supporting format PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian

以下示例代码生成5秒长的白噪声并播放1秒然后结束。它打印了how do I get Mixer channels layout in java上显示的混音器信息。它目前输出到" Java声音音频引擎"混音器,并尝试更改为任何其他混音器抛出上述异常。

import java.io.ByteArrayInputStream;
import java.security.SecureRandom;
import java.util.Random;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Line;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;

public class Main {
    static int SAMPLE_RATE = 44100;
    static AudioFormat format = new AudioFormat(
            AudioFormat.Encoding.PCM_SIGNED, // Encoding
            SAMPLE_RATE,    // sample rate
            8,              // sample size in bits
            2,              // channels
            4,              // frame size
            SAMPLE_RATE,    // frame rate
            true);          // is big endian

    static int DURATION = 5;
    Thread soundThread;

    // Noise audio
    static AudioInputStream inputStream = new AudioInputStream(new ByteArrayInputStream(generateNoise(DURATION*2*SAMPLE_RATE)), format, DURATION*SAMPLE_RATE);

    public static void main(String[] args) {
        try {

            // https://stackoverflow.com/questions/12863081/how-do-i-get-mixer-channels-layout-in-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);
                        }
                    }
                }
                System.out.println("");
            }


            // Code only works for AudioSystem.getMixerInfo()[0] 
            final Clip clip = AudioSystem.getClip(AudioSystem.getMixerInfo()[0]);
            clip.open(inputStream);

            Thread soundThread = new Thread(new Runnable() {                
                @Override
                public void run() {
                    try {
                        clip.loop(Clip.LOOP_CONTINUOUSLY);
                        Thread.sleep(1000);
                        clip.close();
                        System.exit(0);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            soundThread.start();

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

    }

    private static byte[] generateNoise(int size) {
        byte[] out = new byte[size];
        Random r = new SecureRandom();
        r.nextBytes(out);
        return out;
    }
}

这是混音器信息。此代码在连接两个输出插孔的情况下运行,Windows识别为两个不同的输出设备(扬声器和耳机)。看起来只有Java的音频引擎才能播放声音。

info: Java Sound Audio Engine, version 1.0
mixer com.sun.media.sound.HeadspaceMixer@22c84d9
    info: interface SourceDataLine supporting 8 audio formats
    format: PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    format: PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    format: PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    format: PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    format: PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
    format: PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
    format: PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
    format: PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
    info: interface Clip supporting 8 audio formats, and buffers of 0 to 4194304 bytes

info: Microsoft ?T?E???h, version Unknown Version
mixer com.sun.media.sound.SimpleInputDevice@7e0df503

info: Stereo Mixer (Realtek High Defi, version Unknown Version
mixer com.sun.media.sound.SimpleInputDevice@4650d89c

info: Port Realtek HD Audio 2nd output (Re, version 6.1
mixer com.sun.media.sound.PortMixer@65bd0dd4

info: Port Stereo Mixer (Realtek High Defi, version 6.1
mixer com.sun.media.sound.PortMixer@78b5f53a
    info: ?}?X? source port

info: Port Speakers (Realtek High Definiti, version 6.1
mixer com.sun.media.sound.PortMixer@b37c60d

由于其他限制,我使用的是Java 6。此外,一些名称可能因为我在日本环境中被破坏而且Eclipse无法以正确的编码获取名称(我尝试将所有内容更改为UTF-8和Shift_JIS但没有任何改变,但我想这与这个问题无关。)

换句话说,它看起来不能从不同的混音器输出剪辑,因为Java只能输出到#34; Java Sound Audio Engine"这是Windows'默认音频设备。有没有办法让其他调音台工作?有没有其他方法可以使用多个音频输出?

更新:好像是this problem was already fixed,但我还是无法让它发挥作用。我禁用了#34; Stereo Mix"记录设备并运行该链接中给出的示例代码,并获得此输出:

MIXER 0: Java Sound Audio Engine, version 1.0
OUTPUT LINE (SourceDataLine) 0: interface SourceDataLine supporting 8 audio formats
OUTPUT LINE (SourceDataLine) 1: interface Clip supporting 8 audio formats, and buffers of 0 to 4194304 bytes

MIXER 1: Port Realtek HD Audio 2nd output (Re, version 6.1
INPUT LINE (TargetDataLine) 0: HEADPHONE target port

MIXER 2: Port Speakers (Realtek High Definiti, version 6.1
INPUT LINE (TargetDataLine) 0: SPEAKER target port

所以看起来只有Java Sound Audio Engine可以输出声音,因为前后音频插孔的混音器都被视为输入线。通过Windows中的Clip或SourceDataLine输出播放音频。默认播放设备。

此外,看起来他们solved问题by using DirectAudio,但我仍然没有弄清楚如何使用它们。

1 个答案:

答案 0 :(得分:0)

在不同的计算机上进行测试时,我通过升级到Java 1.6.0_17解决了这个问题(我的环境限制仍然可以从Matlab调用Java),因此会列出DirectAudio驱动程序(即使看起来应该如此)从Java 1.5)开始工作。每个DirectAudio声音设备为每个设备提供SourceDataLine和Clip。