Playing a sound from a J# application using DirectX9

This introductory article will show you how to play a sound using DirectX9 from a J# application

Introduction

All cool applications have sound effects. Your J# applications should be no exception, whether they are migrated from Sun Java to .NET or they are completely new J# applications. It is very easy to play sounds from a .NET application using the DirectX 9 libraries. This article will show you how to do this.

Step 1, install the DirectX 9 SDK

The first step is to install the DirectX9 SDK on the development computer. Go to the DirectX 9 download web site, and grab the latest SDK and install it.

Step 2, download the DirectX 9 redistributable for client computers

All client computers that want to run your J# DirectX9 application must have the DirectX9 runtime libraries installed (just like they must have the J# runtime libraries to run J# applications). These libraries are usually installed through the Windows Update, but if your client does not have an internet access you can provide the redistributable DirectX9 install package with your application. You will find the latest redistributable package on the DirectX 9 download page.

Step 3, make the J# application

After installing the DirectX9 SDK, open Visual Studio and open a new J# application.

Add a reference to Microsoft.DirectX.DirectSound

In Visual Studio, choose the “Project” and “Add Reference…” in the menu. Add a reference to Microsoft.DirectX.DirectSound in the .NET tab.

Give me Sound!

To gain performance, DirectX uses buffers to play sounds on hardware sound devices. This means you have to load the sounds into buffers before any hardware devices can play them. Hardware devices are accessed through a Microsoft.DirectX.DirectSound.Device class instance. A buffer instance is made of the Microsoft.DirectX.DirectSound.SecondaryBuffer class. The sound device on your computer is normally shared between different applications. You control how the sound in your application will behave with other running applications through setting a CooperativeLevel on the device instance.

You can force the sound to be loaded into the hardware device buffer by setting the LocateInHardware property to true. However if the hardware does not support this option the call will fail. Equally you can force the sound to be loaded into a software buffer with setting the LocateInSoftware property to true;

How to control the sound volume If you want to change the sound volume, first enable the volume control on the buffer description instance. Second, you can control the volume with the volume property on the sound buffer instance. 0 is full volume, -10000 is mute.

To change the sound balance, enable the pan property on the buffer description instance, and set the balance level with the pan property on the buffer instance. 0 gives equal amount of sound from both left and right speaker. Positive number gives more sound out of the right speaker. Negative number will give more sound from the left speaker.

References

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

About the Author

Comments and Discussions

Hi
In the code I can see that you creates a BufferDescription (dsc), but it does not look like it is beeing applied to the buffer. The SecondaryBuffer constructor comes with an option to include a BufferDescription, but that require that the file i loaded in another call, so you approach would be nice, but I can not make it work. Am I missing something here?

Hi againg...
A closer look a the documentation reveals that a SecondaryBuffer construdtor which takes both the filename and the BufferDescription exists. But I still think that when using the BufferDescription the call to create the SecondaryBuffer should look like this:
SecondaryBuffer snd = new SecondaryBuffer("your_sound_file.wav", dsc, dev );

Can we change the output to both speaker left signal only? I want to try it for my karoke vcd which have a different sound between left or right. The left only produce music, and the right produce human vocal only.