Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Author: Xiphias3
- * Date: Oct 2, 2010
- * Time: 11:50:43 AM
- */
- package xiphias3.client;
- import xiphias3.util.AudioUtil;
- import javax.sound.sampled.*;
- import javax.sound.sampled.AudioFormat;
- import javax.media.protocol.*;
- import javax.media.Time;
- import javax.media.Format;
- import javax.media.Buffer;
- import java.util.Vector;
- import java.io.IOException;
- public class Microphone extends PushBufferDataSource implements Runnable
- {
- private TargetDataLine m_targetLine;
- private DataLine.Info m_lineInfo;
- private Thread m_transferThread;
- private int m_nTransferBufferSize;
- private PushBufferStream[] m_aStreams;
- private long m_lBytesRead;
- private BufferTransferHandler m_bufferTransferHandler;
- private ContentDescriptor m_contentDescriptor;
- private AudioFormat m_jsFormat;
- private MicStream m_micStream;
- private javax.media.format.AudioFormat m_jmfFormat;
- public static final int ENDIAN = javax.media.format.AudioFormat.LITTLE_ENDIAN;
- public static final int SIGNED = javax.media.format.AudioFormat.SIGNED;
- public Microphone() {
- m_micStream = new MicStream();
- m_aStreams = new PushBufferStream[]{ m_micStream };
- m_contentDescriptor = new ContentDescriptor(ContentDescriptor.RAW);
- }
- protected boolean createLine() {
- if ((m_targetLine == null) && (m_lineInfo == null)) {
- try {
- m_targetLine = AudioSystem.getTargetDataLine(null);
- m_lineInfo = (DataLine.Info) m_targetLine.getLineInfo();
- return true;
- }
- catch (LineUnavailableException lux) {
- m_targetLine = null;
- m_lineInfo = null;
- }
- }
- return false;
- }
- public void setAudioFormat(AudioFormat format) {
- m_jsFormat = format;
- m_jmfFormat = new javax.media.format.AudioFormat(javax.media.format.AudioFormat.LINEAR, format.getSampleRate(), format.getSampleSizeInBits(), format.getChannels(), ENDIAN, SIGNED);
- }
- public AudioFormat[] getAvailableFormats() {
- if (m_lineInfo != null) {
- AudioFormat[] aFormats = {
- new AudioFormat(48000.0f, 16, 1, true, false),
- new AudioFormat(44100.0f, 16, 1, true, false),
- new AudioFormat(22050.0f, 16, 1, true, false),
- new AudioFormat(16050.0f, 16, 1, true, false),
- new AudioFormat(11025.0f, 16, 1, true, false),
- new AudioFormat(8000.0f, 16, 1, true, false)
- };
- Vector<AudioFormat> supportedFormatsVector = new Vector<AudioFormat>();
- for (AudioFormat format : aFormats) {
- if (m_lineInfo.isFormatSupported(format)) {
- supportedFormatsVector.add(format);
- }
- }
- if (supportedFormatsVector.size() > 0)
- return supportedFormatsVector.toArray(new AudioFormat[0]);
- }
- return null;
- }
- private boolean openLine(AudioFormat format) throws LineUnavailableException {
- if ((m_targetLine != null) && (!m_targetLine.isOpen())) {
- m_targetLine.open(format);
- return true;
- }
- return false;
- }
- private boolean closeLine() {
- if ((m_targetLine != null) && (m_targetLine.isOpen())) {
- m_targetLine.close();
- return true;
- }
- return false;
- }
- private boolean startLine() {
- if ((m_targetLine != null) && (!m_targetLine.isRunning())) {
- m_targetLine.flush();
- m_targetLine.start();
- m_nTransferBufferSize = (int) AudioUtil.millis2Bytes(20, m_targetLine.getFormat());
- //System.out.println("Transfer Buffer size set to " + m_nTransferBufferSize);
- return true;
- }
- return false;
- }
- private boolean stopLine(boolean bDrain) {
- if ((m_targetLine != null) && (m_targetLine.isRunning())) {
- if (bDrain)
- m_targetLine.drain();
- m_targetLine.flush();
- m_targetLine.stop();
- return true;
- }
- return false;
- }
- public TargetDataLine getLine() {
- return m_targetLine;
- }
- public DataLine.Info getLineInfo() {
- return m_lineInfo;
- }
- public PushBufferStream[] getStreams() {
- return m_aStreams;
- }
- public String getContentType() {
- return ContentDescriptor.RAW;
- }
- public synchronized void connect() throws IOException {
- if (m_targetLine == null) {
- if (!createLine())
- throw new IOException("Could not create line");
- }
- }
- public synchronized void disconnect() {
- if (m_targetLine != null) {
- stopLine(false);
- closeLine();
- m_targetLine = null;
- }
- }
- public void start() throws IOException {
- if (m_targetLine == null)
- connect();
- if (!m_targetLine.isOpen()) {
- try {
- openLine(m_jsFormat);
- }
- catch (LineUnavailableException lux) {
- throw new IOException("Unable to open line");
- }
- }
- if (!m_targetLine.isRunning()) {
- if (!startLine())
- throw new IOException("Unable to start line");
- if (m_transferThread == null) {
- m_transferThread = new Thread(this);
- m_transferThread.start();
- }
- byte[] abData = new byte[(m_jsFormat.getSampleSizeInBits() + 7) / 8];
- m_targetLine.read(abData, 0, abData.length);
- //System.out.println("Started");
- }
- }
- public void run() {
- while (m_targetLine != null) {
- try {
- if (m_targetLine.isRunning()) {
- int nAvailable = m_targetLine.available();
- if (nAvailable >= m_nTransferBufferSize) {
- if (m_bufferTransferHandler != null) {
- m_bufferTransferHandler.transferData(m_micStream);
- }
- }
- else {
- Thread.sleep(5);
- }
- }
- }
- catch (InterruptedException ix) {
- ix.printStackTrace();
- break;
- }
- }
- m_transferThread = null;
- }
- public synchronized void stop() throws IOException {
- if ((m_targetLine != null) && (m_targetLine.isRunning())) {
- stopLine(false);
- //System.out.println("Stopped");
- }
- }
- public Object [] getControls() {
- return new Object[0];
- }
- // DO NOT just return null. The JVM will freeze
- public Object getControl(String controlType) {
- try {
- Class cls = Class.forName(controlType);
- Object cs[] = getControls();
- for (int i = 0; i < cs.length; i++) {
- if (cls.isInstance(cs[i]))
- return cs[i];
- }
- return null;
- } catch (Exception e) { // no such controlType or such control
- return null;
- }
- }
- public Time getDuration() {
- return DURATION_UNKNOWN;
- }
- private class MicStream implements PushBufferStream
- {
- public Object [] getControls() {
- return new Object[0];
- }
- // DO NOT just return null. The JVM will freeze
- public Object getControl(String controlType) {
- try {
- Class cls = Class.forName(controlType);
- Object cs[] = getControls();
- for (int i = 0; i < cs.length; i++) {
- if (cls.isInstance(cs[i]))
- return cs[i];
- }
- return null;
- } catch (Exception e) { // no such controlType or such control
- return null;
- }
- }
- public Format getFormat() {
- return m_jmfFormat;
- }
- public void read(Buffer buffer) throws IOException {
- if ((m_targetLine == null) || ((m_targetLine != null) && (m_targetLine.available() == 0))) {
- buffer.setLength(0);
- buffer.setOffset(0);
- buffer.setDiscard(true);
- return;
- }
- byte[] abData = (byte[]) buffer.getData();
- if ((abData == null) || (abData.length != m_nTransferBufferSize)) {
- abData = new byte[m_nTransferBufferSize];
- buffer.setData(abData);
- }
- int nBytesRead = m_targetLine.read(abData, 0, abData.length);
- if (nBytesRead == -1) {
- buffer.setEOM(true);
- buffer.setOffset(0);
- buffer.setLength(0);
- buffer.setDiscard(true);
- return;
- }
- javax.media.format.AudioFormat format = (javax.media.format.AudioFormat) getFormat();
- m_lBytesRead += nBytesRead;
- buffer.setFormat(getFormat());
- buffer.setOffset(0);
- buffer.setLength(nBytesRead);
- buffer.setTimeStamp(AudioUtil.bytesToNanos(m_lBytesRead, format));
- buffer.setDuration(AudioUtil.bytesToNanos(nBytesRead, format));
- }
- public synchronized void setTransferHandler(BufferTransferHandler bufferTransferHandler) {
- if (bufferTransferHandler == null) {
- m_bufferTransferHandler = null;
- return;
- }
- m_bufferTransferHandler = bufferTransferHandler;
- }
- public ContentDescriptor getContentDescriptor() {
- return m_contentDescriptor;
- }
- public long getContentLength() {
- return LENGTH_UNKNOWN;
- }
- public boolean endOfStream() {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement