Link to home
Start Free TrialLog in
Avatar of kayhustle
kayhustle

asked on

How to play a wav/wave file in C# .NET?

I need sample C# code that shows how to open up a wav file and send the loaded amplitude values to be played by the sound card.
Avatar of vascov
vascov
Flag of United States of America image

Hi,

Do you mean something like:
using System;
using System.Runtime.InteropServices;

class SoundLib
{
      [ DllImport("winmm.dll") ]
      public static extern long PlaySound(String Name, long Module, long Flags);
}

class App
{
      public static void Main( string[] snds )
      {
            foreach( string snd in snds )
            {
                  try
                  {
                        SoundLib.PlaySound( snd, 0, 0 );
                  }
                  catch( Exception e )
                  {
                        Console.WriteLine( e.ToString() );
                  }
            }
      }
}

ps "C:\windows\media\chimes.wav"


Or do you need something more elaborate ?

Vasco
Avatar of kayhustle
kayhustle

ASKER

No, I need to know how to open up the wav file and read the individual amplitude values, then pass these as chunks to be played by the sound card.  Wav files store the sound as the numeric amplitude values in a range from 0 to 2 ^(#bits), I need to read these individual values for each channel and play them back.
While those links are very helpful, they don't actually answer my question.  They show how to play a wav file.  I need to know how to open it and read the individual amp values for processing.
ASKER CERTIFIED SOLUTION
Avatar of vascov
vascov
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If I understand your needs correctly, here's what you need:

You need to make use of DirectX DirectSound technology.
DirectSound uses something called SoundBuffers that contain actual wave data. This data can be static but can also be streaming as you require.

Luckily enough microsoft already provide managed wrappers for all that COM junk.
It's called "Managed DirectX" and you can download it from microsoft.

Here's a snippet from the Managed DirectX SDK documentation which I think answers your needs:

Using Streaming Buffers
================
A streaming buffer plays a long sound that cannot all fit into the buffer at once. As the buffer plays, old data is periodically replaced with new data. Play a streaming sound with the following procedure:

1. Call SecondaryBuffer(Stream,BufferDescription,Device) to create a buffer with the correct waveform format, and of a convenient size. A buffer large enough to hold one or two seconds of data is typical; smaller buffers can be used, but they have to be refreshed more frequently, which can be inefficient.
2. Set notification positions so that your application knows when to refresh a portion of the buffer. For example, you can set notifications halfway through the buffer and at the end. When the play cursor reaches the halfway point, the first part of the buffer is refreshed; when it reaches the end, the second part is refreshed. Alternatively, you can poll the play cursor as the buffer plays, but this is less efficient than notification. For more information, see Play and Write Cursors.
3. Load the entire buffer with data by using Buffer.Write.
4. Call Buffer.Play (inherited by SecondaryBuffer), specifying the BufferStatus.Looping property.
5. When the play cursor reaches the first point at which you want to refresh data, call Write again, writing new data from the start of the buffer up to the play cursor. Save the position of the last byte written.
6. Call Write each time the cursor reaches a refresh point, writing data to the part of the buffer that lies between the saved position and the play cursor. Note that wraparound is handled automatically: if the buffer is 10,000 bytes long, and you write 2000 bytes to offset 9000, the last 1000 bytes are written to the front of the buffer.
7. When all data has been written to the buffer, set a notification position at the last valid byte, or poll for this position. When the cursor reaches the end of the data, call Buffer.Stop.

The full documentation is available on the following link:
http://msdn.microsoft.com/archive/en-us/directx9_m/directx/sound/playingsounds/directsound_buffers.asp

A more general link on DirectSound using managed DirectX:
http://msdn.microsoft.com/archive/en-us/directx9_m/directx/sound/playingsounds/playing_sounds_entry.asp

Hope this helps,
Eran
Ok, so I can get the wave bytes, but do you know how those bytes correspond to the left and right channel amplitude values.  The code project documentation shows how to get the sample rate and a lot of other header info, but do you know how the actual wave data is arranged, so that I could lets say plot the waveform in an image?
Thank you
Found the answer, vascov's link points to an article written by Ianier Munoz, I emailed the guy and asked him.  He pointed me to an article he wrote about programming audio effects in C#, http://www.codeproject.com/cs/media/cswavplayfx.asp,and in those source files, he processes the individual amplitude values in the wave.  Thank you everybody.