2011-03-31 62 views
2

我正在处理声音数据的应用程序系统。第一个应用程序只需从麦克风插孔读取数据并将数据发送到下一个应用程序。主循环重复执行此代码:为什么我的声音滞后?

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秒的延迟,然后我会得到更大的数字。

有没有什么新想法?

回答

2

它已修复。每当我想让声音流(每当我按下播放)时,我都会关闭当前流并打开一个新流。

我没有意识到TargetDataLine实际上保存了一个声音数据的缓冲区,只要读取方法被调用,就可以从中读取声音数据。

它看起来像我从Eclipse运行应用程序时,它使用的是不同类型的TargetDataLine,而不是将它作为可运行jar文件运行时。这由缓冲区之间的大小差异来证明。尽管大小差异只是大约2倍,所以我认为问题不在于缓冲区的大小,而在于与获取的TargetDataLine有关的其他问题。

奇怪的是,删除Globals.mySleep(waitTime)在修复SSCCE中起作用,但不是它应该表示的真正程序。

我试过排水和冲洗线,而不是取代它,但这些似乎都没有工作,虽然我可能一直在错误地使用它们。

所以问题是:DataLine的缓冲区被填满,而程序没有播放时,缓冲区没有被清空,所以当它开始播放时,它继续以通常的播放速率从缓冲区获取数据,导致它落后。

解决方案是:当程序开始播放时,替换DataLine。

- 编辑 -

进一步观察表明,当我从Eclipse中运行,它似乎是使用不同的JRE比当我作为一个jar文件。我将默认的java程序设置为java-6-sun而不是java-6-openjdk,并且它可以在jar文件中正常工作。

另外,我试着运行替换另一台计算机上的DataLine的方法。在这台电脑上,我收到了一个令人讨厌的信号。这似乎需要更长的时间来拉一个新的DataLine,所以我决定这是行不通的。现在,我只是简单地从DataLine中读取数据。如果系统暂停,我不会在任何地方发送信号。

+0

发布答案的好通话。很高兴你对问题进行了整理。 :-) – 2011-05-07 16:39:18

1

我发现在这种情况下最好的做法是,你的代码很慢,但你不知道为什么要使用profiler,http://www.quest.com/jprobe/software_download.aspx你可以得到这个java profiler的免费路径,它会告诉你一行行花了多少时间以及执行了多少次,你应该能够准确地确定你用这个编码减慢了什么。

希望这有助于 埃蒙·

+0

谢谢,Eamonn。根据我发现的情况(我的文章中的一些编辑应该反映出这一点),我不认为这是一个性能问题。 – BCarpe 2011-03-31 15:38:43

1

Globals.mySleep(WAITTIME); //告诉线程的时间适量睡眠对于一个给定的数据格式

嫌疑的“适当” waitTime这里是“0”。

如果你想要更多的怀疑,我建议你发布SSCCE(没有行号)。

+0

谢谢,安德鲁。我发布了我认为是SSCCE的内容。 – BCarpe 2011-03-31 17:04:45

相关问题