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

1    package sound.audioDigitizer; 
2     
3    import net.multicast.McastUtil; 
4    import sound.Utils; 
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 java.io.IOException; 
13    
14   /** 
15    * DocJava, Inc. 
16    * http://www.docjava.com 
17    * Programmer: dlyon 
18    * Date: Dec 6, 2004 
19    * Time: 2:37:32 PM 
20    */ 
21   public class StreamingXmitterAudioMulticastThread 
22           extends Thread implements Runnable { 
23       SourceDataLine line; 
24       String errStr = null; 
25       AudioInputStream ais = null; 
26       private final int bufSize = 16384; 
27       public static final int PORT = 4321; 
28       private McastUtil mcu = new McastUtil(PORT); 
29    
30       private void sendADataGram(byte[] data, 
31                                  int numberOfBytesRead) { 
32           try { 
33               byte b[] = new byte[numberOfBytesRead]; 
34               System.out.println("number of bytes sent=" + numberOfBytesRead); 
35               for (int i = 0; i < b.length; i++) 
36                   b[i] = data[i]; 
37               mcu.sendBytes(b); 
38           } catch (IOException e) { 
39               e.printStackTrace(); 
40           } 
41       } 
42    
43       StreamingXmitterAudioMulticastThread(AudioInputStream ais) { 
44           this.ais = ais; 
45           mcu.setLoopBack(false); 
46       } 
47    
48    
49    
50       public void run() { 
51           AudioFormat format = Utils.get8khzMono8Format(); 
52           AudioInputStream audioInputStream = 
53                   AudioSystem.getAudioInputStream(format, ais); 
54           DataLine.Info info = new DataLine.Info(SourceDataLine.class, format); 
55           try { 
56               line = (SourceDataLine) AudioSystem.getLine(info); 
57               line.open(format, bufSize); 
58           } catch (LineUnavailableException ex) { 
59               ex.printStackTrace(); 
60           } 
61           int numBytesRead = 0; 
62    
63           // start the source data line 
64           line.start(); 
65           while (true) { 
66               byte[] data = new byte[16534]; 
67               try { 
68                   while ((numBytesRead = audioInputStream.read(data)) 
69                           == -1) 
70                       Thread.sleep(1); 
71                   sendADataGram(data, numBytesRead); 
72                   //threadedAudioPlayer(); 
73                   //playAudio(numBytesRemaining, data); 
74               } catch (Exception e) { 
75                   e.printStackTrace(); 
76                   break; 
77               } 
78           } 
79           cleanUp(); 
80       } 
81    
82       public void threadedAudioPlayer() { 
83           Thread t = new Thread(new Runnable() { 
84               public void run() { 
85                   try { 
86                       while (true) { 
87                           byte b[] = mcu.getBytes(); 
88                           System.out.println("rcving bytes!!=" + b.length); 
89                           playAudio(b.length, b); 
90                       } 
91                   } catch (IOException e) { 
92                       e.printStackTrace(); 
93                   } 
94               } 
95           }); 
96           t.start(); 
97       } 
98    
99       private void playAudio(int numBytesRemaining, byte[] data) { 
100          while (numBytesRemaining > 0) 
101              numBytesRemaining -= line.write(data, 0, numBytesRemaining); 
102      } 
103   
104      private void cleanUp() { 
105          line.drain(); 
106          line.stop(); 
107          line.close(); 
108          line = null; 
109      } 
110  } 
111