

We'll save this for the next composition using C# and. This application could be easily expanded to turn the Virtual Piano into a virtual synthesizer because DirectSound gives you all sorts of effects that you can use to alter your wave file. Producing sound just became a lot easier because its encapsulated nicely in. Before we play, make sure we're using the correct settings of volume and pan

recreate a new buffer description as wellīufferDescription desc = new BufferDescription() ĪpplicationBuffer = new SecondaryBuffer(strFileName, desc, applicationDevice) so we have a starting point with fresh data First we need to 'recreate' the buffer Listing 4 - Play the Note Corresponding to the Frequency We need to recreate the buffer each time so we are always altering the original reference data. The PlayNote method creates the secondary sound buffer, assigns the frequency to the secondary sound buffer, and calls play on the buffer. Int freq = FindFrequency( new Point(e.X, e.Y), out CurrentKey) Determine the key that was pressed from the mouse position and lookup the frequency Private void PianoForm_MouseDown( object sender, e) Listing 3 - Handling the Playing of the Piano Key when the Mouse is Pressed Once the frequency is determined we simply need to play the WAV file with the new frequency. (Each PianoKey object contains the frequency associated with it.).
Virtual piano .nat code#
This code determines the frequency of the Secondary Buffer by finding the key that was pressed and looking up the corresponding frequency for that piano key. The code for the mouse being pressed is executed in the mousedown event handler shown below. In the piano program, the sound is played each time a virtual piano key is pressed. Sample will exit.", "Exiting.", MessageBoxButtons.OK, MessageBox.Show("Could not initialize DirectSound. Set the priority of the device with the rest of the operating systemĪpplicationDevice.SetCooperativeLevel( this, CooperativeLevel.Priority) Listing 1 - Creating the DirectSound Device The Device object is constructed with no parameters to obtain the default device. The second step in playing sound is to create our Device object done in the form constructor shown in the code below. You'll also need to declare the include references in your project as shown below: 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. The first step is to put references in our project that allow us to use the DirectSound assemblies. 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. In this way, you can use the same wave file for each key and simply alter the buffer's frequency for the particular key. 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. You can also adjust the volume and panning of the piano with the sliding toolbars. To play the piano in the program, simply click the left mouse button on a virtual key on the keyboard. The piano only contains a little more than one octave, but you can easily extend it given the extensible design shown below:įigure 2 - Virtual Piano UML Design reverse engineered using WithClass 2000 In this article we use DirectSound in conjunction with GDI+ (instead of DirectDraw) to create a virtual piano. Table 1 - DirectSound classes used in this project The buffer object where audio data is written to and played from. The description object for the buffer that sets some of the properties of the buffer The table below lists the classes in the assembly: Playing the sound is accomplished through a method on the secondary buffer. You can also change properties of the secondary buffer (such as frequency) to alter the attributes of the soundĤ. All sound is played using this buffer object. Use the device, description, and the name of the. The description contains information describing how the buffer will behave.ģ. (Here we just use a default constructor for the default device.)Ģ. There are four steps to using the DirectSound technology in C#.ġ. This application takes advantage of DirectSound, one of the many media technologies packaged with DirectX 9. The API's usually involved COM which sometimes requires a degree in Rocket Science or equivalent. NET, I was loathe to touch those convoluted API's.

I was impressed to find that it has several assemblies encapsulating.

Having finally got around to installing DirectX 9.0.
