1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/***********************************************************************/
import javax.media.sound.sampled.*;
import java.applet.*;
import java.awt.*;
import java.io.*;
/*
* Reads data from the input channel and writes to the output stream
*/
public class Capture extends Applet implements Runnable{
TargetDataLine line;
Thread thread;
private Button startCaptureButton, stopCaptureButton;
public void init()
{
System.out.println("init");
this.setBackground(Color.white);
startCaptureButton = new Button("Start Capture");
startCaptureButton.setForeground(Color.black);
startCaptureButton.setBackground(Color.lightGray);
this.add(startCaptureButton);
stopCaptureButton = new Button("Stop Capture");
stopCaptureButton.setForeground(Color.black);
stopCaptureButton.setBackground(Color.lightGray);
this.add(stopCaptureButton);
}
public boolean action(Event event, Object arg)
{
System.out.println("action");
if(event.target == startCaptureButton)
{
this.debut();
return true;
}
else if(event.target == stopCaptureButton)
{
this.fin();
return true;
}
else
return super.action(event, arg);
}
public void debut() {
System.out.println("start1");
thread = new Thread(this);
thread.setName("Capture");
System.out.println("start2");
thread.start();
}
public void fin() {
System.out.println("stop");
thread = null;
}
public void run() {
// define the required attributes for our line,
// and make sure a compatible line is supported.
Type encoding = AudioFormat.PCM_SIGNED;
float rate = 44100;
int sampleSize = 16;
int channels = 2;
boolean bigEndian = true;
AudioFormat format = new AudioFormat(encoding, rate, sampleSize,
channels, (sampleSize/8)*channels, rate, bigEndian);
DataLine.Info info = new DataLine.Info(TargetDataLine.class,
null, null, new Class[0], format, AudioSystem.NOT_SPECIFIED);
System.out.println("run");
if (!AudioSystem.isSupportedLine(info)) {
System.out.println("Line matching " + info + " not supported.");
return;
}
System.out.println("supported");
// get and open the target data line for capture.
try {
line = (TargetDataLine) AudioSystem.getLine(info);
System.out.println("line_open?");
line.open(format, 5000); //line.getBufferSize());
} catch (LineUnavailableException ex) {
System.out.println("Unable to open the line: " + ex);
return;
}
catch (SecurityException ex) {
System.out.println("Unable to open the line: " + ex);
}
System.out.println("open");
// play back the captured audio data
ByteArrayOutputStream out = new ByteArrayOutputStream();
int frameSizeInBytes = format.getFrameSize();
int bufferLengthInFrames = line.getBufferSize() / 8;
int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
byte[] data = new byte[bufferLengthInBytes];
int numFramesRead;
line.start();
System.out.println("line start");
while (thread != null) {
if ((numFramesRead = line.read(data, 0, bufferLengthInFrames)) ==
-1) {
break;
}
out.write(data, 0, (numFramesRead * frameSizeInBytes));
}
System.out.println("line stop");
// we reached the end of the stream. stop and close the line.
line.stop();
line.close();
line = null;
System.out.println("line null");
// stop and close the output stream
try {
out.flush();
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("stream close");
// load bytes into the audio input stream for playback
byte audioBytes[] = out.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes);
AudioInputStream audioInputStream = new AudioInputStream(bais, format,
audioBytes.length / frameSizeInBytes);
try {
audioInputStream.reset();
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("end");
}
}
/***********************************************************************/
|