📄 mpegframeheader.java
字号:
* Gets the layerVersion attribute of the MPEGFrame object
*
* @return The layerVersion value
*/
public int getLayer()
{
return layer;
}
public String getLayerAsString()
{
return layerAsString;
}
/**
* Gets the copyrighted attribute of the MPEGFrame object
*/
private void setCopyrighted()
{
isCopyrighted = (mpegBytes[BYTE_4] & MASK_MP3_COPY) != 0;
}
/**
* Set the version of this frame as an int value (see constants)
*/
private void setVersion() throws InvalidAudioFrameException
{
//MPEG Version
version = (byte) ((mpegBytes[BYTE_2] & MASK_MP3_VERSION) >> 3);
versionAsString = (String)mpegVersionMap.get(version);
if ( versionAsString == null)
{
throw new InvalidAudioFrameException("Invalid mpeg version");
}
}
/**
* Sets the original attribute of the MPEGFrame object
*/
private void setOriginal()
{
isOriginal = (mpegBytes[BYTE_4] & MASK_MP3_HOME) != 0;
}
/**
* Sets the protected attribute of the MPEGFrame object
*/
private void setProtected()
{
isProtected = (mpegBytes[BYTE_2] & MASK_MP3_PROTECTION) == 0x00;
}
/**
* Sets the private attribute of the MPEGFrame object
*/
private void setPrivate()
{
isPrivate = (mpegBytes[BYTE_3] & MASK_MP3_PRIVACY) != 0;
}
/**
* Get the setBitrate of this frame
*/
private void setBitrate() throws InvalidAudioFrameException
{
/* BitRate, get by checking header setBitrate bits and MPEG Version and Layer */
int bitRateIndex = mpegBytes[BYTE_3] & MASK_MP3_BITRATE |
mpegBytes[BYTE_2] & MASK_MP3_ID | mpegBytes[BYTE_2] & MASK_MP3_LAYER;
bitRate = (Integer) bitrateMap.get(bitRateIndex);
if (bitRate == null)
{
throw new InvalidAudioFrameException("Invalid bitrate");
}
}
/**
* Set the Mpeg channel mode of this frame as a constant (see constants)
*/
private void setChannelMode() throws InvalidAudioFrameException
{
channelMode = (mpegBytes[BYTE_4] & MASK_MP3_MODE) >>> 6;
channelModeAsString = (String)modeMap.get(channelMode);
if ( channelModeAsString == null)
{
throw new InvalidAudioFrameException("Invalid channel mode");
}
}
/**
* Get the setEmphasis mode of this frame in a string representation
*/
private void setEmphasis() throws InvalidAudioFrameException
{
emphasis = mpegBytes[BYTE_4] & MASK_MP3_EMPHASIS;
emphasisAsString = (String) emphasisMap.get(emphasis);
if (getEmphasisAsString() == null)
{
throw new InvalidAudioFrameException("Invalid emphasis");
}
}
/**
* Set whether this frame uses padding bytes
*/
private void setPadding()
{
isPadding = (mpegBytes[BYTE_3] & MASK_MP3_PADDING) != 0;
}
/**
* Get the layer version of this frame as a constant int value (see constants)
*/
private void setLayer() throws InvalidAudioFrameException
{
layer = (mpegBytes[BYTE_2] & MASK_MP3_LAYER) >>> 1;
layerAsString = (String)mpegLayerMap.get(layer);
if ( layerAsString== null)
{
throw new InvalidAudioFrameException("Invalid Layer");
}
}
/**
* Sets the string representation of the mode extension of this frame
*/
private void setModeExtension() throws InvalidAudioFrameException
{
int index = (mpegBytes[BYTE_4] & MASK_MP3_MODE_EXTENSION) >> 4;
if (layer == LAYER_III)
{
modeExtension = (String) modeExtensionLayerIIIMap.get(index);
if (getModeExtension() == null)
{
throw new InvalidAudioFrameException("Invalid Mode Extension");
}
}
else
{
modeExtension = (String) modeExtensionMap.get(index);
if (getModeExtension() == null)
{
throw new InvalidAudioFrameException("Invalid Mode Extension");
}
}
}
/**
* set the sampling rate in Hz of this frame
*/
private void setSamplingRate() throws InvalidAudioFrameException
{
//Frequency
int index = (mpegBytes[BYTE_3] & MASK_MP3_FREQUENCY) >>> 2;
HashMap samplingRateMapForVersion = (HashMap) samplingRateMap.get(version);
if (samplingRateMapForVersion == null)
{
throw new InvalidAudioFrameException("Invalid version");
}
samplingRate = (Integer) (samplingRateMapForVersion.get(new Integer(index)));
if (samplingRate == null)
{
throw new InvalidAudioFrameException("Invalid sampling rate");
}
}
/**
* Gets the number of channels
*
* @return The setChannelMode value
*/
public int getNumberOfChannels()
{
switch (channelMode)
{
case MODE_DUAL_CHANNEL:
return 2;
case MODE_JOINT_STEREO:
return 2;
case MODE_MONO:
return 1;
case MODE_STEREO:
return 2;
default:
return 0;
}
}
public int getChannelMode()
{
return channelMode;
}
public String getChannelModeAsString()
{
return channelModeAsString;
}
/**
* Gets the mPEGVersion attribute of the MPEGFrame object
*
* @return The mPEGVersion value
*/
public int getVersion()
{
return version;
}
public String getVersionAsString()
{
return versionAsString;
}
/**
* Gets the paddingLength attribute of the MPEGFrame object
*
* @return The paddingLength value
*/
public int getPaddingLength()
{
if (isPadding())
{
return 1;
}
else
{
return 0;
}
}
public Integer getBitRate()
{
return bitRate;
}
public Integer getSamplingRate()
{
return samplingRate;
}
/*
* Gets this frame length in bytes, value should always be rounded down to the nearest byte (not rounded up)
*
* Calculation is Bitrate (scaled to bps) divided by sampling frequency (in Hz), The larger the bitrate the larger
* the frame but the more samples per second the smaller the value, also have to take into account frame padding
* Have to multiple by a coefficient constant depending upon the layer it is encoded in,
*/
public int getFrameLength()
{
switch( version)
{
case VERSION_2:
case VERSION_2_5:
switch (layer)
{
case LAYER_I:
return (LAYER_I_FRAME_SIZE_COEFFICIENT * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength()) * LAYER_I_SLOT_SIZE;
case LAYER_II:
return (LAYER_II_FRAME_SIZE_COEFFICIENT/2) * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength() * LAYER_II_SLOT_SIZE;
case LAYER_III:
return (LAYER_III_FRAME_SIZE_COEFFICIENT/2) * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength()* LAYER_III_SLOT_SIZE;
default:
return (LAYER_III_FRAME_SIZE_COEFFICIENT/2) * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength()* LAYER_III_SLOT_SIZE;
}
default:
switch (layer)
{
case LAYER_I:
return (LAYER_I_FRAME_SIZE_COEFFICIENT * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength()) * LAYER_I_SLOT_SIZE;
case LAYER_II:
return LAYER_II_FRAME_SIZE_COEFFICIENT * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength() * LAYER_II_SLOT_SIZE;
case LAYER_III:
return LAYER_III_FRAME_SIZE_COEFFICIENT * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength()* LAYER_III_SLOT_SIZE;
default:
return LAYER_III_FRAME_SIZE_COEFFICIENT * (getBitRate() * SCALE_BY_THOUSAND) / getSamplingRate() + getPaddingLength()* LAYER_III_SLOT_SIZE;
}
}
}
/**
* Get the number of samples in a frame, all frames in a file have a set number of samples as defined by their MPEG Versiona
* and Layer
*/
public int getNoOfSamples()
{
Integer noOfSamples = (Integer) ((HashMap) samplesPerFrameMap.get(version)).get(new Integer(layer));
return noOfSamples;
}
public boolean isPadding()
{
return isPadding;
}
public boolean isCopyrighted()
{
return isCopyrighted;
}
public boolean isOriginal()
{
return isOriginal;
}
public boolean isProtected()
{
return isProtected;
}
public boolean isPrivate()
{
return isPrivate;
}
public boolean isVariableBitRate()
{
return false;
}
public int getEmphasis()
{
return emphasis;
}
public String getEmphasisAsString()
{
return emphasisAsString;
}
public String getModeExtension()
{
return modeExtension;
}
/**
* Hide Constructor
*/
private MPEGFrameHeader() throws InvalidAudioFrameException
{
}
/**
* Try and create a new MPEG frame with the given byte array and decodes its contents
* If decoding header causes a problem it is not a valid header
*
* @param b the array of bytes representing this mpeg frame
* @throws InvalidAudioFrameException if does not match expected format
*/
private MPEGFrameHeader(byte[] b) throws InvalidAudioFrameException
{
mpegBytes = b;
setVersion();
setLayer();
setProtected();
setBitrate();
setSamplingRate();
setPadding();
setPrivate();
setChannelMode();
setModeExtension();
setCopyrighted();
setOriginal();
setEmphasis();
}
/**
* Parse the MPEGFrameHeader of an MP3File, file pointer returns at end of the frame header
*
* @param bb the byte buffer containing the header
* @return
* @throws InvalidAudioFrameException if there is no header at this point
*/
public static MPEGFrameHeader parseMPEGHeader(ByteBuffer bb)
throws InvalidAudioFrameException
{
int position = bb.position();
bb.get(header,0,HEADER_SIZE);
bb.position(position);
MPEGFrameHeader frameHeader = new MPEGFrameHeader(header);
return frameHeader;
}
/**
* Gets the MPEGFrame attribute of the MPEGFrame object
*
* @return The mPEGFrame value
*/
public static boolean isMPEGFrame(ByteBuffer bb)
{
position = bb.position();
return (
((bb.get(position) & SYNC_BYTE1) == SYNC_BYTE1)
&&
((bb.get(position + 1) & SYNC_BYTE2) == SYNC_BYTE2)
);
}
/**
*
* @return a string represntation
*/
public String toString()
{
return
" mpeg frameheader:" +
" version:" + versionAsString +
" layer:" + layerAsString +
" noOfSamples:"+ getNoOfSamples() +
" samplingRate:"+ samplingRate +
" isPadding:"+ isPadding +
" isProtected:"+ isProtected +
" isPrivate:"+ isPrivate +
" isCopyrighted:"+ isCopyrighted +
" isOriginal:"+ isCopyrighted +
" isVariableBitRate" + this.isVariableBitRate() +
" header as binary:" +
AbstractTagDisplayFormatter.displayAsBinary(mpegBytes[BYTE_1]) +
AbstractTagDisplayFormatter.displayAsBinary(mpegBytes[BYTE_2]) +
AbstractTagDisplayFormatter.displayAsBinary(mpegBytes[BYTE_3]) +
AbstractTagDisplayFormatter.displayAsBinary(mpegBytes[BYTE_4]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -