public class MP3AudioHeader extends Object implements AudioHeader
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 incorrectly calculated. Strictly speaking MP3 means Layer III file but MP2 Layer II), MP1 Layer I) and MPEG-2 files are sometimes used and named with the .mp3 suffix so this library attempts to supports all these formats.
Modifier and Type | Field and Description |
---|---|
static Logger |
logger |
protected MPEGFrameHeader |
mp3FrameHeader |
protected VbriFrame |
mp3VbriFrame |
protected XingFrame |
mp3XingFrame |
Constructor and Description |
---|
MP3AudioHeader() |
MP3AudioHeader(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(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.
|
Modifier and Type | Method and Description |
---|---|
Long |
getAudioDataEndPosition() |
Long |
getAudioDataLength()
TODO (Was origjnally added for Wavs)
|
Long |
getAudioDataStartPosition() |
String |
getBitRate() |
long |
getBitRateAsNumber() |
int |
getBitsPerSample() |
Integer |
getByteRate()
TODO (Was originally added for Wavs)
|
String |
getChannels() |
String |
getEmphasis() |
String |
getEncoder() |
String |
getEncodingType() |
String |
getFormat() |
long |
getMp3StartByte()
Returns the byte position of the first MP3 Frame that the
file arguement refers to. |
String |
getMpegLayer() |
String |
getMpegVersion() |
Long |
getNoOfSamples() |
long |
getNumberOfFrames() |
long |
getNumberOfFramesEstimate() |
double |
getPreciseTrackLength() |
String |
getSampleRate() |
int |
getSampleRateAsNumber() |
int |
getTrackLength() |
String |
getTrackLengthAsString()
Return the length in user friendly format
|
boolean |
isCopyrighted() |
boolean |
isLossless() |
boolean |
isOriginal() |
boolean |
isPadding() |
boolean |
isPrivate() |
boolean |
isProtected() |
boolean |
isVariableBitRate() |
boolean |
seek(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.
|
void |
setAudioDataEndPosition(Long audioDataEndPosition) |
void |
setAudioDataStartPosition(Long audioDataStartPosition) |
protected void |
setBitRate()
Set bitrate in kbps, if Vbr use Xingheader if possible
|
protected void |
setEncoder() |
protected void |
setFileSize(long fileSize)
Set the size of the file, required in some calculations
|
protected void |
setMp3StartByte(long startByte)
Set the location of where the Audio file begins in the file
|
protected void |
setNumberOfFrames()
Set number of frames in this file, use Xing if exists otherwise ((File Size - Non Audio Part)/Frame Size)
|
protected void |
setTimePerFrame()
Set the time each frame contributes to the audio in fractions of seconds, the higher
the sampling rate the shorter the audio segment provided by the frame,
the number of samples is fixed by the MPEG Version and Layer
|
protected void |
setTrackLength()
Estimate the length of the audio track in seconds
Calculation is Number of frames multiplied by the Time Per Frame using the first frame as a prototype
Time Per Frame is the number of samples in the frame (which is defined by the MPEGVersion/Layer combination)
divided by the sampling rate, i.e the higher the sampling rate the shorter the audio represented by the frame is going
to be.
|
String |
toString() |
protected MPEGFrameHeader mp3FrameHeader
protected XingFrame mp3XingFrame
protected VbriFrame mp3VbriFrame
public static Logger logger
public MP3AudioHeader()
public MP3AudioHeader(File seekFile) throws IOException, InvalidAudioFrameException
seekFile
- IOException
InvalidAudioFrameException
public MP3AudioHeader(File seekFile, long startByte) throws IOException, InvalidAudioFrameException
seekFile
- startByte
- IOException
InvalidAudioFrameException
public boolean seek(File seekFile, long startByte) throws IOException
seekFile
- MP3 file to seekstartByte
- if there is an ID3v2tag we dont want to start reading from the start of the tagIOException
- on any I/O errorprotected void setMp3StartByte(long startByte)
startByte
- public long getMp3StartByte()
file
arguement refers to. This is the first byte of music
data and not the ID3 Tag Frame.protected void setNumberOfFrames()
public long getNumberOfFrames()
public Long getNoOfSamples()
getNoOfSamples
in interface AudioHeader
public long getNumberOfFramesEstimate()
protected void setTimePerFrame()
protected void setTrackLength()
public double getPreciseTrackLength()
getPreciseTrackLength
in interface AudioHeader
public int getTrackLength()
getTrackLength
in interface AudioHeader
public String getTrackLengthAsString()
public String getEncodingType()
getEncodingType
in interface AudioHeader
protected void setBitRate()
protected void setEncoder()
public long getBitRateAsNumber()
getBitRateAsNumber
in interface AudioHeader
public String getBitRate()
getBitRate
in interface AudioHeader
public int getSampleRateAsNumber()
getSampleRateAsNumber
in interface AudioHeader
public int getBitsPerSample()
getBitsPerSample
in interface AudioHeader
public String getSampleRate()
getSampleRate
in interface AudioHeader
public String getMpegVersion()
public String getMpegLayer()
public String getFormat()
getFormat
in interface AudioHeader
public String getChannels()
getChannels
in interface AudioHeader
public String getEmphasis()
public boolean isVariableBitRate()
isVariableBitRate
in interface AudioHeader
public boolean isProtected()
public boolean isPrivate()
public boolean isCopyrighted()
public boolean isOriginal()
public boolean isPadding()
public boolean isLossless()
isLossless
in interface AudioHeader
public String getEncoder()
protected void setFileSize(long fileSize)
fileSize
- public String toString()
public Integer getByteRate()
getByteRate
in interface AudioHeader
public Long getAudioDataLength()
getAudioDataLength
in interface AudioHeader
public Long getAudioDataStartPosition()
getAudioDataStartPosition
in interface AudioHeader
public void setAudioDataStartPosition(Long audioDataStartPosition)
public Long getAudioDataEndPosition()
getAudioDataEndPosition
in interface AudioHeader
public void setAudioDataEndPosition(Long audioDataEndPosition)
Copyright © 2005–2020 jthink.net. All rights reserved.