Example #1
0
        // ---------------------------------------------------------------------------

        // No explicit destructors with C#

        // ---------------------------------------------------------------------------

        public bool ReadFromFile(String FileName)
        {
            FileStream   fs     = null;
            BinaryReader Source = null;

            bool result = false;

            try
            {
                // Reset data and search for file tag
                FResetData();
                FID3v1.ReadFromFile(FileName);
                FID3v2.ReadFromFile(FileName);
                FAPEtag.ReadFromFile(FileName);
                // Set read-access, open file and get file length
                fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                fs.Lock(0, fs.Length);
                Source = new BinaryReader(fs);

                FFileLength = fs.Length;
                // Read header data
                Source.BaseStream.Seek(ID3v2.Size, SeekOrigin.Begin);

                //Source.Read(FHeader, sizeof(FHeader));

                FHeader.ID            = Source.ReadChars(4);
                FHeader.Size          = Source.ReadUInt32();
                FHeader.Length        = Source.ReadUInt32();
                FHeader.HiLength      = Source.ReadUInt16();
                FHeader.SampleType    = Source.ReadByte();
                FHeader.ChannelMode   = Source.ReadByte();
                FHeader.SampleRate    = Source.ReadInt32();
                FHeader.EncoderID     = Source.ReadUInt16();
                FHeader.CompressionID = Source.ReadByte();

                if (Utils.StringEqualsArr("OFR ", FHeader.ID))
                {
                    result = true;
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.StackTrace);
                result = false;
            }

            if (fs != null)
            {
                fs.Unlock(0, fs.Length);
                if (Source != null)
                {
                    Source.Close();
                }
            }
            return(result);
        }
Example #2
0
        // ---------------------------------------------------------------------------

        // No explicit destructor in C#

        // ---------------------------------------------------------------------------

        // Read data from file
        public bool ReadFromFile(String FileName)
        {
            FileStream   fs     = null;
            BinaryReader Source = null;

            bool result = false;

            FResetData();
            // At first search for tags, then try to recognize header type
            FID3v2.ReadFromFile(FileName);
            FID3v1.ReadFromFile(FileName);
            FAPEtag.ReadFromFile(FileName);
            try
            {
                fs = new FileStream(FileName, FileMode.Open);
                fs.Lock(0, fs.Length);
                Source = new BinaryReader(fs);

                FFileSize     = (int)fs.Length;
                FHeaderTypeID = FRecognizeHeaderType(Source);
                // Read header data
                if (AAC_HEADER_TYPE_ADIF == FHeaderTypeID)
                {
                    FReadADIF(Source);
                }
                if (AAC_HEADER_TYPE_ADTS == FHeaderTypeID)
                {
                    FReadADTS(Source);
                }

                result = true;
            }
            catch (Exception e)
            {
                result = false;
                System.Console.WriteLine(e.StackTrace);
                //LogDelegator.GetLogDelegate()( Log.LV_ERROR, e.Message);
            }

            if (fs != null)
            {
                fs.Unlock(0, fs.Length);
                if (Source != null)
                {
                    Source.Close();
                }
            }

            return(result);
        }
Example #3
0
        // ---------------------------------------------------------------------------

        // No explicit destructors with C#

        // ---------------------------------------------------------------------------

        public bool ReadFromFile(String FileName)
        {
            HeaderRecord Header = new HeaderRecord();
            bool         result;

            // Reset data and load header from file to variable
            FResetData();

            Array.Clear(Header.ByteArray, 0, Header.ByteArray.Length);
            Array.Clear(Header.IntegerArray, 0, Header.IntegerArray.Length);
            Header.FileSize  = 0;
            Header.ID3v2Size = 0;

            // At first try to load ID3v2 tag data, then header
            if (FID3v2.ReadFromFile(FileName))
            {
                Header.ID3v2Size = FID3v2.Size;
            }

            result = ReadHeader(FileName, ref Header);
            // Process data if loaded and file valid
            if ((result) && (Header.FileSize > 0) && (GetStreamVersion(Header) > 0))
            {
                FValid = true;
                // Fill properties with header data
                FSampleRate    = GetSampleRate(Header);
                FChannelModeID = GetChannelModeID(Header);
                FFileSize      = Header.FileSize;
                FFrameCount    = GetFrameCount(Header);
                FBitRate       = GetBitRate(Header);
                FStreamVersion = GetStreamVersion(Header);
                FProfileID     = GetProfileID(Header);
                FEncoder       = GetEncoder(Header);
                FID3v1.ReadFromFile(FileName);
                FAPEtag.ReadFromFile(FileName);
            }
            return(result);
        }
Example #4
0
        // ---------------------------------------------------------------------------

        // No explicit destructors in C#

        // ---------------------------------------------------------------------------

        public bool ReadFromFile(String FileName)
        {
            APE_HEADER     APE      = new APE_HEADER();                 // common header
            APE_HEADER_OLD APE_OLD  = new APE_HEADER_OLD();             // old header   <= 3.97
            APE_HEADER_NEW APE_NEW  = new APE_HEADER_NEW();             // new header   >= 3.98
            APE_DESCRIPTOR APE_DESC = new APE_DESCRIPTOR();             // extra header >= 3.98

            FileStream   fs         = null;
            BinaryReader SourceFile = null;

            int  BlocksPerFrame;
            bool LoadSuccess;
            int  TagSize;
            bool result = false;

            FResetData();

            // load tags first
            FID3v2.ReadFromFile(FileName);
            FID3v1.ReadFromFile(FileName);
            FAPEtag.ReadFromFile(FileName);

            // calculate total tag size
            TagSize = 0;
            if (FID3v1.Exists)
            {
                TagSize += 128;
            }
            if (FID3v2.Exists)
            {
                TagSize += FID3v2.Size;
            }
            if (FAPEtag.Exists)
            {
                TagSize += FAPEtag.Size;
            }

            // reading data from file
            LoadSuccess = false;

            try
            {
                try
                {
                    fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                    fs.Lock(0, fs.Length);
                    SourceFile = new BinaryReader(fs);
                    FFileSize  = fs.Length;

                    // seek past id3v2-tag
                    if (FID3v2.Exists)
                    {
                        fs.Seek(FID3v2.Size, SeekOrigin.Begin);
                    }
                    // Read APE Format Header
                    Array.Clear(APE.cID, 0, APE.cID.Length);
                    APE.nVersion = 0;

                    APE.cID      = SourceFile.ReadChars(4);
                    APE.nVersion = SourceFile.ReadUInt16();

                    if (Utils.StringEqualsArr("MAC ", APE.cID))
                    {
                        FVersion = APE.nVersion;

                        FVersionStr = ((double)FVersion / 1000).ToString().Substring(0, 4);                        //Str(FVersion / 1000 : 4 : 2, FVersionStr);

                        // Load New Monkey's Audio Header for version >= 3.98
                        if (APE.nVersion >= 3980)
                        {
                            APE_DESC.padded                 = 0;
                            APE_DESC.nDescriptorBytes       = 0;
                            APE_DESC.nHeaderBytes           = 0;
                            APE_DESC.nSeekTableBytes        = 0;
                            APE_DESC.nHeaderDataBytes       = 0;
                            APE_DESC.nAPEFrameDataBytes     = 0;
                            APE_DESC.nAPEFrameDataBytesHigh = 0;
                            APE_DESC.nTerminatingDataBytes  = 0;
                            Array.Clear(APE_DESC.cFileMD5, 0, APE_DESC.cFileMD5.Length);

                            APE_DESC.padded                 = SourceFile.ReadUInt16();
                            APE_DESC.nDescriptorBytes       = SourceFile.ReadUInt32();
                            APE_DESC.nHeaderBytes           = SourceFile.ReadUInt32();
                            APE_DESC.nSeekTableBytes        = SourceFile.ReadUInt32();
                            APE_DESC.nHeaderDataBytes       = SourceFile.ReadUInt32();
                            APE_DESC.nAPEFrameDataBytes     = SourceFile.ReadUInt32();
                            APE_DESC.nAPEFrameDataBytesHigh = SourceFile.ReadUInt32();
                            APE_DESC.nTerminatingDataBytes  = SourceFile.ReadUInt32();
                            APE_DESC.cFileMD5               = SourceFile.ReadBytes(16);

                            // seek past description header
                            if (APE_DESC.nDescriptorBytes != 52)
                            {
                                fs.Seek(APE_DESC.nDescriptorBytes - 52, SeekOrigin.Current);
                            }
                            // load new ape_header
                            if (APE_DESC.nHeaderBytes > 24 /*sizeof(APE_NEW)*/)
                            {
                                APE_DESC.nHeaderBytes = 24 /*sizeof(APE_NEW)*/;
                            }

                            APE_NEW.nCompressionLevel = 0;
                            APE_NEW.nFormatFlags      = 0;
                            APE_NEW.nBlocksPerFrame   = 0;
                            APE_NEW.nFinalFrameBlocks = 0;
                            APE_NEW.nTotalFrames      = 0;
                            APE_NEW.nBitsPerSample    = 0;
                            APE_NEW.nChannels         = 0;
                            APE_NEW.nSampleRate       = 0;

                            APE_NEW.nCompressionLevel = SourceFile.ReadUInt16();
                            APE_NEW.nFormatFlags      = SourceFile.ReadUInt16();
                            APE_NEW.nBlocksPerFrame   = SourceFile.ReadUInt32();
                            APE_NEW.nFinalFrameBlocks = SourceFile.ReadUInt32();
                            APE_NEW.nTotalFrames      = SourceFile.ReadUInt32();
                            APE_NEW.nBitsPerSample    = SourceFile.ReadUInt16();
                            APE_NEW.nChannels         = SourceFile.ReadUInt16();
                            APE_NEW.nSampleRate       = SourceFile.ReadUInt32();

                            // based on MAC SDK 3.98a1 (APEinfo.h)
                            FSampleRate      = (int)APE_NEW.nSampleRate;
                            FChannels        = APE_NEW.nChannels;
                            FFormatFlags     = APE_NEW.nFormatFlags;
                            FBits            = APE_NEW.nBitsPerSample;
                            FCompressionMode = APE_NEW.nCompressionLevel;
                            // calculate total uncompressed samples
                            if (APE_NEW.nTotalFrames > 0)
                            {
                                FTotalSamples = (long)(APE_NEW.nBlocksPerFrame) *
                                                (long)(APE_NEW.nTotalFrames - 1) +
                                                (long)(APE_NEW.nFinalFrameBlocks);
                            }
                            LoadSuccess = true;
                        }
                        else
                        {
                            // Old Monkey <= 3.97

                            APE_OLD.nCompressionLevel = 0;
                            APE_OLD.nFormatFlags      = 0;
                            APE_OLD.nChannels         = 0;
                            APE_OLD.nSampleRate       = 0;
                            APE_OLD.nHeaderBytes      = 0;
                            APE_OLD.nTerminatingBytes = 0;
                            APE_OLD.nTotalFrames      = 0;
                            APE_OLD.nFinalFrameBlocks = 0;
                            APE_OLD.nInt = 0;

                            APE_OLD.nCompressionLevel = SourceFile.ReadUInt16();
                            APE_OLD.nFormatFlags      = SourceFile.ReadUInt16();
                            APE_OLD.nChannels         = SourceFile.ReadUInt16();
                            APE_OLD.nSampleRate       = SourceFile.ReadUInt32();
                            APE_OLD.nHeaderBytes      = SourceFile.ReadUInt32();
                            APE_OLD.nTerminatingBytes = SourceFile.ReadUInt32();
                            APE_OLD.nTotalFrames      = SourceFile.ReadUInt32();
                            APE_OLD.nFinalFrameBlocks = SourceFile.ReadUInt32();
                            APE_OLD.nInt = SourceFile.ReadInt32();

                            FCompressionMode = APE_OLD.nCompressionLevel;
                            FSampleRate      = (int)APE_OLD.nSampleRate;
                            FChannels        = APE_OLD.nChannels;
                            FFormatFlags     = APE_OLD.nFormatFlags;
                            FBits            = 16;
                            if ((APE_OLD.nFormatFlags & MONKEY_FLAG_8_BIT) != 0)
                            {
                                FBits = 8;
                            }
                            if ((APE_OLD.nFormatFlags & MONKEY_FLAG_24_BIT) != 0)
                            {
                                FBits = 24;
                            }

                            FHasSeekElements = ((APE_OLD.nFormatFlags & MONKEY_FLAG_PEAK_LEVEL) != 0);
                            FWavNotStored    = ((APE_OLD.nFormatFlags & MONKEY_FLAG_SEEK_ELEMENTS) != 0);
                            FHasPeakLevel    = ((APE_OLD.nFormatFlags & MONKEY_FLAG_WAV_NOT_STORED) != 0);

                            if (FHasPeakLevel)
                            {
                                FPeakLevel      = (uint)APE_OLD.nInt;
                                FPeakLevelRatio = (FPeakLevel / (1 << FBits) / 2.0) * 100.0;
                            }

                            // based on MAC_SDK_397 (APEinfo.cpp)
                            if (FVersion >= 3950)
                            {
                                BlocksPerFrame = 73728 * 4;
                            }
                            else if ((FVersion >= 3900) || ((FVersion >= 3800) && (MONKEY_COMPRESSION_EXTRA_HIGH == APE_OLD.nCompressionLevel)))
                            {
                                BlocksPerFrame = 73728;
                            }
                            else
                            {
                                BlocksPerFrame = 9216;
                            }

                            // calculate total uncompressed samples
                            if (APE_OLD.nTotalFrames > 0)
                            {
                                FTotalSamples = (long)(APE_OLD.nTotalFrames - 1) *
                                                (long)(BlocksPerFrame) +
                                                (long)(APE_OLD.nFinalFrameBlocks);
                            }
                            LoadSuccess = true;
                        }
                        if (LoadSuccess)
                        {
                            // compression profile name
                            if ((0 == (FCompressionMode % 1000)) && (FCompressionMode <= 6000))
                            {
                                FCompressionModeStr = MONKEY_COMPRESSION[FCompressionMode / 1000];                                 // int division
                            }
                            else
                            {
                                FCompressionModeStr = FCompressionMode.ToString();
                            }
                            // length
                            if (FSampleRate > 0)
                            {
                                FDuration = ((double)FTotalSamples / FSampleRate);
                            }
                            // average bitrate
                            if (FDuration > 0)
                            {
                                FBitrate = 8 * (FFileSize - (long)(TagSize)) / (FDuration * 1000);
                            }
                            // some extra sanity checks
                            FValid = ((FBits > 0) && (FSampleRate > 0) && (FTotalSamples > 0) && (FChannels > 0));
                            result = FValid;
                        }
                    }
                }
                finally
                {
                    if (fs != null)
                    {
                        fs.Unlock(0, fs.Length);
                        if (SourceFile != null)
                        {
                            SourceFile.Close();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.StackTrace);
                result = false;
            }
            return(result);
        }
Example #5
0
        /* -------------------------------------------------------------------------- */

        public bool GetInfo(String sFile, bool bSetTags)
        {
            FileStream   fs = null;
            BinaryReader r  = null;

            byte[] aMetaDataBlockHeader = new byte[4];
            int    iBlockLength;
            int    iMetaType;
            int    iIndex;
            bool   bPaddingFound;

            bool result = true;

            bPaddingFound = false;
            FResetData(true, false);

            try
            {
                // Read data from ID3 tags
                FID3v2.ReadFromFile(sFile);

                // Set read-access and open file
                fs = new FileStream(sFile, FileMode.Open, FileAccess.Read);
                fs.Lock(0, fs.Length);
                r = new BinaryReader(fs);

                FFileLength = (int)fs.Length;
                FFileName   = sFile;

                // Seek past the ID3v2 tag, if there is one
                if (FID3v2.Exists)
                {
                    fs.Seek(FID3v2.Size, SeekOrigin.Begin);
                }

                // Read header data
                FHeader.Reset();

                FHeader.StreamMarker        = r.ReadChars(4);
                FHeader.MetaDataBlockHeader = r.ReadBytes(4);
                FHeader.Info   = r.ReadBytes(18);
                FHeader.MD5Sum = r.ReadBytes(16);

                // Process data if loaded and header valid
                if (Utils.StringEqualsArr("fLaC", FHeader.StreamMarker))
                {
                    FChannels      = (byte)(((FHeader.Info[12] >> 1) & 0x7) + 1);
                    FSampleRate    = (FHeader.Info[10] << 12 | FHeader.Info[11] << 4 | FHeader.Info[12] >> 4);
                    FBitsPerSample = (byte)(((FHeader.Info[12] & 1) << 4) | (FHeader.Info[13] >> 4) + 1);
                    FSamples       = (FHeader.Info[14] << 24 | FHeader.Info[15] << 16 | FHeader.Info[16] << 8 | FHeader.Info[17]);

                    if (0 == (FHeader.MetaDataBlockHeader[1] & 0x80))                       // metadata block exists
                    {
                        iIndex = 0;
                        do                         // read more metadata blocks if available
                        {
                            aMetaDataBlockHeader = r.ReadBytes(4);

                            iIndex++;                                                                                                // metadatablock index
                            iBlockLength = (aMetaDataBlockHeader[1] << 16 | aMetaDataBlockHeader[2] << 8 | aMetaDataBlockHeader[3]); //decode length
                            if (iBlockLength <= 0)
                            {
                                break;                                                // can it be 0 ?
                            }
                            iMetaType = (aMetaDataBlockHeader[0] & 0x7F);             // decode metablock type

                            if (iMetaType == META_VORBIS_COMMENT)
                            {                              // read vorbis block
                                FVCOffset    = (int)fs.Position;
                                FTagSize     = iBlockLength;
                                FVorbisIndex = iIndex;
                                ReadTag(r, bSetTags);                                 // set up fields
                            }
                            else
                            {
                                if ((iMetaType == META_PADDING) && (!bPaddingFound))                                    // we have padding block
                                {
                                    FPadding      = iBlockLength;                                                       // if we find more skip & put them in metablock array
                                    FPaddingLast  = ((aMetaDataBlockHeader[0] & 0x80) != 0);
                                    FPaddingIndex = iIndex;
                                    bPaddingFound = true;
                                    fs.Seek(FPadding, SeekOrigin.Current); // advance into file till next block or audio data start
                                }
                                else                                       // all other
                                {
                                    if (iMetaType <= 5)                    // is it a valid metablock ?
                                    {
                                        if (META_PADDING == iMetaType)     // set flag for fragmented padding blocks
                                        {
                                            FPaddingFragments = true;
                                        }
                                        AddMetaDataOther(aMetaDataBlockHeader, r, iBlockLength, iIndex);
                                    }
                                    else
                                    {
                                        FSamples = 0;                                         //ops...
                                        break;
                                    }
                                }
                            }
                        }while (0 == (aMetaDataBlockHeader[0] & 0x80));                          // while is not last flag ( first bit == 1 )
                    }
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.StackTrace);
                //LogDelegator.GetLogDelegate()(Log.LV_ERROR,e.Message);
                result = false;
            }

            if (FIsValid())
            {
                FAudioOffset = (int)fs.Position;                                                               // we need that to rebuild the file if nedeed
                FBitrate     = Math.Round(((double)(FFileLength - FAudioOffset) / 1000) * 8 / FGetDuration()); //time to calculate average bitrate
            }
            else
            {
                result = false;
            }

            if (fs != null)
            {
                fs.Unlock(0, fs.Length);
                fs.Close();
            }

            return(result);
        }
Example #6
0
        /* -------------------------------------------------------------------------- */

        // No explicit destructors with C#

        /* -------------------------------------------------------------------------- */

        public bool ReadFromFile(String FileName)
        {
            FileStream   fs     = null;
            BinaryReader source = null;

            char[]     signatureChunk = new char[4];
            tta_header ttaheader      = new tta_header();
            long       TagSize;

            bool result = false;


            FResetData();

            // load tags first
            FID3v2.ReadFromFile(FileName);
            FID3v1.ReadFromFile(FileName);
            FAPEtag.ReadFromFile(FileName);

            // calulate total tag size
            TagSize = 0;
            if (FID3v1.Exists)
            {
                TagSize += 128;
            }
            if (FID3v2.Exists)
            {
                TagSize += FID3v2.Size;
            }
            if (FAPEtag.Exists)
            {
                TagSize += FAPEtag.Size;
            }

            // begin reading data from file
            try
            {
                fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                fs.Lock(0, fs.Length);
                source = new BinaryReader(fs);


                // seek past id3v2-tag
                if (FID3v2.Exists)
                {
                    fs.Seek(FID3v2.Size, SeekOrigin.Begin);
                }

                signatureChunk = source.ReadChars(4);
                if (Utils.StringEqualsArr("TTA1", signatureChunk))
                {
                    // start looking for chunks
                    ttaheader.Reset();

                    ttaheader.AudioFormat   = source.ReadUInt16();
                    ttaheader.NumChannels   = source.ReadUInt16();
                    ttaheader.BitsPerSample = source.ReadUInt16();
                    ttaheader.SampleRate    = source.ReadUInt32();
                    ttaheader.DataLength    = source.ReadUInt32();
                    ttaheader.CRC32         = source.ReadUInt32();

                    FFileSize = fs.Length;
                    FValid    = true;

                    FAudioFormat = ttaheader.AudioFormat;
                    FChannels    = ttaheader.NumChannels;
                    FBits        = ttaheader.BitsPerSample;
                    FSampleRate  = ttaheader.SampleRate;
                    FSamples     = ttaheader.DataLength;
                    FCRC32       = ttaheader.CRC32;

                    FBitrate  = (double)FFileSize * 8 / (FSamples / FSampleRate) / 1000;
                    FDuration = (double)ttaheader.DataLength / ttaheader.SampleRate;

                    result = true;
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.StackTrace);
                result = false;
            }

            if (fs != null)
            {
                fs.Unlock(0, fs.Length);
                if (source != null)
                {
                    source.Close();
                }
            }


            return(result);
        }
Example #7
0
        // ---------------------------------------------------------------------------

        //No explicit destructors in C#

        // ---------------------------------------------------------------------------

        public bool ReadFromFile(String FileName)
        {
            FileStream   fs         = null;
            BinaryReader SourceFile = null;

            byte[] Data = new byte[MAX_MPEG_FRAME_LENGTH * 2];

            bool result = false;

            FResetData();
            // At first search for tags, then search for a MPEG frame and VBR data
            // This part of code rewritten to add more flexibility (no need to have 3 tags to be able to read file info xD)

            //if ( (FID3v2.ReadFromFile(FileName)) && (FID3v1.ReadFromFile(FileName)) && (FAPEtag.ReadFromFile(FileName)) )
            FID3v2.ReadFromFile(FileName);
            FID3v1.ReadFromFile(FileName);
            APEtag.ReadFromFile(FileName);

            try
            {
                // Open file, read first block of data and search for a frame
                fs         = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                SourceFile = new BinaryReader(fs);

                FFileLength = (int)fs.Length;
                fs.Seek(FID3v2.Size, SeekOrigin.Begin);
                Data   = SourceFile.ReadBytes(Data.Length);
                FFrame = FindFrame(Data, ref FVBR);

                // Try to search in the middle if no frame at the beginning found
                if (!FFrame.Found)
                {
                    fs.Seek((int)System.Math.Floor((decimal)(FFileLength - FID3v2.Size) / 2), SeekOrigin.Begin);
                    Data   = SourceFile.ReadBytes(Data.Length);
                    FFrame = FindFrame(Data, ref FVBR);
                }
                // Search for vendor ID at the end if CBR encoded
                if ((FFrame.Found) && (!FVBR.Found))
                {
                    if (!FID3v1.Exists)
                    {
                        fs.Seek(FFileLength - Data.Length, SeekOrigin.Begin);
                    }
                    else
                    {
                        fs.Seek(FFileLength - Data.Length - 128, SeekOrigin.Begin);
                    }
                    Data      = SourceFile.ReadBytes(Data.Length);
                    FVendorID = FindVendorID(Data, (ushort)(FFrame.Size * 5));
                }
                result = true;
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.StackTrace);
                result = false;
            }
            if (!FFrame.Found)
            {
                FResetData();
            }

            if (SourceFile != null)
            {
                SourceFile.Close();
            }
            if (fs != null)
            {
                fs.Close();
            }

            return(result);
        }