10

I'm trying to write a SAMPLER program, where each key has a different sound (a WAV file).

Can someone explain to me or give me a link to an explanation where i can learn how to play the WAV files?

If it matters, I'm working with Microsoft Visual C# and using WinForms.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Somewhat related: * http://stackoverflow.com/questions/4429513/play-wav-file-async-multiple-times-with-net * http://stackoverflow.com/questions/1087763/play-pcm-data-in-net-framework-2 – Pat Nov 15 '13 at 21:01

4 Answers4

30
SoundPlayer simpleSound = new SoundPlayer(strAudioFilePath);
simpleSound.Play();
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • can pause and resume features be made for it? – Rick2047 Feb 03 '10 at 14:26
  • I don't know to be a honest, but take a look to this low level player... http://www.codeproject.com/KB/audio-video/cswavplay.aspx, and if you are using .net 3.5, you can use MediaElement which supports pause and resume. – Arsen Mkrtchyan Feb 04 '10 at 05:50
5

use fmod, which is simply the best sound library in the whole universe

fortunately, they seem to provide a C# wrapper for the best audio API you could try to imagine, and you won't have to change a single line of code to make your code working on playstation or xbox or whatever the developers are pretty much reactive (you report a bug in the evening, go to bed, and the corrected build is available as you wake up) documentation is readable, understandable, and HUGE lot of examples in the SDK, which makes it useless to provide a tutorial since documentation is pretty much perfect

playing a wav with FMOD is just 5 lines of code, and with just 4 lines more you can apply effects while linking the balance and volume of the playback to a 3d engine (to handle intersections between the consc point and the audio source, 4 lines....

if you want to (use C# to) do sound, -> FMOD.

user217299
  • 110
  • 1
  • 4
4
SoundPlayer simpleSound = new SoundPlayer(strAudioFilePath);
simpleSound.PlaySync();

because sound play asynchronically.

Jk1
  • 11,233
  • 9
  • 54
  • 64
Arman
  • 41
  • 1
0

This console-based solution uses LINQPad (thus the .Dump() extension method calls) and NAudio (you'll notice that I use the full namespace on a couple of classes just to clarify). To get set up correctly, you can just download the snippet from http://share.linqpad.net/d7tli8.linq (I added NAudio from NuGet).

To run, open in linqpad, set the value of wavFilePath to a local wave file path, and hit F5. Play is async, so we do a Console.ReadLine to wait until it's done.

string wavFilePath = @"TODO";
var reader = new NAudio.Wave.AudioFileReader(wavFilePath);
reader.Dump("AudioFileReader");
var sampleProvider = reader.ToSampleProvider().Dump("sample provider");

NAudio.Wave.WaveOut.DeviceCount.Dump("num waveout on comp");
var outputDeviceInfo = WaveOut.GetCapabilities(0).Dump();
var outputter = new WaveOut() {
    DesiredLatency = 5000 //arbitrary but <1k is choppy and >1e5 errors
    , NumberOfBuffers = 1 // 1,2,4 all work...
    , DeviceNumber = 0
}.Dump();
outputter.Init(reader);
outputter.Play(); // async
Console.Read();
outputter.Stop();

And this is what the output from all the .Dump calls looks like on my machine, in case you're wondering:

audiofilereader contents

sampleprovider and waveout info

Pat
  • 16,515
  • 15
  • 95
  • 114