Example #1
0
  private void loadSampleData(BufferedInputStream in, Sample sample) throws IOException {
    int sampleLength = sample.getLength();
    int loopEnd = sample.getLoopEnd();
    int loopStart = sample.getLoopStart();
    short[] sampleData;
    if (sample.getQuality() == 16) {
      sampleLength >>= 1;
      loopEnd >>= 1;
      loopStart >>= 1;

      byte[] temp = read(in, 2 * sampleLength);
      sampleData = new short[sampleLength + 4];

      int tmpPos = 0;

      int samp = 0;

      for (int i = 0; i < sampleLength; i++, tmpPos += 2) {
        samp += make16Bit(temp, tmpPos);
        sampleData[i] = (short) (samp);
      }
    } else {
      sampleData = new short[sampleLength + 4];
      byte[] temp = read(in, sampleLength);
      int samp = 0;

      for (int i = 0; i < sampleLength; i++) {
        samp += temp[i] & 0xff;
        sampleData[i] = (short) (samp << 8);
      }
    }

    if (sampleLength > 0) {
      int pos2 = 0;
      int loopType = sample.getLoopType();
      if ((loopType & Sample.PINGPONG_LOOP) == 0) {
        if ((loopType & Sample.FORWARD_LOOP) != 0) pos2 = loopStart;

        for (int i = 0; i < 3; i++) sampleData[sampleLength + 1 + i] = sampleData[pos2 + i];

      } else if ((loopType & Sample.PINGPONG_LOOP) != 0) {
        pos2 = loopStart + loopEnd;

        for (int i = 0; i < 3; i++) sampleData[sampleLength + 1 + i] = sampleData[pos2 - 1 - i];
      }
      System.arraycopy(sampleData, 0, sampleData, 1, sampleLength);

      if ((loopType & Sample.FORWARD_LOOP) != 0) sampleData[0] = sampleData[loopStart + loopEnd];
      else if ((loopType & Sample.PINGPONG_LOOP) != 0) sampleData[0] = sampleData[loopStart + 2];
    }

    loopStart <<= 10;
    loopEnd <<= 10;

    sample.setLoopStart(loopStart);
    sample.setLoopEnd(loopEnd);
    sample.setLength(sampleLength);
    sample.setData(sampleData);
  }