Exemplo n.º 1
0
  private void ProcessBuffer(boolean bFinalize) throws IOException {
    if (m_pBuffer == null) throw new JMACException("Error Undefined");

    // process as much as possible
    int nThreshold = (bFinalize) ? 0 : m_spAPECompressCreate.GetFullFrameBytes();

    while ((m_nBufferTail - m_nBufferHead) >= nThreshold) {
      int nFrameBytes =
          Math.min(m_spAPECompressCreate.GetFullFrameBytes(), m_nBufferTail - m_nBufferHead);

      if (nFrameBytes == 0) break;

      pByteReader.reset(m_pBuffer, m_nBufferHead);
      m_spAPECompressCreate.EncodeFrame(pByteReader, nFrameBytes);

      m_nBufferHead += nFrameBytes;
    }

    // shift the buffer
    if (m_nBufferHead != 0) {
      int nBytesLeft = m_nBufferTail - m_nBufferHead;

      if (nBytesLeft != 0) System.arraycopy(m_pBuffer, m_nBufferHead, m_pBuffer, 0, nBytesLeft);

      m_nBufferTail -= m_nBufferHead;
      m_nBufferHead = 0;
    }
  }
Exemplo n.º 2
0
  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();
  }