~ubuntu-branches/ubuntu/utopic/libjaudiotagger-java/utopic

« back to all changes in this revision

Viewing changes to src/org/jaudiotagger/audio/ogg/util/VorbisIdentificationHeader.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-04-28 23:52:43 UTC
  • mfrom: (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110428235243-pzalvw6lncis3ukf
Tags: 2.0.3-1
* d/control: Drop Depends on default-jre per Debian Java Policy as its
  a library package.
* d/watch: Fix to directly monitor SVN tags.
* Switch to 3.0 (quilt) format.
* Bump Standards-Version to 3.9.2 (no changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.8 2009/09/03 09:19:13 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
 
    public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.ogg.atom");
55
 
 
56
 
    private int audioChannels;
57
 
    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
 
    {
83
 
        decodeHeader(vorbisData);
84
 
    }
85
 
 
86
 
 
87
 
    public int getChannelNumber()
88
 
    {
89
 
        return audioChannels;
90
 
    }
91
 
 
92
 
 
93
 
    public String getEncodingType()
94
 
    {
95
 
        return VorbisVersion.values()[vorbisVersion].toString();
96
 
    }
97
 
 
98
 
 
99
 
    public int getSamplingRate()
100
 
    {
101
 
        return audioSampleRate;
102
 
    }
103
 
 
104
 
    public int getNominalBitrate()
105
 
    {
106
 
        return bitrateNominal;
107
 
    }
108
 
 
109
 
    public int getMaxBitrate()
110
 
    {
111
 
        return bitrateMaximal;
112
 
    }
113
 
 
114
 
    public int getMinBitrate()
115
 
    {
116
 
        return bitrateMinimal;
117
 
    }
118
 
 
119
 
    public boolean isValid()
120
 
    {
121
 
        return isValid;
122
 
    }
123
 
 
124
 
 
125
 
    public void decodeHeader(byte[] b)
126
 
    {
127
 
        int packetType = b[FIELD_PACKET_TYPE_POS];
128
 
        logger.fine("packetType" + packetType);
129
 
        String vorbis = Utils.getString(b, VorbisHeader.FIELD_CAPTURE_PATTERN_POS, VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH, "ISO-8859-1");
130
 
 
131
 
        if (packetType == VorbisPacketType.IDENTIFICATION_HEADER.getType() && vorbis.equals(CAPTURE_PATTERN))
132
 
        {
133
 
            this.vorbisVersion = b[7] + (b[8] << 8) + (b[9] << 16) + (b[10] << 24);
134
 
            logger.fine("vorbisVersion" + vorbisVersion);
135
 
            this.audioChannels = u(b[FIELD_AUDIO_CHANNELS_POS]);
136
 
            logger.fine("audioChannels" + audioChannels);
137
 
            this.audioSampleRate = u(b[12]) + (u(b[13]) << 8) + (u(b[14]) << 16) + (u(b[15]) << 24);
138
 
            logger.fine("audioSampleRate" + audioSampleRate);
139
 
            logger.fine("audioSampleRate" + b[12] + " " + b[13] + " " + b[14]);
140
 
 
141
 
            //TODO is this right spec says signed
142
 
            this.bitrateMinimal = u(b[16]) + (u(b[17]) << 8) + (u(b[18]) << 16) + (u(b[19]) << 24);
143
 
            this.bitrateNominal = u(b[20]) + (u(b[21]) << 8) + (u(b[22]) << 16) + (u(b[23]) << 24);
144
 
            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
 
            int framingFlag = b[FIELD_FRAMING_FLAG_POS];
149
 
            logger.fine("framingFlag" + framingFlag);
150
 
            if (framingFlag != 0)
151
 
            {
152
 
                isValid = true;
153
 
            }
154
 
 
155
 
        }
156
 
    }
157
 
 
158
 
    private int u(int i)
159
 
    {
160
 
        return i & 0xFF;
161
 
    }
162
 
}
163
 
 
 
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 813 2009-09-03 09:23:25Z paultaylor $
 
47
 *
 
48
 * @author Raphael Slinckx (KiKiDonK)
 
49
 * @version 16 d�cembre 2003
 
50
 */
 
51
public class VorbisIdentificationHeader implements VorbisHeader
 
52
{
 
53
    // Logger Object
 
54
    public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.ogg.atom");
 
55
 
 
56
    private int audioChannels;
 
57
    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
    {
 
83
        decodeHeader(vorbisData);
 
84
    }
 
85
 
 
86
 
 
87
    public int getChannelNumber()
 
88
    {
 
89
        return audioChannels;
 
90
    }
 
91
 
 
92
 
 
93
    public String getEncodingType()
 
94
    {
 
95
        return VorbisVersion.values()[vorbisVersion].toString();
 
96
    }
 
97
 
 
98
 
 
99
    public int getSamplingRate()
 
100
    {
 
101
        return audioSampleRate;
 
102
    }
 
103
 
 
104
    public int getNominalBitrate()
 
105
    {
 
106
        return bitrateNominal;
 
107
    }
 
108
 
 
109
    public int getMaxBitrate()
 
110
    {
 
111
        return bitrateMaximal;
 
112
    }
 
113
 
 
114
    public int getMinBitrate()
 
115
    {
 
116
        return bitrateMinimal;
 
117
    }
 
118
 
 
119
    public boolean isValid()
 
120
    {
 
121
        return isValid;
 
122
    }
 
123
 
 
124
 
 
125
    public void decodeHeader(byte[] b)
 
126
    {
 
127
        int packetType = b[FIELD_PACKET_TYPE_POS];
 
128
        logger.fine("packetType" + packetType);
 
129
        String vorbis = Utils.getString(b, VorbisHeader.FIELD_CAPTURE_PATTERN_POS, VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH, "ISO-8859-1");
 
130
 
 
131
        if (packetType == VorbisPacketType.IDENTIFICATION_HEADER.getType() && vorbis.equals(CAPTURE_PATTERN))
 
132
        {
 
133
            this.vorbisVersion = b[7] + (b[8] << 8) + (b[9] << 16) + (b[10] << 24);
 
134
            logger.fine("vorbisVersion" + vorbisVersion);
 
135
            this.audioChannels = u(b[FIELD_AUDIO_CHANNELS_POS]);
 
136
            logger.fine("audioChannels" + audioChannels);
 
137
            this.audioSampleRate = u(b[12]) + (u(b[13]) << 8) + (u(b[14]) << 16) + (u(b[15]) << 24);
 
138
            logger.fine("audioSampleRate" + audioSampleRate);
 
139
            logger.fine("audioSampleRate" + b[12] + " " + b[13] + " " + b[14]);
 
140
 
 
141
            //TODO is this right spec says signed
 
142
            this.bitrateMinimal = u(b[16]) + (u(b[17]) << 8) + (u(b[18]) << 16) + (u(b[19]) << 24);
 
143
            this.bitrateNominal = u(b[20]) + (u(b[21]) << 8) + (u(b[22]) << 16) + (u(b[23]) << 24);
 
144
            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
            int framingFlag = b[FIELD_FRAMING_FLAG_POS];
 
149
            logger.fine("framingFlag" + framingFlag);
 
150
            if (framingFlag != 0)
 
151
            {
 
152
                isValid = true;
 
153
            }
 
154
 
 
155
        }
 
156
    }
 
157
 
 
158
    private int u(int i)
 
159
    {
 
160
        return i & 0xFF;
 
161
    }
 
162
}
 
163