开发者

Using windows codecs to decode audio files

开发者 https://www.devze.com 2023-02-18 14:50 出处:网络
I would like to add support for playing audio files in my application. I need to manipulate the data in real time though(e.g., applying an ADSR, resampling, pitch shifting, etc...) and therefor need t

I would like to add support for playing audio files in my application. I need to manipulate the data in real time though(e.g., applying an ADSR, resampling, pitch shifting, etc...) and therefor need the bits directly.

I've tried searching for how to do this but cannot find much information unless I only want to read wave files or use an external decompressor. Since I have p开发者_开发问答lenty of codecs installed I would like to use them(if it's not too much trouble which it shouldn't be).

How can I go about this in C#? It doesn't seem like there is any native support for using windows codecs but that there are some technologies out there that can help. Unfortunately they all seem to be rather restrictive or problematic.

Windows Media Foundation only works for Win7 and doesn't have a managed interface(tried MF.NET but doesn't work well). DirectShow seems to be depreciated(being replaced by WMF) and has no managed support. etc... etc... etc...

How hard is it to get a codec to return a sample for audio? It should be a few lines of code at most ;/


The BASS Audio Library can decode to just raw data for you. I don't know if it uses the built-in codecs in Windows or its own, but it has support for a wide variety of formats and streams. There is a .NET wrapper for it as well.

I believe you could also use this library for playback with the functions you are hoping to create. At least for the envelope, this is definitely possible. Again though, it provides raw access to the stream so you don't need to use it for playback.


NAudio has some code that you can use to offload audio decoding to the ACM decoders. These are used for MP3 decoding among other purposes. The MP3 decoding can be witnessed here. The code mainly lives under the NAudio.Wave.Compression namespace. The tough bit from your point of view will be writing parsers to extract the right content from streams to feed the the decoder.


How to convert various audio formats to PCM

For headerless vox

using (BinaryReader br = new BinaryReader(File.OpenRead("sample.vox")))
{
    IntPtr format = AudioCompressionManager.GetPcmFormat(1, 16, 8000);
    using (WaveWriter ww = new WaveWriter(new MemoryStream(), 
        AudioCompressionManager.FormatBytes(format)))
    {
        Vox.Vox2Wav(br, ww);
    }
}

for other (mp3, wav, ogg, wma etc)

DsReader dr = new DsReader("sample.ogg");
IntPtr formatPcm = dr.ReadFormat();
byte[] dataPcm = dr.ReadData();
dr.Close();
0

精彩评论

暂无评论...
验证码 换一张
取 消