home *** CD-ROM | disk | FTP | other *** search
/ Print Shop Ensemble 3 / the-print-shop-ensemble-iii.iso / worldnet / disk2 / java.z / MOZ2_01.ZIP / sun / audio / AudioDevice.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-03-08  |  12.1 KB  |  257 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.          System.loadLibrary("mm3220");
  32.       } catch (UnsatisfiedLinkError var2) {
  33.          System.out.println("could not find/load the mmedia library");
  34.       }
  35.  
  36.       this.streams = new Vector();
  37.       int bufferSize = 400;
  38.       this.ulaw = new byte[bufferSize];
  39.       this.linear = new int[bufferSize];
  40.    }
  41.  
  42.    public synchronized void openChannel(InputStream in) {
  43.       this.streams.insertElementAt(in, 0);
  44.    }
  45.  
  46.    public synchronized void closeChannel(InputStream in) {
  47.       if (this.streams.removeElement(in)) {
  48.          try {
  49.             in.close();
  50.          } catch (IOException var2) {
  51.          }
  52.       }
  53.    }
  54.  
  55.    public synchronized void open() {
  56.       int ntries = 1;
  57.       int maxtries = 5;
  58.  
  59.       while(this.dev == 0) {
  60.          this.dev = this.audioOpen();
  61.          if (this.dev < 0) {
  62.             System.out.println("no audio device");
  63.             return;
  64.          }
  65.  
  66.          if (this.dev == 0) {
  67.             System.out.println("audio device busy (attempt " + ntries + " out of " + maxtries + ")");
  68.             if (this.streams.size() != 0) {
  69.                ++ntries;
  70.                if (ntries <= maxtries) {
  71.                   try {
  72.                      this.wait(3000L);
  73.                      continue;
  74.                   } catch (InterruptedException var3) {
  75.                      this.closeStreams();
  76.                      return;
  77.                   }
  78.                }
  79.             }
  80.  
  81.             this.closeStreams();
  82.             return;
  83.          }
  84.       }
  85.  
  86.    }
  87.  
  88.    public synchronized void close() {
  89.       if (this.dev != 0) {
  90.          this.audioClose();
  91.          this.dev = 0;
  92.       }
  93.  
  94.       this.closeStreams();
  95.    }
  96.  
  97.    private synchronized void mix() {
  98.       int len = this.ulaw.length;
  99.       byte[] ubuf = this.ulaw;
  100.       switch (this.streams.size()) {
  101.          case 0:
  102.             for(int n = len; n-- > 0; ubuf[n] = 127) {
  103.             }
  104.  
  105.             return;
  106.          case 1:
  107.             InputStream in = (InputStream)this.streams.elementAt(0);
  108.             int n = 0;
  109.  
  110.             try {
  111.                n = in.read(ubuf, 0, len);
  112.             } catch (IOException var14) {
  113.                n = -1;
  114.             }
  115.  
  116.             if (n <= 0) {
  117.                this.streams.removeElementAt(0);
  118.                n = 0;
  119.  
  120.                try {
  121.                   in.close();
  122.                } catch (IOException var13) {
  123.                }
  124.             }
  125.  
  126.             while(n < len) {
  127.                ubuf[n] = 127;
  128.                ++n;
  129.             }
  130.  
  131.             return;
  132.          default:
  133.             int[] tab = ULAW_TAB;
  134.             int[] lbuf = this.linear;
  135.             int i = this.streams.size() - 1;
  136.             InputStream in = (InputStream)this.streams.elementAt(i);
  137.             int n = 0;
  138.  
  139.             try {
  140.                n = in.read(ubuf, 0, len);
  141.             } catch (IOException var18) {
  142.                n = -1;
  143.             }
  144.  
  145.             if (n <= 0) {
  146.                this.streams.removeElementAt(0);
  147.                n = 0;
  148.  
  149.                try {
  150.                   in.close();
  151.                } catch (IOException var17) {
  152.                }
  153.             }
  154.  
  155.             for(int j = 0; j < n; ++j) {
  156.                lbuf[j] = tab[ubuf[j] & 255];
  157.             }
  158.  
  159.             while(n < len) {
  160.                lbuf[n] = 0;
  161.                ++n;
  162.             }
  163.  
  164.             while(i-- > 0) {
  165.                in = (InputStream)this.streams.elementAt(i);
  166.  
  167.                try {
  168.                   n = in.read(ubuf, 0, len);
  169.                } catch (IOException var16) {
  170.                   n = -1;
  171.                }
  172.  
  173.                if (n <= 0) {
  174.                   this.streams.removeElementAt(0);
  175.                   n = 0;
  176.  
  177.                   try {
  178.                      in.close();
  179.                   } catch (IOException var15) {
  180.                   }
  181.                }
  182.  
  183.                while(n-- > 0) {
  184.                   lbuf[n] += tab[ubuf[n] & 255];
  185.                }
  186.             }
  187.  
  188.             int[] lut = ULAW_LUT;
  189.  
  190.             int sample;
  191.             for(int var26 = len; var26-- > 0; ubuf[var26] = (byte)sample) {
  192.                sample = lbuf[var26];
  193.                if (sample >= 0) {
  194.                   if (sample > 32635) {
  195.                      sample = 32635;
  196.                   }
  197.  
  198.                   sample += 132;
  199.                   int exponent = lut[sample >> 7];
  200.                   int mantissa = sample >> exponent + 3 & 15;
  201.                   sample = (exponent << 4 | mantissa) ^ 255;
  202.                } else {
  203.                   sample = -sample;
  204.                   if (sample > 32635) {
  205.                      sample = 32635;
  206.                   }
  207.  
  208.                   sample += 132;
  209.                   int exponent = lut[sample >> 7];
  210.                   int mantissa = sample >> exponent + 3 & 15;
  211.                   sample = (exponent << 4 | mantissa) ^ 127;
  212.                }
  213.             }
  214.  
  215.       }
  216.    }
  217.  
  218.    public void play() {
  219.       long tm = System.currentTimeMillis() - 16L;
  220.  
  221.       while(this.dev >= 0) {
  222.          this.audioWrite(this.ulaw, this.ulaw.length);
  223.          this.mix();
  224.          tm += 50L;
  225.          long delay = tm - System.currentTimeMillis();
  226.          if (delay > 0L) {
  227.             try {
  228.                Thread.currentThread();
  229.                Thread.sleep(delay);
  230.             } catch (InterruptedException var5) {
  231.                return;
  232.             }
  233.          } else {
  234.             tm = System.currentTimeMillis() - 16L;
  235.          }
  236.       }
  237.  
  238.    }
  239.  
  240.    public synchronized void closeStreams() {
  241.       Enumeration e = this.streams.elements();
  242.  
  243.       while(e.hasMoreElements()) {
  244.          try {
  245.             ((InputStream)e.nextElement()).close();
  246.          } catch (IOException var2) {
  247.          }
  248.       }
  249.  
  250.       this.streams = new Vector();
  251.    }
  252.  
  253.    public int openChannels() {
  254.       return this.streams.size();
  255.    }
  256. }
  257.