📄 genericaudioheader.java
字号:
/*
* Entagged Audio Tag library
* Copyright (c) 2003-2005 Rapha�l Slinckx <raphael@slinckx.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.hadeslee.audiotag.audio.generic;
import com.hadeslee.audiotag.audio.AudioHeader;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Set;
/**
* This class represents a structure for storing and retrieving information
* about the codec respectively the encoding parameters.<br>
* Most of the parameters are available for nearly each audio format. Some
* others would result in standard values.<br>
* <b>Consider:</b> None of the setter methods will actually affect the audio
* file. This is just a structure for retrieving information, not manipulating
* the audio file.<br>
*
* @author Raphael Slinckx
*/
public class GenericAudioHeader implements AudioHeader
{
/**
* The key for the Bitrate.({@link Integer})<br>
*
* @see #content
*/
public final static String FIELD_BITRATE = "BITRATE";
/**
* The key for the number of audio channels.({@link Integer})<br>
*
* @see #content
*/
public final static String FIELD_CHANNEL = "CHANNB";
/**
* The key for the extra encoding information.({@link String})<br>
*
* @see #content
*/
public final static String FIELD_INFOS = "INFOS";
/**
* The key for the audio clip duration in seconds. ({@link Float})<br>
*
* @see #content
*/
public final static String FIELD_LENGTH = "LENGTH";
/**
* The key for the audio sample rate in "Hz". ({@link Integer})<br>
*
* @see #content
*/
public final static String FIELD_SAMPLERATE = "SAMPLING";
/**
* The key for the audio type.({@link String})<br>
*
* @see #content
*/
public final static String FIELD_TYPE = "TYPE";
/**
* The key for the VBR flag. ({@link Boolean})<br>
*
* @see #content
*/
public final static String FIELD_VBR = "VBR";
/**
* This table containts the parameters.<br>
*/
protected HashMap content;
/**
* Creates an instance with emtpy values.<br>
*/
public GenericAudioHeader()
{
content = new HashMap(6);
content.put(FIELD_BITRATE, new Integer(-1));
content.put(FIELD_CHANNEL, new Integer(-1));
content.put(FIELD_TYPE, "");
content.put(FIELD_INFOS, "");
content.put(FIELD_SAMPLERATE, new Integer(-1));
content.put(FIELD_LENGTH, new Float(-1));
content.put(FIELD_VBR, new Boolean(true));
}
public String getBitRate()
{
return ((Integer) content.get(FIELD_BITRATE)).toString();
}
/**
* This method returns the bitrate of the represented audio clip in
* "Kbps".<br>
*
* @return The bitrate in Kbps.
*/
public long getBitRateAsNumber()
{
return ((Integer) content.get(FIELD_BITRATE)).longValue();
}
/**
* This method returns the number of audio channels the clip contains.<br>
* (The stereo, mono thing).
*
* @return The number of channels. (2 for stereo, 1 for mono)
*/
public int getChannelNumber()
{
return ((Integer) content.get(FIELD_CHANNEL)).intValue();
}
/**
* @return
*/
public String getChannels()
{
return String.valueOf(getChannelNumber());
}
/**
* Returns the encoding type.
*
* @return The encoding type
*/
public String getEncodingType()
{
return (String) content.get(FIELD_TYPE);
}
/**
* Returns the format, same as encoding type
*
* @return The encoding type
*/
public String getFormat()
{
return (String) content.get(FIELD_TYPE);
}
/**
* This method returns some extra information about the encoding.<br>
* This may not contain anything for some audio formats.<br>
*
* @return Some extra information.
*/
public String getExtraEncodingInfos()
{
return (String) content.get(FIELD_INFOS);
}
/**
* This method returns the duration of the represented audio clip in
* seconds.<br>
*
* @return The duration in seconds.
* @see #getPreciseLength()
*/
public int getTrackLength()
{
return (int) getPreciseLength();
}
/**
* This method returns the duration of the represented audio clip in seconds
* (single-precision).<br>
*
* @return The duration in seconds.
* @see #getTrackLength()
*/
public float getPreciseLength()
{
return ((Float) content.get(FIELD_LENGTH)).floatValue();
}
/**
* This method returns the sample rate, the audio clip was encoded with.<br>
*
* @return Sample rate of the audio clip in "Hz".
*/
public String getSampleRate()
{
return ((Integer) content.get(FIELD_SAMPLERATE)).toString();
}
public int getSampleRateAsNumber()
{
return ((Integer) content.get(FIELD_SAMPLERATE)).intValue();
}
/**
* This method returns <code>true</code>, if the audio file is encoded
* with "Variable Bitrate".<br>
*
* @return <code>true</code> if audio clip is encoded with VBR.
*/
public boolean isVariableBitRate()
{
return ((Boolean) content.get(FIELD_VBR)).booleanValue();
}
/**
* This Method sets the bitrate in "Kbps".<br>
*
* @param bitrate bitrate in kbps.
*/
public void setBitrate(int bitrate)
{
content.put(FIELD_BITRATE, new Integer(bitrate));
}
/**
* Sets the number of channels.
*
* @param chanNb number of channels (2 for stereo, 1 for mono).
*/
public void setChannelNumber(int chanNb)
{
content.put(FIELD_CHANNEL, new Integer(chanNb));
}
/**
* Sets the type of the encoding.<br>
* This is a bit format specific.<br>
* eg:Layer I/II/III
*
* @param encodingType Encoding type.
*/
public void setEncodingType(String encodingType)
{
content.put(FIELD_TYPE, encodingType);
}
/**
* A string containing anything else that might be interesting
*
* @param infos Extra information.
*/
public void setExtraEncodingInfos(String infos)
{
content.put(FIELD_INFOS, infos);
}
/**
* This method sets the audio duration of the represented clip.<br>
*
* @param length The duration of the audio clip in seconds.
*/
public void setLength(int length)
{
content.put(FIELD_LENGTH, new Float(length));
}
/**
* This method sets the audio duration of the represented clip.<br>
*
* @param seconds The duration of the audio clip in seconds (single-precision).
*/
public void setPreciseLength(float seconds)
{
content.put(FIELD_LENGTH, new Float(seconds));
}
/**
* Sets the Sampling rate in "Hz"<br>
*
* @param samplingRate Sample rate.
*/
public void setSamplingRate(int samplingRate)
{
content.put(FIELD_SAMPLERATE, new Integer(samplingRate));
}
/**
* Sets the VBR flag for the represented audio clip.<br>
*
* @param b <code>true</code> if VBR.
*/
public void setVariableBitRate(boolean b)
{
content.put(FIELD_VBR, new Boolean(b));
}
/**
* Can be used to add additional information
* @param key
* @param value
*/
public void setExtra(String key,Object value)
{
content.put(key, value);
}
/**
* Pretty prints this encoding info
*
* @see java.lang.Object#toString()
*/
public String toString()
{
StringBuffer out = new StringBuffer(50);
out.append("Encoding infos content:\n");
Set set = content.keySet();
for(Object key: set)
{
Object val = content.get(key);
out.append("\t");
out.append(key);
out.append(" : ");
out.append(val);
out.append("\n");
}
return out.toString().substring(0, out.length() - 1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -