org.jaudiotagger.audio.mp3
Class MP3AudioHeader

java.lang.Object
  extended by org.jaudiotagger.audio.mp3.MP3AudioHeader
All Implemented Interfaces:
AudioHeader

public final class MP3AudioHeader
extends java.lang.Object
implements AudioHeader

Represents the audio header of an MP3 File

The audio header consists of a number of audio frames. Because we are not trying to play the audio but only extract some information regarding the audio we only need to read the first audio frames to ensure that we have correctly identified them as audio frames and extracted the metadata we reuire.

Start of Audio id 0xFF (11111111) and then second byte anded with 0xE0(11100000). For example 2nd byte doesnt have to be 0xE0 is just has to have the top 3 signicant bits set. For example 0xFB (11111011) is a common occurence of the second match. The 2nd byte defines flags to indicate various mp3 values.

Having found these two values we then read the header which comprises these two bytes plus a further two to ensure this really is a MP3Header, sometimes the first frame is actually a dummy frame with summary information held within about the whole file, typically using a Xing Header or LAme Header. This is most useful when the file is variable bit rate, if the file is variable bit rate but does not use a summary header it will not be correctly identified as a VBR frame and the track length will be incorreclty calculated. Strictly speaking MP3 means an MPEG-1, Layer III file but MP2 (MPEG-1,Layer II), MP1 (MPEG-1,Layer I) and MPEG-2 files are sometimes used and named with the .mp3 suffix so this library attempts to supports all these formats.


Field Summary
static java.util.logging.Logger logger
           
 
Constructor Summary
MP3AudioHeader(java.io.File seekFile)
          Search for the first MP3Header in the file The search starts from the start of the file, it is usually safer to use the alternative constructor that allows you to provide the length of the tag header as a parameter so the tag can be skipped over.
MP3AudioHeader(java.io.File seekFile, long startByte)
          Search for the first MP3Header in the file Starts searching from location startByte, this is because there is likely to be an ID3TagHeader before the start of the audio.
 
Method Summary
 java.lang.String getBitRate()
           
 long getBitRateAsNumber()
           
 java.lang.String getChannels()
           
 java.lang.String getEmphasis()
           
 java.lang.String getEncoder()
           
 java.lang.String getEncodingType()
           
 java.lang.String getFormat()
           
 long getMp3StartByte()
          Returns the byte position of the first MP3 Frame that the file arguement refers to.
 java.lang.String getMpegLayer()
           
 java.lang.String getMpegVersion()
           
 long getNumberOfFrames()
           
 long getNumberOfFramesEstimate()
           
 double getPreciseTrackLength()
           
 java.lang.String getSampleRate()
           
 int getSampleRateAsNumber()
           
 int getTrackLength()
           
 java.lang.String getTrackLengthAsString()
          Return the length in user friendly format
 boolean isCopyrighted()
           
 boolean isOriginal()
           
 boolean isPadding()
           
 boolean isPrivate()
           
 boolean isProtected()
           
 boolean isVariableBitRate()
           
 boolean seek(java.io.File seekFile, long startByte)
          Returns true if the first MP3 frame can be found for the MP3 file This is the first byte of music data and not the ID3 Tag Frame.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

logger

public static java.util.logging.Logger logger
Constructor Detail

MP3AudioHeader

public MP3AudioHeader(java.io.File seekFile)
               throws java.io.IOException,
                      InvalidAudioFrameException
Search for the first MP3Header in the file The search starts from the start of the file, it is usually safer to use the alternative constructor that allows you to provide the length of the tag header as a parameter so the tag can be skipped over.

Parameters:
seekFile -
Throws:
java.io.IOException
InvalidAudioFrameException

MP3AudioHeader

public MP3AudioHeader(java.io.File seekFile,
                      long startByte)
               throws java.io.IOException,
                      InvalidAudioFrameException
Search for the first MP3Header in the file Starts searching from location startByte, this is because there is likely to be an ID3TagHeader before the start of the audio. If this tagHeader contains unsynchronized information there is a possibility that it might be inaccurately identified as the start of the Audio data. Various checks are done in this code to prevent this happening but it cannot be guaranteed. Of course if the startByte provided overstates the length og the tag header, this could mean the start of the MP3AudioHeader is missed, further checks are done within the MP3 class to recognize if this has occurred and take appropriate action.

Parameters:
seekFile -
startByte -
Throws:
java.io.IOException
InvalidAudioFrameException
Method Detail

seek

public boolean seek(java.io.File seekFile,
                    long startByte)
             throws java.io.IOException
Returns true if the first MP3 frame can be found for the MP3 file This is the first byte of music data and not the ID3 Tag Frame. *

Parameters:
seekFile - MP3 file to seek
startByte - if there is an ID3v2tag we dont want to start reading from the start of the tag
Returns:
true if the first MP3 frame can be found
Throws:
java.io.IOException - on any I/O error

getMp3StartByte

public long getMp3StartByte()
Returns the byte position of the first MP3 Frame that the file arguement refers to. This is the first byte of music data and not the ID3 Tag Frame.

Returns:
the byte position of the first MP3 Frame

getNumberOfFrames

public long getNumberOfFrames()
Returns:
The number of frames within the Audio File, calculated as accurrately as possible

getNumberOfFramesEstimate

public long getNumberOfFramesEstimate()
Returns:
The number of frames within the Audio File, calculated by dividing the filesize by the number of frames, this may not be the most accurate method available.

getPreciseTrackLength

public double getPreciseTrackLength()
Returns:
Track Length in seconds

getTrackLength

public int getTrackLength()
Specified by:
getTrackLength in interface AudioHeader
Returns:
track length

getTrackLengthAsString

public java.lang.String getTrackLengthAsString()
Return the length in user friendly format


getEncodingType

public java.lang.String getEncodingType()
Specified by:
getEncodingType in interface AudioHeader
Returns:
the audio file type

getBitRateAsNumber

public long getBitRateAsNumber()
Specified by:
getBitRateAsNumber in interface AudioHeader
Returns:
bitrate in kbps, no indicator is provided as to whether or not it is vbr

getBitRate

public java.lang.String getBitRate()
Specified by:
getBitRate in interface AudioHeader
Returns:
the BitRate of the Audio, to distinguish cbr from vbr we add a '~' for vbr.

getSampleRateAsNumber

public int getSampleRateAsNumber()
Specified by:
getSampleRateAsNumber in interface AudioHeader
Returns:
the sampling rate in Hz

getSampleRate

public java.lang.String getSampleRate()
Specified by:
getSampleRate in interface AudioHeader
Returns:
the sampling rate as string

getMpegVersion

public java.lang.String getMpegVersion()
Returns:
MPEG Version (1-3)

getMpegLayer

public java.lang.String getMpegLayer()
Returns:
MPEG Layer (1-3)

getFormat

public java.lang.String getFormat()
Specified by:
getFormat in interface AudioHeader
Returns:
the format of the audio (i.e. MPEG-1 Layer3)

getChannels

public java.lang.String getChannels()
Specified by:
getChannels in interface AudioHeader
Returns:
the Channel Mode such as Stero or Mono

getEmphasis

public java.lang.String getEmphasis()
Returns:
Emphasis

isVariableBitRate

public boolean isVariableBitRate()
Specified by:
isVariableBitRate in interface AudioHeader
Returns:
if the bitrate is variable, Xing header takes precedence if we have one

isProtected

public boolean isProtected()

isPrivate

public boolean isPrivate()

isCopyrighted

public boolean isCopyrighted()

isOriginal

public boolean isOriginal()

isPadding

public boolean isPadding()

getEncoder

public java.lang.String getEncoder()
Returns:
encoder

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
a string represntation