Exemplo n.º 1
0
        /// <summary>
        /// 创建SPC文件
        /// </summary>
        /// <param name="mainHeader">文件结构</param>
        /// <param name="subTypes">子数据类型</param>
        /// <param name="dataPoints">各数据的数量</param>
        /// <param name="xDatas">X轴数据,可以为NULL</param>
        /// <param name="yDatas">Y轴数据,一条或者多条,列=mainHeader->fnpts, 行=subTypes.Length</param>
        /// <param name="logText">光谱采集参数</param>
        /// <param name="logBinary">私有数据</param>
        /// <returns>SPC内容</returns>
        private static byte[] CreateCommonXFile(SPCHeader mainHeader, byte[] subTypes, int[] dataPoints, float[] xDatas, float[] yDatas, byte[] logText, byte[] logBinary)
        {
            //xDatas=NULL表示等间距X轴
            if (xDatas == null && mainHeader.fnpts <= 0 || yDatas == null || logText == null || logText.Length == 0)
            {
                return(null);
            }

            if (xDatas == null)     //没有X轴数据,就不能有dataPoints
            {
                dataPoints = null;
                if (yDatas.Length != mainHeader.fnpts * mainHeader.fnsub)   //Y轴数据必须与文件数量和每个文件的数据点数相符
                {
                    return(null);
                }
            }
            else
            {
                if (dataPoints == null)  //如果没有dataPoints,表示只有一个xDatas,由所有Y轴共享
                {
                    if (xDatas.Length != mainHeader.fnpts || yDatas.Length != mainHeader.fnpts * mainHeader.fnsub)
                    {
                        return(null);
                    }
                }
                else    //每个Y都对应不同的X
                {
                    mainHeader.fnpts = 0;
                    mainHeader.fnsub = (uint)dataPoints.Length;
                    int count = dataPoints.Sum();
                    if (xDatas.Length != count || yDatas.Length != count)
                    {
                        return(null);
                    }
                }
            }

            IntPtr retptr   = IntPtr.Zero;
            int    datasize = 0;

            if (CommonMethod.Is64BitVersion())
            {
                retptr = SPCCreateFile64(ref mainHeader, subTypes, dataPoints, xDatas, yDatas, logText, logText == null ? 0 : logText.Length, logBinary, logBinary == null ? 0 : logBinary.Length, ref datasize);
            }
            else
            {
                retptr = SPCCreateFile32(ref mainHeader, subTypes, dataPoints, xDatas, yDatas, logText, logText == null ? 0 : logText.Length, logBinary, logBinary == null ? 0 : logBinary.Length, ref datasize);
            }

            return(CommonMethod.CopyDataArrayFromIntptrAndFree <byte>(ref retptr, datasize));
        }
Exemplo n.º 2
0
 private bool readHeader(ref BinaryReader source, ref SPCHeader header)
 {
     header.FormatTag = Utils.GetStringFromCharArray(Utils.ReadTrueChars(source, 33));
     if (SPC_FORMAT_TAG == header.FormatTag)
     {
         source.BaseStream.Seek(2, SeekOrigin.Current);
         header.TagInHeader = source.ReadByte();
         header.VersionByte = source.ReadByte();
         return(true);
     }
     else
     {
         return(false);
     }
 }
Exemplo n.º 3
0
        protected override bool read(BinaryReader source, ReadTagParams readTagParams)
        {
            bool      result = true;
            SPCHeader header = new SPCHeader();
            SPCExTags footer = new SPCExTags();

            header.Reset();
            footer.Reset();
            resetData();

            source.BaseStream.Seek(sizeInfo.ID3v2Size, SeekOrigin.Begin);

            if (!readHeader(source, ref header))
            {
                throw new InvalidDataException("Not a SPC file");
            }

            // Reads the header tag
            if (SPCHeader.TAG_IN_HEADER == header.TagInHeader)
            {
                tagExists = true;
                source.BaseStream.Seek(REGISTERS_LENGTH, SeekOrigin.Current);
                readHeaderTags(source, ref header, readTagParams);
            }

            AudioDataOffset = source.BaseStream.Position;

            // Reads extended tag
            if (source.BaseStream.Length > SPC_RAW_LENGTH)
            {
                source.BaseStream.Seek(SPC_RAW_LENGTH, SeekOrigin.Begin);
                readExtendedData(source, ref footer, readTagParams);
            }
            else
            {
                if (readTagParams.PrepareForWriting)
                {
                    structureHelper.AddZone(SPC_RAW_LENGTH, 0, ZONE_EXTENDED);
                }
            }

            AudioDataSize = sizeInfo.FileSize - header.Size - footer.Size;
            bitrate       = AudioDataSize * 8 / duration;

            return(result);
        }
Exemplo n.º 4
0
        protected override Boolean read(BinaryReader source, MetaDataIO.ReadTagParams readTagParams)
        {
            var result = true;
            var header = new SPCHeader();
            var footer = new SPCExTags();

            header.Reset();
            footer.Reset();
            resetData();

            source.BaseStream.Seek(sizeInfo.ID3v2Size, SeekOrigin.Begin);

            isValid = readHeader(source, ref header);
            if (!isValid)
            {
                throw new Exception("Not a SPC file");
            }

            // Reads the header tag
            if (SPCHeader.TAG_IN_HEADER == header.TagInHeader)
            {
                tagExists = true;
                source.BaseStream.Seek(REGISTERS_LENGTH, SeekOrigin.Current);
                readHeaderTags(source, ref header, readTagParams);
            }

            // Reads extended tag
            if (source.BaseStream.Length > SPC_RAW_LENGTH)
            {
                source.BaseStream.Seek(SPC_RAW_LENGTH, SeekOrigin.Begin);
                readExtendedData(source, ref footer, readTagParams);
            }
            else
            {
                if (readTagParams.PrepareForWriting)
                {
                    structureHelper.AddZone(SPC_RAW_LENGTH, 0, ZONE_EXTENDED);
                }
            }

            bitrate = (sizeInfo.FileSize - header.Size) * 8 / duration;

            return(result);
        }
Exemplo n.º 5
0
        // === PRIVATE METHODS ===

        private bool readHeader(BinaryReader source, ref SPCHeader header)
        {
            source.BaseStream.Seek(0, SeekOrigin.Begin);

            long initialPosition = source.BaseStream.Position;

            if (SPC_FORMAT_TAG.Equals(Utils.Latin1Encoding.GetString(source.ReadBytes(SPC_FORMAT_TAG.Length))))
            {
                source.BaseStream.Seek(8, SeekOrigin.Current); // Remainder of header tag (version marker vX.XX + 2 bytes)
                header.TagInHeader = source.ReadByte();
                header.VersionByte = source.ReadByte();
                header.Size        = source.BaseStream.Position - initialPosition;
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 读取SPC文件头
        /// </summary>
        /// <param name="fileData">文件数据</param>
        /// <param name="fileSize">文件大小</param>
        /// <returns>SPCHeader结构</returns>
        private static SPCHeader GetMainHeader(byte[] fileData, int fileSize)
        {
            IntPtr retptr   = IntPtr.Zero;
            int    datasize = 0;

            if (CommonMethod.Is64BitVersion())
            {
                retptr = SPCGetMainHeader64(fileData, fileSize, ref datasize);
            }
            else
            {
                retptr = SPCGetMainHeader32(fileData, fileSize, ref datasize);
            }

            bool      retOK;
            SPCHeader retheader = CommonMethod.CopyStructureFromIntptrAndFree <SPCHeader>(ref retptr, out retOK);

            if (!retOK)
            {
                retheader.fnpts = int.MaxValue;     //表示错误数据
            }
            return(retheader);
        }
Exemplo n.º 7
0
        private void readHeaderTags(BinaryReader source, ref SPCHeader header, ReadTagParams readTagParams)
        {
            long initialPosition = source.BaseStream.Position;

            SetMetaField(HEADER_TITLE.ToString(), Utils.Latin1Encoding.GetString(source.ReadBytes(32)).Replace("\0", "").Trim(), readTagParams.ReadAllMetaFrames, ZONE_HEADER);
            SetMetaField(HEADER_ALBUM.ToString(), Utils.Latin1Encoding.GetString(source.ReadBytes(32)).Replace("\0", "").Trim(), readTagParams.ReadAllMetaFrames, ZONE_HEADER);
            SetMetaField(HEADER_DUMPERNAME.ToString(), Utils.Latin1Encoding.GetString(source.ReadBytes(16)).Replace("\0", "").Trim(), readTagParams.ReadAllMetaFrames, ZONE_HEADER);
            SetMetaField(HEADER_COMMENT.ToString(), Utils.Latin1Encoding.GetString(source.ReadBytes(32)).Replace("\0", "").Trim(), readTagParams.ReadAllMetaFrames, ZONE_HEADER);

            byte[] date, song, fade;

            // NB : Dump date is used to determine if the tag is binary or text-based.
            // It won't be recorded as a property of TSPC
            date = source.ReadBytes(11);
            song = source.ReadBytes(3);
            fade = source.ReadBytes(5);

            bool bin;
            int  dateRes = isText(date);
            int  songRes = isText(song);
            int  fadeRes = isText(fade);

            //if ( 0 == (dateRes | songRes | fadeRes) ) // No time nor date -> use default
            //{
            bin = true;
            //}
            //else
            if ((songRes != -1) && (fadeRes != -1)) // No time, or time is text
            {
                if (dateRes > 0)                    //If date is text, then tag is text
                {
                    bin = false;
                }
                else if (0 == dateRes)              //No date
                {
                    bin = PREFER_BIN;               //Times could still be binary (ex. 56 bin = '8' txt)
                }
                else if (-1 == dateRes)             //Date contains invalid characters
                {
                    bin = true;
                    for (int i = 4; i < 8; i++)
                    {
                        bin = bin && (0 == date[i]);
                    }
                }
            }
            else
            {
                bin = true;
            }

            int fadeVal;
            int songVal;

            if (bin)
            {
                fadeVal =
                    fade[0] * 0x000001 +
                    fade[1] * 0x0000FF +
                    fade[2] * 0x00FF00 +
                    fade[3] * 0xFF0000;
                if (fadeVal > 59999)
                {
                    fadeVal = 59999;
                }

                songVal = song[0] * 0x01 + song[1] * 0x10;
                if (songVal > 959)
                {
                    songVal = 959;
                }

                source.BaseStream.Seek(-1, SeekOrigin.Current); // We're one byte ahead
                SetMetaField(HEADER_FADE.ToString(), Utils.Latin1Encoding.GetString(fade), readTagParams.ReadAllMetaFrames, ZONE_HEADER);
            }
            else
            {
                fadeVal = TrackUtils.ExtractTrackNumber(Utils.Latin1Encoding.GetString(fade));
                songVal = TrackUtils.ExtractTrackNumber(Utils.Latin1Encoding.GetString(song));

                SetMetaField(HEADER_FADE.ToString(), Utils.Latin1Encoding.GetString(fade), readTagParams.ReadAllMetaFrames, ZONE_HEADER);
            }

            SetMetaField(HEADER_DUMPDATE.ToString(), Utils.Latin1Encoding.GetString(date), readTagParams.ReadAllMetaFrames, ZONE_HEADER);
            SetMetaField(HEADER_SONGLENGTH.ToString(), Utils.Latin1Encoding.GetString(song), readTagParams.ReadAllMetaFrames, ZONE_HEADER);

            // if fadeval > 0 alone, the fade is applied on the default 3:00 duration without extending it
            if (songVal > 0)
            {
                duration = Math.Round((double)fadeVal) + songVal;
            }

            SetMetaField(HEADER_ARTIST.ToString(), Utils.Latin1Encoding.GetString(source.ReadBytes(32)).Replace("\0", "").Trim(), readTagParams.ReadAllMetaFrames, ZONE_HEADER);
            header.Size += source.BaseStream.Position - initialPosition;

            if (readTagParams.PrepareForWriting)
            {
                structureHelper.AddZone(initialPosition, (int)(source.BaseStream.Position - initialPosition), ZONE_HEADER);
            }
        }
Exemplo n.º 8
0
        // === PUBLIC METHODS ===

        public bool ReadFromFile(String fileName)
        {
            bool      result = true;
            SPCHeader header = new SPCHeader();
            SPCExTags footer = new SPCExTags();

            FResetData();

            header.Reset();
            footer.Reset();

            FileStream   fs     = null;
            BinaryReader source = null;

            try
            {
                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                fs.Lock(0, fs.Length);
                source = new BinaryReader(fs);

                if (!readHeader(ref source, ref header))
                {
                    throw new Exception("Not a PSF file");
                }

                // Reads the header tag
                if (SPCHeader.TAG_IN_HEADER == header.TagInHeader)
                {
                    source.BaseStream.Seek(REGISTERS_LENGTH, SeekOrigin.Current);
                    readHeaderTags(ref source);
                }

                // Reads extended tag
                if (fs.Length > SPC_RAW_LENGTH)
                {
                    source.BaseStream.Seek(SPC_RAW_LENGTH, SeekOrigin.Begin);
                    readExtendedData(ref source, ref footer);

                    if (footer.Items.ContainsKey(XID6_ARTIST))
                    {
                        FArtist = (String)((ExtendedItem)footer.Items[XID6_ARTIST]).Data;
                    }
                    if (footer.Items.ContainsKey(XID6_CMNTS))
                    {
                        FComment = (String)((ExtendedItem)footer.Items[XID6_CMNTS]).Data;
                    }
                    if (footer.Items.ContainsKey(XID6_SONG))
                    {
                        FTitle = (String)((ExtendedItem)footer.Items[XID6_SONG]).Data;
                    }
                    if (footer.Items.ContainsKey(XID6_COPY))
                    {
                        FYear = ((ExtendedItem)footer.Items[XID6_COPY]).Length.ToString();
                    }
                    if (footer.Items.ContainsKey(XID6_TRACK))
                    {
                        FTrack = ((ExtendedItem)footer.Items[XID6_TRACK]).Length >> 8;
                    }
                    if (footer.Items.ContainsKey(XID6_OST))
                    {
                        FAlbum = (String)((ExtendedItem)footer.Items[XID6_OST]).Data;
                    }
                    if (("" == FAlbum) && (footer.Items.ContainsKey(XID6_GAME)))
                    {
                        FAlbum = (String)((ExtendedItem)footer.Items[XID6_GAME]).Data;
                    }

                    long ticks = 0;
                    if (footer.Items.ContainsKey(XID6_LOOP))
                    {
                        ticks += Math.Min(XID6_MAXTICKS, (int)((ExtendedItem)footer.Items[XID6_LOOP]).Data);
                    }
                    if (footer.Items.ContainsKey(XID6_LOOPX))
                    {
                        ticks = ticks * Math.Min(XID6_MAXLOOP, (int)((ExtendedItem)footer.Items[XID6_LOOPX]).Length);
                    }
                    if (footer.Items.ContainsKey(XID6_INTRO))
                    {
                        ticks += Math.Min(XID6_MAXTICKS, (int)((ExtendedItem)footer.Items[XID6_INTRO]).Data);
                    }
                    if (footer.Items.ContainsKey(XID6_END))
                    {
                        ticks += Math.Min(XID6_MAXTICKS, (int)((ExtendedItem)footer.Items[XID6_END]).Data);
                    }

                    if (ticks > 0)
                    {
                        FDuration = Math.Round((double)ticks / XID6_TICKSSEC);
                    }
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.StackTrace);
                //LogDelegator.GetLogDelegate()(Log.LV_ERROR, e.Message);
                result = false;
            }

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

            return(result);
        }
Exemplo n.º 9
0
        public static byte[] SaveFile(FileFormat fileFormat)
        {
            if (fileFormat.xDatas == null || fileFormat.xDatas.Length == 0)
            {
                return(null);
            }
            if (fileFormat.yDataList == null || fileFormat.yDataList.Count == 0)
            {
                return(null);
            }
            if (fileFormat.dataInfoList == null || fileFormat.dataInfoList.Count != fileFormat.yDataList.Count)
            {
                return(null);
            }
            if (fileFormat.xDataList.Count == 1)     //统一的X轴
            {
                //检查是否所有Y轴数据点数量与X数量相同
                foreach (var item in fileFormat.yDataList)
                {
                    if (item.Length != fileFormat.xDatas.Length)
                    {
                        return(null);
                    }
                }
            }
            else
            {
                //检查是否所有Y轴数据点数量与对应的X轴数据数量相同
                if (fileFormat.xDataList.Count != fileFormat.yDataList.Count)
                {
                    return(null);
                }
                for (int i = 0; i < fileFormat.xDataList.Count; i++)
                {
                    if (fileFormat.xDataList[i].Length != fileFormat.yDataList[i].Length)
                    {
                        return(null);
                    }
                }
            }

            //子文件类型
            byte[] subTypes = (from p in fileFormat.dataInfoList select(byte) p.dataType).ToArray();

            //文件主结构
            SPCHeader header = new SPCHeader();

            header.ftflgs  = 0;
            header.fexper  = (byte)fileFormat.fileInfo.specType;
            header.fexp    = 0x80;  //float
            header.fnpts   = (uint)fileFormat.xDatas.Length;
            header.ffirst  = fileFormat.dataInfo.firstX;
            header.flast   = fileFormat.dataInfo.lastX;
            header.fnsub   = (uint)fileFormat.dataInfoList.Count;
            header.fxtype  = (byte)fileFormat.fileInfo.xType;
            header.fytype  = (byte)fileFormat.dataInfo.dataType;
            header.fdate   = CommonMethod.DataTimeToDWord(DateTime.Now);
            header.fres    = fileFormat.fileInfo.resolution.ToString(); // CommonMethod.ConvertStringToFixedByte(fileFormat.fileInfo.resolution.ToString(), header.fres, 9);
            header.fsource = fileFormat.fileInfo.instDescription;       //CommonMethod.ConvertStringToFixedByte(fileFormat.fileInfo.instDescription, header.fsource, 9);
            header.fcmnt   = fileFormat.fileInfo.fileMemo;              //CommonMethod.ConvertStringToFixedByte(fileFormat.fileInfo.fileMemo, header.fcmnt, 130);
            header.fmods   = fileFormat.fileInfo.modifyFlag;
            //CommonMethod.CopyDataArrayToFixedPtr<float>(header.fspare, 8, fileFormat.fileInfo.fspare);
            header.fspare = fileFormat.fileInfo.fspare;

            //设置Z轴格式
            if (fileFormat.dataInfoList.Count < 2)
            {
                header.fztype = 0;
                header.fzinc  = 0;
            }
            else
            {
                header.fztype = fileFormat.fileInfo.fzinc == 0 ? (byte)FileFormat.ZAXISTYPE.XMSEC : (byte)fileFormat.fileInfo.zType; //如果没有设置fzinc,缺省设置为单位=秒
                header.fzinc  = fileFormat.fileInfo.fzinc == 0 ? 0.5f : fileFormat.fileInfo.fzinc;                                   //如果没有设置fzinc,缺省设置为0.5s
            }
            string logstr = ToSPCLogData(fileFormat.acquisitionInfo);

            byte[] logText = Encoding.ASCII.GetBytes(logstr);

            int[] dataPoints = null;    //xData的数据点数

            //XDatas转换到float
            float[] floatxdatas = null;

            //判断是否为均匀的X
            double stepx = (fileFormat.dataInfo.lastX - fileFormat.dataInfo.firstX) / (fileFormat.fileInfo.dataCount - 1);

            stepx += stepx / 1000;  //稍微放大一点
            stepx  = Math.Abs(stepx);

            bool isevenlyx = true;

            if (fileFormat.xDataList.Count > 1)     //超过1个X数据,一定是多X文件
            {
                isevenlyx  = false;
                dataPoints = (from p in fileFormat.xDataList select p.Length).ToArray();    //记录每个X轴的数据点数
            }
            else
            {
                for (int i = 1; i < fileFormat.xDatas.Length; i++)
                {
                    if (Math.Abs(fileFormat.xDatas[i] - fileFormat.xDatas[i - 1]) > stepx)
                    {
                        isevenlyx = false;
                        break;
                    }
                }
            }

            //将所有X轴转换为float,合并在一个Array
            int index = 0;

            if (!isevenlyx && fileFormat.xDatas != null)
            {
                int xcount = (from p in fileFormat.xDataList select p.Length).Sum();     //所有X轴的数据数量
                floatxdatas = new float[xcount];
                foreach (var xdatas in fileFormat.xDataList)
                {
                    for (int i = 0; i < xdatas.Length; i++)
                    {
                        floatxdatas[index++] = (float)xdatas[i];
                    }
                }
            }

            //所有YDatas转换到float,合并在一个Array
            int ycount = (from p in fileFormat.yDataList select p.Length).Sum();     //所有X轴的数据数量

            float[] floatydatas = new float[ycount];
            index = 0;
            foreach (var ydatas in fileFormat.yDataList)
            {
                for (int i = 0; i < ydatas.Length; i++)
                {
                    floatydatas[index++] = (float)ydatas[i];
                }
            }

            return(CreateCommonXFile(header, subTypes, dataPoints, floatxdatas, floatydatas, logText, fileFormat.additionalData));
        }
Exemplo n.º 10
0
        public static bool ReadFile(byte[] fileData, int fileSize, FileFormat fileFormat)
        {
            fileFormat.dataInfoList    = null;
            fileFormat.acquisitionInfo = null;
            fileFormat.xDataList       = null;
            fileFormat.fileInfo        = null;
            fileFormat.yDataList       = null;

            SPCHeader mainheader = GetMainHeader(fileData, fileSize);

            if (mainheader.fnpts == int.MaxValue)  //读取文件头错误
            {
                return(false);
            }

            //填充光谱文件信息
            fileFormat.fileInfo                 = new FileFormat.FileInfo();
            fileFormat.fileInfo.createTime      = CommonMethod.DWordToDateTime(mainheader.fdate);
            fileFormat.fileInfo.dataCount       = (int)mainheader.fnpts;
            fileFormat.fileInfo.fileMemo        = mainheader.fcmnt;   //CommonMethod.ConvertFixedByteToString(mainheader.fcmnt, 130);
            fileFormat.fileInfo.instDescription = mainheader.fsource; //CommonMethod.ConvertFixedByteToString(mainheader.fsource, 9);
            fileFormat.fileInfo.modifyFlag      = mainheader.fmods;
            double tempres = 0;                                       //resolution
            string resstr  = mainheader.fres;                         // CommonMethod.ConvertFixedByteToString(mainheader.fres, 9);

            fileFormat.fileInfo.resolution = double.TryParse(resstr, out tempres) == true ? tempres : 0;

            fileFormat.fileInfo.specType = mainheader.fexper == 0 ? FileFormat.SPECTYPE.SPCNIR : (FileFormat.SPECTYPE)mainheader.fexper;
            fileFormat.fileInfo.xType    = (FileFormat.XAXISTYPE)mainheader.fxtype;
            fileFormat.fileInfo.zType    = (FileFormat.ZAXISTYPE)mainheader.fztype;
            fileFormat.fileInfo.fzinc    = mainheader.fzinc;
            fileFormat.fileInfo.fspare   = mainheader.fspare; //CommonMethod.CopyDataArrayFromFixedPtr<float>(mainheader.fspare, 8);

            //读取X轴数据
            if ((mainheader.ftflgs & (byte)Ftflgs.TXYXYS) == 0)     //统一的X轴
            {
                fileFormat.xDataList = new List <double[]>();
                double[] tempx = GetXData(fileData, fileSize, 0);
                fileFormat.xDataList.Add(tempx);
            }
            else    //多个X轴
            {
                fileFormat.xDataList = new List <double[]>();
                for (int i = 0; i < mainheader.fnsub; i++)
                {
                    double[] tempx = GetXData(fileData, fileSize, i);
                    fileFormat.xDataList.Add(tempx);
                }
            }

            //获取Y数据以及格式信息
            fileFormat.dataInfoList = new List <FileFormat.DataInfo>();
            fileFormat.yDataList    = new List <double[]>();
            for (int i = 0; i < mainheader.fnsub; i++)
            {
                SPCSubHeader subheader = GetSubHeader(fileData, fileSize, i);
                if (subheader.subnpts == int.MaxValue)      //读取结构错误
                {
                    return(false);
                }

                //读取Y轴数据
                double[] ydata = GetYData(fileData, fileSize, i);
                if (ydata == null || ydata.Length == 0)
                {
                    return(false);
                }

                //Y轴数据格式
                FileFormat.DataInfo info = new FileFormat.DataInfo();
                if (subheader.subscan == 0)      //用这个属性来记录子数据的类型(吸收谱,干涉谱等等)
                {
                    info.dataType = mainheader.fytype == 0 ? FileFormat.YAXISTYPE.YABSRB : (FileFormat.YAXISTYPE)mainheader.fytype;
                }
                else
                {
                    info.dataType = (FileFormat.YAXISTYPE)subheader.subscan;
                }
                info.firstX    = mainheader.ffirst;
                info.lastX     = mainheader.flast;
                info.maxYValue = ydata.Max();
                info.minYValue = ydata.Min();

                fileFormat.dataInfoList.Add(info);
                fileFormat.yDataList.Add(ydata);
            }

            //读取光谱参数
            string parastr = GetLogText(fileData, fileSize);

            if (parastr != null)
            {
                fileFormat.acquisitionInfo = FromSPCLogData(parastr);
            }
            if (fileFormat.acquisitionInfo == null)
            {
                fileFormat.acquisitionInfo = new FileFormat.AcquisitionInfo();
            }

            return(true);
        }
Exemplo n.º 11
0
 private static extern IntPtr SPCCreateFile64(ref SPCHeader mainHeader, byte[] subTypes, int[] dataPoints, float[] xDatas, float[] yDatas, byte[] logText, int logTextSize, byte[] logBinary, int logBinarySize, ref int outDataSize);
Exemplo n.º 12
0
		// === PUBLIC METHODS ===

		public bool ReadFromFile(String fileName)
		{
			bool result = true;
			SPCHeader header = new SPCHeader();
			SPCExTags footer = new SPCExTags();

			FResetData();
			
			header.Reset();
			footer.Reset();

			FileStream fs = null;
			BinaryReader source = null;

			try
			{
				fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
				fs.Lock(0,fs.Length);
				source = new BinaryReader(fs);

				if ( !readHeader(ref source, ref header) ) throw new Exception("Not a PSF file");

				// Reads the header tag
				if (SPCHeader.TAG_IN_HEADER == header.TagInHeader)
				{
					source.BaseStream.Seek(REGISTERS_LENGTH,SeekOrigin.Current);
					readHeaderTags(ref source);
				}

				// Reads extended tag
				if (fs.Length > SPC_RAW_LENGTH)
				{
					source.BaseStream.Seek(SPC_RAW_LENGTH,SeekOrigin.Begin);
					readExtendedData(ref source, ref footer);
					
					if (footer.Items.ContainsKey(XID6_ARTIST)) FArtist = (String)((ExtendedItem)footer.Items[XID6_ARTIST]).Data;
					if (footer.Items.ContainsKey(XID6_CMNTS)) FComment = (String)((ExtendedItem)footer.Items[XID6_CMNTS]).Data;
					if (footer.Items.ContainsKey(XID6_SONG)) FTitle = (String)((ExtendedItem)footer.Items[XID6_SONG]).Data;
					if (footer.Items.ContainsKey(XID6_COPY)) FYear = ((ExtendedItem)footer.Items[XID6_COPY]).Length.ToString();
					if (footer.Items.ContainsKey(XID6_TRACK)) FTrack = ((ExtendedItem)footer.Items[XID6_TRACK]).Length >> 8;
					if (footer.Items.ContainsKey(XID6_OST)) FAlbum = (String)((ExtendedItem)footer.Items[XID6_OST]).Data;
					if (("" == FAlbum) && (footer.Items.ContainsKey(XID6_GAME))) FAlbum = (String)((ExtendedItem)footer.Items[XID6_GAME]).Data;
					
					long ticks = 0;
					if (footer.Items.ContainsKey(XID6_LOOP)) ticks += Math.Min(XID6_MAXTICKS, (int)((ExtendedItem)footer.Items[XID6_LOOP]).Data);
					if (footer.Items.ContainsKey(XID6_LOOPX)) ticks = ticks * Math.Min(XID6_MAXLOOP, (int)((ExtendedItem)footer.Items[XID6_LOOPX]).Length );
					if (footer.Items.ContainsKey(XID6_INTRO)) ticks += Math.Min(XID6_MAXTICKS, (int)((ExtendedItem)footer.Items[XID6_INTRO]).Data);
					if (footer.Items.ContainsKey(XID6_END)) ticks += Math.Min(XID6_MAXTICKS, (int)((ExtendedItem)footer.Items[XID6_END]).Data);
					
					if (ticks > 0)
						FDuration = Math.Round( (double)ticks / XID6_TICKSSEC );
				}
			}
			catch (Exception e) 
			{
				System.Console.WriteLine(e.StackTrace);
				//LogDelegator.GetLogDelegate()(Log.LV_ERROR, e.Message);
				result = false;
			}

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

			return result;
		}
Exemplo n.º 13
0
		private bool readHeader(ref BinaryReader source, ref SPCHeader header)
		{
			header.FormatTag = Utils.GetStringFromCharArray( Utils.ReadTrueChars(source, 33) );
			if (SPC_FORMAT_TAG == header.FormatTag)
			{
				source.BaseStream.Seek(2,SeekOrigin.Current);
				header.TagInHeader = source.ReadByte();
				header.VersionByte = source.ReadByte();
				return true;
			}
			else
			{
				return false;
			}
		}