/// <summary> /// Encodes a file /// </summary> /// <param name="outputFile">Output filename (container type is deduced from the filename)</param> /// <param name="inputProvider">Input provider (should be PCM, some encoders will also allow IEEE float)</param> public void Encode(string outputFile, IWaveProvider inputProvider) { if (inputProvider.WaveFormat.Encoding != WaveFormatEncoding.Pcm && inputProvider.WaveFormat.Encoding != WaveFormatEncoding.IeeeFloat) { throw new ArgumentException("Encode input format must be PCM or IEEE float"); } // possibly could use Marshaling to convert instead // given that input should be PCM or IEEE float, this should work just fine var sharpWf = SharpDX.Multimedia.WaveFormat.CreateCustomFormat( (SharpDX.Multimedia.WaveFormatEncoding)inputProvider.WaveFormat.Encoding, inputProvider.WaveFormat.SampleRate, inputProvider.WaveFormat.Channels, inputProvider.WaveFormat.AverageBytesPerSecond, inputProvider.WaveFormat.BlockAlign, inputProvider.WaveFormat.BitsPerSample); var inputMediaType = new MediaType(); var size = 18 + sharpWf.ExtraSize; MediaFactory.InitMediaTypeFromWaveFormatEx(inputMediaType, new[] { sharpWf }, size); using (var writer = CreateSinkWriter(outputFile)) { int streamIndex; writer.AddStream(outputMediaType, out streamIndex); // n.b. can get 0xC00D36B4 - MF_E_INVALIDMEDIATYPE here writer.SetInputMediaType(streamIndex, inputMediaType, null); PerformEncode(writer, streamIndex, inputProvider); } inputMediaType.Dispose(); }
/// <summary> /// Disposes this instance /// </summary> /// <param name="disposing"></param> protected void Dispose(bool disposing) { outputMediaType.Dispose(); }