private CodecFactory() { _codecs = new Dictionary<object, CodecFactoryEntry>(); Register("mp3", new CodecFactoryEntry(s => { if (Mp3MediafoundationDecoder.IsSupported) return new Mp3MediafoundationDecoder(s); return new DmoMp3Decoder(s); }, "mp3", "mpeg3")); Register("wave", new CodecFactoryEntry(s => { IWaveSource res = new WaveFileReader(s); if (res.WaveFormat.WaveFormatTag != AudioEncoding.Pcm && res.WaveFormat.WaveFormatTag != AudioEncoding.IeeeFloat && res.WaveFormat.WaveFormatTag != AudioEncoding.Extensible) { res.Dispose(); res = new MediaFoundationDecoder(s); } return res; }, "wav", "wave")); Register("flac", new CodecFactoryEntry(s => new FlacFile(s), "flac", "fla")); Register("aiff", new CodecFactoryEntry(s => new AiffReader(s), "aiff", "aif", "aifc")); if (AacDecoder.IsSupported) { Register("aac", new CodecFactoryEntry(s => new AacDecoder(s), "aac", "adt", "adts", "m2ts", "mp2", "3g2", "3gp2", "3gp", "3gpp", "m4a", "m4v", "mp4v", "mp4", "mov")); } if (WmaDecoder.IsSupported) { Register("wma", new CodecFactoryEntry(s => new WmaDecoder(s), "asf", "wm", "wmv", "wma")); } if (Mp1Decoder.IsSupported) { Register("mp1", new CodecFactoryEntry(s => new Mp1Decoder(s), "mp1", "m2ts")); } if (Mp2Decoder.IsSupported) { Register("mp2", new CodecFactoryEntry(s => new Mp1Decoder(s), "mp2", "m2ts")); } if (DDPDecoder.IsSupported) { Register("ddp", new CodecFactoryEntry(s => new DDPDecoder(s), "mp2", "m2ts", "m4a", "m4v", "mp4v", "mp4", "mov", "asf", "wm", "wmv", "wma", "avi", "ac3", "ec3")); } }
public byte[] FromWav(byte[] audioFile) { var supportedFormats = MediaFoundationEncoder.GetEncoderMediaTypes(AudioSubTypes.MPEG_HEAAC); _logger.Verbose("Checking for support of AAC encoding."); if (!supportedFormats.Any()) { _logger.Verbose("The current platform does not support AAC encoding."); throw new ApplicationException("Current platform does not support AAC encoding."); } MemoryStream inStream = null; MemoryStream outStream = null; IWaveSource source = null; bool sourceDisposed = false; try { _logger.Verbose("Creating input stream and decoder."); inStream = new MemoryStream(audioFile); source = new CSCore.MediaFoundation.MediaFoundationDecoder(inStream); //in case the encoder does not support the input sample rate -> convert it to any supported samplerate //choose the best sample rate _logger.Verbose("Searching for the optimal sample rate."); _logger.Verbose($"Input wave format: {source.WaveFormat.ToString()}"); int sampleRate = supportedFormats.OrderBy(x => Math.Abs(source.WaveFormat.SampleRate - x.SampleRate)) .First(x => x.Channels == source.WaveFormat.Channels) .SampleRate; if (source.WaveFormat.SampleRate != sampleRate) { _logger.Verbose($"Changing sample rate of the source: {source.WaveFormat.SampleRate} -> {sampleRate}."); source = source.ChangeSampleRate(sampleRate); } _logger.Verbose("Encoding WAV to AAC"); outStream = new MemoryStream(); using (source) { using (var encoder = MediaFoundationEncoder.CreateAACEncoder(source.WaveFormat, outStream)) { byte[] buffer = new byte[source.WaveFormat.BytesPerSecond]; int read; while ((read = source.Read(buffer, 0, buffer.Length)) > 0) { encoder.Write(buffer, 0, read); } } sourceDisposed = true; } _logger.Verbose("Encoding is complete"); return(outStream.ToArray()); } finally { _logger.Verbose("Cleaning up resources"); if (inStream != null) { inStream.Dispose(); } if (source != null && !sourceDisposed) { source.Dispose(); } if (outStream != null) { inStream.Dispose(); } } }
private void Decode(string filePath) { if (!Directory.Exists("temp")) { Directory.CreateDirectory("temp"); } { string toOut = @"temp\" + Path.GetFileNameWithoutExtension(filePath) + ".wav"; using (var decoder = new MediaFoundationDecoder(filePath)) { decoder.WriteToFile(toOut); } } }