예제 #1
0
파일: Prepare.java 프로젝트: jtrfp/javamod
  public void prepare(
      ByteArrayReader pRawData,
      int nBytes,
      final WaveFormat pWaveFormatEx,
      int[] pOutputX,
      int[] pOutputY,
      Crc32 pCRC,
      IntegerPointer pSpecialCodes,
      IntegerPointer pPeakLevel) {
    // initialize the pointers that got passed in
    pCRC.init();
    pSpecialCodes.value = 0;

    // variables
    int nTotalBlocks = nBytes / pWaveFormatEx.nBlockAlign;
    int R, L;

    // the prepare code

    if (pWaveFormatEx.wBitsPerSample == 8) {
      if (pWaveFormatEx.nChannels == 2) {
        for (int nBlockIndex = 0; nBlockIndex < nTotalBlocks; nBlockIndex++) {
          short b1 = pRawData.readUnsignedByte();
          short b2 = pRawData.readUnsignedByte();
          R = b1 - 128;
          L = b2 - 128;

          pCRC.append((byte) b1);
          pCRC.append((byte) b2);

          // check the peak
          if (Math.abs(L) > pPeakLevel.value) pPeakLevel.value = Math.abs(L);
          if (Math.abs(R) > pPeakLevel.value) pPeakLevel.value = Math.abs(R);

          // convert to x,y
          pOutputY[nBlockIndex] = L - R;
          pOutputX[nBlockIndex] = R + (pOutputY[nBlockIndex] / 2);
        }
      } else if (pWaveFormatEx.nChannels == 1) {
        for (int nBlockIndex = 0; nBlockIndex < nTotalBlocks; nBlockIndex++) {
          short b1 = pRawData.readUnsignedByte();
          R = b1 - 128;

          pCRC.append((byte) b1);

          // check the peak
          if (Math.abs(R) > pPeakLevel.value) pPeakLevel.value = Math.abs(R);

          // convert to x,y
          pOutputX[nBlockIndex] = R;
        }
      }
    } else if (pWaveFormatEx.wBitsPerSample == 24) {
      if (pWaveFormatEx.nChannels == 2) {
        for (int nBlockIndex = 0; nBlockIndex < nTotalBlocks; nBlockIndex++) {
          long nTemp = 0;

          short b = pRawData.readUnsignedByte();
          nTemp |= (b << 0);
          pCRC.append((byte) b);

          b = pRawData.readUnsignedByte();
          nTemp |= (b << 8);
          pCRC.append((byte) b);

          b = pRawData.readUnsignedByte();
          nTemp |= (b << 16);
          pCRC.append((byte) b);

          if ((nTemp & 0x800000) != 0) R = (int) (nTemp & 0x7fffff) - 0x800000;
          else R = (int) (nTemp & 0x7fffff);

          nTemp = 0;

          b = pRawData.readUnsignedByte();
          nTemp |= (b << 0);
          pCRC.append((byte) b);

          b = pRawData.readUnsignedByte();
          nTemp |= (b << 8);
          pCRC.append((byte) b);

          b = pRawData.readUnsignedByte();
          nTemp |= (b << 16);
          pCRC.append((byte) b);

          if ((nTemp & 0x800000) != 0) L = (int) (nTemp & 0x7fffff) - 0x800000;
          else L = (int) (nTemp & 0x7fffff);

          // check the peak
          if (Math.abs(L) > pPeakLevel.value) pPeakLevel.value = Math.abs(L);
          if (Math.abs(R) > pPeakLevel.value) pPeakLevel.value = Math.abs(R);

          // convert to x,y
          pOutputY[nBlockIndex] = L - R;
          pOutputX[nBlockIndex] = R + (pOutputY[nBlockIndex] / 2);
        }
      } else if (pWaveFormatEx.nChannels == 1) {
        for (int nBlockIndex = 0; nBlockIndex < nTotalBlocks; nBlockIndex++) {
          long nTemp = 0;

          short b = pRawData.readUnsignedByte();
          nTemp |= (b << 0);
          pCRC.append((byte) b);

          b = pRawData.readUnsignedByte();
          nTemp |= (b << 8);
          pCRC.append((byte) b);

          b = pRawData.readUnsignedByte();
          nTemp |= (b << 16);
          pCRC.append((byte) b);

          if ((nTemp & 0x800000) != 0) R = (int) (nTemp & 0x7fffff) - 0x800000;
          else R = (int) (nTemp & 0x7fffff);

          // check the peak
          if (Math.abs(R) > pPeakLevel.value) pPeakLevel.value = Math.abs(R);

          // convert to x,y
          pOutputX[nBlockIndex] = R;
        }
      }
    } else {
      if (pWaveFormatEx.nChannels == 2) {
        int LPeak = 0;
        int RPeak = 0;
        int nBlockIndex = 0;
        for (nBlockIndex = 0; nBlockIndex < nTotalBlocks; nBlockIndex++) {
          R = pRawData.readShort();
          pCRC.append((short) R);

          L = pRawData.readShort();
          pCRC.append((short) L);

          // check the peak
          if (Math.abs(L) > LPeak) LPeak = Math.abs(L);
          if (Math.abs(R) > RPeak) RPeak = Math.abs(R);

          // convert to x,y
          pOutputY[nBlockIndex] = L - R;
          pOutputX[nBlockIndex] = R + (pOutputY[nBlockIndex] / 2);
        }

        if (LPeak == 0) pSpecialCodes.value |= SpecialFrame.SPECIAL_FRAME_LEFT_SILENCE;
        if (RPeak == 0) pSpecialCodes.value |= SpecialFrame.SPECIAL_FRAME_RIGHT_SILENCE;
        if (Math.max(LPeak, RPeak) > pPeakLevel.value) pPeakLevel.value = Math.max(LPeak, RPeak);

        // check for pseudo-stereo files
        nBlockIndex = 0;
        while (pOutputY[nBlockIndex++] == 0) {
          if (nBlockIndex == (nBytes / 4)) {
            pSpecialCodes.value |= SpecialFrame.SPECIAL_FRAME_PSEUDO_STEREO;
            break;
          }
        }
      } else if (pWaveFormatEx.nChannels == 1) {
        int nPeak = 0;
        for (int nBlockIndex = 0; nBlockIndex < nTotalBlocks; nBlockIndex++) {
          R = pRawData.readUnsignedShort();
          pCRC.append((short) R);

          // check the peak
          if (Math.abs(R) > nPeak) nPeak = Math.abs(R);

          // convert to x,y
          pOutputX[nBlockIndex] = R;
        }

        if (nPeak > pPeakLevel.value) pPeakLevel.value = nPeak;
        if (nPeak == 0) pSpecialCodes.value |= SpecialFrame.SPECIAL_FRAME_MONO_SILENCE;
      }
    }

    pCRC.prefinalizeCrc();

    // add the special code
    pCRC.finalizeCrc();

    if (pSpecialCodes.value != 0) pCRC.doSpecial();
  }
예제 #2
0
파일: Prepare.java 프로젝트: jtrfp/javamod
  public void unprepareOld(
      int[] pInputX,
      int[] pInputY,
      int nBlocks,
      WaveFormat pWaveFormatEx,
      ByteBuffer output,
      Crc32 crc,
      int nFileVersion) {
    // the CRC that will be figured during decompression
    crc.init();

    // decompress and convert from (x,y) -> (l,r)
    // sort of int and ugly.... sorry
    int channels = pWaveFormatEx.nChannels;
    int bitsPerSample = pWaveFormatEx.wBitsPerSample;
    if (channels == 2) {
      // convert the x,y data to raw data
      if (bitsPerSample == 16) {
        short R;
        int pX = 0;
        int pY = 0;

        for (; pX < nBlocks; pX++, pY++) {
          R = (short) (pInputX[pX] - (pInputY[pY] / 2));

          output.append(R);
          crc.append(R);
          R += pInputY[pY];
          output.append(R);
          crc.append(R);
        }
      } else if (bitsPerSample == 8) {
        byte R;
        if (nFileVersion > 3830) {
          for (int SampleIndex = 0; SampleIndex < nBlocks; SampleIndex++) {
            R = (byte) (pInputX[SampleIndex] - (pInputY[SampleIndex] / 2) + 128);
            output.append(R);
            crc.append(R);
            R += pInputY[SampleIndex];
            output.append(R);
            crc.append(R);
          }
        } else {
          for (int SampleIndex = 0; SampleIndex < nBlocks; SampleIndex++) {
            R = (byte) (pInputX[SampleIndex] - (pInputY[SampleIndex] / 2));
            output.append(R);
            crc.append(R);
            R += pInputY[SampleIndex];
            output.append(R);
            crc.append(R);
          }
        }
      } else if (bitsPerSample == 24) {
        int RV, LV;

        for (int SampleIndex = 0; SampleIndex < nBlocks; SampleIndex++) {
          RV = pInputX[SampleIndex] - (pInputY[SampleIndex] / 2);
          LV = RV + pInputY[SampleIndex];

          int nTemp = 0;
          if (RV < 0) nTemp = (RV + 0x800000) | 0x800000;
          else nTemp = RV;

          output.append24(nTemp);
          crc.append24(nTemp);

          nTemp = 0;
          if (LV < 0) nTemp = (LV + 0x800000) | 0x800000;
          else nTemp = LV;

          output.append24(nTemp);
          crc.append24(nTemp);
        }
      }
    } else if (channels == 1) {
      // convert to raw data
      if (bitsPerSample == 8) {
        byte R;
        if (nFileVersion > 3830) {
          for (int SampleIndex = 0; SampleIndex < nBlocks; SampleIndex++) {
            R = (byte) (pInputX[SampleIndex] + 128);
            output.append(R);
            crc.append(R);
          }
        } else {
          for (int SampleIndex = 0; SampleIndex < nBlocks; SampleIndex++) {
            R = (byte) (pInputX[SampleIndex]);
            output.append(R);
            crc.append(R);
          }
        }

      } else if (bitsPerSample == 24) {

        int RV;
        for (int SampleIndex = 0; SampleIndex < nBlocks; SampleIndex++) {
          RV = pInputX[SampleIndex];

          int nTemp = 0;
          if (RV < 0) nTemp = (RV + 0x800000) | 0x800000;
          else nTemp = RV;

          output.append24(nTemp);
          crc.append24(nTemp);
        }
      } else {
        short R;
        for (int SampleIndex = 0; SampleIndex < nBlocks; SampleIndex++) {
          R = (short) (pInputX[SampleIndex]);
          output.append(R);
          crc.append(R);
        }
      }
    }
    crc.prefinalizeCrc();
  }