Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Audio capture (TargetDataLine) latency

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Audio capture (TargetDataLine) latency

    Hi I have a (very) simple program that captures audio from a input line (line in) and plays it to output line (speakers)
    The problem is that the sound has about 100ms of delay to reach the speakers I need to reduce it greatly but just cant find a way. I have tried many different audio formats and buffer size but the best delay I can get is around 100-110ms. The smallest buffer size without loss was a little less than 2048. I have also tried different JREs (5,6,7; 32/64 bit). I am using windows 7 64bit
    Can anyone help me? This is driving me crazy...



    Code below:
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.SourceDataLine;
    import javax.sound.sampled.TargetDataLine;
    import javax.sound.sampled.Mixer.Info;
     
     
    public class Main {
    	AudioFormat format;
    	int BUFFER_SIZE = 2048;
     
    	public Main() throws LineUnavailableException {
    		format = new AudioFormat((float) 44100, 8, 1, true, true); 
     
    		Info[] lines = AudioSystem.getMixerInfo();
    	    for (int i = 0; i < lines.length; i++)
    	    	System.out.println(i+": "+lines[i].getName()+"\n"+lines[i].getDescription());
     
     
    	    SourceDataLine outputLine = null; 
    	    DataLine.Info outInfo = new DataLine.Info(SourceDataLine.class, format); 
    	    outputLine = (SourceDataLine) AudioSystem.getMixer(lines[1]).getLine(outInfo); 
    	    outputLine.open(format, BUFFER_SIZE); 
     
     
    	    TargetDataLine inputLine;
    	    DataLine.Info inInfo = new DataLine.Info(TargetDataLine.class, format); 
    	    inputLine = (TargetDataLine)AudioSystem.getMixer(lines[10]).getLine(inInfo); 
    	    inputLine.open(format, BUFFER_SIZE);
     
    	    inputLine.start(); 
    	    outputLine.start(); 
    	    byte[] buffer = new byte[256]; 
    	    while (true) 
    	    { 	    	
    	        int bytesRead = inputLine.read(buffer,0,buffer.length);
    	        int bytesWritten = outputLine.write(buffer,0, bytesRead);
    	    } 
    	}
     
    	public static void main(String[] args) throws LineUnavailableException {
    		new Main();
    	}
     
     
    }
    Last edited by Pikkurotta; November 1st, 2010 at 02:22 PM.


  2. #2
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Audio capture (TargetDataLine) latency

    Pikkurotta. Thanks for this wonderful audio code. (I have been trying to do something similar for the last few days).

    I haven't tested your code - will run it now. At present I am not concerned about the time delay of 100ms.

    yours truly
    Richard Mullins

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Audio capture (TargetDataLine) latency

    Pikkurotta. Your code did not run on my machine. It bombed on line 31 which says inputline.start

    I will keep looking at your code as I have found nothing better.
    yours truly
    Richard Mullins

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Audio capture (TargetDataLine) latency

    Pikkurotta. I found another example, simular to yours. It is called AudioLoop and I downloaded the files from jsresources.org. As there are two jave source files, plus a java jar file, I don't have a file I can paste here.

    I suspect the AudioLoop will have the same delay that you found in running your code.

    yours truly
    RIchard Mullins

  5. #5
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Audio capture (TargetDataLine) latency

    javax.sound is not good for low-latency sound, and I doubt it ever will be.

    I suspect this is partially due to the way JXS polls for audio, partially because of the size of buffers that JXS uses internally, and partially because of the (relative) inefficiency of copying audio data from native structures through JNI to Java byte[] (would be nice if JXS had support ByteBuffer), and partially because of the need for TargetDataLine.read() and SourceDataLine.write() -- which are backed by JNI methods (Java methods marked with 'native' keyword) to be called very frequently and complete consistently very quickly to keep latency low. Even if you find a way to reduce latency further, it will almost certainly increase CPU consumption to unacceptably high levels.

    Anyway, the two things you could play around with are:

    1) Reduce the size of the byte[] that you pass to TargetDataLine.read. The smaller you make this buffer size, the sooner you will get your first chunk of audio recorded, so that you can start playing it back sooner. However, making this buffer smaller has a cost: it will increase CPU consumption, which could have the ultimate impact of increasing latency, rather than reducing it, and the amount of CPU usage could be unacceptable because it slows the entire computer down, not just your Java program. Even worse, chewing up too much CPU can and eventually will cause audio to skip and sound bad. So the only possibility here is tuning the buffer size by trial and error to your computer's unique performance characteristics. Small enough to reduce latency, but not so small that the CPU usage causes problems.

    2) Play around with the bufferSize argument that can be passed to TargetDataLine.open and SourceDataLine.open. The impact of tuning this value is less certain. Lowering it may reduce latency, but going too small may audio to skip and sound bad.

    For lower latency audio, you may have to go to native code.
    Last edited by mclark; April 14th, 2011 at 05:35 PM.

Similar Threads

  1. Do I need to close an audio clip?
    By m2oswald in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 8th, 2010, 02:20 AM
  2. Help with java audio samples please!
    By Reg in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 19th, 2010, 09:41 AM
  3. How to capture standard output from another program ?
    By ni4ni in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2010, 11:00 AM
  4. sun.audio
    By bguy in forum Java SE APIs
    Replies: 3
    Last Post: November 12th, 2009, 01:17 AM
  5. Java program for remote host screen capture
    By krishnaraoveera1294 in forum Java Networking
    Replies: 1
    Last Post: March 13th, 2009, 05:53 AM