Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which framework for audio playback in WPF?

I know this might sound like a subjective question, but I need some well-founded opinions on this topic:

In a C#/WPF GUI I need to playback short wave files as a response to user interaction.
The specs are as follows:

  • low latency (immediate start of playback)
  • code should be native C# (.Net 4.0)
  • must get along with WPF
  • multiple simultaneous playbacks
  • no restrictions on sound buffer manipulation
  • guaranteed future (I want to use something that will still have support in a few years.)
  • sound player module should be a simple class (= C# code), not an encapsulated dll

Until now I got along quite well with DirectSound (using the Microsoft DirectX SDK), it fits all of the requirements mentioned above. Since Visual Studio 2010 (.Net 4.0), Managed DirectX (MDX) is not supported anymore, and it also disappeared from the latest DirectX SDK.

What are my options now?

  • XNA seems overkill (too large) to me, since I'm not developing a game, but an application. See this question for some more information.
    It's supposed to be the replacement of MDX, but I read a lot of scary stories about it's implementation. Or is that all fairy tales?
  • SlimDX might be an option, but it's a third-party product and again a fairly large project.
  • There are some "smaller" solutions I know of, each with it's own drawbacks:
    • MediaElement (only with WMP10+)
    • P/Invoke with WinMM
    • PlaySound
    • SoundPlayer
    • MediaPlayer
  • There are tons of custom audio libraries in the wild, which work more or less well.
    (NAudio, BASS, waveOut...)

I'm really puzzled about which one I should use for a new project. I don't want to dig into a whole new framework just to find out that its limitations prevent me from using it.

Thanks in advance!

like image 772
Martin Hennings Avatar asked Feb 14 '11 13:02

Martin Hennings


1 Answers

Sounds like quite a few of the options will do what you need, but I'll respond to your requirements for NAudio

low latency (immediate start of playback)

No audio library will start "immediately". NAudio can easily work at latencies of around 50ms with the WaveOut APIs. Possibly quicker if you use WASAPI

code should be native C# (.Net 4.0)

NAudio code is native C# & contains wrappers for Windows APIs

must get along with WPF

NAudio works fine with WPF

multiple simultaneous playbacks

Multiple playbacks are supported. You can optionally create a mixer, to have one playback and mix different inputs in and out.

no restrictions on sound buffer manipulation

This is a big advantage of NAudio over some of the other options you mentioned. You have full access to the sample data and can manipulate in any way you like.

guaranteed future (I want to use something that will still have support in a few years.)

There are no guarantees with open source software. But since its open source, there is nothing stopping you fixing bugs yourself. NAudio has been around for 10 years now.

sound player module should be a simple class (= C# code), not an encapsulated dll

Well you could copy the code into your own project, but there are quite a lot of helper classes so you might actually find it easier just to use the DLL.

like image 57
Mark Heath Avatar answered Sep 30 '22 05:09

Mark Heath