简体   繁体   English

Java Sound API:捕获从目标端口输出的声音

[英]Java Sound API: Capturing sound output from a Target Port

I'm writing a simple piece of software that streams audio over LAN. 我正在写一个简单的软件,可以通过LAN传输音频。 I have all of the network parts implemented, but what I'm stumbling on is using the Java Sound API. 我实现了所有网络部分,但是我绊脚石是使用Java Sound API。 I have successfully captured audio from the microphone, and line-in, but I can't seem to capture from any target ports, like the speakers. 我已经成功地捕获了来自麦克风和线路输入的音频,但似乎无法从任何目标端口(例如扬声器)捕获。 My question is, is it possible to capture from the Master target port? 我的问题是,是否可以从主目标端口捕获? Here is the piece of code that works on initializing the line. 这是初始化行的代码。

private boolean startCapture(){
    try{
        DataLine.Info info = new DataLine.Info( TargetDataLine.class, format);
        line = (TargetDataLine)AudioSystem.getLine(info);
        audioBuffer = new byte[bufferSize];
        line.open(format);
        line.start();
        return true;
    }catch(Exception e){
        System.out.println("Exception thrown when capturing audio:\n" + e);
        return false;
    }
}

Running the code like this will just use the microphone as my line. 像这样运行代码将只使用麦克风作为我的一行。 Here is info about my sound system. 是有关我的音响系统的信息。 Most important is probably the fact that I'm running Linux. 最重要的可能是我正在运行Linux。

Thanks in advance for any and all help you can give me. 预先感谢您能给我的所有帮助。

Here is a way (not programmatic way). 这是一种方法(不是编程方法)。

Goto:Control Panel\\Hardware and Sound\\ click on sound icon.Now goto recording,Right click-> choose both-choose enable,disconnected devices.You will find "Stereo mix" icon.Make it as default device. 转到:控制面板\\硬件和声音\\单击声音图标。现在转到录制,右键单击->选择两者,选择启用,断开连接的设备。您将找到“立体声混音”图标。将其作为默认设备。

Now try to record the sound.Problem solved. 现在尝试录制声音。问题解决了。

One issue with network sound is that the computers at each end may have slightly different sample rates due to differences between the sound card clocks. 网络声音的一个问题是,由于声卡时钟之间的差异,两端的计算机采样率可能会略有不同。 Computer clocks vary. 电脑时钟各不相同。 If the sending computer is running slower than the receiving computer then, even if you do have a buffer, your buffer will slowly empty. 如果发送计算机的运行速度慢于接收计算机的运行速度,则即使您有缓冲区,缓冲区也会慢慢变空。 If it is running faster then you'll slowly gain an excess of data. 如果运行速度更快,那么您将慢慢获得大量数据。 This person tried just what you were doing and saw dropouts. 这个人尝试了您所做的事情,发现辍学了。 Note that buying more expensive sound cards will reduce his problem but not completely solve it, unless he does something like lock them to the GPS time signal. 请注意,购买更昂贵的声卡将减少他的问题,但不能完全解决,除非他做了类似将它们锁定到GPS时间信号的操作。 Your typical casual user won't do that. 您通常的临时用户不会这样做。

Maybe for short transmissions you can get away with it. 也许对于短时间的传输您可以摆脱它。 If you're doing voice for example and you stop transmitting when the speaker is quiet then you can synchronise your buffers when you start again. 例如,如果您正在做语音,并且在扬声器安静时停止传输,则可以在再次启动时同步缓冲区。 I wonder what it would do to latency. 我不知道它将对延迟产生什么影响。 The "proper" solution requires re-sampling the audio at the receiving end to deal with the slight difference of sample rate. “适当的”解决方案需要在接收端重新对音频进行采样,以处理采样率的微小差异。

For such small variation in frequency you could possibly get away with taking the nearest neighbour - effectively skipping or duplicating samples every so often. 对于如此小的频率变化,您可能会选择最接近的邻居,从而每隔一段时间有效地跳过或复制样本。 Digital Amateur radio software I've heard of uses linear interpolation between samples. 我听说过的数字业余无线电软件在样本之间使用线性插值。 You need to maintain a scaling factor and control it in order to ensure that you empty your buffer at the rate that new data comes in, but have a control loop that will not be too upset by network vagaries and not try to make sudden large changes. 您需要保持比例因子并对其进行控制,以确保以新数据进入的速率清空缓冲区,但要有一个控制环,它不会因网络变化而烦恼,并且不要尝试进行突然的大改变。

I don't know whether you've taken this into account or not. 我不知道您是否考虑到了这一点。 I've seen people attempt this who have not. 我见过有人尝试这样做,而没有尝试。 I except nowadays people would use an off-the-shelf audio conferencing library which takes care of this kind of thing. 除了今天,我将使用现成的音频会议库来处理这种事情。 If you're interested in how to do it, the digital amateur radio community is a good place to look. 如果您对操作方法感兴趣,那么数字业余无线电社区是个不错的选择。

speakers are SourceDataLine, not TargetDataLine. 说话者是SourceDataLine,而不是TargetDataLine。 I don't understand how you can "capture" from a speaker? 我不明白您该如何“吸引”演讲者?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM