/Users/lyon/j4p/src/sound/audioDigitizer/CapturePlaybackFrame.java

1    package sound.audioDigitizer; 
2     
3    /** 
4     * Created by IntelliJ IDEA. 
5     * User: Douglas Lyon 
6     * Date: Dec 14, 2004 
7     * Time: 6:28:03 AM 
8     * Copyright DocJava, Inc. 
9     */ 
10    
11   import sound.Utils; 
12    
13   import javax.sound.sampled.*; 
14   import javax.swing.*; 
15   import java.awt.*; 
16   import java.awt.event.ActionEvent; 
17   import java.awt.event.ActionListener; 
18   import java.io.ByteArrayOutputStream; 
19    
20   public class CapturePlaybackFrame extends JFrame { 
21       boolean stopCapture = false; 
22       ByteArrayOutputStream byteArrayOutputStream; 
23    
24       public static void main(String args[]) { 
25           new CapturePlaybackFrame(); 
26       }//end main 
27    
28       public CapturePlaybackFrame() { 
29           final JButton captureBtn = new JButton("Capture"); 
30           final JButton stopBtn = new JButton("Stop"); 
31           final JButton playBtn = new JButton("Playback"); 
32           captureBtn.setEnabled(true); 
33           stopBtn.setEnabled(false); 
34           playBtn.setEnabled(false); 
35   //Register anonymous listeners 
36           captureBtn.addActionListener(new ActionListener() { 
37               public void actionPerformed(ActionEvent e) { 
38                   captureBtn.setEnabled(false); 
39                   stopBtn.setEnabled(true); 
40    
41                   playBtn.setEnabled(false); 
42   //Capture input data from the microphone until the Stop button is clicked. 
43                   captureAudio(); 
44               }//end actionPerformed 
45           }//end ActionListener 
46           ); //end addActionListener() 
47           getContentPane().add(captureBtn); 
48           stopBtn.addActionListener(new ActionListener() { 
49               public void actionPerformed(ActionEvent e) { 
50                   captureBtn.setEnabled(true); 
51                   stopBtn.setEnabled(false); 
52                   playBtn.setEnabled(true); 
53   //Terminate the capturing of input data from the microphone. 
54                   stopCapture = true; 
55               }//end actionPerformed 
56           }//end ActionListener 
57           );//end addActionListener() 
58           getContentPane().add(stopBtn); 
59           playBtn.addActionListener(new ActionListener() { 
60               public void actionPerformed(ActionEvent e) { 
61   //Play back all of the data that was saved during capture. 
62                   PlayThread.playAudio(byteArrayOutputStream); 
63               }//end actionPerformed 
64           }//end ActionListener 
65           );//end addActionListener() 
66           getContentPane().add(playBtn); 
67           getContentPane().setLayout(new FlowLayout()); 
68           setTitle("Capture/Playback Demo"); 
69           setDefaultCloseOperation(EXIT_ON_CLOSE); 
70           setSize(250, 70); 
71           setVisible(true); 
72       } 
73       private void captureAudio() { 
74    
75           try { 
76               Thread captureThread = new Thread(new CaptureThread()); 
77               captureThread.start(); 
78           } catch (Exception e) { 
79               System.out.println(e); 
80               System.exit(0); 
81           }//end catch 
82       }//end captureAudio method 
83   //This method plays back the audio data that has been saved in the 
84   // ByteArrayOutputStream 
85    
86   //This method creates and returns an 
87   // AudioFormat object for a given set 
88   // of format parameters. If these 
89   // parameters don’t work well for 
90   // you, try some of the other 
91   // allowable parameter values, which 
92   // are shown in comments following 
93   // the declarations. 
94   //===================================// 
95   //Inner class to capture data from 
96   // microphone 
97       class CaptureThread extends Thread { 
98   //An arbitrary-size temporary holding buffer 
99           byte tempBuffer[] = new byte[10000]; 
100          AudioFormat audioFormat = Utils.get8khzMono8Format(); 
101          DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, 
102                  audioFormat); 
103   
104          TargetDataLine targetDataLine; 
105   
106          public CaptureThread() { 
107              try { 
108                  targetDataLine  = (TargetDataLine) AudioSystem.getLine(dataLineInfo); 
109                  targetDataLine.open(audioFormat); 
110                  targetDataLine.start(); 
111              } catch (LineUnavailableException e) { 
112                  e.printStackTrace(); 
113              } 
114          } 
115   
116          public void run() { 
117              byteArrayOutputStream = new ByteArrayOutputStream(); 
118   
119              stopCapture = false; 
120              try { //Loop until stopCapture is set by another thread that 
121  // services the Stop button. 
122                  while (!stopCapture) { 
123  //Read data from the internal 
124  // buffer of the data line. 
125                      int cnt = targetDataLine.read(tempBuffer, 0, tempBuffer.length); 
126                      if (cnt > 0) { 
127  //Save data in output stream object. 
128                          byteArrayOutputStream.write(tempBuffer, 0, cnt); 
129                      }//end if 
130                  }//end while 
131                  byteArrayOutputStream.close(); 
132              } catch (Exception e) { 
133                  System.out.println(e); 
134                  System.exit(0); 
135              }//end catch 
136          }//end run 
137      } 
138  } 
139