import javax.sound.sampled.*;
import java.io.*;
import java.net.*;
import java.util.Arrays;
/**
 * A sample program is to demonstrate how to record sound in Java
 */
public class JavaSoundRecorder {
    // record duration, in milliseconds
    static final long RECORD_TIME = 5000;  // 5 seconds
    // format of audio file
    AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
    // the line from which audio data is captured
    TargetDataLine line;
    /**
     * Defines an audio format
     */
    AudioFormat getAudioFormat() {
        float sampleRate = 16000;
        int sampleSizeInBits = 8;
        int channels = 2;
        boolean signed = true;
        boolean bigEndian = true;
        AudioFormat format = new AudioFormat(sampleRate, sampleSizeInBits,
                channels, signed, bigEndian);
        return format;
    }
    /**
     * Captures the sound and record into a WAV file
     */
    void start() {
        try {
            AudioFormat format = getAudioFormat();
            DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
            // checks if system supports the data line
            if (!AudioSystem.isLineSupported(info)) {
                System.out.println("Line not supported");
                System.exit(0);
            }
            line = (TargetDataLine) AudioSystem.getLine(info);
            line.open(format,4096);
            line.start();   // start capturing
            System.out.println("Start capturing...");
            byte tempBuffer[] = new byte[10000];
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            try{
                int c = 0;
                while(line.isOpen()){
                    //Read data from the internal
                    // buffer of the data line.
                    c++;
                    int cnt = line.read(tempBuffer, 0, tempBuffer.length);
                    System.out.println("letti dim buffer: "+cnt+"\n");
                    System.out.print("letti bytes:"+Arrays.toString(tempBuffer));
                    if(cnt > 0){
                        byteArrayOutputStream.write(tempBuffer, 0, cnt);
                    }
                }//end while
                System.out.println("numero di volte che ha letto:"+c);
                byteArrayOutputStream.close();
                String fileName = "/home/zabit/Documents/rec.wav";
                File fileStreamedWav = new File(fileName);
                AudioFileFormat.Type afType = AudioFileFormat.Type.WAVE;
                //AudioFormat adfmt = new AudioFormat(8000.0f, 8, 1, true , true);
                AudioFormat adfmt = getAudioFormat();
                ByteArrayInputStream bis = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
                AudioInputStream ais = new AudioInputStream(bis, adfmt, byteArrayOutputStream.toByteArray().length);
                int W = AudioSystem.write(ais, afType, fileStreamedWav);
                //leggi il file audio e play
                //invia al server socket
                //Socket soc = new Socket("127.0.0.1" ,4444);
                //DataOutputStream out = new DataOutputStream(soc.getOutputStream());
                //out.write(result);
            }catch (Exception e) {
                System.out.println(e);
                System.exit(0);
            }//end catch
        } catch (LineUnavailableException ex) {
            ex.printStackTrace();
        }
        //} catch (IOException ioe) {
        //    ioe.printStackTrace();
        //}
    }
    /**
     * Closes the target data line to finish capturing and recording
     */
    void finish() {
        line.stop();
        line.close();
        System.out.println("Finished");
    }
    /**
     * Entry to run the program
     */
    public static void main(String[] args) {
        final JavaSoundRecorder recorder = new JavaSoundRecorder();
        // creates a new thread that waits for a specified
        // of time before stopping
        Thread stopper = new Thread(new Runnable() {
            public void run() {
                try {
                    Thread.sleep(RECORD_TIME);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                recorder.finish();
            }
        });
        stopper.start();
        // start recording
        recorder.start();
    }
}
Salve sto provando a scrivere la parte di codice java per bufferizzare l'audio stream preso da microfono.
Mi spiego meglio, l'applicazione con un Thread rimane in attesa per 5 secondi.
Durante questi 5 secondi il processo JavaSoundRecorder parte, solo dopo trascorsi i secondi verrà terminato di fatto uscendo dal ciclo e chiudendo la comunicazione con il microfono.
Il processo dopo aver ottenuto "una linea microfono" cattura l'audio. Finchè il microfono e quindi la sua linea è aperta, leggo 10000 bytes e tento di stampare a video questi byte
byte tempBuffer[] = new byte[10000];
int cnt = line.read(tempBuffer,0,tempBuffer.length);
System.out.print("letti:"+Arrays.toString(tempBuffer));devo prima sapere la lunghezza dello stream dal mic, quindi devo bufferizzare. Pertanto creo ByteArrayOutputStream e scrivo dentro ogni chunk di 10000 bytes.
Infine prima di poter provare a inviarlo su socket salvo tutto su file wav.