/// <summary>
        /// WaveStream to resample using the DMO Resampler
        /// </summary>
        /// <param name="inputProvider">Input Stream</param>
        /// <param name="outputFormat">Desired Output Format</param>
        public ResamplerDmoStream(IWaveProvider inputProvider, WaveFormat outputFormat)
        {
            this.inputProvider = inputProvider;
            this.inputStream = inputProvider as WaveStream;
            this.outputFormat = outputFormat;
            this.resampler = new Resampler();
            if (!resampler.MediaObject.SupportsInputWaveFormat(0, inputProvider.WaveFormat))
            {
                throw new ArgumentException("Unsupported Input Stream format", "inputStream");
            }

            resampler.MediaObject.SetInputWaveFormat(0, inputProvider.WaveFormat);
            if (!resampler.MediaObject.SupportsOutputWaveFormat(0, outputFormat))
            {
                throw new ArgumentException("Unsupported Output Stream format", "outputStream");
            }

            resampler.MediaObject.SetOutputWaveFormat(0, outputFormat);
            if (inputStream != null)
            {
                position = InputToOutputPosition(inputStream.Position);
            }
            this.inputMediaBuffer = new MediaBuffer(inputProvider.WaveFormat.AverageBytesPerSecond);
            this.outputBuffer = new DmoOutputDataBuffer(outputFormat.AverageBytesPerSecond);
        }
 public void ResamplerCanCallProcessInput()
 {
     DmoResampler dmoResampler = new DmoResampler();
     dmoResampler.MediaObject.SetInputWaveFormat(0, WaveFormat.CreateIeeeFloatWaveFormat(44100, 2));
     dmoResampler.MediaObject.SetOutputWaveFormat(0, WaveFormat.CreateIeeeFloatWaveFormat(48000, 2));
     using (MediaBuffer buffer = new MediaBuffer(44100 * 2 * 4))
     {
         buffer.Length = 8000;
         dmoResampler.MediaObject.ProcessInput(0, buffer, DmoInputDataBufferFlags.None, 0, 0);
     }
 }
		/// <summary>
		/// Dispose of this obejct and clean up resources
		/// </summary>
		public void Dispose()
		{
			if (inputMediaBuffer != null)
			{
				inputMediaBuffer.Dispose();
				inputMediaBuffer = null;
			}
			outputBuffer.Dispose();
			if (mp3Decoder != null)
			{
				mp3Decoder.Dispose();
				mp3Decoder = null;
			}
		}
        /// <summary>
        /// Initializes a new instance of the DMO MP3 Frame decompressor
        /// </summary>
        /// <param name="sourceFormat"></param>
        public DmoMp3FrameDecompressor(WaveFormat sourceFormat)
        {
            this.mp3Decoder = new WindowsMediaMp3Decoder();
            if (!mp3Decoder.MediaObject.SupportsInputWaveFormat(0, sourceFormat))
            {
                throw new ArgumentException("Unsupported input format");
            }
            mp3Decoder.MediaObject.SetInputWaveFormat(0, sourceFormat);
            pcmFormat = new WaveFormat(sourceFormat.SampleRate, sourceFormat.Channels); // 16 bit
            if (!mp3Decoder.MediaObject.SupportsOutputWaveFormat(0, pcmFormat))
            {
                throw new ArgumentException(String.Format("Unsupported output format {0}", pcmFormat));
            }
            mp3Decoder.MediaObject.SetOutputWaveFormat(0, pcmFormat);

            // a second is more than enough to decompress a frame at a time
            inputMediaBuffer = new MediaBuffer(sourceFormat.AverageBytesPerSecond);
            outputBuffer = new DmoOutputDataBuffer(pcmFormat.AverageBytesPerSecond);
        }
        public void ResamplerCanCallProcessOutput()
        {
            DmoResampler dmoResampler = new DmoResampler();
            WaveFormat inputFormat = WaveFormat.CreateIeeeFloatWaveFormat(44100, 2);
            WaveFormat outputFormat = WaveFormat.CreateIeeeFloatWaveFormat(48000, 2);
            dmoResampler.MediaObject.SetInputWaveFormat(0, inputFormat);
            dmoResampler.MediaObject.SetOutputWaveFormat(0, outputFormat);
            dmoResampler.MediaObject.AllocateStreamingResources();
            using (MediaBuffer inputBuffer = new MediaBuffer(inputFormat.AverageBytesPerSecond))
            {
                inputBuffer.Length = inputFormat.AverageBytesPerSecond / 10;
                Debug.WriteLine(String.Format("Input Length {0}", inputBuffer.Length));
                dmoResampler.MediaObject.ProcessInput(0, inputBuffer, DmoInputDataBufferFlags.None, 0, 0);
                Debug.WriteLine(String.Format("Input Length {0}", inputBuffer.Length));
                Debug.WriteLine(String.Format("Input Lookahead {0}", dmoResampler.MediaObject.GetInputSizeInfo(0).MaxLookahead));
                //Debug.WriteLine(String.Format("Input Max Latency {0}", resampler.MediaObject.GetInputMaxLatency(0)));
                using (DmoOutputDataBuffer outputBuffer = new DmoOutputDataBuffer(outputFormat.AverageBytesPerSecond))
                {
                    // one buffer for each output stream
                    dmoResampler.MediaObject.ProcessOutput(DmoProcessOutputFlags.None, 1, new DmoOutputDataBuffer[] { outputBuffer });
                    Debug.WriteLine(String.Format("Converted length: {0}", outputBuffer.Length));
                    Debug.WriteLine(String.Format("Converted flags: {0}", outputBuffer.StatusFlags));
                    //Assert.AreEqual((int)(inputBuffer.Length * 48000.0 / inputFormat.SampleRate), outputBuffer.Length, "Converted buffer length");
                }

                using (DmoOutputDataBuffer outputBuffer = new DmoOutputDataBuffer(48000 * 2 * 4))
                {
                    // one buffer for each output stream
                    dmoResampler.MediaObject.ProcessOutput(DmoProcessOutputFlags.None, 1, new DmoOutputDataBuffer[] { outputBuffer });
                    Debug.WriteLine(String.Format("Converted length: {0}", outputBuffer.Length));
                    Debug.WriteLine(String.Format("Converted flags: {0}", outputBuffer.StatusFlags));
                    //Assert.AreEqual((int)(inputBuffer.Length * 48000.0 / inputFormat.SampleRate), outputBuffer.Length, "Converted buffer length");
                }
            }
            dmoResampler.MediaObject.FreeStreamingResources();
        }
 /// <summary>
 /// Dispose
 /// </summary>
 /// <param name="disposing">True if disposing (not from finalizer)</param>
 protected override void Dispose(bool disposing)
 {
     if (inputMediaBuffer != null)
     {
         inputMediaBuffer.Dispose();
         inputMediaBuffer = null;
     }
     outputBuffer.Dispose();
     if (resampler != null)
     {
         //resampler.Dispose(); s
         resampler = null;
     }
     base.Dispose(disposing);
 }