// --------------------------------------------------------------------------- // 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); }
// --------------------------------------------------------------------------- // 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); }
// --------------------------------------------------------------------------- // 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); }
// --------------------------------------------------------------------------- // 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); }
// --------------------------------------------------------------------------- //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); }
/* -------------------------------------------------------------------------- */ // 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); }