Search in sources :

Example 1 with MetadataBlockDataPicture

use of org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture 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;
}
Also used : MetadataBlockHeader(org.jaudiotagger.audio.flac.metadatablock.MetadataBlockHeader) FlacTag(org.jaudiotagger.tag.flac.FlacTag) ByteBuffer(java.nio.ByteBuffer) MetadataBlockDataPicture(org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture) MetadataBlockDataPadding(org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPadding)

Example 2 with MetadataBlockDataPicture

use of org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture 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;
}
Also used : MetadataBlockHeader(org.jaudiotagger.audio.flac.metadatablock.MetadataBlockHeader) FlacTag(org.jaudiotagger.tag.flac.FlacTag) ArrayList(java.util.ArrayList) IOException(java.io.IOException) VorbisCommentTag(org.jaudiotagger.tag.vorbiscomment.VorbisCommentTag) InvalidFrameException(org.jaudiotagger.tag.InvalidFrameException) MetadataBlockDataPicture(org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture)

Example 3 with MetadataBlockDataPicture

use of org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture in project MusicDNA by harjot-oberai.

the class FlacTag method getArtworkList.

public List<Artwork> getArtworkList() {
    List<Artwork> artworkList = new ArrayList<Artwork>(images.size());
    for (MetadataBlockDataPicture coverArt : images) {
        Artwork artwork = ArtworkFactory.createArtworkFromMetadataBlockDataPicture(coverArt);
        artworkList.add(artwork);
    }
    return artworkList;
}
Also used : Artwork(org.jaudiotagger.tag.images.Artwork) ArrayList(java.util.ArrayList) MetadataBlockDataPicture(org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture)

Example 4 with MetadataBlockDataPicture

use of org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture in project MusicDNA by harjot-oberai.

the class VorbisCommentTag method getArtworkList.

/**
     *
     * @return list of artwork images
     */
public List<Artwork> getArtworkList() {
    List<Artwork> artworkList = new ArrayList<Artwork>(1);
    //Read Old Format
    if (getArtworkBinaryData() != null & getArtworkBinaryData().length > 0) {
        Artwork artwork = ArtworkFactory.getNew();
        artwork.setMimeType(getArtworkMimeType());
        artwork.setBinaryData(getArtworkBinaryData());
        artworkList.add(artwork);
    }
    //New Format (Supports Multiple Images)
    List<TagField> metadataBlockPics = this.get(VorbisCommentFieldKey.METADATA_BLOCK_PICTURE);
    for (TagField tagField : metadataBlockPics) {
        try {
            byte[] imageBinaryData = Base64Coder.decode(((TagTextField) tagField).getContent());
            MetadataBlockDataPicture coverArt = new MetadataBlockDataPicture(ByteBuffer.wrap(imageBinaryData));
            Artwork artwork = ArtworkFactory.createArtworkFromMetadataBlockDataPicture(coverArt);
            artworkList.add(artwork);
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        } catch (InvalidFrameException ife) {
            throw new RuntimeException(ife);
        }
    }
    return artworkList;
}
Also used : Artwork(org.jaudiotagger.tag.images.Artwork) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MetadataBlockDataPicture(org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture)

Aggregations

MetadataBlockDataPicture (org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 MetadataBlockHeader (org.jaudiotagger.audio.flac.metadatablock.MetadataBlockHeader)2 FlacTag (org.jaudiotagger.tag.flac.FlacTag)2 Artwork (org.jaudiotagger.tag.images.Artwork)2 ByteBuffer (java.nio.ByteBuffer)1 MetadataBlockDataPadding (org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPadding)1 InvalidFrameException (org.jaudiotagger.tag.InvalidFrameException)1 VorbisCommentTag (org.jaudiotagger.tag.vorbiscomment.VorbisCommentTag)1