views:

91

answers:

2

There are many ways of playing sounds on Windows. Which are the differences, advantages and disavantages of each method?

I know there are at least 5 methods:

  1. 1991 WinMM.dll/mmsys.dll PlaySound
  2. 1995 MCIWnd (as suggested by @casablanca)
  3. 1996 DirectSound
  4. 1998 WaveOut
  5. 1999 ASIO
  6. 1999 Windows Media Player ActiveX control?
  7. 2005 WASAPI (which is used by XAudio2 - as suggested by @Han)
  8. 2007 XAudio2
+4  A: 

QSound, then it will fit right in with the rest of your Qt application, and it will work not only on Windows but also on Mac OS X and Linux, as well. It is not uncommon to find a core, platform-specific API that isn't very friendly to developers, and then a myriad of more developer friendly APIs built on top of the core. Using a core API may be negligibly faster, but using the layers on top of these core APIs is almost always more convenient and maintainable, and protects you from changes to the low-level core.

Edit
From the description of XAudio2:

XAudio2 is a low-level, cross-platform audio API for Microsoft Windows and Xbox 360. It provides a signal processing and mixing foundation for games that is similar to its predecessors, DirectSound and XAudio. For Windows game developers, XAudio2 is the long-awaited replacement for DirectSound.

So, it looks like that would be the API to use if you want a core, platform-specific audio library.

Edit 2
I was a little quick with my first answer... really, it depends on what you want to do. If all you want to do is play an audio file, then QSound is the way to go. If, however, you want to mix and generate audio on the fly, then using a more feature-capable library such as XAudio2 (which is a part of DirectX and is intended for creating sound as part of video games) would be the way to go.

Michael Aaron Safyan
I'm really curious how you knew his app is based on Qt. Did the question get edited? Is that in another of Jader's questions?
Ben Voigt
@Ben, I didn't... it was more of a suggestion.
Michael Aaron Safyan
+1  A: 

Really depends on what you want to do. For most common scenarios, I've found that the MCIWnd functions work well: they're really easy to use and can play any format for which a codec is installed.

DirectSound is somewhat more difficult to use but gives you much more control over the output; it lets you add special effects and simulate 3D positioning.

The waveOut functions are the lowest level API you can get to and they are a kind of double-edged sword: you get to control exactly what goes out to the speakers, but they accept only raw waveform data, which means you are responsible for all decoding and post-processing of input data. PlaySound essentially provides a nice wrapper around this.

casablanca
Warning: The waveOut functions have a notorious history of behaving *very slightly differently* in different Windows versions, resulting in #ifdef hell in my code. If there was ever a one-size-fits-all for this I never saw it nor inadvertently stumbled upon it in years of making sound work on multiple Windowss at once.
Eric Towers
When there's no video content, I would argue that [`mciExecute`](http://msdn.microsoft.com/en-us/library/dd757154.aspx) (or mciSendString) is far superior to MCIWnd.
Ben Voigt
I added the dates of introduction of each API for a historical perspective
Jader Dias