Coverage Report - org.jaudiotagger.audio.ogg.util.VorbisIdentificationHeader
 
Classes in this File Line Coverage Branch Coverage Complexity
VorbisIdentificationHeader
97%
31/32
50%
3/6
1.2
 
 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.ogg.util;
 20  
 
 21  
 import org.jaudiotagger.audio.generic.Utils;
 22  
 import org.jaudiotagger.audio.ogg.VorbisVersion;
 23  
 
 24  
 import java.util.logging.Logger;
 25  
 
 26  
 
 27  
 /**
 28  
  * Vorbis Identification header
 29  
  * <p/>
 30  
  * From http://xiph.org/vorbis/doc/Vorbis_I_spec.html#id326710
 31  
  * <p/>
 32  
  * The identification header is a short header of only a few fields used to declare the stream definitively as Vorbis,
 33  
  * and provide a few externally relevant pieces of information about the audio stream. The identification header is
 34  
  * coded as follows:
 35  
  * <p/>
 36  
  * 1) [vorbis_version] = read 32 bits as unsigned integer
 37  
  * 2) [audio_channels] = read 8 bit integer as unsigned
 38  
  * 3) [audio_sample_rate] = read 32 bits as unsigned integer
 39  
  * 4) [bitrate_maximum] = read 32 bits as signed integer
 40  
  * 5) [bitrate_nominal] = read 32 bits as signed integer
 41  
  * 6) [bitrate_minimum] = read 32 bits as signed integer
 42  
  * 7) [blocksize_0] = 2 exponent (read 4 bits as unsigned integer)
 43  
  * 8) [blocksize_1] = 2 exponent (read 4 bits as unsigned integer)
 44  
  * 9) [framing_flag] = read one bit
 45  
  * <p/>
 46  
  * $Id: VorbisIdentificationHeader.java,v 1.7 2008/07/21 10:48:37 paultaylor Exp $
 47  
  *
 48  
  * @author Raphael Slinckx (KiKiDonK)
 49  
  * @version 16 d�cembre 2003
 50  
  */
 51  
 public class VorbisIdentificationHeader implements VorbisHeader
 52  
 {
 53  
     // Logger Object
 54  6
     public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.ogg.atom");
 55  
 
 56  
     private int audioChannels;
 57  43
     private boolean isValid = false;
 58  
 
 59  
     private int vorbisVersion, audioSampleRate;
 60  
     private int bitrateMinimal, bitrateNominal, bitrateMaximal;
 61  
 
 62  
     public static final int FIELD_VORBIS_VERSION_POS = 7;
 63  
     public static final int FIELD_AUDIO_CHANNELS_POS = 11;
 64  
     public static final int FIELD_AUDIO_SAMPLE_RATE_POS = 12;
 65  
     public static final int FIELD_BITRATE_MAX_POS = 16;
 66  
     public static final int FIELD_BITRATE_NOMAIML_POS = 20;
 67  
     public static final int FIELD_BITRATE_MIN_POS = 24;
 68  
     public static final int FIELD_BLOCKSIZE_POS = 28;
 69  
     public static final int FIELD_FRAMING_FLAG_POS = 29;
 70  
 
 71  
     public static final int FIELD_VORBIS_VERSION_LENGTH = 4;
 72  
     public static final int FIELD_AUDIO_CHANNELS_LENGTH = 1;
 73  
     public static final int FIELD_AUDIO_SAMPLE_RATE_LENGTH = 4;
 74  
     public static final int FIELD_BITRATE_MAX_LENGTH = 4;
 75  
     public static final int FIELD_BITRATE_NOMAIML_LENGTH = 4;
 76  
     public static final int FIELD_BITRATE_MIN_LENGTH = 4;
 77  
     public static final int FIELD_BLOCKSIZE_LENGTH = 1;
 78  
     public static final int FIELD_FRAMING_FLAG_LENGTH = 1;
 79  
 
 80  
 
 81  
     public VorbisIdentificationHeader(byte[] vorbisData)
 82  43
     {
 83  43
         decodeHeader(vorbisData);
 84  43
     }
 85  
 
 86  
 
 87  
     public int getChannelNumber()
 88  
     {
 89  43
         return audioChannels;
 90  
     }
 91  
 
 92  
 
 93  
     public String getEncodingType()
 94  
     {
 95  43
         return VorbisVersion.values()[vorbisVersion].toString();
 96  
     }
 97  
 
 98  
 
 99  
     public int getSamplingRate()
 100  
     {
 101  86
         return audioSampleRate;
 102  
     }
 103  
 
 104  
     public int getNominalBitrate()
 105  
     {
 106  172
         return bitrateNominal;
 107  
     }
 108  
 
 109  
     public int getMaxBitrate()
 110  
     {
 111  86
         return bitrateMaximal;
 112  
     }
 113  
 
 114  
     public int getMinBitrate()
 115  
     {
 116  43
         return bitrateMinimal;
 117  
     }
 118  
 
 119  
     public boolean isValid()
 120  
     {
 121  0
         return isValid;
 122  
     }
 123  
 
 124  
 
 125  
     public void decodeHeader(byte[] b)
 126  
     {
 127  43
         int packetType = b[FIELD_PACKET_TYPE_POS];
 128  43
         logger.fine("packetType" + packetType);
 129  43
         String vorbis = Utils.getString(b, VorbisHeader.FIELD_CAPTURE_PATTERN_POS, VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH, "ISO-8859-1");
 130  
 
 131  43
         if (packetType == VorbisPacketType.IDENTIFICATION_HEADER.getType() && vorbis.equals(CAPTURE_PATTERN))
 132  
         {
 133  43
             this.vorbisVersion = b[7] + (b[8] << 8) + (b[9] << 16) + (b[10] << 24);
 134  43
             logger.fine("vorbisVersion" + vorbisVersion);
 135  43
             this.audioChannels = u(b[FIELD_AUDIO_CHANNELS_POS]);
 136  43
             logger.fine("audioChannels" + audioChannels);
 137  43
             this.audioSampleRate = u(b[12]) + (u(b[13]) << 8) + (u(b[14]) << 16) + (u(b[15]) << 24);
 138  43
             logger.fine("audioSampleRate" + audioSampleRate);
 139  43
             logger.fine("audioSampleRate" + b[12] + " " + b[13] + " " + b[14]);
 140  
 
 141  
             //TODO is this right spec says signed
 142  43
             this.bitrateMinimal = u(b[16]) + (u(b[17]) << 8) + (u(b[18]) << 16) + (u(b[19]) << 24);
 143  43
             this.bitrateNominal = u(b[20]) + (u(b[21]) << 8) + (u(b[22]) << 16) + (u(b[23]) << 24);
 144  43
             this.bitrateMaximal = u(b[24]) + (u(b[25]) << 8) + (u(b[26]) << 16) + (u(b[27]) << 24);
 145  
             //byte blockSize0 = (byte) ( b[28] & 240 );
 146  
             //byte blockSize1 = (byte) ( b[28] & 15 );
 147  
 
 148  43
             int framingFlag = b[FIELD_FRAMING_FLAG_POS];
 149  43
             logger.fine("framingFlag" + framingFlag);
 150  43
             if (framingFlag != 0)
 151  
             {
 152  43
                 isValid = true;
 153  
             }
 154  
 
 155  
         }
 156  43
     }
 157  
 
 158  
     private int u(int i)
 159  
     {
 160  731
         return i & 0xFF;
 161  
     }
 162  
 }
 163