Solved

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

Posted on 2004-08-17
8
677 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:kayhustle
8 Comments
 
LVL 12

Expert Comment

by:vascov
ID: 11826659
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
0
 
LVL 1

Author Comment

by:kayhustle
ID: 11826728
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.
0
 
LVL 3

Expert Comment

by:chintan_vaishya
ID: 11827565
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:kayhustle
ID: 11836111
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.
0
 
LVL 12

Accepted Solution

by:
vascov earned 500 total points
ID: 11836388
0
 
LVL 4

Expert Comment

by:Eran_R
ID: 11843283
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
0
 
LVL 1

Author Comment

by:kayhustle
ID: 11899709
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
0
 
LVL 1

Author Comment

by:kayhustle
ID: 11945793
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.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question