Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 10.13 KB | None | 0 0
  1.  
  2. /*
  3. * Author: Xiphias3
  4. * Date:   Oct 2, 2010
  5. * Time:   11:50:43 AM
  6. */
  7.  
  8. package xiphias3.client;
  9.  
  10. import xiphias3.util.AudioUtil;
  11.  
  12. import javax.sound.sampled.*;
  13. import javax.sound.sampled.AudioFormat;
  14. import javax.media.protocol.*;
  15. import javax.media.Time;
  16. import javax.media.Format;
  17. import javax.media.Buffer;
  18. import java.util.Vector;
  19. import java.io.IOException;
  20.  
  21. public class Microphone extends PushBufferDataSource implements Runnable
  22. {
  23.     private TargetDataLine        m_targetLine;
  24.     private DataLine.Info         m_lineInfo;
  25.     private Thread                m_transferThread;
  26.     private int                   m_nTransferBufferSize;
  27.     private PushBufferStream[]    m_aStreams;
  28.     private long                  m_lBytesRead;
  29.     private BufferTransferHandler m_bufferTransferHandler;
  30.     private ContentDescriptor     m_contentDescriptor;
  31.     private AudioFormat           m_jsFormat;
  32.     private MicStream             m_micStream;
  33.  
  34.     private javax.media.format.AudioFormat m_jmfFormat;
  35.  
  36.     public static final int ENDIAN = javax.media.format.AudioFormat.LITTLE_ENDIAN;
  37.     public static final int SIGNED = javax.media.format.AudioFormat.SIGNED;
  38.  
  39.     public Microphone() {
  40.         m_micStream = new MicStream();
  41.         m_aStreams = new PushBufferStream[]{ m_micStream };
  42.         m_contentDescriptor = new ContentDescriptor(ContentDescriptor.RAW);
  43.     }
  44.  
  45.     protected boolean createLine() {
  46.         if ((m_targetLine == null) && (m_lineInfo == null)) {
  47.             try {
  48.                 m_targetLine = AudioSystem.getTargetDataLine(null);
  49.                 m_lineInfo = (DataLine.Info) m_targetLine.getLineInfo();
  50.                 return true;
  51.             }
  52.             catch (LineUnavailableException lux) {
  53.                 m_targetLine = null;
  54.                 m_lineInfo  = null;
  55.             }
  56.         }
  57.         return false;
  58.     }
  59.  
  60.     public void setAudioFormat(AudioFormat format) {
  61.         m_jsFormat = format;
  62.         m_jmfFormat = new javax.media.format.AudioFormat(javax.media.format.AudioFormat.LINEAR, format.getSampleRate(), format.getSampleSizeInBits(), format.getChannels(), ENDIAN, SIGNED);
  63.     }
  64.  
  65.     public AudioFormat[] getAvailableFormats() {
  66.         if (m_lineInfo != null) {
  67.             AudioFormat[] aFormats = {
  68.                 new AudioFormat(48000.0f, 16, 1, true, false),
  69.                 new AudioFormat(44100.0f, 16, 1, true, false),
  70.                 new AudioFormat(22050.0f, 16, 1, true, false),
  71.                 new AudioFormat(16050.0f, 16, 1, true, false),
  72.                 new AudioFormat(11025.0f, 16, 1, true, false),
  73.                 new AudioFormat(8000.0f, 16, 1, true, false)
  74.             };
  75.  
  76.             Vector<AudioFormat> supportedFormatsVector = new Vector<AudioFormat>();
  77.             for (AudioFormat format : aFormats) {
  78.                 if (m_lineInfo.isFormatSupported(format)) {
  79.                     supportedFormatsVector.add(format);
  80.                 }
  81.             }
  82.  
  83.             if (supportedFormatsVector.size() > 0)
  84.                 return supportedFormatsVector.toArray(new AudioFormat[0]);
  85.         }
  86.         return null;
  87.     }
  88.  
  89.     private boolean openLine(AudioFormat format) throws LineUnavailableException {
  90.         if ((m_targetLine != null) && (!m_targetLine.isOpen())) {
  91.             m_targetLine.open(format);
  92.             return true;
  93.         }
  94.         return false;
  95.     }
  96.  
  97.     private boolean closeLine() {
  98.         if ((m_targetLine != null) && (m_targetLine.isOpen())) {
  99.             m_targetLine.close();
  100.             return true;
  101.         }
  102.         return false;
  103.     }
  104.  
  105.     private boolean startLine() {
  106.         if ((m_targetLine != null) && (!m_targetLine.isRunning())) {
  107.             m_targetLine.flush();
  108.             m_targetLine.start();
  109.             m_nTransferBufferSize = (int) AudioUtil.millis2Bytes(20, m_targetLine.getFormat());
  110.             //System.out.println("Transfer Buffer size set to " + m_nTransferBufferSize);
  111.             return true;
  112.         }
  113.         return false;
  114.     }
  115.  
  116.     private boolean stopLine(boolean bDrain) {
  117.         if ((m_targetLine != null) && (m_targetLine.isRunning())) {
  118.             if (bDrain)
  119.                 m_targetLine.drain();
  120.             m_targetLine.flush();
  121.             m_targetLine.stop();
  122.             return true;
  123.         }
  124.         return false;
  125.     }
  126.  
  127.     public TargetDataLine getLine() {
  128.         return m_targetLine;
  129.     }
  130.  
  131.     public DataLine.Info getLineInfo() {
  132.         return m_lineInfo;
  133.     }
  134.  
  135.  
  136.  
  137.  
  138.     public PushBufferStream[] getStreams() {
  139.         return m_aStreams;
  140.     }
  141.  
  142.     public String getContentType() {
  143.         return ContentDescriptor.RAW;
  144.     }
  145.  
  146.     public synchronized void connect() throws IOException {
  147.         if (m_targetLine == null) {
  148.             if (!createLine())
  149.                 throw new IOException("Could not create line");
  150.         }
  151.     }
  152.  
  153.     public synchronized void disconnect() {
  154.         if (m_targetLine != null) {
  155.             stopLine(false);
  156.             closeLine();
  157.             m_targetLine = null;
  158.         }
  159.     }
  160.  
  161.     public void start() throws IOException {
  162.         if (m_targetLine == null)
  163.             connect();
  164.  
  165.         if (!m_targetLine.isOpen()) {
  166.             try {
  167.                 openLine(m_jsFormat);
  168.             }
  169.             catch (LineUnavailableException lux) {
  170.                 throw new IOException("Unable to open line");
  171.             }
  172.         }
  173.  
  174.         if (!m_targetLine.isRunning()) {
  175.             if (!startLine())
  176.                 throw new IOException("Unable to start line");
  177.             if (m_transferThread == null) {
  178.                 m_transferThread = new Thread(this);
  179.                 m_transferThread.start();
  180.             }
  181.  
  182.             byte[] abData = new byte[(m_jsFormat.getSampleSizeInBits() + 7) / 8];
  183.             m_targetLine.read(abData, 0, abData.length);
  184.             //System.out.println("Started");
  185.         }
  186.     }
  187.  
  188.     public void run() {
  189.         while (m_targetLine != null) {
  190.             try {
  191.                 if (m_targetLine.isRunning()) {
  192.                     int nAvailable = m_targetLine.available();
  193.                     if (nAvailable >= m_nTransferBufferSize) {
  194.                         if (m_bufferTransferHandler != null) {
  195.                             m_bufferTransferHandler.transferData(m_micStream);
  196.                         }
  197.                     }
  198.                     else {
  199.                         Thread.sleep(5);
  200.                     }
  201.                 }
  202.             }
  203.             catch (InterruptedException ix) {
  204.                 ix.printStackTrace();
  205.                 break;
  206.             }
  207.         }
  208.         m_transferThread = null;
  209.     }
  210.  
  211.     public synchronized void stop() throws IOException {
  212.         if ((m_targetLine != null) && (m_targetLine.isRunning())) {
  213.             stopLine(false);
  214.             //System.out.println("Stopped");
  215.         }
  216.     }
  217.  
  218.     public Object [] getControls() {
  219.         return new Object[0];
  220.     }
  221.  
  222.     // DO NOT just return null. The JVM will freeze
  223.     public Object getControl(String controlType) {
  224.         try {
  225.             Class  cls = Class.forName(controlType);
  226.             Object cs[] = getControls();
  227.             for (int i = 0; i < cs.length; i++) {
  228.                 if (cls.isInstance(cs[i]))
  229.                     return cs[i];
  230.             }
  231.             return null;
  232.  
  233.         } catch (Exception e) {   // no such controlType or such control
  234.             return null;
  235.         }
  236.     }
  237.  
  238.     public Time getDuration() {
  239.         return DURATION_UNKNOWN;
  240.     }
  241.  
  242.     private class MicStream implements PushBufferStream
  243.     {
  244.         public Object [] getControls() {
  245.             return new Object[0];
  246.         }
  247.  
  248.         // DO NOT just return null. The JVM will freeze
  249.         public Object getControl(String controlType) {
  250.             try {
  251.                 Class  cls = Class.forName(controlType);
  252.                 Object cs[] = getControls();
  253.                 for (int i = 0; i < cs.length; i++) {
  254.                     if (cls.isInstance(cs[i]))
  255.                         return cs[i];
  256.                 }
  257.                 return null;
  258.  
  259.             } catch (Exception e) {   // no such controlType or such control
  260.                 return null;
  261.             }
  262.         }
  263.  
  264.         public Format getFormat() {
  265.             return m_jmfFormat;
  266.         }
  267.  
  268.         public void read(Buffer buffer) throws IOException {
  269.             if ((m_targetLine == null) || ((m_targetLine != null) && (m_targetLine.available() == 0))) {
  270.                 buffer.setLength(0);
  271.                 buffer.setOffset(0);
  272.                 buffer.setDiscard(true);
  273.                 return;
  274.             }
  275.  
  276.             byte[] abData = (byte[]) buffer.getData();
  277.             if ((abData == null) || (abData.length != m_nTransferBufferSize)) {
  278.                 abData = new byte[m_nTransferBufferSize];
  279.                 buffer.setData(abData);
  280.             }
  281.  
  282.             int nBytesRead = m_targetLine.read(abData, 0, abData.length);
  283.             if (nBytesRead == -1) {
  284.                 buffer.setEOM(true);
  285.                 buffer.setOffset(0);
  286.                 buffer.setLength(0);
  287.                 buffer.setDiscard(true);
  288.                 return;
  289.             }
  290.  
  291.             javax.media.format.AudioFormat format = (javax.media.format.AudioFormat) getFormat();
  292.             m_lBytesRead += nBytesRead;
  293.             buffer.setFormat(getFormat());
  294.             buffer.setOffset(0);
  295.             buffer.setLength(nBytesRead);
  296.             buffer.setTimeStamp(AudioUtil.bytesToNanos(m_lBytesRead, format));
  297.             buffer.setDuration(AudioUtil.bytesToNanos(nBytesRead, format));
  298.         }
  299.  
  300.         public synchronized void setTransferHandler(BufferTransferHandler bufferTransferHandler) {
  301.             if (bufferTransferHandler == null) {
  302.                 m_bufferTransferHandler = null;
  303.                 return;
  304.             }
  305.             m_bufferTransferHandler = bufferTransferHandler;
  306.         }
  307.  
  308.         public ContentDescriptor getContentDescriptor() {
  309.             return m_contentDescriptor;
  310.         }
  311.  
  312.         public long getContentLength() {
  313.             return LENGTH_UNKNOWN;
  314.         }
  315.  
  316.         public boolean endOfStream() {
  317.             return false;
  318.         }
  319.     }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement