示例#1
0
        public void Analyze(string analyzer, CancellationToken cancelToken, GroupToken groupToken = null)
        {
            Contract.Requires <ArgumentNullException>(!string.IsNullOrEmpty(analyzer));

            var groupTokenOwned = false;

            try
            {
                if (groupToken == null)
                {
                    groupToken      = new GroupToken(1);
                    groupTokenOwned = true;
                }

                ExportFactory <ISampleAnalyzer> analyzerFactory =
                    ExtensionProvider.GetFactories <ISampleAnalyzer>("Name", analyzer).SingleOrDefault();
                if (analyzerFactory == null)
                {
                    throw new ArgumentException(
                              string.Format(CultureInfo.CurrentCulture, Resources.AnalyzableAudioFileFactoryError, analyzer),
                              nameof(analyzer));
                }

                using (ExportLifetimeContext <ISampleAnalyzer> analyzerLifetime = analyzerFactory.CreateExport())
                    DoAnalyze(analyzerLifetime.Value, cancelToken, groupToken);
            }
            finally
            {
                if (groupTokenOwned)
                {
                    groupToken.Dispose();
                }
            }
        }
示例#2
0
        void LoadAudioInfo()
        {
            Contract.Ensures(AudioInfo != null);

            using (FileStream fileStream = FileInfo.OpenRead())
            {
                // Try each info decoder that supports this file extension:
                foreach (ExportFactory <IAudioInfoDecoder> decoderFactory in
                         ExtensionProvider.GetFactories <IAudioInfoDecoder>("Extension", FileInfo.Extension))
                {
                    try
                    {
                        using (ExportLifetimeContext <IAudioInfoDecoder> lifetimeContext = decoderFactory.CreateExport())
                            AudioInfo = lifetimeContext.Value.ReadAudioInfo(fileStream);
                        return;
                    }
                    catch (UnsupportedAudioException)
                    {
                        // If a decoder wasn't supported, rewind the stream and try another:
                        fileStream.Position = 0;
                    }
                }
            }

            throw new UnsupportedAudioException(Resources.AudioFileDecodeError);
        }
示例#3
0
        void LoadMetadata(Stream stream)
        {
            Contract.Requires(stream != null);
            Contract.Requires(stream.CanRead);
            Contract.Requires(stream.CanSeek);
            Contract.Ensures(_metadata != null);

            // Try each decoder that supports this file extension:
            foreach (ExportFactory <IMetadataDecoder> decoderFactory in
                     ExtensionProvider.GetFactories <IMetadataDecoder>("Extension", FileInfo.Extension))
            {
                try
                {
                    using (ExportLifetimeContext <IMetadataDecoder> lifetimeContext = decoderFactory.CreateExport())
                        _metadata = lifetimeContext.Value.ReadMetadata(stream);
                    return;
                }
                catch (UnsupportedAudioException)
                {
                    // If a decoder wasn't supported, rewind the stream and try another:
                    stream.Position = 0;
                }
            }

            _metadata = new MetadataDictionary();
        }
示例#4
0
        /// <summary>
        /// Saves the metadata, using an available <see cref="IMetadataEncoder"/>. If no extensions are able to write
        /// to this file extension, an <see cref="UnsupportedAudioException"/>
        /// </summary>
        /// <param name="settings">The settings.</param>
        /// <exception cref="UnsupportedAudioException">
        /// No metadata encoders are able to save metadata in the required format.
        /// </exception>
        public void SaveMetadata(SettingsDictionary settings = null)
        {
            if (settings == null)
            {
                settings = new SettingsDictionary();
            }

            using (FileStream fileStream = FileInfo.Open(FileMode.Open, FileAccess.ReadWrite))
            {
                // Ensure the existing metadata has been loaded:
                if (_metadata == null)
                {
                    LoadMetadata(fileStream);
                    fileStream.Position = 0;
                }

                // Try each encoder that supports the file extension:
                foreach (ExportFactory <IMetadataEncoder> encoderFactory in
                         ExtensionProvider.GetFactories <IMetadataEncoder>("Extension", FileInfo.Extension))
                {
                    using (ExportLifetimeContext <IMetadataEncoder> lifetimeContext = encoderFactory.CreateExport())
                    {
                        IMetadataEncoder encoder = lifetimeContext.Value;
                        ValidateSettings(settings, encoder);
                        encoder.WriteMetadata(fileStream, Metadata, settings);

                        return;
                    }
                }
            }

            throw new UnsupportedAudioException(Resources.TaggedAudioFileUnsupportedError);
        }
示例#5
0
        void DoAnalyze(ISampleAnalyzer sampleAnalyzer, CancellationToken cancelToken, GroupToken groupToken)
        {
            Contract.Requires(sampleAnalyzer != null);
            Contract.Requires(groupToken != null);

            sampleAnalyzer.Initialize(AudioInfo, groupToken);

            using (FileStream fileStream = FileInfo.OpenRead())
            {
                // Try each decoder that supports this file extension:
                foreach (ExportFactory <ISampleDecoder> decoderFactory in
                         ExtensionProvider.GetFactories <ISampleDecoder>("Extension", FileInfo.Extension))
                {
                    try
                    {
                        using (ExportLifetimeContext <ISampleDecoder> decoderLifetime = decoderFactory.CreateExport())
                        {
                            ISampleDecoder sampleDecoder = decoderLifetime.Value;

                            sampleDecoder.Initialize(fileStream);
                            sampleDecoder.ReadWriteParallel(sampleAnalyzer, cancelToken,
                                                            sampleAnalyzer.ManuallyFreesSamples);
                            sampleAnalyzer.GetResult().CopyTo(Metadata);
                            return;
                        }
                    }
                    catch (UnsupportedAudioException)
                    {
                        // If a decoder wasn't supported, rewind the stream and try another:
                        fileStream.Position = 0;
                    }
                }
            }

            throw new UnsupportedAudioException(Resources.AudioFileDecodeError);
        }
        void DoExport(ISampleEncoder encoder, Stream outputStream, SettingsDictionary settings, CancellationToken cancelToken)
        {
            Contract.Requires(encoder != null);
            Contract.Requires(outputStream != null);
            Contract.Requires(settings != null);

            encoder.Initialize(outputStream, AudioInfo, Metadata, settings);

            using (FileStream inputStream = FileInfo.OpenRead())
            {
                // Try each decoder that supports this file extension:
                foreach (ExportFactory <ISampleDecoder> decoderFactory in
                         ExtensionProvider.GetFactories <ISampleDecoder>("Extension", FileInfo.Extension))
                {
                    try
                    {
                        using (ExportLifetimeContext <ISampleDecoder> decoderLifetime = decoderFactory.CreateExport())
                        {
                            ISampleDecoder sampleDecoder = decoderLifetime.Value;

                            sampleDecoder.Initialize(inputStream);
                            sampleDecoder.ReadWriteParallel(encoder, cancelToken, encoder.ManuallyFreesSamples);

                            return;
                        }
                    }
                    catch (UnsupportedAudioException)
                    {
                        // If a decoder wasn't supported, rewind the stream and try another:
                        inputStream.Position = 0;
                    }
                }

                throw new UnsupportedAudioException(Resources.AudioFileDecodeError);
            }
        }
        public ExportableAudioFile Export(string encoder, CancellationToken cancelToken, SettingsDictionary settings = null, DirectoryInfo outputDirectory = null, string outputFileName = null, bool replaceExisting = false)
        {
            Contract.Requires <ArgumentNullException>(!string.IsNullOrEmpty(encoder));
            Contract.Ensures(Contract.Result <ExportableAudioFile>() != null);

            if (settings == null)
            {
                settings = new SettingsDictionary();
            }

            ExportFactory <ISampleEncoder> encoderFactory =
                ExtensionProvider.GetFactories <ISampleEncoder>("Name", encoder).SingleOrDefault();

            if (encoderFactory == null)
            {
                throw new ArgumentException(
                          string.Format(CultureInfo.CurrentCulture, Resources.ExportableAudioFileFactoryError, encoder),
                          nameof(encoder));
            }

            ExportLifetimeContext <ISampleEncoder> encoderLifetime = null;
            FileInfo   outputFileInfo = null;
            FileInfo   finalOutputFileInfo;
            FileStream outputStream = null;

            try
            {
                try
                {
                    encoderLifetime = encoderFactory.CreateExport();

                    ValidateSettings(settings, encoderLifetime.Value);

                    outputFileInfo = finalOutputFileInfo = GetOutputFileInfo(FileInfo, outputDirectory, outputFileName, encoderLifetime.Value);

                    // If the output file already exists, write to a temporary file first:
                    if (outputFileInfo.Exists)
                    {
                        outputFileInfo = new FileInfo(Path.Combine(outputFileInfo.DirectoryName ?? string.Empty, Path.GetRandomFileName()));

                        if (!replaceExisting)
                        {
                            throw new IOException(string.Format(CultureInfo.CurrentCulture,
                                                                Resources.ExportableAudioFileFileExistsError, finalOutputFileInfo.FullName));
                        }
                    }

                    outputStream = new FileStream(outputFileInfo.FullName, replaceExisting ? FileMode.Create : FileMode.CreateNew, FileAccess.ReadWrite);

                    DoExport(encoderLifetime.Value, outputStream, settings, cancelToken);
                }
                finally
                {
                    // Dispose the encoder before closing the output stream:
                    encoderLifetime?.Dispose();
                    outputStream?.Dispose();
                }
            }
            catch (Exception)
            {
                outputFileInfo?.Delete();
                throw;
            }

            // If using a temporary file, replace the original:
            if (outputFileInfo != finalOutputFileInfo)
            {
                finalOutputFileInfo.Delete();
                outputFileInfo.MoveTo(finalOutputFileInfo.FullName);
            }

            outputFileInfo.Refresh();
            return(new ExportableAudioFile(outputFileInfo));
        }