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

1    package sound.audioDigitizer; 
2     
3    import sound.soundDemo.JavaSound; 
4     
5    import javax.sound.sampled.AudioFormat; 
6    import javax.sound.sampled.AudioInputStream; 
7    import javax.sound.sampled.AudioSystem; 
8    import javax.sound.sampled.DataLine; 
9    import javax.sound.sampled.LineUnavailableException; 
10   import javax.sound.sampled.TargetDataLine; 
11   import java.io.IOException; 
12    
13   /** 
14    * DocJava, Inc. 
15    * http://www.docjava.com 
16    * Programmer: dlyon 
17    * Date: Nov 29, 2004 
18    * Time: 3:20:19 PM 
19    */ 
20   public class StreamAudio implements Runnable { 
21       TargetDataLine targetDataLine; 
22       Thread thread; 
23       String errStr = ""; 
24       CapturePlayBackPanel.FormatControls formatControls 
25               = new CapturePlayBackPanel.FormatControls(); 
26    
27       public StreamAudio() { 
28       } 
29    
30       public static void main(String[] args) { 
31           StreamAudio sa = new StreamAudio(); 
32           sa.start(); 
33       } 
34    
35       public void start() { 
36           errStr = null; 
37           thread = new Thread(this); 
38           thread.setName("Capture"); 
39           thread.start(); 
40       } 
41    
42       public void stop() { 
43           thread = null; 
44       } 
45    
46       public void run() { 
47           AudioFormat format = initAudioStuff(); 
48           int numBytesRead; 
49           targetDataLine.start(); 
50           processAudioData(); 
51    
52           // we reached the end of the stream.  stop and close the line. 
53           targetDataLine.stop(); 
54           targetDataLine.close(); 
55           targetDataLine = null; 
56       } 
57    
58       private void processAudioData() { 
59           AudioInputStream ais = new AudioInputStream(targetDataLine); 
60           StreamingPlaybackThread sp = new StreamingPlaybackThread(ais, formatControls); 
61           sp.start(); 
62           while (thread != null) { 
63               System.out.println("Running!"); 
64               try { 
65                   Thread.sleep(1000); 
66               } catch (InterruptedException e) { 
67                   e.printStackTrace(); 
68               } 
69           } 
70           try { 
71               ais.reset(); 
72           } catch (IOException e) { 
73               e.printStackTrace(); 
74           } 
75       } 
76    
77       private AudioFormat initAudioStuff() { 
78           // define the required attributes for our line, 
79           // and make sure a compatible line is supported. 
80    
81           AudioFormat format = formatControls.getFormat(); 
82           DataLine.Info info = new DataLine.Info(TargetDataLine.class, 
83                   format); 
84           if (!AudioSystem.isLineSupported(info)) { 
85               shutDown("Line matching " + info + " not supported."); 
86           } 
87           getAndInitTDL(info, format); 
88           return format; 
89       } 
90    
91       private void getAndInitTDL(DataLine.Info info, AudioFormat format) { 
92           // get and open the target data line for capture. 
93    
94           try { 
95               initTargetDataLine(info, format); 
96           } catch (LineUnavailableException ex) { 
97               shutDown("Unable to open the line: " + ex); 
98           } catch (SecurityException ex) { 
99               JavaSound.showInfoDialog(); 
100          } catch (Exception ex) { 
101          } 
102      } 
103   
104      public void initTargetDataLine(DataLine.Info info, AudioFormat format) 
105              throws LineUnavailableException { 
106          targetDataLine = (TargetDataLine) AudioSystem.getLine(info); 
107          targetDataLine.open(format, targetDataLine.getBufferSize()); 
108      } 
109   
110      private void shutDown(String s) { 
111          System.out.println(s); 
112          System.exit(0); 
113      } 
114  } 
115   
116