Esempio n. 1
0
  public int write(byte[] data, int ofs, int len) {

    int maxTries = 10;
    int totWrite = 0;
    int nofs = ofs, nlen = len;
    while (nofs < ofs + len && maxTries > 0) {

      int written = 0;
      if (format.getBits() == 16 && format.getChannels() == 2 && format.getRate() == INPUT_RATE)
        written = sdl.write(data, nofs, nlen);
      else written = writeConv(data, nofs, nlen);

      nofs += written;
      nlen -= written;
      totWrite += written;
      maxTries--;
      deliveredData += written;

      // calculations when channels and bits and rate varies...
      // int divisor = (format.getBits() / 8) * format.getChannels();
      // int deliveredTime =
      // (int) (1000 * deliveredData / (format.getRate() * divisor));

      int deliveredTime = (int) (1000 * deliveredData / (INPUT_RATE * 4));
      long pos = sdl.getMicrosecondPosition() / 1000;
      long sleepTime = deliveredTime - pos - bufferTime;
      if (sleepTime < 0) sleepTime = 0;

      try {
        Thread.sleep(sleepTime);
      } catch (InterruptedException e) {
      }
    }
    return totWrite;
  }
Esempio n. 2
0
 protected int writeConv(byte[] data, int ofs, int len) {
   double grad = (double) INPUT_RATE / format.getRate();
   int length = 0;
   if (format.getBits() == 8 && format.getChannels() == 1) length = mono8(data, ofs, len, grad);
   else if (format.getBits() == 8 && format.getChannels() == 2)
     length = stereo8(data, ofs, len, grad);
   else if (format.getBits() == 16 && format.getChannels() == 1)
     length = mono16(data, ofs, len, grad);
   else if (format.getBits() == 16 && format.getChannels() == 2)
     length = stereo16(data, ofs, len, grad);
   return length;
 }
Esempio n. 3
0
  public boolean supports(SoundDataFormat format) {
    boolean rateSupported = false;
    int rate = format.getRate();
    for (int n = 0; n < supportedRates.length; n++)
      if (supportedRates[n] == rate) rateSupported = true;

    boolean bitsSupported = false;
    int bits = format.getBits();
    for (int n = 0; n < supportedBits.length; n++)
      if (supportedBits[n] == bits) bitsSupported = true;

    boolean channelsSupported = false;
    int channels = format.getChannels();
    for (int n = 0; n < supportedChannels.length; n++)
      if (supportedChannels[n] == channels) channelsSupported = true;

    return rateSupported && bitsSupported && channelsSupported;
  }
Esempio n. 4
0
  private void init() throws PlayerException {

    if (!supports(format)) throw new PlayerException("data format not supported: " + format);

    af =
        new AudioFormat(
            format.getRate(), format.getBits(), format.getChannels(), signed, bigendian);
    DataLine.Info dli = new DataLine.Info(SourceDataLine.class, af);
    if (AudioSystem.isLineSupported(dli)) {
      Line l = null;
      try {
        l = AudioSystem.getLine(dli);
      } catch (LineUnavailableException e) {
        throw new PlayerException("Can't get line", e);
      }
      if (!(l instanceof SourceDataLine)) throw new PlayerException("line not a SourceDataLine!");
      sdl = (SourceDataLine) l;
    } else throw new PlayerException("Line not supported");
    if (sdl == null) throw new PlayerException("line not found");
  }