use of org.jaudiotagger.audio.flac.metadatablock.MetadataBlockHeader in project MusicDNA by harjot-oberai.
the class FlacTagCreator method convert.
/**
* @param tag
* @param paddingSize extra padding to be added
* @return
* @throws UnsupportedEncodingException
*/
public ByteBuffer convert(Tag tag, int paddingSize) throws UnsupportedEncodingException {
logger.config("Convert flac tag:padding:" + paddingSize);
FlacTag flacTag = (FlacTag) tag;
int tagLength = 0;
ByteBuffer vorbiscomment = null;
if (flacTag.getVorbisCommentTag() != null) {
vorbiscomment = creator.convert(flacTag.getVorbisCommentTag());
tagLength = vorbiscomment.capacity() + MetadataBlockHeader.HEADER_LENGTH;
}
for (MetadataBlockDataPicture image : flacTag.getImages()) {
tagLength += image.getBytes().length + MetadataBlockHeader.HEADER_LENGTH;
}
logger.config("Convert flac tag:taglength:" + tagLength);
ByteBuffer buf = ByteBuffer.allocate(tagLength + paddingSize);
MetadataBlockHeader vorbisHeader;
//If there are other metadata blocks
if (flacTag.getVorbisCommentTag() != null) {
if ((paddingSize > 0) || (flacTag.getImages().size() > 0)) {
vorbisHeader = new MetadataBlockHeader(false, BlockType.VORBIS_COMMENT, vorbiscomment.capacity());
} else {
vorbisHeader = new MetadataBlockHeader(true, BlockType.VORBIS_COMMENT, vorbiscomment.capacity());
}
buf.put(vorbisHeader.getBytes());
buf.put(vorbiscomment);
}
//Images
ListIterator<MetadataBlockDataPicture> li = flacTag.getImages().listIterator();
while (li.hasNext()) {
MetadataBlockDataPicture imageField = li.next();
MetadataBlockHeader imageHeader;
if (paddingSize > 0 || li.hasNext()) {
imageHeader = new MetadataBlockHeader(false, BlockType.PICTURE, imageField.getLength());
} else {
imageHeader = new MetadataBlockHeader(true, BlockType.PICTURE, imageField.getLength());
}
buf.put(imageHeader.getBytes());
buf.put(imageField.getBytes());
}
//Padding
logger.config("Convert flac tag at" + buf.position());
if (paddingSize > 0) {
int paddingDataSize = paddingSize - MetadataBlockHeader.HEADER_LENGTH;
MetadataBlockHeader paddingHeader = new MetadataBlockHeader(true, BlockType.PADDING, paddingDataSize);
MetadataBlockDataPadding padding = new MetadataBlockDataPadding(paddingDataSize);
buf.put(paddingHeader.getBytes());
buf.put(padding.getBytes());
}
buf.rewind();
return buf;
}
use of org.jaudiotagger.audio.flac.metadatablock.MetadataBlockHeader in project MusicDNA by harjot-oberai.
the class FlacTagReader method read.
public FlacTag read(RandomAccessFile raf) throws CannotReadException, IOException {
FlacStreamReader flacStream = new FlacStreamReader(raf);
flacStream.findStream();
//Hold the metadata
VorbisCommentTag tag = null;
List<MetadataBlockDataPicture> images = new ArrayList<MetadataBlockDataPicture>();
//Seems like we have a valid stream
boolean isLastBlock = false;
while (!isLastBlock) {
if (logger.isLoggable(Level.CONFIG)) {
logger.config("Looking for MetaBlockHeader at:" + raf.getFilePointer());
}
//Read the header
MetadataBlockHeader mbh = MetadataBlockHeader.readHeader(raf);
if (mbh == null) {
break;
}
if (logger.isLoggable(Level.CONFIG)) {
logger.config("Reading MetadataBlockHeader:" + mbh.toString() + " ending at " + raf.getFilePointer());
}
//JAUDIOTAGGER-466:CBlocktype can be null
if (mbh.getBlockType() != null) {
switch(mbh.getBlockType()) {
//We got a vorbiscomment comment block, parse it
case VORBIS_COMMENT:
byte[] commentHeaderRawPacket = new byte[mbh.getDataLength()];
raf.read(commentHeaderRawPacket);
tag = vorbisCommentReader.read(commentHeaderRawPacket, false);
break;
case PICTURE:
try {
MetadataBlockDataPicture mbdp = new MetadataBlockDataPicture(mbh, raf);
images.add(mbdp);
} catch (IOException ioe) {
logger.warning("Unable to read picture metablock, ignoring:" + ioe.getMessage());
} catch (InvalidFrameException ive) {
logger.warning("Unable to read picture metablock, ignoring" + ive.getMessage());
}
break;
//This is not a metadata block we are interested in so we skip to next block
default:
if (logger.isLoggable(Level.CONFIG)) {
logger.config("Ignoring MetadataBlock:" + mbh.getBlockType());
}
raf.seek(raf.getFilePointer() + mbh.getDataLength());
break;
}
}
isLastBlock = mbh.isLastBlock();
mbh = null;
}
//just initialize Flac with an empty VorbisTag
if (tag == null) {
tag = VorbisCommentTag.createNewTag();
}
FlacTag flacTag = new FlacTag(tag, images);
return flacTag;
}
use of org.jaudiotagger.audio.flac.metadatablock.MetadataBlockHeader in project MusicDNA by harjot-oberai.
the class FlacInfoReader method countMetaBlocks.
/**
* Count the number of metadatablocks, useful for debugging
*
* @param f
* @return
* @throws CannotReadException
* @throws IOException
*/
public int countMetaBlocks(File f) throws CannotReadException, IOException {
RandomAccessFile raf = new RandomAccessFile(f, "r");
FlacStreamReader flacStream = new FlacStreamReader(raf);
flacStream.findStream();
boolean isLastBlock = false;
int count = 0;
while (!isLastBlock) {
MetadataBlockHeader mbh = MetadataBlockHeader.readHeader(raf);
logger.config("Found block:" + mbh.getBlockType());
raf.seek(raf.getFilePointer() + mbh.getDataLength());
isLastBlock = mbh.isLastBlock();
//Free memory
mbh = null;
count++;
}
raf.close();
return count;
}
use of org.jaudiotagger.audio.flac.metadatablock.MetadataBlockHeader in project MusicDNA by harjot-oberai.
the class FlacInfoReader method read.
public FlacAudioHeader read(RandomAccessFile raf) throws CannotReadException, IOException {
FlacStreamReader flacStream = new FlacStreamReader(raf);
flacStream.findStream();
MetadataBlockDataStreamInfo mbdsi = null;
boolean isLastBlock = false;
//the bitrate
while (!isLastBlock) {
MetadataBlockHeader mbh = MetadataBlockHeader.readHeader(raf);
if (mbh.getBlockType() == BlockType.STREAMINFO) {
mbdsi = new MetadataBlockDataStreamInfo(mbh, raf);
if (!mbdsi.isValid()) {
throw new CannotReadException("FLAC StreamInfo not valid");
}
//TODO We have found streaminfo so do we need to continue checking, effects bitrate calc which is correct
//break;
} else {
raf.seek(raf.getFilePointer() + mbh.getDataLength());
}
isLastBlock = mbh.isLastBlock();
//Free memory
mbh = null;
}
if (mbdsi == null) {
throw new CannotReadException("Unable to find Flac StreamInfo");
}
FlacAudioHeader info = new FlacAudioHeader();
info.setLength(mbdsi.getSongLength());
info.setPreciseLength(mbdsi.getPreciseLength());
info.setChannelNumber(mbdsi.getChannelNumber());
info.setSamplingRate(mbdsi.getSamplingRate());
info.setBitsPerSample(mbdsi.getBitsPerSample());
info.setEncodingType(mbdsi.getEncodingType());
info.setExtraEncodingInfos("");
info.setBitrate(computeBitrate(mbdsi.getPreciseLength(), raf.length() - raf.getFilePointer()));
info.setLossless(true);
info.setMd5(mbdsi.getMD5Signature());
return info;
}
Aggregations