我正在处理声音数据的应用程序系统。第一个应用程序只需从麦克风插孔读取数据并将数据发送到下一个应用程序。主循环重复执行此代码:为什么我的声音滞后?
0 : Globals.mySleep(waitTime); // tells the thread to sleep for the proper amount of time for a given data format
1 : inputLine.read(buffer, 0, bufferSize); // reads sound data from the microphone jack into buffer
2 : if(connections.get(REGISTER) != null) { // if the next application is connected
3 : DataSlice slice = new DataSlice(buffer, serialIDCounter++, getDeviceName()); // create a slice of data to send, containing the sound data
4 : try{
5 : connections.get(REGISTER).sendDataSlice(slice); // send the data to the next application. supposed to block until next application receives the data
6 : connections.get(REGISTER).flush(); // make sure data gets sent
7 : } catch (IOException e) {
8 : // Stream has been broken. Shut Down
9 : close();
10: }
11: }
当我启动系统时,它总是落后几秒。如果我暂停系统(GUI应用程序告诉应用程序在输入应用程序停止接收来自输入应用程序的数据之后,所以输入应用程序在暂停时应该在第5行阻止),等待然后再次播放,系统会滞后很长时间我刚刚停了下来。例如,如果它以10秒的延迟开始,然后暂停5秒并再次播放,则会滞后15秒。
当我将程序作为可运行jar文件运行时,会发生这种情况。从Eclipse运行它时不会发生。
我已经在两台运行Ubuntu Linux 10.04 LTS的计算机上测试过它。它发生在一个,而不是另一个。尽管另一方面,当我尝试从Eclipse运行它时,确实遇到了一个完全不同的问题。不知道该怎么做。如果你想在电脑上看一些规格,我很乐意把它们给你。只要告诉我你想要什么样的规格以及如何获得它们。
谁能告诉我什么可能会导致滞后?谢谢。
- 编辑 -
每安德鲁的建议下,我创造了什么,我相信是一个SSCCE:
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.sound.sampled.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main implements MouseListener{
// Class that reads a signal from Line-in source and sends that signal
// to either a recorder module or the signal-viewing pipeline
public class PlayThread extends Thread {
byte[] buffer = new byte[bufferSize];
boolean playing = false;
boolean connected = false;
PlayThread() {}
public void run() {
while(true) {
try {
sleep(waitTime);
inputLine.read(buffer, 0, bufferSize);
if(connected) {
while(!playing)
sleep(100);
int max = 0;
for(int i = 0; i < buffer.length; i++) {
if(Math.abs(buffer[i]) > max)
max = Math.abs(buffer[i]);
}
System.out.println("Max: " + max);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void setPlaying(boolean playing) {
this.playing = playing;
}
public void setConnected(boolean connected) {
this.connected = connected;
}
}
TargetDataLine inputLine;
AudioFormat format;
float sampleRate;
int sampleSizeBits;
int channels;
int waitTime;
int bufferSize;
int slicesPerSecond;
int windowSize = 512;
PlayThread pThread;
JFrame gui = new JFrame("Sound Lag");
JPanel panel = new JPanel();
JButton play = new JButton("Play"), pause = new JButton("Pause"),
connect = new JButton("Connect"), disconnect = new JButton("Disconnect");
Main() {
sampleRate = 44100;
sampleSizeBits = 16;
channels = 2;
bufferSize = (sampleSizeBits/8)*channels*windowSize;
slicesPerSecond = (int) ((sampleRate/(float)channels)/(float)windowSize);
waitTime = (int)((((1000f/sampleRate)/(float)sampleSizeBits)/2f)*8f*(float)bufferSize);
play.addMouseListener(this);
pause.addMouseListener(this);
connect.addMouseListener(this);
disconnect.addMouseListener(this);
panel.add(play);
panel.add(pause);
panel.add(connect);
panel.add(disconnect);
gui.add(panel);
gui.setVisible(true);
gui.pack();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void read() {
// Open line from line-in
format = new AudioFormat(sampleRate, sampleSizeBits, channels, true, true);
// Obtain and open the lines.
inputLine = getTargetDataLine();
pThread = new PlayThread();
pThread.start();
}
private TargetDataLine getTargetDataLine() {
try {
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
for (Mixer.Info mi : AudioSystem.getMixerInfo()) {
TargetDataLine dataline = null;
try {
Mixer mixer = AudioSystem.getMixer(mi);
dataline = (TargetDataLine)mixer.getLine(info);
dataline.open(format);
dataline.start();
return dataline;
}
catch (Exception e) {}
if (dataline != null)
try {
dataline.close();
}
catch (Exception e) {}
}
}
catch (Exception e) {}
return null;
}
public static void main(String[] args) {
Main main = new Main();
main.read();
}
@Override
public void mouseClicked(MouseEvent arg0) {
if(arg0.getSource() == play) {
System.out.println("Playing");
pThread.setPlaying(true);
}
else if(arg0.getSource() == pause) {
System.out.println("Paused");
pThread.setPlaying(false);
}
else if(arg0.getSource() == connect) {
System.out.println("Connected");
pThread.setConnected(true);
}
else if(arg0.getSource() == disconnect) {
System.out.println("Disconnected");
pThread.setConnected(false);
}
}
@Override public void mouseEntered(MouseEvent arg0) {}
@Override public void mouseExited(MouseEvent arg0) {}
@Override public void mousePressed(MouseEvent arg0) {}
@Override public void mouseReleased(MouseEvent arg0) {}
}
此代码生成与它的四个按钮的窗口:播放,暂停,连接,并断开连接。如果按下播放,就好像该节目处于“播放”模式。如果单击连接,就好像声音输入应用程序已连接到下一个模块。
要测试,请执行以下操作:
将声音设备连接到麦克风插孔(但不要播放任何内容)。
从此代码创建可运行jar文件。
从终端运行文件。
点击“播放”。
点击“连接”。
在这一点上,你应该看到一堆更小的数字沿着终端。
在您的音响设备上,开始播放声音。
你应该立即开始在终端上看到更大的数字。
停止播放声音设备上的声音(应该返回到终端中的较小号码)。
点击“暂停”。
等5秒钟。
点击“播放”。
用音频设备开始播放声音。
这是错误发生的地方。如果我在Eclipse中运行此代码,我会立即再次获得更大的数字。如果我只是运行jar文件,那么会有5秒的延迟,然后我会得到更大的数字。
有没有什么新想法?
发布答案的好通话。很高兴你对问题进行了整理。 :-) – 2011-05-07 16:39:18