/Users/lyon/j4p/src/sound/recorder/AudioRecorder3.java

1    package sound.recorder; 
2     
3    /** 
4     * Created by IntelliJ IDEA. 
5     * User: Douglas Lyon 
6     * Date: Dec 13, 2004 
7     * Time: 8:02:58 PM 
8     * Copyright DocJava, Inc. 
9     */ 
10   import javax.swing.*; 
11   import java.awt.*; 
12   import java.awt.event.*; 
13   import java.io.*; 
14   import javax.sound.sampled.*; 
15    
16   public class AudioRecorder3 extends JFrame{ 
17    
18     AudioFormat audioFormat; 
19     TargetDataLine targetDataLine; 
20    
21     final JButton captureBtn = 
22                             new JButton("Capture"); 
23     final JButton stopBtn = new JButton("Stop"); 
24    
25     final JPanel btnPanel = new JPanel(); 
26     final ButtonGroup btnGroup = new ButtonGroup(); 
27     JRadioButton[] radioBtnArray; 
28     AudioFileFormat.Type[] fileTypes; 
29    
30     public static void main( String args[]){ 
31       new AudioRecorder3(); 
32     }//end main 
33    
34     public AudioRecorder3(){//constructor 
35       captureBtn.setEnabled(true); 
36       stopBtn.setEnabled(false); 
37    
38       //Register anonymous listeners 
39       captureBtn.addActionListener( 
40         new ActionListener(){ 
41           public void actionPerformed( 
42                                     ActionEvent e){ 
43             captureBtn.setEnabled(false); 
44             stopBtn.setEnabled(true); 
45             //Capture input data from the 
46             // microphone until the Stop button is 
47             // clicked. 
48             captureAudio(); 
49           }//end actionPerformed 
50         }//end ActionListener 
51       );//end addActionListener() 
52    
53       stopBtn.addActionListener( 
54         new ActionListener(){ 
55           public void actionPerformed( 
56                                     ActionEvent e){ 
57             captureBtn.setEnabled(true); 
58             stopBtn.setEnabled(false); 
59             //Terminate the capturing of input data 
60             // from the microphone. 
61             targetDataLine.stop(); 
62             targetDataLine.close(); 
63           }//end actionPerformed 
64         }//end ActionListener 
65       );//end addActionListener() 
66    
67       //Put the buttons in the JFrame 
68       getContentPane().add(captureBtn); 
69       getContentPane().add(stopBtn); 
70    
71       //Get the file types for which file writing 
72       // support is provided by the system. 
73       fileTypes = AudioSystem.getAudioFileTypes(); 
74    
75       //Create an array of radio buttons 
76       radioBtnArray = new JRadioButton[ 
77                                  fileTypes.length]; 
78    
79       for(int cnt = 0; cnt < fileTypes.length; 
80                                             cnt++){ 
81         String strType = fileTypes[cnt].toString(); 
82         if(cnt == 0){ 
83           radioBtnArray[cnt] = new JRadioButton( 
84                                      strType,true); 
85         }else{ 
86           radioBtnArray[cnt] = new JRadioButton( 
87                                           strType); 
88         }//end else 
89         radioBtnArray[cnt].setActionCommand( 
90                                           strType); 
91       }//end for loop 
92    
93       //Include the radio buttons in a group 
94       for(int cnt = 0; cnt < fileTypes.length; 
95                                             cnt++){ 
96         btnGroup.add(radioBtnArray[cnt]); 
97       }//end for loop 
98    
99       //Add the radio buttons to the JPanel 
100      for(int cnt = 0; cnt < fileTypes.length; 
101                                            cnt++){ 
102        btnPanel.add(radioBtnArray[cnt]); 
103      }//end for loop 
104   
105      //Put the JPanel in the JFrame 
106      getContentPane().add(btnPanel); 
107   
108      //Finish the GUI and make it visible 
109      getContentPane().setLayout(new FlowLayout()); 
110      setTitle("Copyright 2003, R.G.Baldwin"); 
111      setDefaultCloseOperation(EXIT_ON_CLOSE); 
112      setSize(300,120); 
113      setVisible(true); 
114    }//end constructor 
115   
116    //This method captures audio input from a 
117    // microphone and saves it in an audio file. 
118    private void captureAudio(){ 
119      try{ 
120        //Get things set up for capture 
121        audioFormat = getAudioFormat(); 
122        DataLine.Info dataLineInfo = 
123                            new DataLine.Info( 
124                              TargetDataLine.class, 
125                              audioFormat); 
126        targetDataLine = (TargetDataLine) 
127                 AudioSystem.getLine(dataLineInfo); 
128   
129        //Create a thread to capture the microphone 
130        // data into an audio file and start the 
131        // thread running.  It will run until the 
132        // Stop button is clicked.  This method 
133        // will return after starting the thread. 
134        new CaptureThread().start(); 
135      }catch (Exception e) { 
136        e.printStackTrace(); 
137        System.exit(0); 
138      }//end catch 
139    }//end captureAudio method 
140   
141    //This method creates and returns an 
142    // AudioFormat object for a given set of format 
143    // parameters.  If these parameters don't work 
144    // well for you, try some of the other 
145    // allowable parameter values, which are shown 
146    // in comments following the declarations. 
147    private AudioFormat getAudioFormat(){ 
148      float sampleRate = 8000.0F; 
149      //8000,11025,16000,22050,44100 
150      int sampleSizeInBits = 16; 
151      //8,16 
152      int channels = 1; 
153      //1,2 
154      boolean signed = true; 
155      //true,false 
156      boolean bigEndian = false; 
157      //true,false 
158      return new AudioFormat(sampleRate, 
159                             sampleSizeInBits, 
160                             channels, 
161                             signed, 
162                             bigEndian); 
163    }//end get8khzMono8Format 
164  //=============================================// 
165   
166  //Inner class to capture data from microphone 
167  // and write it to an output audio file. 
168  class CaptureThread extends Thread{ 
169    public void run(){ 
170      AudioFileFormat.Type fileType = null; 
171      File audioFile = null; 
172   
173      //Get the selected file type described as 
174      // a String 
175      String strType = btnGroup.getSelection(). 
176                                getActionCommand(); 
177      //Set the file type and the file extension 
178      // based on the selected radio button.  Test 
179      // for the common audio file types supported 
180      // by Java SDK version 1.4.1.  If the type 
181      // doesn't match one of the common types, 
182      // create a file of the default type AU. 
183      if(strType.equals("AIFC")){ 
184        fileType = AudioFileFormat.Type.AIFC; 
185        audioFile = new File("junk." + 
186                          fileType.getExtension()); 
187      }else if(strType.equals("AIFF")){ 
188        fileType = AudioFileFormat.Type.AIFF; 
189        audioFile = new File("junk." + 
190                          fileType.getExtension()); 
191      }else if(strType.equals("AU")){ 
192        fileType = AudioFileFormat.Type.AU; 
193        audioFile = new File("junk." + 
194                          fileType.getExtension()); 
195      }else if(strType.equals("SND")){ 
196        fileType = AudioFileFormat.Type.SND; 
197        audioFile = new File("junk." + 
198                          fileType.getExtension()); 
199      }else if(strType.equals("WAVE")){ 
200        fileType = AudioFileFormat.Type.WAVE; 
201        audioFile = new File("junk." + 
202                          fileType.getExtension()); 
203      }else{ 
204        System.out.println( 
205           "File type not recognized by program."); 
206        System.out.println( 
207                       "Creating default type AU"); 
208        fileType = AudioFileFormat.Type.AU; 
209        audioFile = new File("junk." + 
210                          fileType.getExtension()); 
211      }//end else 
212   
213      try{ 
214        targetDataLine.open(audioFormat); 
215        targetDataLine.start(); 
216        AudioSystem.write( 
217              new AudioInputStream(targetDataLine), 
218              fileType, 
219              audioFile); 
220      }catch (Exception e){ 
221        e.printStackTrace(); 
222      }//end catch 
223   
224    }//end run 
225  }//end inner class CaptureThread 
226  //=============================================// 
227   
228  } 
229