/// <inheritdoc/> public void SaveMetadata(SettingDictionary settings = null) { if (settings == null) { settings = new SettingDictionary(); } var extension = IO.Path.GetExtension(Path); // Make sure the provided settings are clean AudioMetadataEncoderManager.GetSettingInfoByExtension(extension).ValidateSettings(settings); using (var fileStream = File.Open(Path, FileMode.Open)) { // Try each encoder that supports this file extension foreach (var factory in ExtensionProviderWrapper.GetFactories <IAudioMetadataEncoder>( "Extension", extension)) { using (var export = factory.CreateExport()) { if (_metadata == null) { _metadata = LoadMetadata(fileStream); } export.Value.WriteMetadata(fileStream, _metadata, settings); return; } } } throw new AudioUnsupportedException("No supporting extensions are available."); }
AudioInfo LoadInfo() { var logger = LoggerManager.LoggerFactory.CreateLogger <AudioFile>(); using (var fileStream = File.OpenRead(Path)) { // Try each info decoder that supports this file extension foreach (var factory in ExtensionProviderWrapper.GetFactories <IAudioInfoDecoder>( "Extension", IO.Path.GetExtension(Path))) { var format = string.Empty; try { using (var export = factory.CreateExport()) { format = export.Value.Format; logger.LogDebug("Attempting to decode '{0}' as '{1}'.", Path, format); return(export.Value.ReadAudioInfo(fileStream)); } } catch (AudioUnsupportedException e) { logger.LogDebug(e, "Unable to decode '{0}' as '{1}'.", Path, format); // If a decoder wasn't supported, rewind the stream and try another fileStream.Position = 0; } } } throw new AudioUnsupportedException($"Unable to decode '{Path}' with any loaded extension."); }
/// <summary> /// Gets information about the available settings that can be passed to an <see cref="ITaggedAudioFile"/>'s /// SaveMetadata method, for a given metadata format. /// </summary> /// <param name="format">The format.</param> /// <returns>Information about the available settings.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="format"/> is null.</exception> public static SettingInfoDictionary GetSettingInfoByFormat(string format) { if (format == null) { throw new ArgumentNullException(nameof(format)); } // Try each encoder that supports this format: foreach (var factory in ExtensionProviderWrapper.GetFactories <IAudioMetadataEncoder>( "Format", format)) { using (var export = factory.CreateExport()) return(export.Value.SettingInfo); } return(new()); }
public static SettingInfoDictionary GetSettingInfoByExtension([NotNull] string extension) { if (extension == null) { throw new ArgumentNullException(nameof(extension)); } // Try each encoder that supports this file extension: foreach (var factory in ExtensionProviderWrapper.GetFactories <IAudioMetadataEncoder>( "Extension", extension)) { using (var export = factory.CreateExport()) return(export.Value.SettingInfo); } return(new SettingInfoDictionary()); }
/// <summary> /// Gets information about the available settings that can be passed to an <see cref="AudioFileAnalyzer"/>'s /// Analyze method, for a given analyzer. /// </summary> /// <param name="name">The name of the analyzer.</param> /// <returns>Information about the available settings.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="name"/> is null.</exception> public static SettingInfoDictionary GetSettingInfo(string name) { if (name == null) { throw new ArgumentNullException(nameof(name)); } // Try each encoder that supports this file extension: foreach (var factory in ExtensionProviderWrapper.GetFactories <IAudioAnalyzer>( "Name", name)) { using (var export = factory.CreateExport()) return(export.Value.SettingInfo); } return(new()); }
internal static void ProcessSamples( [NotNull] this ISampleProcessor sampleProcessor, [NotNull] string inputFilePath, [CanBeNull] IProgress <int> progress, CancellationToken cancellationToken) { using (var inputStream = File.OpenRead(inputFilePath)) { // Try each decoder that supports this file extension foreach (var decoderFactory in ExtensionProviderWrapper.GetFactories <IAudioDecoder>( "Extension", Path.GetExtension(inputFilePath))) { try { using (var decoderExport = decoderFactory.CreateExport()) { decoderExport.Value.Initialize(inputStream); while (!decoderExport.Value.Finished) { cancellationToken.ThrowIfCancellationRequested(); using (var samples = decoderExport.Value.DecodeSamples()) { sampleProcessor.Submit(samples); progress?.Report(samples.Frames); } } } return; } catch (AudioUnsupportedException) { // If a decoder wasn't supported, rewind the stream and try another: inputStream.Position = 0; } } throw new AudioUnsupportedException("No supporting decoders are available."); } }
static AudioMetadata LoadMetadata([NotNull] FileStream stream) { var initialPosition = stream.Position; var logger = LoggerManager.LoggerFactory.CreateLogger <TaggedAudioFile>(); try { // Try each decoder that supports this file extension foreach (var decoderFactory in ExtensionProviderWrapper.GetFactories <IAudioMetadataDecoder>( // ReSharper disable once AssignNullToNotNullAttribute "Extension", IO.Path.GetExtension(stream.Name))) { var format = string.Empty; try { using (var export = decoderFactory.CreateExport()) { format = export.Value.Format; logger.LogDebug("Attempting to read '{0}' metadata from '{1}'.", format, stream.Name); return(export.Value.ReadMetadata(stream)); } } catch (AudioUnsupportedException e) { logger.LogDebug(e, "Unable to read '{0}' metadata from '{1}'.", format, stream.Name); // If a decoder wasn't supported, rewind the stream and try another stream.Position = 0; } } logger.LogDebug("Unable to read any metadata from '{0}'.", stream.Name); return(new AudioMetadata()); } finally { stream.Position = initialPosition; } }
public static IEnumerable <AudioFileFormatInfo> GetFormatInfo() { return(ExtensionProviderWrapper.GetFactories <IAudioInfoDecoder>() .Select(factory => new AudioFileFormatInfo(factory.Metadata))); }
/// <summary> /// Gets information about the available metadata encoders. /// </summary> /// <returns>The encoder info.</returns> public static IEnumerable <AudioMetadataEncoderInfo> GetEncoderInfo() => ExtensionProviderWrapper.GetFactories <IAudioMetadataEncoder>() .Select(factory => new AudioMetadataEncoderInfo(factory.Metadata));
public static IEnumerable <AudioAnalyzerInfo> GetAnalyzerInfo() { return(ExtensionProviderWrapper.GetFactories <IAudioAnalyzer>() .Select(factory => new AudioAnalyzerInfo(factory.Metadata))); }