5

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!

Community
  • 1
  • 1
Martin Hennings
  • 14,732
  • 8
  • 38
  • 65
  • I'm curious why you're ruling out third-party unmanaged libraries. DirectX is unmanaged code, after all. Any sound library will have unmanaged code at some level. Why are Microsoft unmanaged libraries OK, and third-party ones not? – Joe White Feb 14 '11 at 13:32
  • They were OK at first because I thought M$ would provide their products with better support for their own than for third-party solutions, but since they discontinued DirectX I'm not even sure about that anymore... Now I'm afraid that some future .Net version will not support whatever might work now because it's not from M$. – Martin Hennings Feb 14 '11 at 13:45
  • Are you working with NAudio now? Or have you decided to use another framework? – 10ff Jun 03 '12 at 17:32
  • I stayed with DirectSound and passed the decision on to my successor (the way it always goes, I guess...). The next project that might come up needing sound I'll try NAudio or Qt, depending on the platform. – Martin Hennings Jun 04 '12 at 09:03

1 Answers1

7

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.

Mark Heath
  • 45,254
  • 26
  • 128
  • 188