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