2
* Entagged Audio Tag library
3
* Copyright (c) 2003-2005 Raphaël Slinckx <raphael@slinckx.net>
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.
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.
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
19
package org.jaudiotagger.audio.flac.metadatablock;
21
import java.io.IOException;
22
import java.io.RandomAccessFile;
23
import java.nio.ByteBuffer;
24
import java.util.logging.Logger;
29
* <p>This block has information about the whole stream, like sample rate, number of channels, total number of samples,
30
* etc. It must be present as the first metadata block in the stream. Other metadata blocks may follow, and ones
31
* that the decoder doesn't understand, it will skip.
34
* <16> The minimum block size (in samples) used in the stream.
35
* <16> The maximum block size (in samples) used in the stream. (Minimum blocksize == maximum blocksize) implies a fixed-blocksize stream.
36
* <24> The minimum frame size (in bytes) used in the stream. May be 0 to imply the value is not known.
37
* <24> The maximum frame size (in bytes) used in the stream. May be 0 to imply the value is not known.
38
* <20> Sample rate in Hz. Though 20 bits are available, the maximum sample rate is limited by the structure of frame headers to 655350Hz. Also,
39
* a value of 0 is invalid.
40
* <3> (number of channels)-1. FLAC supports from 1 to 8 channels
41
* <5> (bits per sample)-1. FLAC supports from 4 to 32 bits per sample. Currently the reference encoder and decoders only support up to 24 bits per sample.
42
* <36> Total samples in stream. 'Samples' means inter-channel sample,
43
* i.e. one second of 44.1Khz audio will have 44100 samples regardless of the number of channels.
44
* A value of zero here means the number of total samples is unknown.
45
* <128> MD5 signature of the unencoded audio data. This allows the decoder to determine if an error exists in the audio data
46
* even when the error does not result in an invalid bitstream.
48
* * FLAC specifies a minimum block size of 16 and a maximum block size of 65535, meaning the bit patterns corresponding to the numbers 0-15 in the minimum blocksize and maximum blocksize fields are invalid.
50
public class MetadataBlockDataStreamInfo
52
public static final int STREAM_INFO_DATA_LENGTH = 34;
55
public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.flac.MetadataBlockDataStreamInfo");
57
private int minBlockSize, maxBlockSize, minFrameSize, maxFrameSize, samplingRate, samplingRatePerChannel, bitsPerSample, channelNumber, totalNumberOfSamples;
59
private boolean isValid = true;
61
public MetadataBlockDataStreamInfo(MetadataBlockHeader header, RandomAccessFile raf) throws IOException
63
ByteBuffer rawdata = ByteBuffer.allocate(header.getDataLength());
64
int bytesRead = raf.getChannel().read(rawdata);
65
if (bytesRead < header.getDataLength())
67
throw new IOException("Unable to read required number of databytes read:" + bytesRead + ":required:" + header.getDataLength());
71
minBlockSize = rawdata.getShort();
72
maxBlockSize = rawdata.getShort();
73
minFrameSize = readThreeByteInteger(rawdata.get(), rawdata.get(), rawdata.get());
74
maxFrameSize = readThreeByteInteger(rawdata.get(), rawdata.get(), rawdata.get());
76
samplingRate = readSamplingRate(rawdata.get(), rawdata.get(), rawdata.get());
77
channelNumber = ((u(rawdata.get(12)) & 0x0E) >>> 1) + 1;
78
samplingRatePerChannel = samplingRate / channelNumber;
79
bitsPerSample = ((u(rawdata.get(12)) & 0x01) << 4) + ((u(rawdata.get(13)) & 0xF0) >>> 4) + 1;
81
totalNumberOfSamples = readTotalNumberOfSamples(rawdata.get(13), rawdata.get(14), rawdata.get(15), rawdata.get(16), rawdata.get(17));
83
length = (float) ((double) totalNumberOfSamples / samplingRate);
84
logger.info(this.toString());
87
public String toString()
90
return "MinBlockSize:" + minBlockSize + "MaxBlockSize:" + maxBlockSize + "MinFrameSize:" + minFrameSize + "MaxFrameSize:" + maxFrameSize + "SampleRateTotal:" + samplingRate + "SampleRatePerChannel:" + samplingRatePerChannel + ":Channel number:" + channelNumber + ":Bits per sample: " + bitsPerSample + ":TotalNumberOfSamples: " + totalNumberOfSamples + ":Length: " + length;
94
public int getLength()
99
public float getPreciseLength()
104
public int getChannelNumber()
106
return channelNumber;
109
public int getSamplingRate()
114
public int getSamplingRatePerChannel()
116
return samplingRatePerChannel;
119
public String getEncodingType()
121
return "FLAC " + bitsPerSample + " bits";
124
public boolean isValid()
129
private int readThreeByteInteger(byte b1, byte b2, byte b3)
131
int rate = (u(b1) << 16) + (u(b2) << 8) + (u(b3));
135
//TODO this code seems to be give a sampling rate over 21 bytes instead of 20 bytes but attempt to change
136
//to 21 bytes give wrong value
137
private int readSamplingRate(byte b1, byte b2, byte b3)
139
int rate = (u(b1) << 12) + (u(b2) << 4) + ((u(b3) & 0xF0) >>> 4);
144
private int readTotalNumberOfSamples(byte b1, byte b2, byte b3, byte b4, byte b5)
150
nb += (u(b1) & 0x0F) << 32;
2
* Entagged Audio Tag library
3
* Copyright (c) 2003-2005 Raphaël Slinckx <raphael@slinckx.net>
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.
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.
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
19
package org.jaudiotagger.audio.flac.metadatablock;
21
import java.io.IOException;
22
import java.io.RandomAccessFile;
23
import java.nio.ByteBuffer;
24
import java.util.logging.Logger;
29
* <p>This block has information about the whole stream, like sample rate, number of channels, total number of samples,
30
* etc. It must be present as the first metadata block in the stream. Other metadata blocks may follow, and ones
31
* that the decoder doesn't understand, it will skip.
34
* <16> The minimum block size (in samples) used in the stream.
35
* <16> The maximum block size (in samples) used in the stream. (Minimum blocksize == maximum blocksize) implies a fixed-blocksize stream.
36
* <24> The minimum frame size (in bytes) used in the stream. May be 0 to imply the value is not known.
37
* <24> The maximum frame size (in bytes) used in the stream. May be 0 to imply the value is not known.
38
* <20> Sample rate in Hz. Though 20 bits are available, the maximum sample rate is limited by the structure of frame headers to 655350Hz. Also,
39
* a value of 0 is invalid.
40
* <3> (number of channels)-1. FLAC supports from 1 to 8 channels
41
* <5> (bits per sample)-1. FLAC supports from 4 to 32 bits per sample. Currently the reference encoder and decoders only support up to 24 bits per sample.
42
* <36> Total samples in stream. 'Samples' means inter-channel sample,
43
* i.e. one second of 44.1Khz audio will have 44100 samples regardless of the number of channels.
44
* A value of zero here means the number of total samples is unknown.
45
* <128> MD5 signature of the unencoded audio data. This allows the decoder to determine if an error exists in the audio data
46
* even when the error does not result in an invalid bitstream.
48
* * FLAC specifies a minimum block size of 16 and a maximum block size of 65535, meaning the bit patterns corresponding to the numbers 0-15 in the minimum blocksize and maximum blocksize fields are invalid.
50
public class MetadataBlockDataStreamInfo implements MetadataBlockData
52
public static final int STREAM_INFO_DATA_LENGTH = 34;
55
public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.flac.MetadataBlockDataStreamInfo");
57
private int minBlockSize, maxBlockSize, minFrameSize, maxFrameSize, samplingRate, samplingRatePerChannel, bitsPerSample, channelNumber, totalNumberOfSamples;
58
private float songLength;
59
private boolean isValid = true;
61
private ByteBuffer rawdata;
63
public MetadataBlockDataStreamInfo(MetadataBlockHeader header, RandomAccessFile raf) throws IOException
65
rawdata = ByteBuffer.allocate(header.getDataLength());
66
int bytesRead = raf.getChannel().read(rawdata);
67
if (bytesRead < header.getDataLength())
69
throw new IOException("Unable to read required number of databytes read:" + bytesRead + ":required:" + header.getDataLength());
73
minBlockSize = rawdata.getShort();
74
maxBlockSize = rawdata.getShort();
75
minFrameSize = readThreeByteInteger(rawdata.get(), rawdata.get(), rawdata.get());
76
maxFrameSize = readThreeByteInteger(rawdata.get(), rawdata.get(), rawdata.get());
78
samplingRate = readSamplingRate(rawdata.get(), rawdata.get(), rawdata.get());
79
channelNumber = ((u(rawdata.get(12)) & 0x0E) >>> 1) + 1;
80
samplingRatePerChannel = samplingRate / channelNumber;
81
bitsPerSample = ((u(rawdata.get(12)) & 0x01) << 4) + ((u(rawdata.get(13)) & 0xF0) >>> 4) + 1;
83
totalNumberOfSamples = readTotalNumberOfSamples(rawdata.get(13), rawdata.get(14), rawdata.get(15), rawdata.get(16), rawdata.get(17));
85
songLength = (float) ((double) totalNumberOfSamples / samplingRate);
86
logger.info(this.toString());
90
* @return the rawdata as it will be written to file
92
public byte[] getBytes()
94
return rawdata.array();
97
public int getLength()
99
return getBytes().length;
103
public String toString()
106
return "MinBlockSize:" + minBlockSize + "MaxBlockSize:" + maxBlockSize + "MinFrameSize:" + minFrameSize + "MaxFrameSize:" + maxFrameSize + "SampleRateTotal:" + samplingRate + "SampleRatePerChannel:" + samplingRatePerChannel + ":Channel number:" + channelNumber + ":Bits per sample: " + bitsPerSample + ":TotalNumberOfSamples: " + totalNumberOfSamples + ":Length: " + songLength;
110
public int getSongLength()
112
return (int) songLength;
115
public float getPreciseLength()
120
public int getChannelNumber()
122
return channelNumber;
125
public int getSamplingRate()
130
public int getSamplingRatePerChannel()
132
return samplingRatePerChannel;
135
public String getEncodingType()
137
return "FLAC " + bitsPerSample + " bits";
140
public boolean isValid()
145
private int readThreeByteInteger(byte b1, byte b2, byte b3)
147
int rate = (u(b1) << 16) + (u(b2) << 8) + (u(b3));
151
//TODO this code seems to be give a sampling rate over 21 bytes instead of 20 bytes but attempt to change
152
//to 21 bytes give wrong value
153
private int readSamplingRate(byte b1, byte b2, byte b3)
155
int rate = (u(b1) << 12) + (u(b2) << 4) + ((u(b3) & 0xF0) >>> 4);
160
private int readTotalNumberOfSamples(byte b1, byte b2, byte b3, byte b4, byte b5)
166
nb += (u(b1) & 0x0F) << 32;