Click here to Skip to main content
Click here to Skip to main content

Playing a sound from a J# application using DirectX9

By , 21 Nov 2004
 

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.

The Direct X 9 download page:
http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp

When this article was written, the latest SDK was the October 2004 update.
http://www.microsoft.com/downloads/details.aspx?FamilyId=B7BC31FA-2DF1-44FD-95A4-C2555446AED4&displaylang=en

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.

The DirectX 9 download page:
http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp

When this article was written, the latest redistributable was the October 2004 edition:
http://www.microsoft.com/downloads/details.aspx?FamilyId=143EF8C6-CFF7-4AF0-209-1CDF49C58BC0&displaylang=en

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.

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 the Microsoft.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 GlobalFocus property to true.

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 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;

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

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.

A list of licenses authors might use can be found here

About the Author

Lars-Inge Tønnessen
Web Developer
Norway Norway
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralBufferDescription to Buffer connectionmemberThorbjørn Jørgensen26 Mar '07 - 13:28 
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?
 
Regards
Thorbjørn
GeneralRe: BufferDescription to Buffer connectionmemberThorbjørn Jørgensen26 Mar '07 - 13:38 
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 );
 
Unless I still am missing something?
 
Regards
Thorbjørn

QuestionHow to Change the output both speaker Left or Right Onlymemberaka^_^chan23 Jul '06 - 21:21 
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.
 
thanks in advanceBig Grin | :-D
 

GeneralInstalling DirectXmemberDipeshKhakhkhar23 Nov '04 - 17:24 
Hi,
 
After installing DirectX, it is not integrated with Visual Studio .NET environment.
 
I mean it VS .NET does add references of Direct X dlls. Do i need to add it one by one or I am doing something wrong ??
 
Thanks!
Regards,
Dipesh
 
Dipesh
GeneralRe: Installing DirectXmemberLars-Inge Tønnessen23 Nov '04 - 19:28 
Do you have the Visual Studio .net/2002 edition?

GeneralRe: Installing DirectXmemberDipeshKhakhkhar23 Nov '04 - 20:00 
Hi,
 
Thanks for replyinig.
i have visual studio .net 2003.
 
Regards,
 

 
Dipesh
GeneralRe: Installing DirectXmemberLars-Inge Tønnessen24 Nov '04 - 10:22 
Please try to add a reference to the DLL file:
 
C:\WINDOWS\Microsoft.NET\Managed DirectX\v9.02.3900\Microsoft.DirectX.DirectSound.dll
 
(Choose the "Browse" button)

GeneralRe: Installing DirectXmemberDipeshKhakhkhar24 Nov '04 - 13:29 
Hi,
 
I am not able to find the dll which you described in your last email.
 
There were xml files in that folder.
 
Well I have uninstalled that version of directX now. So shall i first get the SDK from this address.
 

 
[^]
 
Please confirm this.
 
Thanks!
Regards,
Dipesh
 
Dipesh
GeneralRe: Installing DirectXmemberDipeshKhakhkhar24 Nov '04 - 13:32 
I mean I am downloading DirectX again from the site you have mentioned.
 
Dipesh
GeneralRe: Installing DirectXmemberLars-Inge Tønnessen27 Nov '04 - 1:57 

You should find these files in the folder:
 
Microsoft.DirectX.AudioVideoPlayback.xml Microsoft.DirectX.Diagnostics.dll
Microsoft.DirectX.Direct3D.dll Microsoft.DirectX.Direct3D.xml
Microsoft.DirectX.Direct3DX.xml Microsoft.DirectX.DirectDraw.dll
Microsoft.DirectX.DirectInput.dll Microsoft.DirectX.DirectInput.xml
Microsoft.DirectX.DirectPlay.xml Microsoft.DirectX.DirectSound.dll
Microsoft.DirectX.dll Microsoft.DirectX.xml
18 File(s) 9 574 967 bytes
 
Please remember to disable any antivirus or security software while installing.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 21 Nov 2004
Article Copyright 2004 by Lars-Inge Tønnessen
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid