Example #1
0
        /// <summary>
        /// Decode a signal from the specified file.
        /// </summary>
        ///
        /// <param name="fileName">File name to read signal from.</param>
        /// <param name="frameInfo">Information about the decoded signal.</param>
        ///
        /// <returns>Return decoded signal.</returns>
        ///
        public static Signal DecodeFromFile(string fileName, out FrameInfo frameInfo)
        {
            string fileExtension = FormatDecoderAttribute.GetNormalizedExtension(fileName);

            IAudioDecoder decoder = FormatDecoderAttribute.GetDecoders(fileExtension, decoderTypes, decoders.Value);

            if (decoder != null)
            {
                // open stream
                using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    // open decoder
                    decoder.Open(stream);

                    // read all audio frames
                    Signal signal = decoder.Decode();

                    decoder.Close();

                    frameInfo = new FrameInfo(signal.NumberOfChannels, signal.SampleRate, Signal.GetSampleSize(signal.SampleFormat), 0, signal.Length);

                    return(signal);
                }
            }

            throw new ArgumentException(String.Format("No suitable decoder has been found for the file format {0}. If ", fileExtension) +
                                        "you are trying to decode .wav files, please add a reference to Accord.Audio.DirectSound. You might need to instantiate" +
                                        "at least one type from this assembly to make sure it has been loaded in the AppDomain of your applicatoin.", "fileName");
        }
Example #2
0
        /// <summary>
        /// Decode first frame for the specified file.
        /// </summary>
        ///
        /// <param name="fileName">File name to read image from.</param>
        /// <param name="imageInfo">Information about the decoded image.</param>
        ///
        /// <returns>Return decoded image. In the case if file format support multiple
        /// frames, the method return the first frame.</returns>
        ///
        /// <remarks><para>The method uses table of registered image decoders to find the one,
        /// which should be used for the specified file. If there is not appropriate decoder
        /// found, the method uses default .NET's image decoding routine (see
        /// <see cref="System.Drawing.Image.FromFile(string)"/>).</para></remarks>
        ///
        public static Bitmap DecodeFromFile(string fileName, out ImageInfo imageInfo)
        {
            string fileExtension = FormatDecoderAttribute.GetNormalizedExtension(fileName);

            IImageDecoder decoder = FormatDecoderAttribute.GetDecoders(fileExtension, decoderTypes, decoders.Value);

            if (decoder != null)
            {
                // open stream
                using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    // open decoder
                    decoder.Open(stream);

                    // read the first frame from the image
                    Bitmap bitmap = decoder.DecodeFrame(0, out imageInfo);

                    // close the decoder and return the bitmap
                    decoder.Close();
                    return(bitmap);
                }
            }
            else
            {
                // use default .NET's image decoding routine
                Bitmap bitmap = FromFile(fileName);

                decoderTypes[fileExtension] = null; // mark that the file could be loaded using default .NET decoders

                imageInfo = new ImageInfo(bitmap.Width, bitmap.Height, Image.GetPixelFormatSize(bitmap.PixelFormat), 0, 1);

                return(bitmap);
            }
        }