|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionAll 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 SDKThe 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.The Direct X 9 download page: When this article was written, the latest SDK was the October 2004 update.
Step 2, download the DirectX 9 redistributable for client computersAll 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.The DirectX 9 download page: When this article was written, the latest redistributable was the October 2004 edition:
Step 3, make the J# applicationAfter 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 The complete code will look like this: // Setup the sound device Microsoft.DirectX.DirectSound.Device dev = new Microsoft.DirectX.DirectSound.Device(); dev.SetCooperativeLevel( this, Microsoft.DirectX.DirectSound.CooperativeLevel.Normal ); // Setup a buffer to hold the sound Microsoft.DirectX.DirectSound.SecondaryBuffer snd = new Microsoft.DirectX.DirectSound.SecondaryBuffer( "your_sound_file.wav", dev ); // Play the sound snd.Play( 0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default ); This code will play a sound file called “your_sound_file.wav”.
Do we have more config options?Yes, they are accessed through an instance of theMicrosoft.DirectX.DirectSound.BufferDescription class you pass as an argument to the buffer.
Mute when switching applications Did you notice the sound did not play if you switched to another application? To enable the sound all the time set the Microsoft.DirectX.DirectSound.Device dev =
new Microsoft.DirectX.DirectSound.Device();
dev.SetCooperativeLevel( this,
Microsoft.DirectX.DirectSound.CooperativeLevel.Normal );
Microsoft.DirectX.DirectSound.BufferDescription dsc =
new Microsoft.DirectX.DirectSound.BufferDescription();
dsc.set_GlobalFocus( false );
Microsoft.DirectX.DirectSound.SecondaryBuffer snd =
new Microsoft.DirectX.DirectSound.SecondaryBuffer(
"your_sound_file.wav", dev );
snd.Play( 0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default );
Hardware / Software buffers You can force the sound to be loaded into the hardware device buffer by setting the Microsoft.DirectX.DirectSound.Device dev =
new Microsoft.DirectX.DirectSound.Device();
dev.SetCooperativeLevel( this,
Microsoft.DirectX.DirectSound.CooperativeLevel.Normal );
Microsoft.DirectX.DirectSound.BufferDescription dsc =
new Microsoft.DirectX.DirectSound.BufferDescription();
dsc.set_GlobalFocus( false );
// dsc.set_LocateInHardware( true );
dsc.set_LocateInSoftware( true );
Microsoft.DirectX.DirectSound.SecondaryBuffer snd =
new Microsoft.DirectX.DirectSound.SecondaryBuffer(
"your_sound_file.wav", dev );
snd.Play( 0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default );
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. Microsoft.DirectX.DirectSound.Device dev =
new Microsoft.DirectX.DirectSound.Device();
dev.SetCooperativeLevel( this,
Microsoft.DirectX.DirectSound.CooperativeLevel.Normal );
Microsoft.DirectX.DirectSound.BufferDescription dsc =
new Microsoft.DirectX.DirectSound.BufferDescription();
dsc.set_ControlVolume( true );
Microsoft.DirectX.DirectSound.SecondaryBuffer snd =
new Microsoft.DirectX.DirectSound.SecondaryBuffer(
"your_sound_file.wav", dev );
snd.set_Volume( -2000 );
snd.Play( 0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default );
How to control the left – right balance 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. Microsoft.DirectX.DirectSound.Device dev =
new Microsoft.DirectX.DirectSound.Device();
dev.SetCooperativeLevel( this,
Microsoft.DirectX.DirectSound.CooperativeLevel.Normal );
Microsoft.DirectX.DirectSound.BufferDescription dsc =
new Microsoft.DirectX.DirectSound.BufferDescription();
dsc.set_LocateInSoftware( true );
dsc.set_ControlVolume( true );
dsc.set_ControlPan( true );
Microsoft.DirectX.DirectSound.SecondaryBuffer snd =
new Microsoft.DirectX.DirectSound.SecondaryBuffer(
"your_sound_file.wav", dev );
snd.set_Volume( 0 );
snd.set_Pan( 5000 );
snd.Play( 0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default );
How to change the frequency You can set the frequency of the sound by enabling the control frequency property and setting the frequency on the sound buffer. Microsoft.DirectX.DirectSound.Device dev =
new Microsoft.DirectX.DirectSound.Device();
dev.SetCooperativeLevel( this,
Microsoft.DirectX.DirectSound.CooperativeLevel.Normal );
Microsoft.DirectX.DirectSound.BufferDescription dsc =
new Microsoft.DirectX.DirectSound.BufferDescription();
dsc.set_ControlFrequency( true );
Microsoft.DirectX.DirectSound.SecondaryBuffer snd =
new Microsoft.DirectX.DirectSound.SecondaryBuffer(
"your_sound_file.wav", dev );
snd.set_Frequency( 10000 );
snd.Play( 0, Microsoft.DirectX.DirectSound.BufferPlayFlags.Default );
References
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||