package testaudio; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.net.Socket; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.TargetDataLine; public class CaptureAudio implements Runnable { // Capture Class final int bufSize = 16384; AudioInputStream audioInputStream; String errStr; double duration, seconds; TargetDataLine line; Thread thread; String ip = ""; int port = 15100; int tiempoEsperaServidorReceptorAudio = 5000; private float rate = 44100.0f; private int channels = 2; private int sampleSize = 16; private Boolean bigEndian = true; private List lFormat = null; public CaptureAudio(String ip, int port, int tiempoEspera) { this.ip = ip; this.port = port; this.tiempoEsperaServidorReceptorAudio = tiempoEspera; } public CaptureAudio() { } public CaptureAudio(float rate, int channels, int sampleSize, Boolean bigEndian) { this.rate = rate; this.channels = channels; this.sampleSize = sampleSize; this.bigEndian = bigEndian; } public void start() { errStr = null; thread = new Thread(this); thread.setName("Capture"); thread.start(); } public void stop() { thread = null; } private void shutDown(String message) { if ((errStr = message) != null && thread != null) { thread = null; System.err.println(errStr); } } public void run() { duration = 0; audioInputStream = null; // define the required attributes for our line, // and make sure a compatible line is supported. javax.sound.sampled.AudioFormat.Encoding encoding = javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED; // javax.sound.sampled.AudioFormat format = new javax.sound.sampled.AudioFormat(encoding, rate, sampleSize, channels, (sampleSize / 8) // * channels, rate, bigEndian); javax.sound.sampled.AudioFormat format = new javax.sound.sampled.AudioFormat(8000.0f, 16, 1, true, true); DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); if (!AudioSystem.isLineSupported(info)) { System.out.println("Line matching " + info + " not supported."); shutDown("Line matching " + info + " not supported."); return; } // get and open the target data line for capture. try { line = AudioSystem.getTargetDataLine(format); line.open(format, line.getBufferSize()); } catch (LineUnavailableException ex) { System.out.println("ERROR -> Line unavailable"); shutDown("Unable to open the line: " + ex); return; } catch (SecurityException ex) { shutDown(ex.toString()); //JavaSound.showInfoDialog(); return; } catch (Exception ex) { shutDown(ex.toString()); return; } // play back the captured audio data ByteArrayOutputStream out = new ByteArrayOutputStream(); int frameSizeInBytes = format.getFrameSize(); int bufferLengthInFrames = line.getBufferSize() / 8; int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes; byte[] data = new byte[bufferLengthInBytes]; int numBytesRead; line.start(); Socket socket = null; try { long i1 = System.currentTimeMillis(); while (thread != null) { if ((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1) { break; } out.write(data, 0, numBytesRead); if ((System.currentTimeMillis() - i1) > this.tiempoEsperaServidorReceptorAudio) { i1 = System.currentTimeMillis(); byte[] stream = out.toByteArray(); System.out.println("SEND: " + stream.length); socket = new Socket(this.ip, this.port); socket.getOutputStream().write(stream); out.reset(); socket.getOutputStream().flush(); try { socket.close(); } catch (Exception e) { }; } } } catch (Exception e) { e.printStackTrace(); } // we reached the end of the stream. // stop and close the line. line.stop(); line.close(); line = null; // stop and close the output stream try { out.flush(); out.close(); } catch (IOException ex) { ex.printStackTrace(); } // load bytes into the audio input stream for playback byte audioBytes[] = out.toByteArray(); try { try { socket.close(); } catch (Exception e) { }; } catch (Exception e) { e.printStackTrace(); } long milliseconds = (long) ((audioInputStream.getFrameLength() * 1000) / format .getFrameRate()); duration = milliseconds / 1000.0; try { audioInputStream.reset(); } catch (Exception ex) { ex.printStackTrace(); return; } } }