Exemplo n.º 1
0
        internal static void ReadWriteParallel(this ISampleDecoder decoder, ISampleConsumer consumer, CancellationToken cancelToken, bool samplesAreManuallyFreed)
        {
            Contract.Requires(decoder != null);
            Contract.Requires(consumer != null);

            using (var outputQueue = new BlockingCollection <SampleCollection>(10))
            {
                Task decode = Task.Run(() =>
                {
                    SampleCollection samples;
                    do
                    {
                        cancelToken.ThrowIfCancellationRequested();
                        samples = decoder.DecodeSamples();
                        outputQueue.Add(samples);
                    } while (!samples.IsLast);
                }).ContinueWith(task => outputQueue.CompleteAdding());

                foreach (SampleCollection queuedSamples in outputQueue.GetConsumingEnumerable(cancelToken))
                {
                    consumer.Submit(queuedSamples);
                    if (!samplesAreManuallyFreed)
                    {
                        SampleCollectionFactory.Instance.Free(queuedSamples);
                    }
                }

                // This will re-throw any decoding exceptions:
                decode.Wait();
            }
        }
Exemplo n.º 2
0
 public void Start()
 {
     triggerState = true;
     if (decoder == null)
     {
         decoder = ISampleDecoder.Alloc();
     }
 }
Exemplo n.º 3
0
 public void Stop()
 {
     triggerState = false;
     stopped      = true;
     if (decoder != null)
     {
         ISampleDecoder.Free(decoder); decoder = null;
     }
 }
Exemplo n.º 4
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);
            }
        }
Exemplo n.º 6
0
 public SoundChannel()
 {
     decoder = null;
     Clear();
 }