Ejemplo n.º 1
0
  /**
   * Regets the audio header starting from start of file, and write appropriate logging to indicate
   * potential problem to user.
   *
   * @param startByte
   * @param firstHeaderAfterTag
   * @return
   * @throws IOException
   * @throws InvalidAudioFrameException
   */
  private MP3AudioHeader checkAudioStart(long startByte, MP3AudioHeader firstHeaderAfterTag)
      throws IOException, InvalidAudioFrameException {
    MP3AudioHeader headerOne;
    MP3AudioHeader headerTwo;

    logger.warning(
        ErrorMessage.MP3_ID3TAG_LENGTH_INCORRECT.getMsg(
            file.getPath(),
            Hex.asHex(startByte),
            Hex.asHex(firstHeaderAfterTag.getMp3StartByte())));

    // because we cant agree on start location we reread the audioheader from the start of the file,
    // at least
    // this way we cant overwrite the audio although we might overwrite part of the tag if we write
    // this file
    // back later
    headerOne = new MP3AudioHeader(file, 0);
    logger.config("Checking from start:" + headerOne);

    // Although the id3 tag size appears to be incorrect at least we have found the same location
    // for the start
    // of audio whether we start searching from start of file or at the end of the alleged of file
    // so no real
    // problem
    if (firstHeaderAfterTag.getMp3StartByte() == headerOne.getMp3StartByte()) {
      logger.config(
          ErrorMessage.MP3_START_OF_AUDIO_CONFIRMED.getMsg(
              file.getPath(), Hex.asHex(headerOne.getMp3StartByte())));
      return firstHeaderAfterTag;
    } else {

      // We get a different value if read from start, can't guarantee 100% correct lets do some more
      // checks
      logger.config(
          (ErrorMessage.MP3_RECALCULATED_POSSIBLE_START_OF_MP3_AUDIO.getMsg(
              file.getPath(), Hex.asHex(headerOne.getMp3StartByte()))));

      // Same frame count so probably both audio headers with newAudioHeader being the first one
      if (firstHeaderAfterTag.getNumberOfFrames() == headerOne.getNumberOfFrames()) {
        logger.warning(
            (ErrorMessage.MP3_RECALCULATED_START_OF_MP3_AUDIO.getMsg(
                file.getPath(), Hex.asHex(headerOne.getMp3StartByte()))));
        return headerOne;
      }

      // If the size reported by the tag header is a little short and there is only nulls between
      // the recorded value
      // and the start of the first audio found then we stick with the original header as more
      // likely that currentHeader
      // DataInputStream not really a header
      if (isFilePortionNull((int) startByte, (int) firstHeaderAfterTag.getMp3StartByte())) {
        return firstHeaderAfterTag;
      }

      // Skip to the next header (header 2, counting from start of file)
      headerTwo =
          new MP3AudioHeader(
              file, headerOne.getMp3StartByte() + headerOne.mp3FrameHeader.getFrameLength());

      // It matches the header we found when doing the original search from after the ID3Tag
      // therefore it
      // seems that newAudioHeader was a false match and the original header was correct
      if (headerTwo.getMp3StartByte() == firstHeaderAfterTag.getMp3StartByte()) {
        logger.warning(
            (ErrorMessage.MP3_START_OF_AUDIO_CONFIRMED.getMsg(
                file.getPath(), Hex.asHex(firstHeaderAfterTag.getMp3StartByte()))));
        return firstHeaderAfterTag;
      }

      // It matches the frameCount the header we just found so lends weight to the fact that the
      // audio does indeed start at new header
      // however it maybe that neither are really headers and just contain the same data being
      // misrepresented as headers.
      if (headerTwo.getNumberOfFrames() == headerOne.getNumberOfFrames()) {
        logger.warning(
            (ErrorMessage.MP3_RECALCULATED_START_OF_MP3_AUDIO.getMsg(
                file.getPath(), Hex.asHex(headerOne.getMp3StartByte()))));
        return headerOne;
      }
      /// Doesnt match the frameCount lets go back to the original header
      else {
        logger.warning(
            (ErrorMessage.MP3_RECALCULATED_START_OF_MP3_AUDIO.getMsg(
                file.getPath(), Hex.asHex(firstHeaderAfterTag.getMp3StartByte()))));
        return firstHeaderAfterTag;
      }
    }
  }