Search in sources :

Example 1 with GenericAudioHeader

use of org.jaudiotagger.audio.generic.GenericAudioHeader in project MusicDNA by harjot-oberai.

the class AsfFileReader method getAudioHeader.

/**
     * Creates a generic audio header instance with provided data from header.
     * 
     * @param header
     *            ASF header which contains the information.
     * @return generic audio header representation.
     * @throws CannotReadException
     *             If header does not contain mandatory information. (Audio
     *             stream chunk and file header chunk)
     */
private GenericAudioHeader getAudioHeader(final AsfHeader header) throws CannotReadException {
    final GenericAudioHeader info = new GenericAudioHeader();
    if (header.getFileHeader() == null) {
        throw new CannotReadException("Invalid ASF/WMA file. File header object not available.");
    }
    if (header.getAudioStreamChunk() == null) {
        throw new CannotReadException("Invalid ASF/WMA file. No audio stream contained.");
    }
    info.setBitrate(header.getAudioStreamChunk().getKbps());
    info.setChannelNumber((int) header.getAudioStreamChunk().getChannelCount());
    info.setEncodingType("ASF (audio): " + header.getAudioStreamChunk().getCodecDescription());
    info.setLossless(header.getAudioStreamChunk().getCompressionFormat() == AudioStreamChunk.WMA_LOSSLESS);
    info.setPreciseLength(header.getFileHeader().getPreciseDuration());
    info.setSamplingRate((int) header.getAudioStreamChunk().getSamplingRate());
    info.setVariableBitRate(determineVariableBitrate(header));
    info.setBitsPerSample(header.getAudioStreamChunk().getBitsPerSample());
    return info;
}
Also used : CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) GenericAudioHeader(org.jaudiotagger.audio.generic.GenericAudioHeader)

Example 2 with GenericAudioHeader

use of org.jaudiotagger.audio.generic.GenericAudioHeader in project MusicDNA by harjot-oberai.

the class OggInfoReader method read.

public GenericAudioHeader read(RandomAccessFile raf) throws CannotReadException, IOException {
    long start = raf.getFilePointer();
    GenericAudioHeader info = new GenericAudioHeader();
    logger.fine("Started");
    long oldPos;
    //Check start of file does it have Ogg pattern
    byte[] b = new byte[OggPageHeader.CAPTURE_PATTERN.length];
    raf.read(b);
    if (!(Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN))) {
        raf.seek(0);
        if (AbstractID3v2Tag.isId3Tag(raf)) {
            raf.read(b);
            if ((Arrays.equals(b, OggPageHeader.CAPTURE_PATTERN))) {
                start = raf.getFilePointer();
            }
        } else {
            throw new CannotReadException(ErrorMessage.OGG_HEADER_CANNOT_BE_FOUND.getMsg(new String(b)));
        }
    }
    //Now work backwards from file looking for the last ogg page, it reads the granule position for this last page
    //which must be set.
    //TODO should do buffering to cut down the number of file reads
    raf.seek(start);
    double pcmSamplesNumber = -1;
    raf.seek(raf.length() - 2);
    while (raf.getFilePointer() >= 4) {
        if (raf.read() == OggPageHeader.CAPTURE_PATTERN[3]) {
            raf.seek(raf.getFilePointer() - OggPageHeader.FIELD_CAPTURE_PATTERN_LENGTH);
            byte[] ogg = new byte[3];
            raf.readFully(ogg);
            if (ogg[0] == OggPageHeader.CAPTURE_PATTERN[0] && ogg[1] == OggPageHeader.CAPTURE_PATTERN[1] && ogg[2] == OggPageHeader.CAPTURE_PATTERN[2]) {
                raf.seek(raf.getFilePointer() - 3);
                oldPos = raf.getFilePointer();
                raf.seek(raf.getFilePointer() + OggPageHeader.FIELD_PAGE_SEGMENTS_POS);
                //Unsigned
                int pageSegments = raf.readByte() & 0xFF;
                raf.seek(oldPos);
                b = new byte[OggPageHeader.OGG_PAGE_HEADER_FIXED_LENGTH + pageSegments];
                raf.readFully(b);
                OggPageHeader pageHeader = new OggPageHeader(b);
                raf.seek(0);
                pcmSamplesNumber = pageHeader.getAbsoluteGranulePosition();
                break;
            }
        }
        raf.seek(raf.getFilePointer() - 2);
    }
    if (pcmSamplesNumber == -1) {
        //According to spec a value of -1 indicates no packet finished on this page, this should not occur
        throw new CannotReadException(ErrorMessage.OGG_VORBIS_NO_SETUP_BLOCK.getMsg());
    }
    //1st page = Identification Header
    OggPageHeader pageHeader = OggPageHeader.read(raf);
    byte[] vorbisData = new byte[pageHeader.getPageLength()];
    raf.read(vorbisData);
    VorbisIdentificationHeader vorbisIdentificationHeader = new VorbisIdentificationHeader(vorbisData);
    //Map to generic encodingInfo
    info.setPreciseLength((float) (pcmSamplesNumber / vorbisIdentificationHeader.getSamplingRate()));
    info.setChannelNumber(vorbisIdentificationHeader.getChannelNumber());
    info.setSamplingRate(vorbisIdentificationHeader.getSamplingRate());
    info.setEncodingType(vorbisIdentificationHeader.getEncodingType());
    info.setExtraEncodingInfos("");
    //According to Wikipedia Vorbis Page, Vorbis only works on 16bits 44khz 
    info.setBitsPerSample(16);
    //TODO this calculation should be done within identification header
    if (vorbisIdentificationHeader.getNominalBitrate() != 0 && vorbisIdentificationHeader.getMaxBitrate() == vorbisIdentificationHeader.getNominalBitrate() && vorbisIdentificationHeader.getMinBitrate() == vorbisIdentificationHeader.getNominalBitrate()) {
        //CBR (in kbps)
        info.setBitrate(vorbisIdentificationHeader.getNominalBitrate() / 1000);
        info.setVariableBitRate(false);
    } else if (vorbisIdentificationHeader.getNominalBitrate() != 0 && vorbisIdentificationHeader.getMaxBitrate() == 0 && vorbisIdentificationHeader.getMinBitrate() == 0) {
        //Average vbr (in kpbs)
        info.setBitrate(vorbisIdentificationHeader.getNominalBitrate() / 1000);
        info.setVariableBitRate(true);
    } else {
        //TODO need to remove comment from raf.getLength()
        info.setBitrate(computeBitrate(info.getTrackLength(), raf.length()));
        info.setVariableBitRate(true);
    }
    logger.fine("Finished");
    return info;
}
Also used : CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) GenericAudioHeader(org.jaudiotagger.audio.generic.GenericAudioHeader)

Example 3 with GenericAudioHeader

use of org.jaudiotagger.audio.generic.GenericAudioHeader in project MusicDNA by harjot-oberai.

the class RealFileReader method getEncodingInfo.

@Override
protected GenericAudioHeader getEncodingInfo(RandomAccessFile raf) throws CannotReadException, IOException {
    final GenericAudioHeader rv = new GenericAudioHeader();
    final RealChunk prop = findPropChunk(raf);
    final DataInputStream dis = prop.getDataInputStream();
    final int objVersion = Utils.readUint16(dis);
    if (objVersion == 0) {
        final long maxBitRate = Utils.readUint32(dis) / 1000;
        final long avgBitRate = Utils.readUint32(dis) / 1000;
        final long maxPacketSize = Utils.readUint32(dis);
        final long avgPacketSize = Utils.readUint32(dis);
        final long packetCnt = Utils.readUint32(dis);
        final int duration = Utils.readUint32AsInt(dis) / 1000;
        final long preroll = Utils.readUint32(dis);
        final long indexOffset = Utils.readUint32(dis);
        final long dataOffset = Utils.readUint32(dis);
        final int numStreams = Utils.readUint16(dis);
        final int flags = Utils.readUint16(dis);
        rv.setBitrate((int) avgBitRate);
        rv.setLength(duration);
        rv.setVariableBitRate(maxBitRate != avgBitRate);
    }
    return rv;
}
Also used : DataInputStream(java.io.DataInputStream) GenericAudioHeader(org.jaudiotagger.audio.generic.GenericAudioHeader)

Example 4 with GenericAudioHeader

use of org.jaudiotagger.audio.generic.GenericAudioHeader in project MusicDNA by harjot-oberai.

the class WavInfoReader method read.

public GenericAudioHeader read(RandomAccessFile raf) throws CannotReadException, IOException {
    // Reads wav header----------------------------------------
    GenericAudioHeader info = new GenericAudioHeader();
    if (raf.length() < 12) {
        throw new CannotReadException("This is not a WAV File (<12 bytes)");
    }
    byte[] b = new byte[12];
    raf.read(b);
    WavRIFFHeader wh = new WavRIFFHeader(b);
    if (wh.isValid()) {
        b = new byte[34];
        raf.read(b);
        WavFormatHeader wfh = new WavFormatHeader(b);
        if (wfh.isValid()) {
            // Populates
            // encodingInfo----------------------------------------------------
            info.setPreciseLength(((float) raf.length() - (float) 36) / wfh.getBytesPerSecond());
            info.setChannelNumber(wfh.getChannelNumber());
            info.setSamplingRate(wfh.getSamplingRate());
            info.setBitsPerSample(wfh.getBitsPerSample());
            info.setEncodingType("WAV-RIFF " + wfh.getBitsPerSample() + " bits");
            info.setExtraEncodingInfos("");
            info.setBitrate(wfh.getBytesPerSecond() * 8 / 1000);
            info.setVariableBitRate(false);
        } else {
            throw new CannotReadException("Wav Format Header not valid");
        }
    } else {
        throw new CannotReadException("Wav RIFF Header not valid");
    }
    return info;
}
Also used : CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) GenericAudioHeader(org.jaudiotagger.audio.generic.GenericAudioHeader)

Example 5 with GenericAudioHeader

use of org.jaudiotagger.audio.generic.GenericAudioHeader in project MusicDNA by harjot-oberai.

the class AsfFileReader method getEncodingInfo.

/**
     * (overridden)
     * 
     * @see org.jaudiotagger.audio.generic.AudioFileReader#getEncodingInfo(java.io.RandomAccessFile)
     */
@Override
protected GenericAudioHeader getEncodingInfo(final RandomAccessFile raf) throws CannotReadException, IOException {
    raf.seek(0);
    GenericAudioHeader info;
    try {
        final AsfHeader header = AsfHeaderReader.readInfoHeader(raf);
        if (header == null) {
            throw new CannotReadException("Some values must have been " + "incorrect for interpretation as asf with wma content.");
        }
        info = getAudioHeader(header);
    } catch (final Exception e) {
        if (e instanceof IOException) {
            throw (IOException) e;
        } else if (e instanceof CannotReadException) {
            throw (CannotReadException) e;
        } else {
            throw new CannotReadException("Failed to read. Cause: " + e.getMessage(), e);
        }
    }
    return info;
}
Also used : CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) AsfHeader(org.jaudiotagger.audio.asf.data.AsfHeader) ReadOnlyFileException(org.jaudiotagger.audio.exceptions.ReadOnlyFileException) CannotReadException(org.jaudiotagger.audio.exceptions.CannotReadException) TagException(org.jaudiotagger.tag.TagException) InvalidAudioFrameException(org.jaudiotagger.audio.exceptions.InvalidAudioFrameException) GenericAudioHeader(org.jaudiotagger.audio.generic.GenericAudioHeader)

Aggregations

GenericAudioHeader (org.jaudiotagger.audio.generic.GenericAudioHeader)5 CannotReadException (org.jaudiotagger.audio.exceptions.CannotReadException)4 DataInputStream (java.io.DataInputStream)1 AsfHeader (org.jaudiotagger.audio.asf.data.AsfHeader)1 InvalidAudioFrameException (org.jaudiotagger.audio.exceptions.InvalidAudioFrameException)1 ReadOnlyFileException (org.jaudiotagger.audio.exceptions.ReadOnlyFileException)1 TagException (org.jaudiotagger.tag.TagException)1