home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / sun / audio / AudioDevice.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  12.4 KB  |  274 lines

  1. package sun.audio;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.Enumeration;
  6. import java.util.Vector;
  7.  
  8. public class AudioDevice {
  9.    private Vector streams;
  10.    private byte[] ulaw;
  11.    private int[] linear;
  12.    private int dev;
  13.    private static final int MSCLICK = 50;
  14.    private static final int MSMARGIN = 16;
  15.    private static final int BYTES_PER_SAMPLE = 1;
  16.    private static final int SAMPLE_RATE = 8000;
  17.    private static final int ULAW_BIAS = 132;
  18.    private static final int ULAW_CLIP = 32635;
  19.    private static final int[] ULAW_TAB = new int[]{-32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764, -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412, -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140, -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004, -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, -876, -844, -812, -780, -748, -716, -684, -652, -620, -588, -556, -524, -492, -460, -428, -396, -372, -356, -340, -324, -308, -292, -276, -260, -244, -228, -212, -196, -180, -164, -148, -132, -120, -112, -104, -96, -88, -80, -72, -64, -56, -48, -40, -32, -24, -16, -8, 0, 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956, 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412, 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140, 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004, 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436, 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, 876, 844, 812, 780, 748, 716, 684, 652, 620, 588, 556, 524, 492, 460, 428, 396, 372, 356, 340, 324, 308, 292, 276, 260, 244, 228, 212, 196, 180, 164, 148, 132, 120, 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0};
  20.    private static final int[] ULAW_LUT = new int[]{0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7};
  21.    public static final AudioDevice device = new AudioDevice();
  22.  
  23.    private native int audioOpen();
  24.  
  25.    private native void audioClose();
  26.  
  27.    private synchronized native void audioWrite(byte[] var1, int var2);
  28.  
  29.    private AudioDevice() {
  30.       try {
  31.          SecurityManager.setScopePermission();
  32.          System.loadLibrary("mm3230");
  33.          SecurityManager.resetScopePermission();
  34.       } catch (UnsatisfiedLinkError var2) {
  35.          System.out.println("could not find/load the mmedia library");
  36.       }
  37.  
  38.       this.streams = new Vector();
  39.       int bufferSize = 400;
  40.       this.ulaw = new byte[bufferSize];
  41.       this.linear = new int[bufferSize];
  42.    }
  43.  
  44.    public synchronized void openChannel(InputStream in) {
  45.       this.streams.insertElementAt(in, 0);
  46.       this.notify();
  47.    }
  48.  
  49.    public synchronized void closeChannel(InputStream in) {
  50.       if (this.streams.removeElement(in)) {
  51.          try {
  52.             in.close();
  53.          } catch (IOException var2) {
  54.          }
  55.       }
  56.    }
  57.  
  58.    public synchronized void open() {
  59.       int ntries = 1;
  60.       int maxtries = 5;
  61.  
  62.       while(this.dev == 0) {
  63.          this.dev = this.audioOpen();
  64.          if (this.dev < 0) {
  65.             System.out.println("no audio device");
  66.             return;
  67.          }
  68.  
  69.          if (this.dev == 0) {
  70.             System.out.println("audio device busy (attempt " + ntries + " out of " + maxtries + ")");
  71.             if (this.streams.size() != 0) {
  72.                ++ntries;
  73.                if (ntries <= maxtries) {
  74.                   try {
  75.                      this.wait(3000L);
  76.                      continue;
  77.                   } catch (InterruptedException var3) {
  78.                      this.closeStreams();
  79.                      return;
  80.                   }
  81.                }
  82.             }
  83.  
  84.             this.closeStreams();
  85.             return;
  86.          }
  87.       }
  88.  
  89.    }
  90.  
  91.    public synchronized void close() {
  92.       if (this.dev != 0) {
  93.          this.audioClose();
  94.          this.dev = 0;
  95.       }
  96.  
  97.       this.closeStreams();
  98.    }
  99.  
  100.    private synchronized void mix() {
  101.       int len = this.ulaw.length;
  102.       byte[] ubuf = this.ulaw;
  103.       switch (this.streams.size()) {
  104.          case 0:
  105.             for(int n = len; n-- > 0; ubuf[n] = 127) {
  106.             }
  107.  
  108.             return;
  109.          case 1:
  110.             InputStream in = (InputStream)this.streams.elementAt(0);
  111.             int n = 0;
  112.  
  113.             try {
  114.                n = in.read(ubuf, 0, len);
  115.             } catch (IOException var14) {
  116.                n = -1;
  117.             }
  118.  
  119.             if (n <= 0) {
  120.                this.streams.removeElementAt(0);
  121.                n = 0;
  122.  
  123.                try {
  124.                   in.close();
  125.                } catch (IOException var13) {
  126.                }
  127.             }
  128.  
  129.             while(n < len) {
  130.                ubuf[n] = 127;
  131.                ++n;
  132.             }
  133.  
  134.             return;
  135.          default:
  136.             int[] tab = ULAW_TAB;
  137.             int[] lbuf = this.linear;
  138.             int i = this.streams.size() - 1;
  139.             InputStream in = (InputStream)this.streams.elementAt(i);
  140.             int n = 0;
  141.  
  142.             try {
  143.                n = in.read(ubuf, 0, len);
  144.             } catch (IOException var18) {
  145.                n = -1;
  146.             }
  147.  
  148.             if (n <= 0) {
  149.                this.streams.removeElementAt(i);
  150.                n = 0;
  151.  
  152.                try {
  153.                   in.close();
  154.                } catch (IOException var17) {
  155.                }
  156.             }
  157.  
  158.             for(int j = 0; j < n; ++j) {
  159.                lbuf[j] = tab[ubuf[j] & 255];
  160.             }
  161.  
  162.             while(n < len) {
  163.                lbuf[n] = 0;
  164.                ++n;
  165.             }
  166.  
  167.             while(i-- > 0) {
  168.                in = (InputStream)this.streams.elementAt(i);
  169.  
  170.                try {
  171.                   n = in.read(ubuf, 0, len);
  172.                } catch (IOException var16) {
  173.                   n = -1;
  174.                }
  175.  
  176.                if (n <= 0) {
  177.                   this.streams.removeElementAt(i);
  178.                   n = 0;
  179.  
  180.                   try {
  181.                      in.close();
  182.                   } catch (IOException var15) {
  183.                   }
  184.                }
  185.  
  186.                while(n-- > 0) {
  187.                   lbuf[n] += tab[ubuf[n] & 255];
  188.                }
  189.             }
  190.  
  191.             int[] lut = ULAW_LUT;
  192.  
  193.             int sample;
  194.             for(int var26 = len; var26-- > 0; ubuf[var26] = (byte)sample) {
  195.                sample = lbuf[var26];
  196.                if (sample >= 0) {
  197.                   if (sample > 32635) {
  198.                      sample = 32635;
  199.                   }
  200.  
  201.                   sample += 132;
  202.                   int exponent = lut[sample >> 7];
  203.                   int mantissa = sample >> exponent + 3 & 15;
  204.                   sample = (exponent << 4 | mantissa) ^ 255;
  205.                } else {
  206.                   sample = -sample;
  207.                   if (sample > 32635) {
  208.                      sample = 32635;
  209.                   }
  210.  
  211.                   sample += 132;
  212.                   int exponent = lut[sample >> 7];
  213.                   int mantissa = sample >> exponent + 3 & 15;
  214.                   sample = (exponent << 4 | mantissa) ^ 127;
  215.                }
  216.             }
  217.  
  218.       }
  219.    }
  220.  
  221.    private synchronized boolean waitForData() throws InterruptedException {
  222.       if (this.streams.size() == 0) {
  223.          this.close();
  224.          this.wait();
  225.          this.open();
  226.          return true;
  227.       } else {
  228.          return false;
  229.       }
  230.    }
  231.  
  232.    public void play() {
  233.       try {
  234.          long tm = System.currentTimeMillis() - 16L;
  235.  
  236.          while(this.dev > 0) {
  237.             if (this.waitForData()) {
  238.                tm = System.currentTimeMillis() - 16L;
  239.             }
  240.  
  241.             this.mix();
  242.             this.audioWrite(this.ulaw, this.ulaw.length);
  243.             tm += 50L;
  244.             long delay = tm - System.currentTimeMillis();
  245.             if (delay > 0L) {
  246.                Thread.currentThread();
  247.                Thread.sleep(delay);
  248.             } else {
  249.                tm = System.currentTimeMillis() - 16L;
  250.             }
  251.          }
  252.  
  253.       } catch (InterruptedException var5) {
  254.       }
  255.    }
  256.  
  257.    public synchronized void closeStreams() {
  258.       Enumeration e = this.streams.elements();
  259.  
  260.       while(e.hasMoreElements()) {
  261.          try {
  262.             ((InputStream)e.nextElement()).close();
  263.          } catch (IOException var2) {
  264.          }
  265.       }
  266.  
  267.       this.streams = new Vector();
  268.    }
  269.  
  270.    public int openChannels() {
  271.       return this.streams.size();
  272.    }
  273. }
  274.