Beispiel #1
0
        internal void Initialize(WaveFormat inputformat, WaveFormat outputformat)
        {
            Ratio = (decimal)outputformat.BytesPerSecond / inputformat.BytesPerSecond;
            lock (LockObj)
            {
                Resampler = new WMResampler();

                MediaObject mediaObject = Resampler.MediaObject;
                if (!mediaObject.SupportsInputFormat(0, inputformat))
                {
                    throw new NotSupportedException("Inputformat not supported.");
                }
                mediaObject.SetInputType(0, inputformat);

                if (!mediaObject.SupportsOutputFormat(0, outputformat))
                {
                    throw new NotSupportedException("Outputformat not supported.");
                }
                mediaObject.SetOutputType(0, outputformat);

                InputBuffer  = new DMO.MONO.MediaBuffer(inputformat.BytesPerSecond / 2);
                OutputBuffer = new DmoOutputDataBuffer(outputformat.BytesPerSecond / 2);
            }
        }
Beispiel #2
0
        /// <summary>
        ///     Reads a resampled sequence of bytes from the <see cref="DmoResampler" /> and advances the position within the
        ///     stream by the
        ///     number of bytes read.
        /// </summary>
        /// <param name="buffer">
        ///     An array of bytes. When this method returns, the <paramref name="buffer" /> contains the specified
        ///     byte array with the values between <paramref name="offset" /> and (<paramref name="offset" /> +
        ///     <paramref name="count" /> - 1) replaced by the bytes read from the current source.
        /// </param>
        /// <param name="offset">
        ///     The zero-based byte offset in the <paramref name="buffer" /> at which to begin storing the data
        ///     read from the current stream.
        /// </param>
        /// <param name="count">The maximum number of bytes to read from the current source.</param>
        /// <returns>The total number of bytes read into the buffer.</returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            lock (LockObj)
            {
                int read = 0;
                while (read < count)
                {
                    MediaObject mediaObject = Resampler.MediaObject;
                    if (mediaObject.IsReadyForInput(0))
                    {
                        var bytesToRead = (int)OutputToInput(count - read);
                        _readBuffer = _readBuffer.CheckBuffer(bytesToRead);
                        int bytesRead = base.Read(_readBuffer, 0, bytesToRead);
                        if (bytesRead <= 0)
                        {
                            break;
                        }

                        if (_disposed)
                        {
                            break;
                        }

                        if (InputBuffer.MaxLength < bytesRead)
                        {
                            InputBuffer.Dispose();
                            InputBuffer = new DMO.MONO.MediaBuffer(bytesRead);
                        }
                        InputBuffer.Write(_readBuffer, 0, bytesRead);

                        mediaObject.ProcessInput(0, InputBuffer);

                        OutputBuffer.Reset();
                        do
                        {
                            var outputBuffer = OutputBuffer.Buffer;
                            if (outputBuffer.MaxLength < count)
                            {
                                OutputBuffer.Buffer.Dispose();
                                OutputBuffer.Buffer = new DMO.MONO.MediaBuffer(count);
                            }
                            OutputBuffer.Buffer.SetLength(0);

                            mediaObject.ProcessOutput(OutputBuffer);
                            //mediaObject.ProcessOutput(ProcessOutputFlags.None, new[] {OutputBuffer}, 1);

                            if (OutputBuffer.Length <= 0)
                            {
                                Debug.WriteLine("DmoResampler::Read: No data in output buffer.");
                                break;
                            }

                            OutputBuffer.Read(buffer, offset + read);
                            read += OutputBuffer.Length;
                        } while (/*_outputBuffer.DataAvailable*/ false); //todo: Implement DataAvailable
                    }
                    else
                    {
                        Debug.WriteLine("Case of not ready for input is not implemented yet."); //todo: .
                    }
                }

                if (_ignoreBaseStreamPosition)
                {
                    _position += read;
                }

                return(read);
            }
        }