当前位置: 首页>>代码示例>>C#>>正文


C# WaveFormat.Load方法代码示例

本文整理汇总了C#中WaveFormat.Load方法的典型用法代码示例。如果您正苦于以下问题:C# WaveFormat.Load方法的具体用法?C# WaveFormat.Load怎么用?C# WaveFormat.Load使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在WaveFormat的用法示例。


在下文中一共展示了WaveFormat.Load方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ReadSampleCount

        /// <summary>
        /// Read the sample count of a waveform file.
        /// </summary>
        /// <param name="filePath">Waveform file to read sample count.</param>
        /// <returns>Sample count of the waveform file. if -1 is returned.</returns>
        public static int ReadSampleCount(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            WaveFormat format;

            try
            {
                FileStream fs = new FileStream(filePath,
                    FileMode.Open, FileAccess.Read, FileShare.Read);
                try
                {
                    using (BinaryReader br = new BinaryReader(fs))
                    {
                        fs = null;
                        Riff riff = new Riff();
                        riff.LoadHead(br);

                        RiffChunk fmt = new RiffChunk();
                        fmt.Load(br, Riff.IdFormat);

                        format = new WaveFormat();
                        format.Load(fmt.GetData());

                        int chunkId = 0;
                        int dataSize = 0;
                        do
                        {
                            chunkId = br.ReadInt32();
                            dataSize = br.ReadInt32();
                            if (dataSize < 0)
                            {
                                string message = string.Format(CultureInfo.InvariantCulture,
                                    "Invalid data size [{0}], which should not be negative integer.",
                                    dataSize);
                                throw new InvalidDataException(message);
                            }

                            long currPos = br.BaseStream.Position;
                            long newPos = br.BaseStream.Seek(dataSize, SeekOrigin.Current);
                            if (newPos != currPos + dataSize)
                            {
                                string message = string.Format(CultureInfo.InvariantCulture,
                                    "Invalid data size [{0}], which may be too large.",
                                    dataSize);
                                throw new InvalidDataException(message);
                            }
                        }
                        while (Riff.IdData != chunkId);

                        if (Riff.IdData != chunkId)
                        {
                            string message = string.Format(CultureInfo.InvariantCulture,
                                "Invalid waveform format for not data chunk found in file {0}",
                                filePath);
                            throw new InvalidDataException(message);
                        }

                        return dataSize / (format.BlockAlign * format.Channels);
                    }
                }
                finally
                {
                    if (null != fs)
                    {
                        fs.Dispose();
                    }
                }
            }
            catch (InvalidDataException ide)
            {
                string message = string.Format(CultureInfo.InvariantCulture,
                    "Fail to read sample count of waveform file [{0}] for invalid data.",
                    filePath);
                throw new InvalidDataException(message, ide);
            }
            catch (EndOfStreamException ese)
            {
                string message = string.Format(CultureInfo.InvariantCulture,
                    "Fail to read sample count of waveform file [{0}] for no enough data.",
                    filePath);
                throw new InvalidDataException(message, ese);
            }
        }
开发者ID:JohnsonYuan,项目名称:TTSFramework,代码行数:92,代码来源:WaveFile.cs

示例2: ReadFormat

        public static WaveFormat ReadFormat(string filePath)
        {
            WaveFormat format;

            try
            {
                using (FileStream fs =
                    new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (BinaryReader br = new BinaryReader(fs))
                {
                    Riff riff = new Riff();
                    riff.LoadHead(br);

                    RiffChunk fmt = new RiffChunk();
                    fmt.Load(br, Riff.IdFormat);

                    format = new WaveFormat();
                    format.Load(fmt.GetData());
                }
            }
            catch (InvalidDataException ide)
            {
                string message = string.Format(CultureInfo.InvariantCulture,
                    "Fail to read format of waveform file [{0}] for invalid data.",
                    filePath);
                throw new InvalidDataException(message, ide);
            }

            return format;
        }
开发者ID:JohnsonYuan,项目名称:TTSFramework,代码行数:30,代码来源:WaveFile.cs


注:本文中的WaveFormat.Load方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。