java - 在 JAVA 中捕获音频流

标签 java windows

我是 Java 的新手,虽然我已经掌握了语法和结构,但我很难从麦克风中获取经过处理的数字音频样本。

我想要实现的目标非常简单,而从长远来看,我正在尝试创建一个非常简单的频谱图,但只是为了理解/掌握我试图从头开始的音频处理过程。


这是我的问题

当麦克风检测到一声哔哔声或任何声音时,我想捕获该二进制数据,并以其原始格式简单地显示它。


JAVA 的要求真的过分吗?

我已经阅读了有关模拟/数字信号、FFT、matlab 的内容,并且我已经搜索了很多链接,例如:

Is there a way of recording audio of streaming broadcast from a webpage?

Working with audio in Java

OpenAL playback captured audio data c++

还有著名的oracle介绍

http://docs.oracle.com/javase/tutorial/sound/capturing.html

这实际上是一个很好的教程 http://www.developer.com/java/other/article.php/3380031/Spectrum-Analysis-using-Java-Sampling-Frequency-Folding-Frequency-and-the-FFT-Algorithm.htm

但是他们都没有为我的回答提供解决方案。

我不是要代码,虽然它会很棒,只是阅读每一行并理解所涉及的机制,但一个简单的提示也很好。


这是一个简单的代码,用于捕获字节,但只能从现有的 wav 文件

import java.io.FileInputStream;
import java.io.IOException;

public class Boo{
    public static void main(String[] arguments){
        try {
            FileInputStream file = new FileInputStream("beep.wav");
            boolean eof = false;
            int count = 0;
            while(!eof){
                int input = file.read();
                System.out.print(input + "");
                if(input == -1)
                    eof = true;
                else 
                    count++;
            }
            file.close();
            System.out.println("\nBytes read: " + count);
        }catch (IOException e){
            System.out.println("Error - " + e.toString());
        }
    }
}

赏金之后

-为了更清楚-

我只是想让它成为一个简单的程序来读取麦克风。并实时显示其捕获的声音的二进制数据。

把它想象成一个频谱图,当声音被捕获时,图形会根据信号电平的变化上下波动,但在这种情况下,不需要将二进制数据转换为音频图形,只需要显示任何原始数据本身。无需写入/读取文件。只需从麦克风捕捉,并显示从麦克风读取的内容。

如果上面的内容很难,因为我在网上搜索过,但找不到任何有用的东西,你可以给我指导/方向.. 谢谢

最佳答案

javax.sound.sampled 包应该有你需要的一切。

例子:

    int duration = 5; // sample for 5 seconds
    TargetDataLine line = null;
    // find a DataLine that can be read
    // (maybe hardcode this if you have multiple microphones)
    Info[] mixerInfo = AudioSystem.getMixerInfo();
    for (int i = 0; i < mixerInfo.length; i++) {
        Mixer mixer = AudioSystem.getMixer(mixerInfo[i]);
        Line.Info[] targetLineInfo = mixer.getTargetLineInfo();
        if (targetLineInfo.length > 0) {
            line = (TargetDataLine) mixer.getLine(targetLineInfo[0]);
            break;
        }
    }
    if (line == null)
        throw new UnsupportedOperationException("No recording device found");
    AudioFormat af = new AudioFormat(11000, 8, 1, true, false);
    line.open(af);
    line.start();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[(int)af.getSampleRate() * af.getFrameSize()];
    long end = System.currentTimeMillis() + 1000 * duration;
    int len;
    while (System.currentTimeMillis() < end && ((len = line.read(buf, 0, buf.length)) != -1)) {
        baos.write(buf, 0, len);
    }
    line.stop();
    line.close();
    baos.close();

之后,您可以从字节数组输出流中提取字节。或者,如果您愿意,您当然可以在 while 循环中处理它们。

关于java - 在 JAVA 中捕获音频流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17255344/

相关文章:

java - 使用 Struts2 和 hibernate 创建 Web 服务(SOAP 或 REST)

windows - 如何在纹理上的 3d 空间中显示 windows/X 窗口系统窗口?

C++:将控制台窗口设置为 WS_POPUP

windows - sed 可以在 Windows 中递归吗?

c++ - 确定调用者是从 EXE 还是 DLL 调用

java - Eclipse OSGi SystemBundleActivator 与 BundleActivator

java - 如何在核心java应用程序中维护 session 超时?

java - 使用 Spring Data 将两个对象合并到 Map

java - try/catch block 捕获异常有什么意义?

c# - 如何在给定设备路径的情况下获取 USB_DEVICE_DESCRIPTOR