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); } }