/Users/lyon/j4p/src/midterm/StreamAudio.java

1    package midterm; 
2     
3    import sound.soundDemo.JavaSound; 
4    import sound.audioDigitizer.CapturePlayBackPanel; 
5     
6    import javax.sound.sampled.AudioFormat; 
7    import javax.sound.sampled.AudioInputStream; 
8    import javax.sound.sampled.AudioSystem; 
9    import javax.sound.sampled.DataLine; 
10   import javax.sound.sampled.LineUnavailableException; 
11   import javax.sound.sampled.SourceDataLine; 
12   import javax.sound.sampled.TargetDataLine; 
13   import java.io.IOException; 
14   import java.awt.*; 
15    
16    
17   /** 
18    * DocJava, Inc. 
19    * http://www.docjava.com 
20    * Programmer: dlyon 
21    * Date: Nov 29, 2004 
22    * Time: 3:20:19 PM 
23    */ 
24   public class StreamAudio implements Runnable { 
25       TargetDataLine targetDataLine; 
26       Thread thread; 
27       String errStr = ""; 
28       int oldx=0; 
29       CapturePlayBackPanel.FormatControls formatControls 
30               = new CapturePlayBackPanel.FormatControls(); 
31       public int delay = 10; 
32    
33      private static StreamDelaySliderPanel sl =new StreamDelaySliderPanel(); 
34    
35       public static void main(String[] args) { 
36           StreamAudio sa = new StreamAudio(); 
37           gui.ClosableJFrame cf = new gui.ClosableJFrame(); 
38           Container c = cf.getContentPane(); 
39           c.add(sl); 
40           cf.setSize(200, 150); 
41           cf.show(); 
42           sa.start(); 
43       } 
44    
45       public void start() { 
46           errStr = null; 
47           thread = new Thread(this); 
48           thread.setName("Capture"); 
49           thread.start(); 
50       } 
51    
52       public void stop() { 
53           thread = null; 
54       } 
55    
56       public void run() { 
57           AudioFormat format = initAudioStuff(); 
58           int numBytesRead; 
59           targetDataLine.start(); 
60           processAudioData(); 
61    
62           // we reached the end of the stream.  stop and close the line. 
63           targetDataLine.stop(); 
64           targetDataLine.close(); 
65           targetDataLine = null; 
66       } 
67       public int getDelay(){ 
68    
69           return sl.x; 
70       } 
71       private void processAudioData() { 
72           AudioInputStream ais = new AudioInputStream(targetDataLine); 
73           StreamPlayback sp = new StreamPlayback(ais,formatControls); 
74    
75    
76           sp.start(); 
77             System.out.println("Start audio"); 
78           while (thread != null) {            
79               try { 
80                   Thread.sleep(100); 
81               } catch (InterruptedException e) { 
82                   e.printStackTrace(); 
83               } 
84           } 
85           try { 
86               ais.reset(); 
87           } catch (IOException e) { 
88               e.printStackTrace(); 
89           } 
90       } 
91    
92    
93       private AudioFormat initAudioStuff() { 
94           // define the required attributes for our line, 
95           // and make sure a compatible line is supported. 
96    
97           AudioFormat format = formatControls.getFormat(); 
98           DataLine.Info info = new DataLine.Info(TargetDataLine.class, 
99                   format); 
100          if (!AudioSystem.isLineSupported(info)) { 
101              shutDown("Line matching " + info + " not supported."); 
102          } 
103          getAndInitTDL(info, format); 
104          return format; 
105      } 
106   
107      private void getAndInitTDL(DataLine.Info info, AudioFormat format) { 
108          // get and open the target data line for capture. 
109   
110          try { 
111              initTargetDataLine(info, format); 
112          } catch (LineUnavailableException ex) { 
113              shutDown("Unable to open the line: " + ex); 
114          } catch (SecurityException ex) { 
115              JavaSound.showInfoDialog(); 
116          } catch (Exception ex) { 
117          } 
118      } 
119   
120      public void initTargetDataLine(DataLine.Info info, AudioFormat format) 
121              throws LineUnavailableException { 
122          targetDataLine = (TargetDataLine) AudioSystem.getLine(info); 
123          targetDataLine.open(format, targetDataLine.getBufferSize()); 
124      } 
125   
126      private void shutDown(String s) { 
127          System.out.println(s); 
128          System.exit(0); 
129      } 
130  } // End class Capture 
131   
132  class StreamPlayback implements Runnable { 
133      SourceDataLine line; 
134      Thread thread; 
135      String errStr = null; 
136      AudioInputStream ais = null; 
137      private final int bufSize = 16384; 
138      CapturePlayBackPanel.FormatControls formatControls = null; 
139      public static StreamAudio str = new StreamAudio(); 
140       StreamPlayback (){ 
141   
142      } 
143      StreamPlayback(AudioInputStream ais, CapturePlayBackPanel.FormatControls formatControls) { 
144   
145          this.ais = ais; 
146          this.formatControls = formatControls; 
147      } 
148   
149      public void start() { 
150          thread = new Thread(this); 
151          thread.setName("Playback"); 
152          thread.start(); 
153      } 
154   
155      public void stop() { 
156          thread = null; 
157      } 
158   
159      private void shutDown(String message) { 
160          if ((errStr = message) != null) { 
161              System.err.println(errStr); 
162          } 
163          if (thread != null) { 
164              thread = null; 
165          } 
166      } 
167   
168      public void run() { 
169   
170          AudioFormat format = getAudioFormat(); 
171          AudioInputStream playbackInputStream = AudioSystem.getAudioInputStream(format, ais); 
172          DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); 
173          try { 
174              line = (SourceDataLine) AudioSystem.getLine(info); 
175              line.open(format, bufSize); 
176          } catch (LineUnavailableException ex) { 
177              shutDown("Unable to open the line: " + ex); 
178          } 
179          // play back the captured audio data 
180   
181          int frameSizeInBytes = format.getFrameSize(); 
182          int bufferLengthInFrames = line.getBufferSize() / 8; 
183          int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes; 
184   
185          //System.out.println("DEEEEEEEEEL"+ str.delay); 
186           byte[] data; 
187          int numBytesRead = 0; 
188           int delay; 
189          // start the source data line 
190          line.start(); 
191          while (thread != null) { 
192              delay =str.getDelay()*500; 
193               data = new byte[delay] ; 
194              try { 
195                  if ((numBytesRead = playbackInputStream.read(data)) == -1) { 
196                      Thread.sleep(100); 
197                  } 
198                 int numBytesRemaining = numBytesRead; 
199                 while (numBytesRemaining > 0) 
200                     numBytesRemaining -= line.write(data, 0, numBytesRemaining); 
201              } catch (Exception e) { 
202                  shutDown("Error during playback: " + e); 
203                  break; 
204              } 
205          } 
206          cleanUp(); 
207      } 
208   
209      private void cleanUp() { 
210          // we reached the end of the stream.  let the data play out, then 
211          // stop and close the line. 
212          if (thread != null) { 
213              line.drain(); 
214   
215          } 
216   
217          line.stop(); 
218          line.close(); 
219          line = null; 
220          shutDown(null); 
221      } 
222   
223      private AudioFormat getAudioFormat() { 
224          // make sure we have something to play 
225          if (ais == null) 
226              shutDown("No loaded audio to play back"); 
227   
228          // get an AudioInputStream of the desired format for playback 
229          AudioFormat format = formatControls.getFormat(); 
230          return format; 
231      } 
232  } // End class Playback 
233   
234   
235   
236