Java 音频可视化工具 - 如何捕获实时声音输出到扬声器?

标签 java audio output javasound

tl;dr 对于 future 的读者,使用 Java 或 C# 录制实时音频不可能(目前)。使用 C++,因为它提供了大量的音频 API。


我的目标是获取当前在 Windows 机器上播放的声音,并像图形音频可视化工具一样分析声音(获取音量属性和 Hz(低音和高音))。当我说当前声音时,我的意思是如果要播放 Youtube 视频或 Spotify 歌曲,并且该程序将读取该音频输出。我无意播放声音,而是实时捕捉并可视化。

在尝试这样做时,我阅读了如何 build an audio waveform display它涉及如何将音频文件转换为字节数组(一行)。这没有帮助,因为它不会得到当前的声音。我还阅读了如何 capture audio还有,和this java accessing sound tutorial ,这些都没有回答我的问题,因为它们都需要加载歌曲文件。

我只是完全不明白这一点。我完全一无所知,如有任何帮助,我们将不胜感激。

编辑:我环顾四周,second answer from this source让我得出这样的结论:我可以找到所有的音频设备,看看哪个在发出声音。我不知道之后该怎么办。

编辑 2(再次编辑):通过试验和环顾四周,我在下面编写了这段代码。我认为这让我朝着我想要的方向前进,但我不知道如何完成它。

    Mixer.Info[] mixers = AudioSystem.getMixerInfo();
    for (Mixer.Info mixerInfo : mixers) {
        Mixer mixer = AudioSystem.getMixer(mixerInfo);
        try {
            mixer.open();
            Line.Info[] lines = mixer.getTargetLineInfo();
            for (Line.Info linfo : lines) {

                Line line = AudioSystem.getLine(linfo);

                //here I'm opening the line, but I don't know how to grab data
                line.open();

            }
        } catch (LineUnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

我使用了这个来源:Checking The Level of Audio-Playback in a mixers line ,但我不想检查所有正在播放音量的线路,我只需要用户默认的混音器,获取该线路,并能够分析数据。

编辑 3:我试过:

    //creating a format for getting sound
    float sampleRate = 8000;
    int sampleSizeInBits = 16;
    int channels = 2;
    boolean signed = true;
    boolean bigEndian = true;
    AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits, channels, 
        signed, bigEndian);

    //creating a line based off of the format
    DataLine.Info info = new DataLine.Info( TargetDataLine.class, format);
    TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);

    //opening and starting that line
    line.open(format);
    line.start();

    while (conditionIsTrue){
        //here, I don't know what to put as the parameters.
        //Had I known, I don't know how I would get to analyze the data
        line.read();
    }

我认为我使用上面的代码是正确的,但我不知道如何提取声音并找到 bpm、低音、高音等。

编辑 4:这是一篇有趣的读物:Real-time low latency audio processing in Java .这并没有涉及什么类以及如何实际实现它,但它提供了一些见解。

编辑 5:@AndrewThompson 使用基于您的链接的这段代码,我能够迭代可用的源和目标行。

Mixer.Info[] mixers = AudioSystem.getMixerInfo();
    for (Mixer.Info mixerInfo : mixers) {
        Mixer mixer = AudioSystem.getMixer(mixerInfo);
        try {
            mixer.open();
            Line.Info[] sourceLines = mixer.getSourceLineInfo();
            Line.Info[] targetLine = mixer.getTargetLineInfo();
            for (Line.Info sourceLinfo : sourceLines) {
                System.out.println(sourceLinfo );
            }
            for (Line.Info targetLinefo : targetLine) {
                System.out.println(targetLinefo);
            }

        } catch (LineUnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

输出看起来像这样:

interface SourceDataLine supporting 8 audio formats, and buffers of at least 32 bytes
interface Clip supporting 8 audio formats, and buffers of at least 32 bytes
interface SourceDataLine supporting 8 audio formats, and buffers of at least 32 bytes
interface Clip supporting 8 audio formats, and buffers of at least 32 bytes
interface SourceDataLine supporting 8 audio formats, and buffers of at least 32 bytes
interface Clip supporting 8 audio formats, and buffers of at least 32 bytes
HEADPHONE target port
SPEAKER target port

然后我创建了一个方法来获取所有线路的声级,如下所示:

private static void getVolumeOfAllLines() {
    Mixer.Info[] mixers = AudioSystem.getMixerInfo();
    for (Mixer.Info mixerInfo : mixers) {
        Mixer mixer = AudioSystem.getMixer(mixerInfo);
        try {
            mixer.open();
            Line.Info[] lines = mixer.getSourceLineInfo();
            for (Line.Info linfo : lines) {
                DataLine line = (DataLine)AudioSystem.getLine(linfo);
                if(line != null)
                    System.out.println(line.getLevel());
            }
        } catch (LineUnavailableException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

-in 尝试找到当前播放声音的行,表示音量更高。这将返回:

-1.0
-1.0
-1.0
-1.0
-1.0
-1.0

没有进展。


新代码:

    private static void debug(){
    Mixer.Info[] mixers = AudioSystem.getMixerInfo();
    for (Mixer.Info mixerInfo : mixers) {
        Mixer mixer = AudioSystem.getMixer(mixerInfo);
        try {
            mixer.open();
            Line.Info[] lines = mixer.getTargetLineInfo();


            AudioFormat format = new AudioFormat(
                    AudioFormat.Encoding.PCM_SIGNED,
                    44100,
                    16, 2, 4,
                    44100, false);

            AudioFormat[] tdl = AudioSystem.getTargetFormats(AudioFormat.Encoding.PCM_SIGNED, format);

            for (Line.Info linfo : lines) {

                //Line line = AudioSystem.getLine(linfo);


                TargetDataLine line = null;
                DataLine.Info info = new DataLine.Info(TargetDataLine.class,
                        format); // format is an AudioFormat object
                if (!AudioSystem.isLineSupported(info))
                {
                    System.out.println("line not supported:" + line );
                }

                try
                {
                    line = (TargetDataLine) AudioSystem.getLine(info); //error
                    line.open(format);
                    System.out.println("line opened:" + line);

                    line.start();

                    byte[] buffer = new byte[1024];
                    int ii = 0;
                    int numBytesRead = 0;
                    while (ii++ < 100) {
                        // Read the next chunk of data from the TargetDataLine.
                        numBytesRead =  line.read(buffer, 0, buffer.length);

                        System.out.println("\nnumBytesRead:" + numBytesRead);
                        if (numBytesRead == 0) continue;
                        // following is a quickie test to see if content is only 0 vals
                        // present in the data that was read.

                        for (int i = 0; i < 16; i++)
                        {
                            if (buffer[i] != 0)
                                System.out.print(".");
                            else
                                System.out.print("0");
                        }
                    }

                } catch (LineUnavailableException ex) {
                    ex.printStackTrace();
                    //...
                }
            }
        } catch (LineUnavailableException e) {
            e.printStackTrace();
        }
    }
}

最佳答案

Java 教程中有一个很好的示例可以帮助您从一行中提取 PCM 数据。在标题为 Using Files and Format Converters 的教程中在“读取声音文件”部分标题下有一个代码示例。相关部分是“片段”示例,并用代码标记:

  // Here, do something useful with the audio data that's 
  // now in the audioBytes array...

至此,您已经可以访问该行的各个字节,并可以根据声音文件的格式将它们组装成PCM。还有其他几个 stackoverflow 问题,它们处理从字节到 PCM 的具体情况。


正在添加一些代码以响应评论。

由于无法转换为 TargetDataLine,从教程中提取的以下内容允许我转换为 TargetDataLine。

    AudioFormat format = new AudioFormat(
        AudioFormat.Encoding.PCM_SIGNED, 
        44100,
        16, 2, 4, 
        44100, false);

    TargetDataLine line = null;
    DataLine.Info info = new DataLine.Info(TargetDataLine.class, 
        format); // format is an AudioFormat object
    if (!AudioSystem.isLineSupported(info)) 
    {
        System.out.println("line not supported:" + line );
    }

    try 
    {
        line = (TargetDataLine) AudioSystem.getLine(info);
        line.open(format);
        System.out.println("line opened:" + line);

        line.start();

        byte[] buffer = new byte[1024];
        int ii = 0;
        int numBytesRead = 0;
        while (ii++ < 100) {
       // Read the next chunk of data from the TargetDataLine.
            numBytesRead =  line.read(buffer, 0, buffer.length);

            System.out.println("\nnumBytesRead:" + numBytesRead);
               if (numBytesRead == 0) continue;
     // following is a quickie test to see if content is only 0 vals
    // present in the data that was read.

               for (int i = 0; i < 16; i++)
               {
                   if (buffer[i] != 0)
                       System.out.print(".");
                   else
                       System.out.print("0");
               }
            }

        } catch (LineUnavailableException ex) {
            ex.printStackTrace();
        //... 
    }
}

但我只是使用 CD 质量格式方案抓取一条线,我没有试图找出哪条线有来自正在播放的 YouTube channel 的声音。


OP 和我去聊天并继续破解这个问题,但无法找到解决方案。似乎许多其他人也看到了这一点并放弃了。我希望赏金很有吸引力——这是一个有趣的问题。

关于Java 音频可视化工具 - 如何捕获实时声音输出到扬声器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38842255/

相关文章:

algorithm - 您能说出这种音频压缩算法吗?

javascript - 打印山脉算法

java - 线程中出现异常 "main"java.lang.UnsatisfiedLinkError : no swt-pi-gtk-3139 in java. library.path

java - "Kill a process tree"在使用 Java 的 Windows 上

java - eclipse中的ant调试

java - 安装 Project Lombok 后处理 RealmObject 后代时出错

Java 线程,具有视觉和音频

javascript - Phonegap android中的背景声音

java - 使用 system.out.printf 格式化 java 字符串

scala - 将scala中生成的数据写入文本文件