tl; dr 对于未来的读者,使用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,base,treble等。
编辑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();
}
}
}
答案 0 :(得分:4)
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频道的声音。
OP和我去聊天并继续破解,但无法解决问题。似乎很多人都看过这个并放弃了。我希望赏金证明是诱人的 - 这是一个有趣的问题。答案 1 :(得分:1)
使用java没有很好的解决方案。最好使用jni访问不同的操作系统硬件。
在Windows中,NAudio是一个不错的选择。我遵循其demo-Record Soundcard Output with WasapiLoopbackCapture,编译控制台exe,并在Runtime.getRuntime().exec
答案 2 :(得分:1)
对我有用的是使用虚拟音频电缆。它创建了一个虚拟输入输出二重奏,其中输出将音频数据提供给输入,然后您可以使用目标数据线轻松获取数据。其他什么都不起作用,因为这是硬件限制而不是软件限制,因此我们必须模拟虚拟硬件以实时捕获输出。