| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.jaudiotagger.audio.flac.metadatablock; |
| 20 | |
|
| 21 | |
import java.io.IOException; |
| 22 | |
import java.io.RandomAccessFile; |
| 23 | |
import java.nio.ByteBuffer; |
| 24 | |
import java.util.logging.Logger; |
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public class MetadataBlockDataStreamInfo |
| 51 | |
{ |
| 52 | |
public static final int STREAM_INFO_DATA_LENGTH = 34; |
| 53 | |
|
| 54 | |
|
| 55 | 5 | public static Logger logger = Logger.getLogger("org.jaudiotagger.audio.flac.MetadataBlockDataStreamInfo"); |
| 56 | |
|
| 57 | |
private int minBlockSize, maxBlockSize, minFrameSize, maxFrameSize, samplingRate, samplingRatePerChannel, bitsPerSample, channelNumber, totalNumberOfSamples; |
| 58 | |
private float length; |
| 59 | 23 | private boolean isValid = true; |
| 60 | |
|
| 61 | |
public MetadataBlockDataStreamInfo(MetadataBlockHeader header, RandomAccessFile raf) throws IOException |
| 62 | 23 | { |
| 63 | 23 | ByteBuffer rawdata = ByteBuffer.allocate(header.getDataLength()); |
| 64 | 23 | int bytesRead = raf.getChannel().read(rawdata); |
| 65 | 23 | if (bytesRead < header.getDataLength()) |
| 66 | |
{ |
| 67 | 0 | throw new IOException("Unable to read required number of databytes read:" + bytesRead + ":required:" + header.getDataLength()); |
| 68 | |
} |
| 69 | 23 | rawdata.rewind(); |
| 70 | |
|
| 71 | 23 | minBlockSize = rawdata.getShort(); |
| 72 | 23 | maxBlockSize = rawdata.getShort(); |
| 73 | 23 | minFrameSize = readThreeByteInteger(rawdata.get(), rawdata.get(), rawdata.get()); |
| 74 | 23 | maxFrameSize = readThreeByteInteger(rawdata.get(), rawdata.get(), rawdata.get()); |
| 75 | |
|
| 76 | 23 | samplingRate = readSamplingRate(rawdata.get(), rawdata.get(), rawdata.get()); |
| 77 | 23 | channelNumber = ((u(rawdata.get(12)) & 0x0E) >>> 1) + 1; |
| 78 | 23 | samplingRatePerChannel = samplingRate / channelNumber; |
| 79 | 23 | bitsPerSample = ((u(rawdata.get(12)) & 0x01) << 4) + ((u(rawdata.get(13)) & 0xF0) >>> 4) + 1; |
| 80 | |
|
| 81 | 23 | totalNumberOfSamples = readTotalNumberOfSamples(rawdata.get(13), rawdata.get(14), rawdata.get(15), rawdata.get(16), rawdata.get(17)); |
| 82 | |
|
| 83 | 23 | length = (float) ((double) totalNumberOfSamples / samplingRate); |
| 84 | 23 | logger.info(this.toString()); |
| 85 | 23 | } |
| 86 | |
|
| 87 | |
public String toString() |
| 88 | |
{ |
| 89 | |
|
| 90 | 23 | return "MinBlockSize:" + minBlockSize + "MaxBlockSize:" + maxBlockSize + "MinFrameSize:" + minFrameSize + "MaxFrameSize:" + maxFrameSize + "SampleRateTotal:" + samplingRate + "SampleRatePerChannel:" + samplingRatePerChannel + ":Channel number:" + channelNumber + ":Bits per sample: " + bitsPerSample + ":TotalNumberOfSamples: " + totalNumberOfSamples + ":Length: " + length; |
| 91 | |
|
| 92 | |
} |
| 93 | |
|
| 94 | |
public int getLength() |
| 95 | |
{ |
| 96 | 23 | return (int) length; |
| 97 | |
} |
| 98 | |
|
| 99 | |
public float getPreciseLength() |
| 100 | |
{ |
| 101 | 46 | return length; |
| 102 | |
} |
| 103 | |
|
| 104 | |
public int getChannelNumber() |
| 105 | |
{ |
| 106 | 23 | return channelNumber; |
| 107 | |
} |
| 108 | |
|
| 109 | |
public int getSamplingRate() |
| 110 | |
{ |
| 111 | 23 | return samplingRate; |
| 112 | |
} |
| 113 | |
|
| 114 | |
public int getSamplingRatePerChannel() |
| 115 | |
{ |
| 116 | 0 | return samplingRatePerChannel; |
| 117 | |
} |
| 118 | |
|
| 119 | |
public String getEncodingType() |
| 120 | |
{ |
| 121 | 23 | return "FLAC " + bitsPerSample + " bits"; |
| 122 | |
} |
| 123 | |
|
| 124 | |
public boolean isValid() |
| 125 | |
{ |
| 126 | 23 | return isValid; |
| 127 | |
} |
| 128 | |
|
| 129 | |
private int readThreeByteInteger(byte b1, byte b2, byte b3) |
| 130 | |
{ |
| 131 | 46 | int rate = (u(b1) << 16) + (u(b2) << 8) + (u(b3)); |
| 132 | 46 | return rate; |
| 133 | |
} |
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | |
private int readSamplingRate(byte b1, byte b2, byte b3) |
| 138 | |
{ |
| 139 | 23 | int rate = (u(b1) << 12) + (u(b2) << 4) + ((u(b3) & 0xF0) >>> 4); |
| 140 | 23 | return rate; |
| 141 | |
|
| 142 | |
} |
| 143 | |
|
| 144 | |
private int readTotalNumberOfSamples(byte b1, byte b2, byte b3, byte b4, byte b5) |
| 145 | |
{ |
| 146 | 23 | int nb = u(b5); |
| 147 | 23 | nb += u(b4) << 8; |
| 148 | 23 | nb += u(b3) << 16; |
| 149 | 23 | nb += u(b2) << 24; |
| 150 | 23 | nb += (u(b1) & 0x0F) << 32; |
| 151 | 23 | return nb; |
| 152 | |
} |
| 153 | |
|
| 154 | |
private int u(int i) |
| 155 | |
{ |
| 156 | 391 | return i & 0xFF; |
| 157 | |
} |
| 158 | |
} |