/* 
 * Build instructions:
 * $(JDKPATH)/bin/javac mic2wav.java
 * $(JDKPATH)/bin/java mic2wav
 */
import java.io.*;
import javax.sound.sampled.*;

public class mic2wav {

	/* Record sound with this sampling parameters */
	final static int SAMPLES_PER_SEC = 16000; /* e.g.: 16000 44100 88200 */
	final static int BITS_PER_SAMPLE = 8; /* e.g.: 8 or 16 bits/sample */
	final static int NUM_CHANNELS = 1; /* e.g.: 1 for mono, 2 for stereo */

	/* Seconds to record */
	final static int SECONDS = 5;
	
	public static void main(String[] args) 
			throws IOException, LineUnavailableException {

		/* Modify the constants for a different audio format */
		AudioFormat format = new AudioFormat(SAMPLES_PER_SEC, BITS_PER_SAMPLE, 
				                             NUM_CHANNELS, true, true);
		
		/* TargetDataLine represents the MIC input */
		TargetDataLine line = AudioSystem.getTargetDataLine(format);
		
		/* Use a dynamic array for storing the captured data */
		ByteArrayOutputStream captured = new ByteArrayOutputStream();
		
		/* We will fetch captured data in "chunks" from the line's buffer" */
		byte[] chunk = new byte[4096];
		int nbytes;	
		
		/* START CAPTURING!! */
		line.open(format);
		line.start();		
		
		while (captured.size() < (SAMPLES_PER_SEC*SECONDS)) {
            nbytes = line.read(chunk, 0, chunk.length);
            captured.write(chunk, 0, nbytes);
        }
		
		/* STOP CAPTURING */
		line.drain();
		line.close();

		/* Create a wav file to store the samples */
        File wav = new File("captured.wav");      
        
        /* Copy the captured samples into a audioInputStream */
        byte[] captured_bytes = captured.toByteArray();
		ByteArrayInputStream inputstream = 
				new ByteArrayInputStream(captured_bytes);
        AudioInputStream audioInputStream = 
        		new AudioInputStream(inputstream, format, captured.size());
  
        /* Write the samples into the WAV file */
        AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE, wav);
 
        /* Housekeeping */
        audioInputStream.close();
        captured.close();	
	}
}



@@ -1,11 +1,10 @@
 /*
- 
 %0A * Buil
@@ -164,17 +164,20 @@
 2wav %7B%0A%0A
-%09
+    
 /* Recor
@@ -213,25 +213,28 @@
 rameters */%0A
-%09
+    
 final static
@@ -289,25 +289,28 @@
 00 88200 */%0A
-%09
+    
 final static
@@ -363,25 +363,28 @@
 s/sample */%0A
-%09
+    
 final static
@@ -444,17 +444,20 @@
 reo */%0A%0A
-%09
+    
 /* Secon
@@ -472,17 +472,20 @@
 cord */%0A
-%09
+    
 final st
@@ -506,19 +506,21 @@
 DS = 5;%0A
-%09%0A%09
+%0A    
 public s
@@ -549,21 +549,29 @@
 %5B%5D args)
- %0A%09%09%09
+%0A            
 throws I
@@ -610,18 +610,24 @@
 tion %7B%0A%0A
-%09%09
+        
 /* Modif
@@ -674,18 +674,24 @@
 rmat */%0A
-%09%09
+        
 AudioFor
@@ -752,22 +752,19 @@
 _SAMPLE,
- %0A%09%09%09%09
+%0A  
         
@@ -784,16 +784,30 @@
         
+              
 NUM_CHAN
@@ -825,21 +825,25 @@
  true);%0A
-%09%09%0A%09%09
+%0A        
 /* Targe
@@ -880,18 +880,24 @@
 nput */%0A
-%09%09
+        
 TargetDa
@@ -949,21 +949,25 @@
 ormat);%0A
-%09%09%0A%09%09
+%0A        
 /* Use a
@@ -1014,18 +1014,24 @@
 data */%0A
-%09%09
+        
 ByteArra
@@ -1084,21 +1084,25 @@
 ream();%0A
-%09%09%0A%09%09
+%0A        
 /* We wi
@@ -1163,18 +1163,24 @@
 fer%22 */%0A
-%09%09
+        
 byte%5B%5D c
@@ -1202,18 +1202,24 @@
 %5B4096%5D;%0A
-%09%09
+        
 int nbyt
@@ -1221,23 +1221,26 @@
  nbytes;
-%09%0A%09%09%0A%09%09
+%0A%0A        
 /* START
@@ -1251,26 +1251,32 @@
 TURING!! */%0A
-%09%09
+        
 line.open(fo
@@ -1282,18 +1282,24 @@
 ormat);%0A
-%09%09
+        
 line.sta
@@ -1307,16 +1307,18 @@
 t();
-%09%09%0A%09%09%0A%09%09
+%0A%0A        
 whil
@@ -1483,13 +1483,17 @@
   %7D%0A
-%09%09%0A%09%09
+%0A        
 /* S
@@ -1509,18 +1509,24 @@
 RING */%0A
-%09%09
+        
 line.dra
@@ -1531,18 +1531,24 @@
 rain();%0A
-%09%09
+        
 line.clo
@@ -1554,18 +1554,24 @@
 ose();%0A%0A
-%09%09
+        
 /* Creat
@@ -1651,31 +1651,17 @@
 d.wav%22);
-      %0A        
+%0A
 %0A       
@@ -1773,18 +1773,24 @@
 rray();%0A
-%09%09
+        
 ByteArra
@@ -1819,14 +1819,25 @@
 am =
- %0A%09%09%09%09
+%0A                
 new 
@@ -1917,17 +1917,16 @@
 Stream =
- 
 %0A       
@@ -1930,10 +1930,16 @@
     
-%09%09
+        
 new 
@@ -1994,18 +1994,16 @@
 ize());%0A
-  
 %0A       
@@ -2122,17 +2122,16 @@
 , wav);%0A
- 
 %0A       
@@ -2213,13 +2213,13 @@
 e();
-%09%0A%09
+%0A    
 %7D%0A%7D%0A
-%0A%0A

correct Eclipse indentation crap