2

I need something that will allow my program to play several audio files (.wav and/or .mp3) at the same time. It would be ideal if I could simply pass a file path to some method, and it will play that file without me having to worry about any of the details.

Additional requirements:

1) I need to be able to programmatically start and stop the playing of any audio file at any time.

2) I need to know when each audio file has finished playing (by either responding to an event or by knowing the duration of the sound file (samples, seconds, etc.) through some property)

3) This tool must be able to play .wav (and preferably .mp3) files of any length/duration.

4) This tool must be able to play multiple audio files at the same time (preferably at least 5 or 6 audio files at the same time).

I have already tried using SlimDX and SharpDX, but I could not find any way of programmatically determining when the file has stopped playing, or of finding the duration (length) of the file.

EDIT:

I believe I found what I was looking for! GStreamer seems to do everything that I was asking for. http://www.gstreamer.com/

Jackson Dean Goodwin
  • 617
  • 3
  • 11
  • 20
  • What research have you already done? What have you tried? Did you check to see whether anybody asked this question in the past? (hint: about 3 or 4 of them in the last week for various programming languages) I suspect the answer is none, none, and no respectively. No effort questions tend not to last long. – marko Jan 26 '13 at 18:53
  • You could improve your question by telling us what you've tried and what didn't work for you (and why it didn't work). Your question should stand alone. As it stands your question is overly broad, and unlikely to solicit many answers. Reading the [FAQ](http://stackoverflow.com/faq) is worthwhile if you haven't already done so. – marko Jan 26 '13 at 19:16
  • I found it! GStreamer seems to be exactly what I was looking for. http://www.gstreamer.com/ – Jackson Dean Goodwin Feb 11 '13 at 14:44

2 Answers2

1

All audio APIs will allow playback of multiple files simultaneously (mixing takes places behind the scenes, you don't need to worry about it).

Native APIs:

  • waveOut* family of functions, ACM for decompression
  • DirectSound (playback)
  • DirectShow (decoding, playback)
  • WASAPI (playback)
  • Media Foundation

Or, use NAudio for C# wrapper for the mentioned above. You will find a number of posts on NAudio right here on StackOverflow when you need certain details and code snippets.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
1

The generic answer to this question (good for most of the how do I play multiple samples at once in frameworks/language X questions) is to software mix the files together.

This has the benefit of giving you complete control of when the individual audio streams start and stop and of synchronisation - something that is typically very difficult when using higher level media player components.

To steps for doing this are:

  1. Load (and format convert/decode) all audio to be output into a memory buffer
  2. For every audio file playing at a given instant in time, mix the corresponding sample from each file by adding them together.
  3. [Not always necessary] adjust the gain of the resulting sample - typically this is division by the number of samples mixed
  4. Store the result into another memory buffer

You have now reduced your problem to a single audio stream to output. Most likely you use your framework of choice's audio output APIs, which periodically request a buffer of samples to output.

If your audio streams are large, the problem becomes a bit more complex as you will need to process your input streams a bit at a time.

marko
  • 9,029
  • 4
  • 30
  • 46
  • Could you show me an example of this? Can I still have some way of knowing when each audio file has finished playing, or better than that, to know how long (how may samples, seconds, etc.) each audio file is (so that I can overlap successive audio files by a certain percentage)? – Jackson Dean Goodwin Jan 26 '13 at 19:44
  • I added details to my question. This should give you a better idea of what I need. – Jackson Dean Goodwin Jan 26 '13 at 19:55
  • I haven't got an example to hand in C#.NET (if it were iOS or MacOSX you'd be in luck), but concerning the other questions, yes - after decoding the files into the memory buffer, you know precisely how long they are in samples - and therefore time. Achieving an overlap is simply a matter of adding the new file into the mix at the appropriate point. – marko Jan 26 '13 at 19:56
  • It's worth pointing out here that you could create a DirectShow graph that includes a mixer from a number of memory buffered inputs - but it's almost certainly easier not to! – marko Jan 26 '13 at 20:01