Having finally got around to installing DirectX 9.0. I was impressed to find that it has several assemblies encapsulating .NET. Previous to the existence of .NET, I was loathe to touch those convoluted API's. The API's usually involved COM which sometimes requires a degree in Rocket Science or equivalent. Now perhaps its worth exploring.
This application takes advantage of DirectSound, one of the many media technologies packaged with DirectX 9. There are four steps to using the DirectSound technology in C#.
1. Create a sound device. (Here we just use a default constructor for the default device.)
2. Create a Buffer Description. The description contains information describing how the buffer will behave.
3. Use the device, description, and the name of the .WAV file to create a secondary buffer. All sound is played using this buffer object. You can also change properties of the secondary buffer (such as frequency) to alter the attributes of the sound
4. Play the sound. Playing the sound is accomplished through a method on the secondary buffer.

Figure 1 - Dot Net Piano Application
The table below lists the classes in the Microsoft.DirectX.DirectSound assembly:
Device
DirectSound Class
Description
The DirectSound Device
BufferDescription
The description object for the buffer that sets some of the properties of the buffer
SecondaryBuffer
The buffer object where audio data is written to and played from.
Table 1 - DirectSound classes used in this project
In this article we use DirectSound in conjunction with GDI+ (instead of DirectDraw) to create a virtual piano. The piano only contains a little more than one octave, but you can easily extend it given the extensible design shown below:

Figure 2 - Virtual Piano UML Design reverse engineered using WithClass 2000
To play the piano in the program, simply click the left mouse button on a virtual key on the keyboard. You can also adjust the volume and panning of the piano with the sliding toolbars.
This program uses DirectSound to take advantage of the fact that you can alter the frequency of the DirectSound buffer by changing the Frequency property. In this way, you can use the same wave file for each key and simply alter the buffer's frequency for the particular key. For this application, we chose the ding.wav file to produce the sound for the key, but the truth is, you could replace this file with any wave file of your choosing possessing a similar duration.
The first step is to put references in our project that allow us to use the DirectSound assemblies. You can either use the DirectX 9 Visual C# Project Wizard that is automatically installed with DirectX 9 or you can manually insert the assemblies into your project. You'll also need to declare the include references in your project as shown below:
using Microsoft.DirectX.DirectSound;
using Buffer = Microsoft.DirectX.DirectSound.Buffer;
The second step in playing sound is to create our Device object done in the form constructor shown in the code below. The Device object is constructed with no parameters to obtain the default device.
Listing 1 - Creating the DirectSound Device
Sam HobbsPosted Mar 16, 2011, 8:50 PM
I built the project using VS 2010 and when I execute it it says the image is not a valid Win32 application. I am currently exploring and don't really need this but if I did need it I would try creating a new project and migrating the source files etc. over but I just wanted to post this in case it helps.
Amal SrivastavPosted Aug 5, 2010, 1:10 AM
Nice product
j aPosted Apr 3, 2009, 12:51 AM
This is another version that doens't require directx installed on the machine: http://code.msdn.microsoft.com/virtualpitchpipe/
NorbertPosted Feb 17, 2008, 12:15 PM
Managed Debugging Assistant 'LoaderLock' has detected a problem in... What to do to get rid of this error (I mean: which parts of the code)?
Bishoy HarbyPosted Jul 22, 2007, 10:50 PM
That's exactly what i was looking for thank u very much
Michael GrumbachPosted Nov 25, 2006, 1:09 AM
Mike, Great app. Exactly what I was looking for. This will really help with my learning. Thanks!! But no sound is comming out for me. And the values on the form don't change for me when I adjust the sliders. Any idea what is going on? I have the newest version of VC#Express and 2006DirectX9.0. My system converted the file when I unzipped it. I even hard coded the values for the text and integer values, but no luck. Thanks, Mike