Search in sources :

Example 1 with OggPageHeader

use of org.jaudiotagger.audio.ogg.util.OggPageHeader in project MusicDNA by harjot-oberai.

the class OggVorbisTagReader method readOggVorbisHeaderSizes.

/**
     * Calculate the size of the packet data for the comment and setup headers
     *
     * @param raf
     * @return
     * @throws CannotReadException
     * @throws IOException
     */
public OggVorbisHeaderSizes readOggVorbisHeaderSizes(RandomAccessFile raf) throws CannotReadException, IOException {
    logger.fine("Started to read comment and setup header sizes:");
    //Stores filepointers so return file in same state
    long filepointer = raf.getFilePointer();
    //Extra Packets on same page as setup header
    List<OggPageHeader.PacketStartAndLength> extraPackets = new ArrayList<OggPageHeader.PacketStartAndLength>();
    long commentHeaderStartPosition;
    long setupHeaderStartPosition;
    int commentHeaderSize = 0;
    int setupHeaderSize;
    //1st page = codec infos
    OggPageHeader pageHeader = OggPageHeader.read(raf);
    //Skip over data to end of page header 1
    raf.seek(raf.getFilePointer() + pageHeader.getPageLength());
    //2nd page = comment, may extend to additional pages or not , may also have setup header
    pageHeader = OggPageHeader.read(raf);
    commentHeaderStartPosition = raf.getFilePointer() - (OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageHeader.getSegmentTable().length);
    //Now at start of packets on page 2 , check this is the vorbis comment header
    byte[] b = new byte[VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH];
    raf.read(b);
    if (!isVorbisCommentHeader(b)) {
        throw new CannotReadException("Cannot find comment block (no vorbiscomment header)");
    }
    raf.seek(raf.getFilePointer() - (VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH));
    logger.config("Found start of comment header at:" + raf.getFilePointer());
    //Calculate Comment Size (not inc header)
    while (true) {
        List<OggPageHeader.PacketStartAndLength> packetList = pageHeader.getPacketList();
        commentHeaderSize += packetList.get(0).getLength();
        raf.skipBytes(packetList.get(0).getLength());
        //end son this page and we can break
        if (packetList.size() > 1 || !pageHeader.isLastPacketIncomplete()) {
            //done comment size
            logger.config("Found end of comment:size:" + commentHeaderSize + "finishes at file position:" + raf.getFilePointer());
            break;
        }
        pageHeader = OggPageHeader.read(raf);
    }
    //If there are no more packets on this page we need to go to next page to get the setup header
    OggPageHeader.PacketStartAndLength packet;
    if (pageHeader.getPacketList().size() == 1) {
        pageHeader = OggPageHeader.read(raf);
        List<OggPageHeader.PacketStartAndLength> packetList = pageHeader.getPacketList();
        packet = pageHeader.getPacketList().get(0);
        //Now at start of next packet , check this is the vorbis setup header
        b = new byte[VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH];
        raf.read(b);
        if (!isVorbisSetupHeader(b)) {
            throw new CannotReadException(ErrorMessage.OGG_VORBIS_NO_VORBIS_HEADER_FOUND.getMsg());
        }
        raf.seek(raf.getFilePointer() - (VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH));
        logger.config("Found start of vorbis setup header at file position:" + raf.getFilePointer());
        //Set this to the  start of the OggPage that setupheader was found on
        setupHeaderStartPosition = raf.getFilePointer() - (OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageHeader.getSegmentTable().length);
        //Add packet data to size to the setup header size
        setupHeaderSize = packet.getLength();
        logger.fine("Adding:" + packet.getLength() + " to setup header size");
        //Skip over the packet data
        raf.skipBytes(packet.getLength());
        //got the size of the setup header.
        if (packetList.size() > 1 || !pageHeader.isLastPacketIncomplete()) {
            logger.config("Found end of setupheader:size:" + setupHeaderSize + "finishes at:" + raf.getFilePointer());
            if (packetList.size() > 1) {
                extraPackets = packetList.subList(1, packetList.size());
            }
        } else //The setup header continues onto the next page
        {
            pageHeader = OggPageHeader.read(raf);
            packetList = pageHeader.getPacketList();
            while (true) {
                setupHeaderSize += packetList.get(0).getLength();
                logger.fine("Adding:" + packetList.get(0).getLength() + " to setup header size");
                raf.skipBytes(packetList.get(0).getLength());
                if (packetList.size() > 1 || !pageHeader.isLastPacketIncomplete()) {
                    //done setup size
                    logger.fine("Found end of setupheader:size:" + setupHeaderSize + "finishes at:" + raf.getFilePointer());
                    if (packetList.size() > 1) {
                        extraPackets = packetList.subList(1, packetList.size());
                    }
                    break;
                }
                //Continues onto another page
                pageHeader = OggPageHeader.read(raf);
            }
        }
    } else //else its next packet on this page
    {
        packet = pageHeader.getPacketList().get(1);
        List<OggPageHeader.PacketStartAndLength> packetList = pageHeader.getPacketList();
        //Now at start of next packet , check this is the vorbis setup header
        b = new byte[VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH];
        raf.read(b);
        if (!isVorbisSetupHeader(b)) {
            logger.warning("Expecting but got:" + new String(b) + "at " + (raf.getFilePointer() - b.length));
            throw new CannotReadException(ErrorMessage.OGG_VORBIS_NO_VORBIS_HEADER_FOUND.getMsg());
        }
        raf.seek(raf.getFilePointer() - (VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH));
        logger.config("Found start of vorbis setup header at file position:" + raf.getFilePointer());
        //Set this to the  start of the OggPage that setupheader was found on
        setupHeaderStartPosition = raf.getFilePointer() - (OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageHeader.getSegmentTable().length) - pageHeader.getPacketList().get(0).getLength();
        //Add packet data to size to the setup header size
        setupHeaderSize = packet.getLength();
        logger.fine("Adding:" + packet.getLength() + " to setup header size");
        //Skip over the packet data
        raf.skipBytes(packet.getLength());
        //got the size of the setup header.
        if (packetList.size() > 2 || !pageHeader.isLastPacketIncomplete()) {
            logger.fine("Found end of setupheader:size:" + setupHeaderSize + "finishes at:" + raf.getFilePointer());
            if (packetList.size() > 2) {
                extraPackets = packetList.subList(2, packetList.size());
            }
        } else //The setup header continues onto the next page
        {
            pageHeader = OggPageHeader.read(raf);
            packetList = pageHeader.getPacketList();
            while (true) {
                setupHeaderSize += packetList.get(0).getLength();
                logger.fine("Adding:" + packetList.get(0).getLength() + " to setup header size");
                raf.skipBytes(packetList.get(0).getLength());
                if (packetList.size() > 1 || !pageHeader.isLastPacketIncomplete()) {
                    //done setup size
                    logger.fine("Found end of setupheader:size:" + setupHeaderSize + "finishes at:" + raf.getFilePointer());
                    if (packetList.size() > 1) {
                        extraPackets = packetList.subList(1, packetList.size());
                    }
                    break;
                }
                //Continues onto another page
                pageHeader = OggPageHeader.read(raf);
            }
        }
    }
    //Reset filepointer to location that it was in at start of method
    raf.seek(filepointer);
    return new OggVorbisHeaderSizes(commentHeaderStartPosition, setupHeaderStartPosition, commentHeaderSize, setupHeaderSize, extraPackets);
}
Also used : CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) ArrayList(java.util.ArrayList) OggPageHeader(org.jaudiotagger.audio.ogg.util.OggPageHeader)

Example 2 with OggPageHeader

use of org.jaudiotagger.audio.ogg.util.OggPageHeader in project MusicDNA by harjot-oberai.

the class OggVorbisTagReader method readRawPacketData.

/**
     * Retrieve the raw VorbisComment packet data, does not include the OggVorbis header
     *
     * @param raf
     * @return
     * @throws CannotReadException if unable to find vorbiscomment header
     * @throws IOException
     */
public byte[] readRawPacketData(RandomAccessFile raf) throws CannotReadException, IOException {
    logger.fine("Read 1st page");
    //1st page = codec infos
    OggPageHeader pageHeader = OggPageHeader.read(raf);
    //Skip over data to end of page header 1
    raf.seek(raf.getFilePointer() + pageHeader.getPageLength());
    logger.fine("Read 2nd page");
    //2nd page = comment, may extend to additional pages or not , may also have setup header
    pageHeader = OggPageHeader.read(raf);
    //Now at start of packets on page 2 , check this is the vorbis comment header 
    byte[] b = new byte[VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH];
    raf.read(b);
    if (!isVorbisCommentHeader(b)) {
        throw new CannotReadException("Cannot find comment block (no vorbiscomment header)");
    }
    //Convert the comment raw data which maybe over many pages back into raw packet
    byte[] rawVorbisCommentData = convertToVorbisCommentPacket(pageHeader, raf);
    return rawVorbisCommentData;
}
Also used : CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) OggPageHeader(org.jaudiotagger.audio.ogg.util.OggPageHeader)

Example 3 with OggPageHeader

use of org.jaudiotagger.audio.ogg.util.OggPageHeader in project MusicDNA by harjot-oberai.

the class OggVorbisTagReader method convertToVorbisCommentPacket.

/**
     * The Vorbis Comment may span multiple pages so we we need to identify the pages they contain and then
     * extract the packet data from the pages
     * @param startVorbisCommentPage
     * @param raf
     * @throws org.jaudiotagger.audio.exceptions.CannotReadException
     * @throws java.io.IOException
     * @return
     */
private byte[] convertToVorbisCommentPacket(OggPageHeader startVorbisCommentPage, RandomAccessFile raf) throws IOException, CannotReadException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] b = new byte[startVorbisCommentPage.getPacketList().get(0).getLength() - (VorbisHeader.FIELD_PACKET_TYPE_LENGTH + VorbisHeader.FIELD_CAPTURE_PATTERN_LENGTH)];
    raf.read(b);
    baos.write(b);
    //on this page so thats all we need and we can return
    if (startVorbisCommentPage.getPacketList().size() > 1) {
        logger.config("Comments finish on 2nd Page because there is another packet on this page");
        return baos.toByteArray();
    }
    //There is only the VorbisComment packet on page if it has completed on this page we can return
    if (!startVorbisCommentPage.isLastPacketIncomplete()) {
        logger.config("Comments finish on 2nd Page because this packet is complete");
        return baos.toByteArray();
    }
    //so carry on reading pages until we get to the end of comment
    while (true) {
        logger.config("Reading next page");
        OggPageHeader nextPageHeader = OggPageHeader.read(raf);
        b = new byte[nextPageHeader.getPacketList().get(0).getLength()];
        raf.read(b);
        baos.write(b);
        //on this page so thats all we need and we can return
        if (nextPageHeader.getPacketList().size() > 1) {
            logger.config("Comments finish on Page because there is another packet on this page");
            return baos.toByteArray();
        }
        //There is only the VorbisComment packet on page if it has completed on this page we can return
        if (!nextPageHeader.isLastPacketIncomplete()) {
            logger.config("Comments finish on Page because this packet is complete");
            return baos.toByteArray();
        }
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OggPageHeader(org.jaudiotagger.audio.ogg.util.OggPageHeader)

Example 4 with OggPageHeader

use of org.jaudiotagger.audio.ogg.util.OggPageHeader in project MusicDNA by harjot-oberai.

the class OggFileReader method summarizeOggPageHeaders.

/**
     * Summarize all the ogg headers in a file
     *
     * A useful utility function
     *
     * @param oggFile
     * @throws CannotReadException
     * @throws IOException
     */
public void summarizeOggPageHeaders(File oggFile) throws CannotReadException, IOException {
    RandomAccessFile raf = new RandomAccessFile(oggFile, "r");
    while (raf.getFilePointer() < raf.length()) {
        System.out.println("pageHeader starts at absolute file position:" + raf.getFilePointer());
        OggPageHeader pageHeader = OggPageHeader.read(raf);
        System.out.println("pageHeader finishes at absolute file position:" + raf.getFilePointer());
        System.out.println(pageHeader + "\n");
        raf.seek(raf.getFilePointer() + pageHeader.getPageLength());
    }
    System.out.println("Raf File Pointer at:" + raf.getFilePointer() + "File Size is:" + raf.length());
    raf.close();
}
Also used : RandomAccessFile(java.io.RandomAccessFile) OggPageHeader(org.jaudiotagger.audio.ogg.util.OggPageHeader)

Example 5 with OggPageHeader

use of org.jaudiotagger.audio.ogg.util.OggPageHeader in project MusicDNA by harjot-oberai.

the class OggFileReader method shortSummarizeOggPageHeaders.

/**
     * Summarizes the first five pages, normally all we are interested in
     *
     * @param oggFile
     * @throws CannotReadException
     * @throws IOException
     */
public void shortSummarizeOggPageHeaders(File oggFile) throws CannotReadException, IOException {
    RandomAccessFile raf = new RandomAccessFile(oggFile, "r");
    int i = 0;
    while (raf.getFilePointer() < raf.length()) {
        System.out.println("pageHeader starts at absolute file position:" + raf.getFilePointer());
        OggPageHeader pageHeader = OggPageHeader.read(raf);
        System.out.println("pageHeader finishes at absolute file position:" + raf.getFilePointer());
        System.out.println(pageHeader + "\n");
        raf.seek(raf.getFilePointer() + pageHeader.getPageLength());
        i++;
        if (i >= 5) {
            break;
        }
    }
    System.out.println("Raf File Pointer at:" + raf.getFilePointer() + "File Size is:" + raf.length());
    raf.close();
}
Also used : RandomAccessFile(java.io.RandomAccessFile) OggPageHeader(org.jaudiotagger.audio.ogg.util.OggPageHeader)

Aggregations

OggPageHeader (org.jaudiotagger.audio.ogg.util.OggPageHeader)11 CannotReadException (org.jaudiotagger.audio.exceptions.CannotReadException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ByteBuffer (java.nio.ByteBuffer)3 RandomAccessFile (java.io.RandomAccessFile)2 CannotWriteException (org.jaudiotagger.audio.exceptions.CannotWriteException)2 ArrayList (java.util.ArrayList)1