Coverage Report - org.jaudiotagger.audio.asf.data.AudioStreamChunk
 
Classes in this File Line Coverage Branch Coverage Complexity
AudioStreamChunk
72%
36/50
62%
5/8
1.238
 
 1  
 /*
 2  
  * Entagged Audio Tag library
 3  
  * Copyright (c) 2004-2005 Christian Laireiter <liree@web.de>
 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.asf.data;
 20  
 
 21  
 import org.jaudiotagger.audio.asf.util.Utils;
 22  
 
 23  
 import java.math.BigInteger;
 24  
 
 25  
 /**
 26  
  * This class represents the stream chunk describing an audio stream. <br>
 27  
  * 
 28  
  * @author Christian Laireiter
 29  
  */
 30  
 public final class AudioStreamChunk extends StreamChunk {
 31  
     /**
 32  
      * Stores the hex values of codec identifiers to their descriptions. <br>
 33  
      */
 34  4
     public final static String[][] CODEC_DESCRIPTIONS = {
 35  
             { "161", " (Windows Media Audio (ver 7,8,9))" },
 36  
             { "162", " (Windows Media Audio 9 series (Professional))" },
 37  
             { "163", "(Windows Media Audio 9 series (Lossless))" },
 38  
             { "7A21", " (GSM-AMR (CBR))" }, { "7A22", " (GSM-AMR (VBR))" } };
 39  
     /**
 40  
      * Stores the audio codec number for WMA
 41  
      */
 42  
     public final static long WMA = 0x161;
 43  
     /**
 44  
      * Stores the audio codec number for WMA (CBR)
 45  
      */
 46  
     public final static long WMA_CBR = 0x7A21;
 47  
     /**
 48  
      * Stores the audio codec number for WMA_LOSSLESS
 49  
      */
 50  
     public final static long WMA_LOSSLESS = 0x163;
 51  
     /**
 52  
      * Stores the audio codec number for WMA_PRO
 53  
      */
 54  
     public final static long WMA_PRO = 0x162;
 55  
 
 56  
     /**
 57  
      * Stores the audio codec number for WMA (VBR)
 58  
      */
 59  
     public final static long WMA_VBR = 0x7A22;
 60  
 
 61  
     /**
 62  
      * Stores the average amount of bytes used by audio stream. <br>
 63  
      * This value is a field within type specific data of audio stream. Maybe it
 64  
      * could be used to calculate the KBPs.
 65  
      */
 66  
     private long averageBytesPerSec;
 67  
 
 68  
     /**
 69  
      * Amount of bits used per sample. <br>
 70  
      */
 71  
     private int bitsPerSample;
 72  
 
 73  
     /**
 74  
      * The block alignment of the audio data.
 75  
      */
 76  
     private long blockAlignment;
 77  
 
 78  
     /**
 79  
      * Number of channels.
 80  
      */
 81  
     private long channelCount;
 82  
 
 83  
     /**
 84  
      * Some data which needs to be interpreted if the codec is handled.
 85  
      */
 86  282
     private byte[] codecData = new byte[0];
 87  
 
 88  
     /**
 89  
      * The audio compression format code.
 90  
      */
 91  
     private long compressionFormat;
 92  
 
 93  
     /**
 94  
      * this field stores the error concealment type.
 95  
      */
 96  
     private GUID errorConcealment;
 97  
 
 98  
     /**
 99  
      * Sampling rate of audio stream.
 100  
      */
 101  
     private long samplingRate;
 102  
 
 103  
     /**
 104  
      * Creates an instance.
 105  
      * 
 106  
      * @param chunkLen
 107  
      *            Length of the entire chunk (including guid and size)
 108  
      */
 109  
     public AudioStreamChunk(final BigInteger chunkLen) {
 110  282
         super(GUID.GUID_AUDIOSTREAM, chunkLen);
 111  282
     }
 112  
 
 113  
     /**
 114  
      * @return Returns the averageBytesPerSec.
 115  
      */
 116  
     public long getAverageBytesPerSec() {
 117  198
         return this.averageBytesPerSec;
 118  
     }
 119  
 
 120  
     /**
 121  
      * @return Returns the bitsPerSample.
 122  
      */
 123  
     public int getBitsPerSample() {
 124  0
         return this.bitsPerSample;
 125  
     }
 126  
 
 127  
     /**
 128  
      * @return Returns the blockAlignment.
 129  
      */
 130  
     public long getBlockAlignment() {
 131  0
         return this.blockAlignment;
 132  
     }
 133  
 
 134  
     /**
 135  
      * @return Returns the channelCount.
 136  
      */
 137  
     public long getChannelCount() {
 138  198
         return this.channelCount;
 139  
     }
 140  
 
 141  
     /**
 142  
      * @return Returns the codecData.
 143  
      */
 144  
     public byte[] getCodecData() {
 145  0
         return this.codecData.clone();
 146  
     }
 147  
 
 148  
     /**
 149  
      * This method will take a look at {@link #compressionFormat}and returns a
 150  
      * String with its hex value and if known a textual note on what coded it
 151  
      * represents. <br>
 152  
      * 
 153  
      * @return A description for the used codec.
 154  
      */
 155  
     public String getCodecDescription() {
 156  198
         final StringBuilder result = new StringBuilder(Long
 157  
                 .toHexString(getCompressionFormat()));
 158  198
         String furtherDesc = " (Unknown)";
 159  210
         for (final String[] aCODEC_DESCRIPTIONS : CODEC_DESCRIPTIONS) {
 160  210
             if (aCODEC_DESCRIPTIONS[0].equalsIgnoreCase(result.toString())) {
 161  198
                 furtherDesc = aCODEC_DESCRIPTIONS[1];
 162  198
                 break;
 163  
             }
 164  
         }
 165  198
         if (result.length() % 2 == 0) {
 166  0
             result.insert(0, "0x");
 167  
         } else {
 168  198
             result.insert(0, "0x0");
 169  
         }
 170  198
         result.append(furtherDesc);
 171  198
         return result.toString();
 172  
     }
 173  
 
 174  
     /**
 175  
      * @return Returns the compressionFormat.
 176  
      */
 177  
     public long getCompressionFormat() {
 178  396
         return this.compressionFormat;
 179  
     }
 180  
 
 181  
     /**
 182  
      * @return Returns the errorConcealment.
 183  
      */
 184  
     public GUID getErrorConcealment() {
 185  0
         return this.errorConcealment;
 186  
     }
 187  
 
 188  
     /**
 189  
      * This method takes the value of {@link #getAverageBytesPerSec()}and
 190  
      * calculates the kbps out of it, by simply multiplying by 8 and dividing by
 191  
      * 1000. <br>
 192  
      * 
 193  
      * @return amount of bits per second in kilo bits.
 194  
      */
 195  
     public int getKbps() {
 196  198
         return (int) getAverageBytesPerSec() * 8 / 1000;
 197  
     }
 198  
 
 199  
     /**
 200  
      * @return Returns the samplingRate.
 201  
      */
 202  
     public long getSamplingRate() {
 203  198
         return this.samplingRate;
 204  
     }
 205  
 
 206  
     /**
 207  
      * This mehtod returns whether the audio stream data is error concealed. <br>
 208  
      * For now only interleaved concealment is known. <br>
 209  
      * 
 210  
      * @return <code>true</code> if error concealment is used.
 211  
      */
 212  
     public boolean isErrorConcealed() {
 213  0
         return getErrorConcealment().equals(
 214  
                 GUID.GUID_AUDIO_ERROR_CONCEALEMENT_INTERLEAVED);
 215  
     }
 216  
 
 217  
     /**
 218  
      * 
 219  
      * {@inheritDoc}
 220  
      */
 221  
     @Override
 222  
     public String prettyPrint(final String prefix) {
 223  0
         final StringBuilder result = new StringBuilder(super.prettyPrint(prefix));
 224  0
         result.append(prefix).append("  |-> Audio info:").append(
 225  
                 Utils.LINE_SEPARATOR);
 226  0
         result.append(prefix).append("  |  : Bitrate : ").append(getKbps())
 227  
                 .append(Utils.LINE_SEPARATOR);
 228  0
         result.append(prefix).append("  |  : Channels : ").append(
 229  
                 getChannelCount()).append(" at ").append(getSamplingRate())
 230  
                 .append(" Hz").append(Utils.LINE_SEPARATOR);
 231  0
         result.append(prefix).append("  |  : Bits per Sample: ").append(
 232  
                 getBitsPerSample()).append(Utils.LINE_SEPARATOR);
 233  0
         result.append(prefix).append("  |  : Formatcode: ").append(
 234  
                 getCodecDescription()).append(Utils.LINE_SEPARATOR);
 235  0
         return result.toString();
 236  
     }
 237  
 
 238  
     /**
 239  
      * @param avgeBytesPerSec
 240  
      *            The averageBytesPerSec to set.
 241  
      */
 242  
     public void setAverageBytesPerSec(final long avgeBytesPerSec) {
 243  278
         this.averageBytesPerSec = avgeBytesPerSec;
 244  278
     }
 245  
 
 246  
     /**
 247  
      * Sets the bitsPerSample
 248  
      * 
 249  
      * @param bps
 250  
      */
 251  
     public void setBitsPerSample(final int bps) {
 252  278
         this.bitsPerSample = bps;
 253  278
     }
 254  
 
 255  
     /**
 256  
      * Sets the blockAlignment.
 257  
      * 
 258  
      * @param align
 259  
      */
 260  
     public void setBlockAlignment(final long align) {
 261  278
         this.blockAlignment = align;
 262  278
     }
 263  
 
 264  
     /**
 265  
      * @param channels
 266  
      *            The channelCount to set.
 267  
      */
 268  
     public void setChannelCount(final long channels) {
 269  278
         this.channelCount = channels;
 270  278
     }
 271  
 
 272  
     /**
 273  
      * Sets the codecData
 274  
      * 
 275  
      * @param codecSpecificData
 276  
      */
 277  
     public void setCodecData(final byte[] codecSpecificData) {
 278  278
         if (codecSpecificData == null) {
 279  0
             throw new IllegalArgumentException();
 280  
         }
 281  278
         this.codecData = codecSpecificData.clone();
 282  278
     }
 283  
 
 284  
     /**
 285  
      * @param cFormatCode
 286  
      *            The compressionFormat to set.
 287  
      */
 288  
     public void setCompressionFormat(final long cFormatCode) {
 289  278
         this.compressionFormat = cFormatCode;
 290  278
     }
 291  
 
 292  
     /**
 293  
      * This method sets the error concealment type which is given by two GUIDs. <br>
 294  
      * 
 295  
      * @param errConc
 296  
      *            the type of error concealment the audio stream is stored as.
 297  
      */
 298  
     public void setErrorConcealment(final GUID errConc) {
 299  278
         this.errorConcealment = errConc;
 300  278
     }
 301  
 
 302  
     /**
 303  
      * @param sampRate
 304  
      *            The samplingRate to set.
 305  
      */
 306  
     public void setSamplingRate(final long sampRate) {
 307  278
         this.samplingRate = sampRate;
 308  278
     }
 309  
 }