Package entagged.audioformats

Examples of entagged.audioformats.EncodingInfo


import java.io.*;

public class MpcInfoReader {

  public EncodingInfo read( RandomAccessFile raf ) throws CannotReadException, IOException {
    EncodingInfo info = new EncodingInfo();
   
    //Begin info fetch-------------------------------------------
    if ( raf.length()==0 ) {
      //Empty File
      System.err.println("Error: File empty");
   
      throw new CannotReadException("File is empty");
    }
    raf.seek( 0 );
 
   
    //MP+ Header string
    byte[] b = new byte[3];
    raf.read(b);
    String mpc = new String(b);
    if (!mpc.equals("MP+") && mpc.equals("ID3")) {
      //TODO Do we have to do this ??
      //we have an ID3v2 tag at the beginning
      //We quickly jump to MPC data
      raf.seek(6);
      int tagSize = read_syncsafe_integer(raf);
      raf.seek(tagSize+10);
     
      //retry to read MPC stream
      b = new byte[3];
      raf.read(b);
      mpc = new String(b);
      if (!mpc.equals("MP+")) {
        //We could definitely not go there
        throw new CannotReadException("MP+ Header not found");
      }
    } else if (!mpc.equals("MP+")){
      throw new CannotReadException("MP+ Header not found");
    }
   
    b = new byte[25];
    raf.read(b);
    MpcHeader mpcH = new MpcHeader(b);
    //We only support v7 Stream format, so if it isn't v7, then returned values
    //will be bogus, and the file will be ignored
   
    double pcm = mpcH.getSamplesNumber();
//    info.setLength( (int) ( pcm * 1152 / mpcH.getSamplingRate() ) );
    info.setPreciseLength( (float) ( pcm * 1152 / mpcH.getSamplingRate() ) );
    info.setChannelNumber( mpcH.getChannelNumber() );
    info.setSamplingRate( mpcH.getSamplingRate() );
    info.setEncodingType( mpcH.getEncodingType() );
    info.setExtraEncodingInfos( mpcH.getEncoderInfo() );
    info.setBitrate( computeBitrate( info.getLength(), raf.length() ) );

    return info;
  }
View Full Code Here


import java.io.*;

public class Mp3InfoReader {

  public EncodingInfo read( RandomAccessFile raf ) throws CannotReadException, IOException {
    EncodingInfo encodingInfo = new EncodingInfo();
   
    //Begin info fetch-------------------------------------------
    if ( raf.length()==0 ) {
      //Empty File
      System.err.println("Error: File empty");
   
      throw new CannotReadException("File is empty");
    }
   
    int id3TagSize = 0;
    raf.seek( 0 );
  // skip id3v2 tag, because there may be long pictures inside with
  // slows reading and they can be not unsyncronized
    byte[] bbb = new byte[3];
      raf.read(bbb);
      raf.seek(0);
      String ID3 = new String(bbb);
      if (ID3.equals("ID3")) {
        raf.seek(6);
        id3TagSize = read_syncsafe_integer(raf);
        raf.seek(id3TagSize+10);
        //System.err.println("TagSize: "+tagSize);
      }

    MPEGFrame firstFrame = null;
   
    byte[] b = new byte[4];
    raf.read(b);
   
    // search for sync mark, but also for a right bitrate, samplerate and layer(that way you can
    // read corrupted but playable files)
    while ( !( (b[0]&0xFF)==0xFF  &&  (b[1]&0xE0)==0xE0 && (b[1]&0x06)!=&& (b[2]&0xF0)!=0xF0  && (b[2]&0x0C)!=0x0C ) && raf.getFilePointer() < raf.length()-4) {
      //System.err.println(sync[0]+"|"+(sync[1]&0xE0)+"|"+((sync[1]&0xE0)==0xE0));
      raf.seek( raf.getFilePointer() - 3);
      //raf.read(sync);
      raf.read(b);
    }

    //raf.seek( raf.getFilePointer() - 2 );
    //System.err.println(raf.getFilePointer());
    //raf.read( b );
    firstFrame = new MPEGFrame( b );
    //System.err.println(b[0]+"|"+b[1]+"|"+b[2]+"|"+b[3]);
    //System.err.println("Frame at offset:"+new Long(raf.getFilePointer()-4)+firstFrame);

    if ( firstFrame == null || !firstFrame.isValid() || firstFrame.getSamplingRate() == 0 ) {
      //MP3File corrupted, no valid MPEG frames
      //System.err.println("Error: could not synchronize to first mp3 frame");
     
      throw new CannotReadException("Error: could not synchronize to first mp3 frame");
    }

    int firstFrameLength = firstFrame.getFrameLength();
    //----------------------------------------------------------------------------
    int skippedLength = 0;

    if ( firstFrame.getMPEGVersion() == MPEGFrame.MPEG_VERSION_1 && firstFrame.getChannelMode() == MPEGFrame.CHANNEL_MODE_MONO ) {
      raf.seek( raf.getFilePointer() + 17 );
      skippedLength += 17;
    }
    else if ( firstFrame.getMPEGVersion() == MPEGFrame.MPEG_VERSION_1 ) {
      raf.seek( raf.getFilePointer() + 32 );
      skippedLength += 32;
    }
    else if ( firstFrame.getMPEGVersion() == MPEGFrame.MPEG_VERSION_2 && firstFrame.getChannelMode() == MPEGFrame.CHANNEL_MODE_MONO ) {
      raf.seek( raf.getFilePointer() + 9 );
      skippedLength += 9;
    }
    else if ( firstFrame.getMPEGVersion() == MPEGFrame.MPEG_VERSION_2 ) {
      raf.seek( raf.getFilePointer() + 17 );
      skippedLength += 17;
    }
    int optionalFrameLength = 0;
    //System.err.println(mp3File);
    //System.err.println(raf.getFilePointer());
    byte[] xingPart1 = new byte[16];

    raf.read( xingPart1 );
    raf.seek( raf.getFilePointer() + 100 );

    byte[] xingPart2 = new byte[4];

    raf.read( xingPart2 );
   
    VbrInfoFrame vbrInfoFrame = new XingMPEGFrame( xingPart1, xingPart2 );
    if ( vbrInfoFrame.isValid() ) {
      optionalFrameLength += 120;
      byte[] lameHeader = new byte[36];
      raf.read( lameHeader );

      LameMPEGFrame currentLameFrame = new LameMPEGFrame( lameHeader );
      if ( !currentLameFrame.isValid() )
        raf.seek( raf.getFilePointer() - 36 ); //Skipping Lame frame reading
      else
        optionalFrameLength += 36; //Lame Frame read
     
      raf.seek( raf.getFilePointer() + firstFrameLength - ( skippedLength + optionalFrameLength + 4 ) );
    } else {
      //Skipping Xing frame reading
      raf.seek( raf.getFilePointer() - 120 - skippedLength + 32)//120 Xing bytes, unused skipped bytes then go to vbri location

      // Try to read VBRI frame
      byte[] vbriHeader = new byte[18];
      raf.read( vbriHeader );
     
      vbrInfoFrame = new VBRIMPEGFrame(vbriHeader);
     
      raf.seek( raf.getFilePointer() - 18 - 4); //18 VBRI bytes and 4 mpeg info bytes
    }
   
    //----------------------------------------------------------------------------
    //Length computation
    double timePerFrame = ((double) firstFrame.getSampleNumber()) / firstFrame.getSamplingRate();   
   
    double lengthInSeconds;
    if (vbrInfoFrame.isValid()) {
        //Preffered Method: extracts time length with the Xing Header (vbr:Xing or cbr:Info) or VBRI****************
        lengthInSeconds = ( timePerFrame * vbrInfoFrame.getFrameCount() );
        encodingInfo.setVbr(vbrInfoFrame.isVbr());
        int fs = vbrInfoFrame.getFileSize();
       
        encodingInfo.setBitrate((int)( ( (fs==0 ? raf.length()-id3TagSize : fs) * 8 ) / ( timePerFrame * vbrInfoFrame.getFrameCount() * 1000 ) ));
    }
    else {
        //Default Method: extracts time length using the file length and assuming CBR********************
        int frameLength = firstFrame.getFrameLength();
      if (frameLength==0)
        throw new CannotReadException("Error while reading header(maybe file is corrupted, or missing first mpeg frame before xing header)");

        lengthInSeconds =  timePerFrame * ((raf.length()-id3TagSize) / frameLength);
       
        encodingInfo.setVbr(false);
        encodingInfo.setBitrate( firstFrame.getBitrate() );
    }
   
    //Populates encodingInfo----------------------------------------------------
    encodingInfo.setPreciseLength ((float)lengthInSeconds );
    encodingInfo.setChannelNumber( firstFrame.getChannelNumber() );
    encodingInfo.setSamplingRate( firstFrame.getSamplingRate() );
    encodingInfo.setEncodingType( firstFrame.MPEGVersionToString( firstFrame.getMPEGVersion() ) + " || " + firstFrame.layerToString( firstFrame.getLayerVersion() ) );
    encodingInfo.setExtraEncodingInfos( "" );
 
    return encodingInfo;
  }
View Full Code Here

    RandomAccessFile raf = null;
    try{
      raf = new RandomAccessFile( f, "r" );
      raf.seek( 0 );
     
      EncodingInfo info = getEncodingInfo(raf);
   
      Tag tag;
      try {
        raf.seek( 0 );
        tag = getTag(raf);
View Full Code Here

import java.io.*;

public class OggInfoReader {
  public EncodingInfo read( RandomAccessFile raf throws CannotReadException, IOException {
    EncodingInfo info = new EncodingInfo();
   
    long oldPos = 0;
   
    //Reads the file encoding infos -----------------------------------
    raf.seek( 0 );
    double PCMSamplesNumber = -1;
    raf.seek( raf.length()-2);
    while(raf.getFilePointer() >= 4) {
      if(raf.read()==0x53) {
        raf.seek( raf.getFilePointer() - 4);
        byte[] ogg = new byte[3];
        raf.readFully(ogg);
        if(ogg[0]==0x4F && ogg[1]==0x67 && ogg[2]==0x67) {
          raf.seek( raf.getFilePointer() - 3);
         
          oldPos = raf.getFilePointer();
          raf.seek(raf.getFilePointer() + 26);
          int pageSegments = raf.readByte()&0xFF; //Unsigned
          raf.seek( oldPos );
         
          byte[] b = new byte[27 + pageSegments];
          raf.readFully( b );

          OggPageHeader pageHeader = new OggPageHeader( b );
          raf.seek(0);
          PCMSamplesNumber = pageHeader.getAbsoluteGranulePosition();
          break;
        }
      } 
      raf.seek( raf.getFilePointer() - 2);
    }
   
    if(PCMSamplesNumber == -1){
   
      throw new CannotReadException("Error: Could not find the Ogg Setup block");
    }
   

    //Supposing 1st page = codec infos
    //      2nd page = comment+decode info
    //...Extracting 1st page
    byte[] b = new byte[4];
   
    oldPos = raf.getFilePointer();
    raf.seek(26);
    int pageSegments = raf.read()&0xFF; //Unsigned
    raf.seek( oldPos );

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

    OggPageHeader pageHeader = new OggPageHeader( b );

    byte[] vorbisData = new byte[pageHeader.getPageLength()];

    raf.read( vorbisData );

    VorbisCodecHeader vorbisCodecHeader = new VorbisCodecHeader( vorbisData );

    //Populates encodingInfo----------------------------------------------------
    info.setPreciseLength( (float) (PCMSamplesNumber / vorbisCodecHeader.getSamplingRate()));
    info.setChannelNumber( vorbisCodecHeader.getChannelNumber() );
    info.setSamplingRate( vorbisCodecHeader.getSamplingRate() );
    info.setEncodingType( vorbisCodecHeader.getEncodingType() );
    info.setExtraEncodingInfos( "" );
   
    if(vorbisCodecHeader.getNominalBitrate() != 0
            && vorbisCodecHeader.getMaxBitrate() == vorbisCodecHeader.getNominalBitrate()
            && vorbisCodecHeader.getMinBitrate() == vorbisCodecHeader.getNominalBitrate()) {
        //CBR (in kbps)
        info.setBitrate(vorbisCodecHeader.getNominalBitrate() / 1000);
        info.setVbr(false);
    }
    else if(vorbisCodecHeader.getNominalBitrate() != 0
            && vorbisCodecHeader.getMaxBitrate() == 0
            && vorbisCodecHeader.getMinBitrate() == 0) {
        //Average vbr (in kpbs)
        info.setBitrate(vorbisCodecHeader.getNominalBitrate() / 1000);
        info.setVbr(true);
    }
    else {
      info.setBitrate( computeBitrate( info.getLength(), raf.length() ) );
      info.setVbr(true);
    }
   
    return info;
  }
View Full Code Here

public class WavInfoReader {
  public EncodingInfo read(RandomAccessFile raf) throws CannotReadException,
      IOException {
    // Reads wav header----------------------------------------
    EncodingInfo info = new EncodingInfo();

    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[24];
      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.setEncodingType("WAV-RIFF " + wfh.getBitrate() + " bits");
        info.setExtraEncodingInfos("");
        info.setBitrate(wfh.getBytesPerSecond() * 8 / 1000);
        info.setVbr(false);
      } else {
        throw new CannotReadException("Wav Format Header not valid");
      }
    } else {
      throw new CannotReadException("Wav RIFF Header not valid");
View Full Code Here

      isLastBlock = mbh.isLastBlock();
      mbh = null; //Free memory
    }
    assert mbdsi != null;

    EncodingInfo info = new EncodingInfo();
//    info.setLength(mbdsi.getLength());
    info.setPreciseLength(mbdsi.getPreciseLength());
    info.setChannelNumber(mbdsi.getChannelNumber());
    info.setSamplingRate(mbdsi.getSamplingRate());
    info.setEncodingType(mbdsi.getEncodingType());
    info.setExtraEncodingInfos("");
    info.setBitrate(computeBitrate(mbdsi.getLength(), raf.length()));

    return info;
  }
View Full Code Here

import java.io.*;

public class MonkeyInfoReader {

  public EncodingInfo read( RandomAccessFile raf ) throws CannotReadException, IOException {
    EncodingInfo info = new EncodingInfo();
   
    //Begin info fetch-------------------------------------------
    if ( raf.length()==0 ) {
      //Empty File
      System.err.println("Error: File empty");
   
      throw new CannotReadException("File is empty");
    }
    raf.seek( 0 );
 
    //MP+ Header string
    byte[] b = new byte[4];
    raf.read(b);
    String mpc = new String(b);
    if (!mpc.equals("MAC ")) {
      throw new CannotReadException("'MAC ' Header not found");
    }
   
    b = new byte[4];
    raf.read(b);
    int version = Utils.getNumber(b, 0,3);
    if(version < 3970)
      throw new CannotReadException("Monkey Audio version <= 3.97 is not supported");
   
    b = new byte[44];
    raf.read(b);
    MonkeyDescriptor md = new MonkeyDescriptor(b);
   
    b = new byte[24];
    raf.read(b);
    MonkeyHeader mh = new MonkeyHeader(b);
   
    raf.seek(md.getRiffWavOffset());
    b = new byte[12];
    raf.read(b);
    WavRIFFHeader wrh = new WavRIFFHeader(b);
    if(!wrh.isValid())
      throw new CannotReadException("No valid RIFF Header found");
   
    b = new byte[24];
    raf.read(b);
    WavFormatHeader wfh = new WavFormatHeader(b);
    if(!wfh.isValid())
      throw new CannotReadException("No valid WAV Header found");
   
//    info.setLength( mh.getLength());
    info.setPreciseLength(mh.getPreciseLength());
    info.setChannelNumber( wfh.getChannelNumber() );
    info.setSamplingRate( wfh.getSamplingRate() );
    info.setBitrate( computeBitrate(info.getLength(), raf.length()) );
   
    info.setEncodingType( "Monkey Audio v" + (((double)version)/1000)+", compression level "+mh.getCompressionLevel());
    info.setExtraEncodingInfos( "" );
   
    return info;
  }
View Full Code Here

   * @see entagged.audioformats.generic.AudioFileReader#getEncodingInfo(java.io.RandomAccessFile)
   */
  protected EncodingInfo getEncodingInfo(RandomAccessFile raf)
      throws CannotReadException, IOException {
    raf.seek(0);
    EncodingInfo info = new EncodingInfo();
    try {
      AsfHeader header = AsfHeaderReader.readHeader(raf);
      if (header == null) {
        throw new CannotReadException(
            "Some values must have been "
                + "incorrect for interpretation as asf with wma content.");
      }
      info.setBitrate(header.getAudioStreamChunk().getKbps());
      info.setChannelNumber((int) header.getAudioStreamChunk()
          .getChannelCount());
      info.setEncodingType("ASF (audio): "
          + header.getAudioStreamChunk().getCodecDescription());
      info.setPreciseLength(header.getFileHeader().getPreciseDuration());
      info.setSamplingRate((int) header.getAudioStreamChunk()
          .getSamplingRate());
    } catch (Exception e) {
      if (e instanceof IOException)
        throw (IOException) e;
      else if (e instanceof CannotReadException)
View Full Code Here

import entagged.audioformats.EncodingInfo;
import entagged.audioformats.exceptions.CannotReadException;

public class Mp4InfoReader {
    public EncodingInfo read( RandomAccessFile raf ) throws CannotReadException, IOException {
        EncodingInfo info = new EncodingInfo();
       
        Mp4Box box = new Mp4Box();
       
        //Get to the facts
        //1-Searching for "moov"
        seek(raf, box, "moov");
       
        //2-Searching for "udta"
        seek(raf, box, "mvhd");
       
        byte[] b = new byte[box.getOffset()-8];
        raf.read(b);
       
        Mp4MvhdBox mvhd = new Mp4MvhdBox(b);
        info.setLength(mvhd.getLength());
       
        System.out.println(info);
        return info;
    }
View Full Code Here

TOP

Related Classes of entagged.audioformats.EncodingInfo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.