Coverage Report - org.jaudiotagger.audio.generic.GenericAudioHeader
 
Classes in this File Line Coverage Branch Coverage Complexity
GenericAudioHeader
91%
51/56
100%
2/2
1.04
 
 1  
 /*
 2  
  * Entagged Audio Tag library
 3  
  * Copyright (c) 2003-2005 Rapha�l Slinckx <raphael@slinckx.net>
 4  
  * 
 5  
  * This library is free software; you can redistribute it and/or
 6  
  * modify it under the terms of the GNU Lesser General Public
 7  
  * License as published by the Free Software Foundation; either
 8  
  * version 2.1 of the License, or (at your option) any later version.
 9  
  *  
 10  
  * This library is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13  
  * Lesser General Public License for more details.
 14  
  * 
 15  
  * You should have received a copy of the GNU Lesser General Public
 16  
  * License along with this library; if not, write to the Free Software
 17  
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 18  
  */
 19  
 package org.jaudiotagger.audio.generic;
 20  
 
 21  
 import org.jaudiotagger.audio.AudioHeader;
 22  
 
 23  
 import java.util.HashMap;
 24  
 import java.util.Set;
 25  
 
 26  
 /**
 27  
  * This class represents a structure for storing and retrieving information
 28  
  * about the codec respectively the encoding parameters.<br>
 29  
  * Most of the parameters are available for nearly each audio format. Some
 30  
  * others would result in standard values.<br>
 31  
  * <b>Consider:</b> None of the setter methods will actually affect the audio
 32  
  * file. This is just a structure for retrieving information, not manipulating
 33  
  * the audio file.<br>
 34  
  *
 35  
  * @author Raphael Slinckx
 36  
  */
 37  
 public class GenericAudioHeader implements AudioHeader
 38  
 {
 39  
 
 40  
     /**
 41  
      * The key for the Bitrate.({@link Integer})<br>
 42  
      *
 43  
      * @see #content
 44  
      */
 45  
     public final static String FIELD_BITRATE = "BITRATE";
 46  
 
 47  
     /**
 48  
      * The key for the number of audio channels.({@link Integer})<br>
 49  
      *
 50  
      * @see #content
 51  
      */
 52  
     public final static String FIELD_CHANNEL = "CHANNB";
 53  
 
 54  
     /**
 55  
      * The key for the extra encoding information.({@link String})<br>
 56  
      *
 57  
      * @see #content
 58  
      */
 59  
     public final static String FIELD_INFOS = "INFOS";
 60  
 
 61  
     /**
 62  
      * The key for the audio clip duration in seconds. ({@link Float})<br>
 63  
      *
 64  
      * @see #content
 65  
      */
 66  
     public final static String FIELD_LENGTH = "LENGTH";
 67  
 
 68  
     /**
 69  
      * The key for the audio sample rate in &quot;Hz&quot;. ({@link Integer})<br>
 70  
      *
 71  
      * @see #content
 72  
      */
 73  
     public final static String FIELD_SAMPLERATE = "SAMPLING";
 74  
 
 75  
     /**
 76  
      * The key for the audio type.({@link String})<br>
 77  
      *
 78  
      * @see #content
 79  
      */
 80  
     public final static String FIELD_TYPE = "TYPE";
 81  
 
 82  
     /**
 83  
      * The key for the VBR flag. ({@link Boolean})<br>
 84  
      *
 85  
      * @see #content
 86  
      */
 87  
     public final static String FIELD_VBR = "VBR";
 88  
 
 89  
     /**
 90  
      * Used for WMA files
 91  
      */
 92  946
     private boolean isLossless = false;
 93  
 
 94  
     /**
 95  
      * This table containts the parameters.<br>
 96  
      */
 97  
     protected HashMap<String, Object> content;
 98  
 
 99  
     /**
 100  
      * Creates an instance with emtpy values.<br>
 101  
      */
 102  
     public GenericAudioHeader()
 103  946
     {
 104  946
         content = new HashMap<String, Object>(6);
 105  946
         content.put(FIELD_BITRATE, -1);
 106  946
         content.put(FIELD_CHANNEL, -1);
 107  946
         content.put(FIELD_TYPE, "");
 108  946
         content.put(FIELD_INFOS, "");
 109  946
         content.put(FIELD_SAMPLERATE, -1);
 110  946
         content.put(FIELD_LENGTH, (float) -1);
 111  946
         content.put(FIELD_VBR, true);
 112  946
     }
 113  
 
 114  
     public String getBitRate()
 115  
     {
 116  49
         return content.get(FIELD_BITRATE).toString();
 117  
     }
 118  
 
 119  
 
 120  
     /**
 121  
      * This method returns the bitrate of the represented audio clip in
 122  
      * &quot;Kbps&quot;.<br>
 123  
      *
 124  
      * @return The bitrate in Kbps.
 125  
      */
 126  
     public long getBitRateAsNumber()
 127  
     {
 128  424
         return ((Integer) content.get(FIELD_BITRATE)).longValue();
 129  
     }
 130  
 
 131  
     /**
 132  
      * This method returns the number of audio channels the clip contains.<br>
 133  
      * (The stereo, mono thing).
 134  
      *
 135  
      * @return The number of channels. (2 for stereo, 1 for mono)
 136  
      */
 137  
     public int getChannelNumber()
 138  
     {
 139  473
         return (Integer) content.get(FIELD_CHANNEL);
 140  
     }
 141  
 
 142  
     /**
 143  
      * @return
 144  
      */
 145  
     public String getChannels()
 146  
     {
 147  67
         return String.valueOf(getChannelNumber());
 148  
     }
 149  
 
 150  
     /**
 151  
      * Returns the encoding type.
 152  
      *
 153  
      * @return The encoding type
 154  
      */
 155  
     public String getEncodingType()
 156  
     {
 157  456
         return (String) content.get(FIELD_TYPE);
 158  
     }
 159  
 
 160  
     /**
 161  
      * Returns the format, same as encoding type
 162  
      *
 163  
      * @return The encoding type
 164  
      */
 165  
     public String getFormat()
 166  
     {
 167  0
         return (String) content.get(FIELD_TYPE);
 168  
     }
 169  
 
 170  
     /**
 171  
      * This method returns some extra information about the encoding.<br>
 172  
      * This may not contain anything for some audio formats.<br>
 173  
      *
 174  
      * @return Some extra information.
 175  
      */
 176  
     public String getExtraEncodingInfos()
 177  
     {
 178  0
         return (String) content.get(FIELD_INFOS);
 179  
     }
 180  
 
 181  
     /**
 182  
      * This method returns the duration of the represented audio clip in
 183  
      * seconds.<br>
 184  
      *
 185  
      * @return The duration in seconds.
 186  
      * @see #getPreciseLength()
 187  
      */
 188  
     public int getTrackLength()
 189  
     {
 190  120
         return (int) getPreciseLength();
 191  
     }
 192  
 
 193  
     /**
 194  
      * This method returns the duration of the represented audio clip in seconds
 195  
      * (single-precision).<br>
 196  
      *
 197  
      * @return The duration in seconds.
 198  
      * @see #getTrackLength()
 199  
      */
 200  
     public float getPreciseLength()
 201  
     {
 202  120
         return (Float) content.get(FIELD_LENGTH);
 203  
     }
 204  
 
 205  
     /**
 206  
      * This method returns the sample rate, the audio clip was encoded with.<br>
 207  
      *
 208  
      * @return Sample rate of the audio clip in &quot;Hz&quot;.
 209  
      */
 210  
     public String getSampleRate()
 211  
     {
 212  49
         return content.get(FIELD_SAMPLERATE).toString();
 213  
     }
 214  
 
 215  
     public int getSampleRateAsNumber()
 216  
     {
 217  107
         return (Integer) content.get(FIELD_SAMPLERATE);
 218  
     }
 219  
 
 220  
     /**
 221  
      * This method returns <code>true</code>, if the audio file is encoded
 222  
      * with &quot;Variable Bitrate&quot;.<br>
 223  
      *
 224  
      * @return <code>true</code> if audio clip is encoded with VBR.
 225  
      */
 226  
     public boolean isVariableBitRate()
 227  
     {
 228  40
         return (Boolean) content.get(FIELD_VBR);
 229  
     }
 230  
 
 231  
     /**
 232  
      * This method returns <code>true</code>, if the audio file is encoded
 233  
      * with &quot;Lossless&quot;.<br>
 234  
      *
 235  
      * @return <code>true</code> if audio clip is encoded with VBR.
 236  
      */
 237  
     public boolean isLossless()
 238  
     {
 239  0
         return isLossless;
 240  
     }
 241  
 
 242  
     /**
 243  
      * This Method sets the bitrate in &quot;Kbps&quot;.<br>
 244  
      *
 245  
      * @param bitrate bitrate in kbps.
 246  
      */
 247  
     public void setBitrate(int bitrate)
 248  
     {
 249  942
         content.put(FIELD_BITRATE, bitrate);
 250  942
     }
 251  
 
 252  
     /**
 253  
      * Sets the number of channels.
 254  
      *
 255  
      * @param chanNb number of channels (2 for stereo, 1 for mono).
 256  
      */
 257  
     public void setChannelNumber(int chanNb)
 258  
     {
 259  882
         content.put(FIELD_CHANNEL, chanNb);
 260  882
     }
 261  
 
 262  
     /**
 263  
      * Sets the type of the encoding.<br>
 264  
      * This is a bit format specific.<br>
 265  
      * eg:Layer I/II/III
 266  
      *
 267  
      * @param encodingType Encoding type.
 268  
      */
 269  
     public void setEncodingType(String encodingType)
 270  
     {
 271  882
         content.put(FIELD_TYPE, encodingType);
 272  882
     }
 273  
 
 274  
     /**
 275  
      * A string containing anything else that might be interesting
 276  
      *
 277  
      * @param infos Extra information.
 278  
      */
 279  
     public void setExtraEncodingInfos(String infos)
 280  
     {
 281  278
         content.put(FIELD_INFOS, infos);
 282  278
     }
 283  
 
 284  
     /**
 285  
      * This method sets the audio duration of the represented clip.<br>
 286  
      *
 287  
      * @param length The duration of the audio clip in seconds.
 288  
      */
 289  
     public void setLength(int length)
 290  
     {
 291  552
         content.put(FIELD_LENGTH, (float) length);
 292  552
     }
 293  
 
 294  
     /**
 295  
      * This method sets the audio duration of the represented clip.<br>
 296  
      *
 297  
      * @param seconds The duration of the audio clip in seconds (single-precision).
 298  
      */
 299  
     public void setPreciseLength(float seconds)
 300  
     {
 301  476
         content.put(FIELD_LENGTH, seconds);
 302  476
     }
 303  
 
 304  
     /**
 305  
      * Sets the Sampling rate in &quot;Hz&quot;<br>
 306  
      *
 307  
      * @param samplingRate Sample rate.
 308  
      */
 309  
     public void setSamplingRate(int samplingRate)
 310  
     {
 311  882
         content.put(FIELD_SAMPLERATE, samplingRate);
 312  882
     }
 313  
 
 314  
     /**
 315  
      * Sets the VBR flag for the represented audio clip.<br>
 316  
      *
 317  
      * @param b <code>true</code> if VBR.
 318  
      */
 319  
     public void setVariableBitRate(boolean b)
 320  
     {
 321  450
         content.put(FIELD_VBR, b);
 322  450
     }
 323  
 
 324  
     /**
 325  
      * Sets the Lossless flag for the represented audio clip.<br>
 326  
      *
 327  
      * @param b <code>true</code> if Lossless.
 328  
      */
 329  
     public void setLossless(boolean b)
 330  
     {
 331  198
         isLossless = b;
 332  198
     }
 333  
 
 334  
     /**
 335  
      * Can be used to add additional information
 336  
      *
 337  
      * @param key
 338  
      * @param value
 339  
      */
 340  
     public void setExtra(String key, Object value)
 341  
     {
 342  0
         content.put(key, value);
 343  0
     }
 344  
 
 345  
     /**
 346  
      * Pretty prints this encoding info
 347  
      *
 348  
      * @see java.lang.Object#toString()
 349  
      */
 350  
     public String toString()
 351  
     {
 352  478
         StringBuffer out = new StringBuffer(50);
 353  478
         out.append("Encoding infos content:\n");
 354  478
         Set<String> set = content.keySet();
 355  478
         for (String key : set)
 356  
         {
 357  4722
             Object val = content.get(key);
 358  4722
             out.append("\t");
 359  4722
             out.append(key);
 360  4722
             out.append(" : ");
 361  4722
             out.append(val);
 362  4722
             out.append("\n");
 363  4722
         }
 364  478
         return out.toString().substring(0, out.length() - 1);
 365  
     }
 366  
 }