Java Code Examples for org.jaudiotagger.tag.id3.AbstractID3v2Tag

The following are top voted examples for showing how to use org.jaudiotagger.tag.id3.AbstractID3v2Tag. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: MetadataEditor   File: AudioFile.java   View source code 6 votes vote down vote up
/**
 * Get the tag and convert to the default tag version or if the file doesn't have one at all, create a default tag
 * set as tag for this file
 *
 * Conversions are currently only necessary/available for formats that support ID3
 *
 * @return
 */
public Tag getTagAndConvertOrCreateAndSetDefault()
{
    Tag tag = getTagOrCreateDefault();

    /* TODO Currently only works for Dsf We need additional check here for Wav and Aif because they wrap the ID3 tag so never return
     * null for getTag() and the wrapper stores the location of the existing tag, would that be broken if tag set to something else
     */
    if(tag instanceof AbstractID3v2Tag)
    {
        Tag convertedTag = convertID3Tag((AbstractID3v2Tag)tag, TagOptionSingleton.getInstance().getID3V2Version());
        if(convertedTag!=null)
        {
            setTag(convertedTag);
        }
        else
        {
            setTag(tag);
        }
    }
    else
    {
        setTag(tag);
    }
    return getTag();
}
 
Example 2
Project: MetadataEditor   File: MP3File.java   View source code 6 votes vote down vote up
/**
 * Used by tags when writing to calculate the location of the music file
 *
 * @param file
 * @return the location within the file that the audio starts
 * @throws java.io.IOException
 * @throws org.jaudiotagger.audio.exceptions.InvalidAudioFrameException
 */
public long getMP3StartByte(File file) throws InvalidAudioFrameException, IOException {
    try {
        //Read ID3v2 tag size (if tag exists) to allow audio header parsing to skip over tag
        long startByte = AbstractID3v2Tag.getV2TagSizeIfExists(file);

        MP3AudioHeader audioHeader = new MP3AudioHeader(file, startByte);
        if (startByte != audioHeader.getMp3StartByte()) {
            logger.config("First header found after tag:" + audioHeader);
            audioHeader = checkAudioStart(startByte, audioHeader);
        }
        return audioHeader.getMp3StartByte();
    } catch (InvalidAudioFrameException iafe) {
        throw iafe;
    } catch (IOException ioe) {
        throw ioe;
    }
}
 
Example 3
Project: Musique   File: DsfAudioFileReader.java   View source code 6 votes vote down vote up
private Tag readTag(RandomAccessFile file, long tagOffset) throws CannotReadException, IOException {
    //Move to start of ID3Tag and read rest of file into ByteBuffer
    file.seek(tagOffset);
    ByteBuffer tagBuffer = Utils.readFileDataIntoBufferLE(file, (int) (file.length() - file.getFilePointer()));

    //Work out ID3 major version
    int majorVersion = tagBuffer.get(AbstractID3v2Tag.FIELD_TAG_MAJOR_VERSION_POS);
    try {
        logger.log(Level.FINE, "Start creating ID3v2 Tag for version: " + majorVersion);
        switch (majorVersion) {
            case ID3v22Tag.MAJOR_VERSION:
                return new ID3v22Tag(tagBuffer, "");
            case ID3v23Tag.MAJOR_VERSION:
                return new ID3v23Tag(tagBuffer, "");
            case ID3v24Tag.MAJOR_VERSION:
                return new ID3v24Tag(tagBuffer, "");
            default:
                logger.log(Level.WARNING, "Unknown major ID3v2 version " + majorVersion + ". Returning an empty ID3v2 Tag.");
                return new ID3v24Tag();
        }
    } catch (TagException e) {
        throw new CannotReadException("Could not create ID3v2 Tag");
    }
}
 
Example 4
Project: Musique   File: MP3File.java   View source code 6 votes vote down vote up
@SuppressWarnings("TryWithIdenticalCatches")
public long getMP3StartByte(File file) throws InvalidAudioFrameException, IOException {
    //noinspection CaughtExceptionImmediatelyRethrown,CaughtExceptionImmediatelyRethrown
    try {
        //Read ID3v2 tag size (if tag exists) to allow audio header parsing to skip over tag
        long startByte = AbstractID3v2Tag.getV2TagSizeIfExists(file);

        MP3AudioHeader audioHeader = new MP3AudioHeader(file, startByte);
        if (startByte != audioHeader.getMp3StartByte()) {
            logger.config("First header found after tag:" + audioHeader);
            audioHeader = checkAudioStart(startByte, audioHeader);
        }
        return audioHeader.getMp3StartByte();
    } catch (InvalidAudioFrameException iafe) {
        throw iafe;
    } catch (IOException ioe) {
        throw ioe;
    }
}
 
Example 5
Project: MetadataEditor   File: ID3Chunk.java   View source code 5 votes vote down vote up
/**
 * Reads 3 bytes to determine if the tag really looks like ID3 data.
 */
private boolean isId3v2Tag(final ByteBuffer headerData) throws IOException
{
    for (int i = 0; i < AbstractID3v2Tag.FIELD_TAGID_LENGTH; i++)
    {
        if (headerData.get() != AbstractID3v2Tag.TAG_ID[i])
        {
            return false;
        }
    }
    return true;
}
 
Example 6
Project: MetadataEditor   File: WavId3Chunk.java   View source code 5 votes vote down vote up
/**
 * Reads 3 bytes to determine if the tag really looks like ID3 data.
 */
private boolean isId3v2Tag(final ByteBuffer headerData) throws IOException
{
    for (int i = 0; i < AbstractID3v2Tag.FIELD_TAGID_LENGTH; i++)
    {
        if (headerData.get() != AbstractID3v2Tag.TAG_ID[i])
        {
            return false;
        }
    }
    return true;
}
 
Example 7
Project: MetadataEditor   File: OggPageHeader.java   View source code 5 votes vote down vote up
/**
 * Read next PageHeader from file
 * @param raf
 * @return
 * @throws IOException
 * @throws CannotReadException
 */
public static OggPageHeader read(RandomAccessFile raf) throws IOException, CannotReadException
{
    long start = raf.getFilePointer();
    logger.fine("Trying to read OggPage at:" + start);

    byte[] b = new byte[OggPageHeader.CAPTURE_PATTERN.length];
    raf.read(b);
    if (!(Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN)))
    {
        raf.seek(start);
        if(AbstractID3v2Tag.isId3Tag(raf))
        {
            logger.warning(ErrorMessage.OGG_CONTAINS_ID3TAG.getMsg(raf.getFilePointer() - start));
            raf.read(b);
            if ((Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN)))
            {
                //Go to the end of the ID3 header
                start=raf.getFilePointer() - OggPageHeader.CAPTURE_PATTERN.length;
            }
        }
        else
        {
            throw new CannotReadException(ErrorMessage.OGG_HEADER_CANNOT_BE_FOUND.getMsg(new String(b)));
        }
    }

    raf.seek(start + OggPageHeader.FIELD_PAGE_SEGMENTS_POS);
    int pageSegments = raf.readByte() & 0xFF; //unsigned
    raf.seek(start);

    b = new byte[OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageSegments];
    raf.read(b);


    OggPageHeader pageHeader = new OggPageHeader(b);
    pageHeader.setStartByte(start);
    //Now just after PageHeader, ready for Packet Data
    return pageHeader;
}
 
Example 8
Project: MetadataEditor   File: DsfFileReader.java   View source code 5 votes vote down vote up
/**
 * Reads the ID3v2 tag starting at the {@code tagOffset} position in the
 * supplied file.
 *
 * @param fc       the filechannel from which to read
 * @param dsd      the dsd chunk
 * @param fileName
 * @return the read tag or an empty tag if something went wrong. Never
 * <code>null</code>.
 * @throws IOException if cannot read file.
 */
private Tag readTag(FileChannel fc, DsdChunk dsd, String fileName) throws CannotReadException, IOException {
    if (dsd.getMetadataOffset() > 0) {
        fc.position(dsd.getMetadataOffset());
        ID3Chunk id3Chunk = ID3Chunk.readChunk(Utils.readFileDataIntoBufferLE(fc, (int) (fc.size() - fc.position())));
        if (id3Chunk != null) {
            int version = id3Chunk.getDataBuffer().get(AbstractID3v2Tag.FIELD_TAG_MAJOR_VERSION_POS);
            try {
                switch (version) {
                    case ID3v22Tag.MAJOR_VERSION:
                        return new ID3v22Tag(id3Chunk.getDataBuffer(), "");
                    case ID3v23Tag.MAJOR_VERSION:
                        return new ID3v23Tag(id3Chunk.getDataBuffer(), "");
                    case ID3v24Tag.MAJOR_VERSION:
                        return new ID3v24Tag(id3Chunk.getDataBuffer(), "");
                    default:
                        logger.log(Level.WARNING, fileName + " Unknown ID3v2 version " + version + ". Returning an empty ID3v2 Tag.");
                        return null;
                }
            } catch (TagException e) {
                throw new CannotReadException(fileName + " Could not read ID3v2 tag:corruption");
            }
        } else {
            logger.log(Level.WARNING, fileName + " No existing ID3 tag(1)");
            return null;
        }
    } else {
        logger.log(Level.WARNING, fileName + " No existing ID3 tag(2)");
        return null;
    }
}
 
Example 9
Project: MetadataEditor   File: MP3File.java   View source code 5 votes vote down vote up
/**
 * Sets the v2 tag to the v2 tag provided as an argument.
 * Also store a v24 version of tag as v24 is the interface to be used
 * when talking with client applications.
 *
 * @param id3v2tag
 */
public void setID3v2Tag(AbstractID3v2Tag id3v2tag) {
    this.id3v2tag = id3v2tag;
    if (id3v2tag instanceof ID3v24Tag) {
        this.id3v2Asv24tag = (ID3v24Tag) this.id3v2tag;
    } else {
        this.id3v2Asv24tag = new ID3v24Tag(id3v2tag);
    }
}
 
Example 10
Project: MetadataEditor   File: MP3File.java   View source code 5 votes vote down vote up
/**
 * Remove tag from file
 *
 * @param mp3tag
 * @throws FileNotFoundException
 * @throws IOException
 */
public void delete(AbstractTag mp3tag) throws FileNotFoundException, IOException {
    RandomAccessFile raf = new RandomAccessFile(this.file, "rw");
    mp3tag.delete(raf);
    raf.close();
    if (mp3tag instanceof ID3v1Tag) {
        id3v1tag = null;
    }

    if (mp3tag instanceof AbstractID3v2Tag) {
        id3v2tag = null;
    }
}
 
Example 11
Project: MetadataEditor   File: MP3File.java   View source code 5 votes vote down vote up
/**
 * Set the Tag
 * <p>
 * If the parameter tag is a v1tag then the v1 tag is set if v2tag then the v2tag.
 *
 * @param tag
 */
public void setTag(Tag tag) {
    this.tag = tag;
    if (tag instanceof ID3v1Tag) {
        setID3v1Tag((ID3v1Tag) tag);
    } else {
        setID3v2Tag((AbstractID3v2Tag) tag);
    }
}
 
Example 12
Project: MetadataEditor   File: MP3File.java   View source code 5 votes vote down vote up
/**
 * Get the ID3v2 tag and convert to preferred version or if the file doesn't have one at all
 * create a default tag of preferred version and set it. The file may already contain a ID3v1 tag but because
 * this is not terribly useful the v1tag is not considered for this problem.
 *
 * @return
 */
@Override
public Tag getTagAndConvertOrCreateAndSetDefault() {
    Tag tag = getTagOrCreateDefault();
    Tag convertedTag = convertID3Tag((AbstractID3v2Tag) tag, TagOptionSingleton.getInstance().getID3V2Version());
    if (convertedTag != null) {
        setTag(convertedTag);
    } else {
        setTag(tag);
    }
    return getTag();
}
 
Example 13
Project: MetadataEditor   File: FlacStreamReader.java   View source code 5 votes vote down vote up
private boolean isId3v2Tag() throws IOException
{
    fc.position(0);
    if(AbstractID3v2Tag.isId3Tag(fc))
    {
        logger.warning(loggingName + ErrorMessage.FLAC_CONTAINS_ID3TAG.getMsg(fc.position()));
        //FLAC Stream immediately after end of id3 tag
        if (isFlacHeader())
        {
            return true;
        }
    }
    return false;
}
 
Example 14
Project: openaudible   File: OggPageHeader.java   View source code 5 votes vote down vote up
/**
 * Read next PageHeader from file
 *
 * @param raf
 * @return
 * @throws IOException
 * @throws CannotReadException
 */
public static OggPageHeader read(RandomAccessFile raf) throws IOException, CannotReadException {
    long start = raf.getFilePointer();
    logger.fine("Trying to read OggPage at:" + start);

    byte[] b = new byte[OggPageHeader.CAPTURE_PATTERN.length];
    raf.read(b);
    if (!(Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN))) {
        raf.seek(start);
        if (AbstractID3v2Tag.isId3Tag(raf)) {
            logger.warning(ErrorMessage.OGG_CONTAINS_ID3TAG.getMsg(raf.getFilePointer() - start));
            raf.read(b);
            if ((Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN))) {
                //Go to the end of the ID3 header
                start = raf.getFilePointer() - OggPageHeader.CAPTURE_PATTERN.length;
            }
        } else {
            throw new CannotReadException(ErrorMessage.OGG_HEADER_CANNOT_BE_FOUND.getMsg(new String(b)));
        }
    }

    raf.seek(start + OggPageHeader.FIELD_PAGE_SEGMENTS_POS);
    int pageSegments = raf.readByte() & 0xFF; //unsigned
    raf.seek(start);

    b = new byte[OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageSegments];
    raf.read(b);


    OggPageHeader pageHeader = new OggPageHeader(b);
    pageHeader.setStartByte(start);
    //Now just after PageHeader, ready for Packet Data
    return pageHeader;
}
 
Example 15
Project: openaudible   File: FlacStreamReader.java   View source code 5 votes vote down vote up
private boolean isId3v2Tag() throws IOException {
    raf.seek(0);
    if (AbstractID3v2Tag.isId3Tag(raf)) {
        logger.warning(ErrorMessage.FLAC_CONTAINS_ID3TAG.getMsg(raf.getFilePointer()));
        //FLAC Stream immediately after end of id3 tag
        if (isFlacHeader()) {
            return true;
        }
    }
    return false;
}
 
Example 16
Project: Musique   File: ID3Chunk.java   View source code 5 votes vote down vote up
private boolean isId3v2Tag(final ByteBuffer headerData) throws IOException {
    for (int i = 0; i < AbstractID3v2Tag.FIELD_TAGID_LENGTH; i++) {
        if (headerData.get() != AbstractID3v2Tag.TAG_ID[i]) {
            return false;
        }
    }
    return true;
}
 
Example 17
Project: Musique   File: WavId3Chunk.java   View source code 5 votes vote down vote up
private boolean isId3v2Tag(final ByteBuffer headerData) throws IOException {
    for (int i = 0; i < AbstractID3v2Tag.FIELD_TAGID_LENGTH; i++) {
        if (headerData.get() != AbstractID3v2Tag.TAG_ID[i]) {
            return false;
        }
    }
    return true;
}
 
Example 18
Project: Musique   File: OggPageHeader.java   View source code 5 votes vote down vote up
public static OggPageHeader read(RandomAccessFile raf) throws IOException, CannotReadException {
    long start = raf.getFilePointer();
    logger.fine("Trying to read OggPage at:" + start);

    byte[] b = new byte[OggPageHeader.CAPTURE_PATTERN.length];
    raf.read(b);
    if (!(Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN))) {
        raf.seek(start);
        if (AbstractID3v2Tag.isId3Tag(raf)) {
            logger.warning(ErrorMessage.OGG_CONTAINS_ID3TAG.getMsg(raf.getFilePointer() - start));
            raf.read(b);
            if ((Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN))) {
                //Go to the end of the ID3 header
                start = raf.getFilePointer() - OggPageHeader.CAPTURE_PATTERN.length;
            }
        } else {
            throw new CannotReadException(ErrorMessage.OGG_HEADER_CANNOT_BE_FOUND.getMsg(new String(b)));
        }
    }

    raf.seek(start + OggPageHeader.FIELD_PAGE_SEGMENTS_POS);
    int pageSegments = raf.readByte() & 0xFF; //unsigned
    raf.seek(start);

    b = new byte[OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageSegments];
    raf.read(b);


    OggPageHeader pageHeader = new OggPageHeader(b);
    pageHeader.setStartByte(start);
    //Now just after PageHeader, ready for Packet Data
    return pageHeader;
}
 
Example 19
Project: Musique   File: MP3File.java   View source code 5 votes vote down vote up
public MP3File(File file, int loadOptions, boolean readOnly) throws IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {
    RandomAccessFile newFile = null;
    try {
        this.file = file;

        //Check File accessibility
        newFile = checkFilePermissions(file, readOnly);

        //Read ID3v2 tag size (if tag exists) to allow audioHeader parsing to skip over tag
        long tagSizeReportedByHeader = AbstractID3v2Tag.getV2TagSizeIfExists(file);
        logger.config("TagHeaderSize:" + Hex.asHex(tagSizeReportedByHeader));
        audioHeader = new MP3AudioHeader(file, tagSizeReportedByHeader);

        //If the audio header is not straight after the end of the tag then search from start of file
        if (tagSizeReportedByHeader != ((MP3AudioHeader) audioHeader).getMp3StartByte()) {
            logger.config("First header found after tag:" + audioHeader);
            audioHeader = checkAudioStart(tagSizeReportedByHeader, (MP3AudioHeader) audioHeader);
        }

        //Read v1 tags (if any)
        readV1Tag(file, newFile, loadOptions);

        //Read v2 tags (if any)
        readV2Tag(file, loadOptions, (int) ((MP3AudioHeader) audioHeader).getMp3StartByte());

        //If we have a v2 tag use that, if we do not but have v1 tag use that
        //otherwise use nothing
        //TODO:if have both should we merge
        //rather than just returning specific ID3v22 tag, would it be better to return v24 version ?
        if (this.getID3v2Tag() != null) {
            tag = this.getID3v2Tag();
        } else if (id3v1tag != null) {
            tag = id3v1tag;
        }
    } finally {
        if (newFile != null) {
            newFile.close();
        }
    }
}
 
Example 20
Project: Musique   File: MP3File.java   View source code 5 votes vote down vote up
public void setID3v2Tag(AbstractID3v2Tag id3v2tag) {
    this.id3v2tag = id3v2tag;
    if (id3v2tag instanceof ID3v24Tag) {
        this.id3v2Asv24tag = (ID3v24Tag) this.id3v2tag;
    } else {
        this.id3v2Asv24tag = new ID3v24Tag(id3v2tag);
    }
}
 
Example 21
Project: Musique   File: MP3File.java   View source code 5 votes vote down vote up
public void delete(AbstractTag mp3tag) throws FileNotFoundException, IOException {
    RandomAccessFile raf = new RandomAccessFile(this.file, "rw");
    mp3tag.delete(raf);
    raf.close();
    if (mp3tag instanceof ID3v1Tag) {
        id3v1tag = null;
    }

    if (mp3tag instanceof AbstractID3v2Tag) {
        id3v2tag = null;
    }
}
 
Example 22
Project: Musique   File: MP3File.java   View source code 5 votes vote down vote up
public void setTag(Tag tag) {
    this.tag = tag;
    if (tag instanceof ID3v1Tag) {
        setID3v1Tag((ID3v1Tag) tag);
    } else {
        setID3v2Tag((AbstractID3v2Tag) tag);
    }
}
 
Example 23
Project: Musique   File: FlacStreamReader.java   View source code 5 votes vote down vote up
private boolean isId3v2Tag() throws IOException {
    raf.seek(0);
    if (AbstractID3v2Tag.isId3Tag(raf)) {
        logger.warning(ErrorMessage.FLAC_CONTAINS_ID3TAG.getMsg(raf.getFilePointer()));
        //FLAC Stream immediately after end of id3 tag
        if (isFlacHeader()) {
            return true;
        }
    }
    return false;
}
 
Example 24
Project: SoundsCool   File: OggPageHeader.java   View source code 5 votes vote down vote up
/**
 * Read next PageHeader from file
 * @param raf
 * @return
 * @throws IOException
 * @throws CannotReadException
 */
public static OggPageHeader read(RandomAccessFile raf) throws IOException, CannotReadException
{
    long start = raf.getFilePointer();
    logger.fine("Trying to read OggPage at:" + start);

    byte[] b = new byte[OggPageHeader.CAPTURE_PATTERN.length];
    raf.read(b);
    if (!(Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN)))
    {
        raf.seek(start);
        if(AbstractID3v2Tag.isId3Tag(raf))
        {
            logger.warning(ErrorMessage.OGG_CONTAINS_ID3TAG.getMsg(raf.getFilePointer() - start));
            raf.read(b);
            if ((Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN)))
            {
                //Go to the end of the ID3 header
                start=raf.getFilePointer() - OggPageHeader.CAPTURE_PATTERN.length;
            }
        }
        else
        {
            throw new CannotReadException(ErrorMessage.OGG_HEADER_CANNOT_BE_FOUND.getMsg(new String(b)));
        }
    }

    raf.seek(start + OggPageHeader.FIELD_PAGE_SEGMENTS_POS);
    int pageSegments = raf.readByte() & 0xFF; //unsigned
    raf.seek(start);

    b = new byte[OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageSegments];
    raf.read(b);


    OggPageHeader pageHeader = new OggPageHeader(b);
    pageHeader.setStartByte(start);
    //Now just after PageHeader, ready for Packet Data
    return pageHeader;
}
 
Example 25
Project: SoundsCool   File: FlacStreamReader.java   View source code 5 votes vote down vote up
private boolean isId3v2Tag() throws IOException
{
    raf.seek(0);
    if(AbstractID3v2Tag.isId3Tag(raf))
    {
        logger.warning(ErrorMessage.FLAC_CONTAINS_ID3TAG.getMsg(raf.getFilePointer()));
        //FLAC Stream immediately after end of id3 tag
        if (isFlacHeader())
        {
            return true;
        }
    }
    return false;
}
 
Example 26
Project: jmetadata   File: Mp3Reader.java   View source code 5 votes vote down vote up
public Metadata getMetadata(File file) throws CannotReadException, IOException, TagException, ReadOnlyFileException, MetadataException {
	try{
		audioFile = audioFileHelper.read(file);
	} catch (InvalidAudioFrameException ina){
		return null;
	} catch (FileNotFoundException fnf){
		log.error("File: " + file.getAbsolutePath() + " Not found");
		configurator.getControlEngine().fireEvent(Events.LOAD_FILE, new ValueEvent<String>(file.getAbsolutePath()));
		return null;
	}
	if (audioFile instanceof MP3File) {
		MP3File audioMP3 = (MP3File) audioFile;
		if (!audioMP3.hasID3v2Tag()) {
			AbstractID3v2Tag id3v2tag = new ID3v24Tag();
			audioMP3.setID3v2TagOnly(id3v2tag);
			try {
				audioFile.commit();
			} catch (CannotWriteException cwe) {
				log.error("An error occurs when I tried to update to ID3 v2");
				cwe.printStackTrace();
			}
		}
		tag = audioFile.getTag();
		header = audioFile.getAudioHeader();
		log.info("tag: " + ToStringBuilder.reflectionToString(tag));
		log.info("header: " + ToStringBuilder.reflectionToString(header));
		if(jAudioTaggerCollaborator.isValid(tag, header)){
			return generateMetadata(file);
		}
	}
	return new Metadata();
}
 
Example 27
Project: jmetadata   File: TestMp3Reader.java   View source code 5 votes vote down vote up
@Test
public void shouldUpdateID3toV2() throws Exception {
	when(audioFile.hasID3v2Tag()).thenReturn(false);
	reader.getMetadata(file);
	
	((MP3File) verify(audioFile)).setID3v2TagOnly((AbstractID3v2Tag) anyObject());
	((MP3File) verify(audioFile)).commit();
}
 
Example 28
Project: MetadataEditor   File: AudioFile.java   View source code 4 votes vote down vote up
/**
 * If using ID3 format convert tag from current version to another as specified by id3V2Version,
 *
 * @return null if no conversion necessary
 */
public AbstractID3v2Tag convertID3Tag(AbstractID3v2Tag tag, ID3V2Version id3V2Version)
{
    if(tag instanceof ID3v24Tag)
    {
        switch(id3V2Version)
        {
            case ID3_V22:
                return new ID3v22Tag((ID3v24Tag)tag);
            case ID3_V23:
                return new ID3v23Tag((ID3v24Tag)tag);
            case ID3_V24:
                return null;
        }
    }
    else if(tag instanceof ID3v23Tag)
    {
        switch(id3V2Version)
        {
            case ID3_V22:
                return new ID3v22Tag((ID3v23Tag)tag);
            case ID3_V23:
                return null;
            case ID3_V24:
                return new ID3v24Tag((ID3v23Tag)tag);
        }
    }
    else if(tag instanceof ID3v22Tag)
    {
        switch(id3V2Version)
        {
            case ID3_V22:
                return null;
            case ID3_V23:
                return new ID3v23Tag((ID3v22Tag)tag);
            case ID3_V24:
                return new ID3v24Tag((ID3v22Tag)tag);
        }
    }
    return null;
}
 
Example 29
Project: MetadataEditor   File: MP3File.java   View source code 4 votes vote down vote up
/**
 * Creates a new MP3File dataType and parse the tag from the given file
 * Object, files can be opened read only if required.
 *
 * @param file        MP3 file
 * @param loadOptions decide what tags to load
 * @param readOnly    causes the files to be opened readonly
 * @throws IOException                                                  on any I/O error
 * @throws TagException                                                 on any exception generated by this library.
 * @throws org.jaudiotagger.audio.exceptions.ReadOnlyFileException
 * @throws org.jaudiotagger.audio.exceptions.InvalidAudioFrameException
 */
public MP3File(File file, int loadOptions, boolean readOnly) throws IOException, TagException, ReadOnlyFileException, CannotReadException, InvalidAudioFrameException {
    RandomAccessFile newFile = null;
    try {
        this.file = file;

        //Check File accessibility
        newFile = checkFilePermissions(file, readOnly);

        //Read ID3v2 tag size (if tag exists) to allow audioHeader parsing to skip over tag
        long tagSizeReportedByHeader = AbstractID3v2Tag.getV2TagSizeIfExists(file);
        logger.config("TagHeaderSize:" + Hex.asHex(tagSizeReportedByHeader));
        audioHeader = new MP3AudioHeader(file, tagSizeReportedByHeader);

        //If the audio header is not straight after the end of the tag then search from start of file
        if (tagSizeReportedByHeader != ((MP3AudioHeader) audioHeader).getMp3StartByte()) {
            logger.config("First header found after tag:" + audioHeader);
            audioHeader = checkAudioStart(tagSizeReportedByHeader, (MP3AudioHeader) audioHeader);
        }

        //Read v1 tags (if any)
        readV1Tag(file, newFile, loadOptions);

        //Read v2 tags (if any)
        readV2Tag(file, loadOptions, (int) ((MP3AudioHeader) audioHeader).getMp3StartByte());

        //If we have a v2 tag use that, if we do not but have v1 tag use that
        //otherwise use nothing
        //TODO:if have both should we merge
        //rather than just returning specific ID3v22 tag, would it be better to return v24 version ?
        if (this.getID3v2Tag() != null) {
            tag = this.getID3v2Tag();
        } else if (id3v1tag != null) {
            tag = id3v1tag;
        }
    } finally {
        if (newFile != null) {
            newFile.close();
        }
    }
}
 
Example 30
Project: MetadataEditor   File: MP3File.java   View source code 4 votes vote down vote up
/**
 * Set v2 tag ,don't need to set v24 tag because saving
 *
 * @param id3v2tag
 */
//TODO temp its rather messy
public void setID3v2TagOnly(AbstractID3v2Tag id3v2tag) {
    this.id3v2tag = id3v2tag;
    this.id3v2Asv24tag = null;
}
 
Example 31
Project: openaudible   File: AiffTag.java   View source code 4 votes vote down vote up
public AiffTag(AbstractID3v2Tag t) {
    id3Tag = t;
}
 
Example 32
Project: openaudible   File: AiffTag.java   View source code 4 votes vote down vote up
/**
 * Returns the ID3 tag
 */
public AbstractID3v2Tag getID3Tag() {
    return id3Tag;
}
 
Example 33
Project: openaudible   File: AiffTag.java   View source code 4 votes vote down vote up
/**
 * Sets the ID3 tag
 */
public void setID3Tag(AbstractID3v2Tag t) {
    id3Tag = t;
}
 
Example 34
Project: vanilla-music-tag-editor   File: PluginService.java   View source code 4 votes vote down vote up
/**
 * upgrades ID3v2.x tag to ID3v2.4 for loaded file.
 * Call this method only if you know exactly that file contains ID3 tag.
 */
public void upgradeID3v2() {
    mTag = mAudioFile.convertID3Tag((AbstractID3v2Tag) mTag, ID3V2Version.ID3_V24);
    mAudioFile.setTag(mTag);
    writeFile();
}
 
Example 35
Project: Musique   File: AiffTag.java   View source code 4 votes vote down vote up
public AiffTag(AbstractID3v2Tag t) {
    id3Tag = t;
}
 
Example 36
Project: Musique   File: AiffTag.java   View source code 4 votes vote down vote up
public AbstractID3v2Tag getID3Tag() {
    return id3Tag;
}
 
Example 37
Project: Musique   File: AiffTag.java   View source code 4 votes vote down vote up
public void setID3Tag(AbstractID3v2Tag t) {
    id3Tag = t;
}
 
Example 38
Project: Musique   File: WavTag.java   View source code 4 votes vote down vote up
public AbstractID3v2Tag getID3Tag() {
    return id3Tag;
}
 
Example 39
Project: Musique   File: WavTag.java   View source code 4 votes vote down vote up
public void setID3Tag(AbstractID3v2Tag t) {
    id3Tag = t;
}
 
Example 40
Project: Musique   File: WavTagWriter.java   View source code 4 votes vote down vote up
private void saveBoth(WavTag wavTag, RandomAccessFile raf, final WavTag existingTag)
        throws CannotWriteException, IOException {
    //Convert both tags and get existing ones
    final ByteBuffer infoTagBuffer = convertInfoChunk(wavTag);
    final long newInfoTagSize = infoTagBuffer.limit();
    final WavInfoTag existingInfoTag = existingTag.getInfoTag();

    final ByteBuffer id3TagBuffer = convertID3Chunk(wavTag);
    final AbstractID3v2Tag existingId3Tag = existingTag.getID3Tag();

    //If both tags already exist in file
    if (existingTag.isExistingInfoTag() && existingTag.isExistingId3Tag()) {
        BothTagsFileStructure fs = checkExistingLocations(wavTag, raf);
        //We can write both chunks without affecting anything else
        if (fs.isContiguous && fs.isAtEnd) {
            if (fs.isInfoTagFirst) {
                seekToStartOfListInfoMetadata(raf, existingTag);
                writeInfoChunk(raf, existingInfoTag, infoTagBuffer);
                writeId3Chunk(raf, existingId3Tag, id3TagBuffer);
            } else {
                seekToStartOfId3Metadata(raf, existingTag);
                writeId3Chunk(raf, existingId3Tag, id3TagBuffer);
                writeInfoChunk(raf, existingInfoTag, infoTagBuffer);
            }
        }
        //Both chunks are together but there is another chunk after them
        else {
            raf.seek(raf.length());
            writeInfoDataToFile(raf, infoTagBuffer, newInfoTagSize);
            writeId3Chunk(raf, existingId3Tag, id3TagBuffer);
        }
    }
    //If only INFO chunk exists
    else if (existingTag.isExistingInfoTag() && !existingTag.isExistingId3Tag()) {
        ChunkHeader infoChunkHeader = seekToStartOfListInfoMetadata(raf, existingTag);
        if (existingInfoTag.getEndLocationInFile() == raf.length()) {
            writeInfoChunk(raf, existingInfoTag, infoTagBuffer);
            writeId3Chunk(raf, existingId3Tag, id3TagBuffer);
        } else {
            deleteInfoTagChunk(raf, existingTag, infoChunkHeader);
            raf.seek(raf.length());
            writeInfoDataToFile(raf, infoTagBuffer, newInfoTagSize);
            writeId3Chunk(raf, existingId3Tag, id3TagBuffer);
        }
    }
    //If only ID3 chunk exists
    else if (!existingTag.isExistingInfoTag() && existingTag.isExistingId3Tag()) {
        ChunkHeader id3ChunkHeader = seekToStartOfId3Metadata(raf, existingTag);
        if (existingId3Tag.getEndLocationInFile() == raf.length()) {
            writeId3Chunk(raf, existingId3Tag, id3TagBuffer);
            writeInfoChunk(raf, existingInfoTag, infoTagBuffer);
        } else {
            deleteId3TagChunk(raf, existingTag, id3ChunkHeader);
            raf.seek(raf.length());
            writeId3Chunk(raf, existingId3Tag, id3TagBuffer);
            writeInfoDataToFile(raf, infoTagBuffer, newInfoTagSize);
        }
    }
    //No existing tags so write both to end
    else {
        //Go to end of file
        raf.seek(raf.length());
        writeInfoDataToFile(raf, infoTagBuffer, newInfoTagSize);
        writeId3Chunk(raf, existingId3Tag, id3TagBuffer);
    }
}