Java - reading audio from TargetDataLine - reads only zeroes/

Viewed 122

I'm having trouble with TargetDataLine - it keeps reading only zeroes from the mixer.

The following is using arecord which works perfectly:

arecord -D plughw:1,0,0 -r 44100 -f s16_le -c 2 -d 3 /tmp/file.wav

When I try this with javax.sound.sampled, I keep getting zeroes. The following opens the TargetDataLine with a thread to stop the reading after 5 seconds. the read buffer is consistently filled with zeroes.

package com.example;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.Mixer.Info;

public class AudioTest {
    
    static final long RECORD_TIME = 5000; 
    private TargetDataLine line;
    private boolean isRecording = false;
    
    private void run() {

        Thread stopper = new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(RECORD_TIME);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                System.out.println("\nSleep for " + RECORD_TIME + " complete - attempting to end");
                finish();
            }
        });
        
        stopper.start();
        
        try {
            AudioFormat format = new AudioFormat(44100, 16, 2, true, false);
            
            Info[] infos = AudioSystem.getMixerInfo();
            
            int mixerNumber = 3;  // This is the one that matches the one that works with arecord.
            
            Mixer mixer = AudioSystem.getMixer(infos[mixerNumber]);
            
            System.out.println("Using infos " + mixerNumber + ", " + infos[mixerNumber].getName() + ", " +infos[mixerNumber].getDescription());
            System.out.println("Using format to open TargetDataLine: " + format.toString());
            
            DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
 
            line = (TargetDataLine)mixer.getLine(info);
             
             int BUFFER_SIZE = 2048;
             byte[] bytesBuffer = new byte[BUFFER_SIZE];
             int bytesRead = -1;
             
             line.open(format);
             isRecording = true;

             while (((bytesRead = line.read(bytesBuffer, 0 , BUFFER_SIZE)) != -1) && (isRecording)) {
                 
                 if (bytesRead != 0)
                     System.out.println("Read Something - yeah");
             }
             
             System.out.println("");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private void finish() {
        line.stop();
        line.close();
        isRecording = false;
    }
    
    public static void main(String[] args) {
        AudioTest audioTest = new AudioTest();
        audioTest.run();
    }
    
}

The output typically looks like this:

Using infos 3, Loopback [plughw:1,0], Direct Audio Device: Loopback, Loopback PCM, Loopback PCM
Using format to open TargetDataLine: PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
Sleep for 5000 complete - attempting to end
logout

Is there anything obvious that I'm missing - thanks

0 Answers
Related