Small tip for playing sounds with DirectX



Just a small tip for playing sounds using DirectX. I needed to play
multiple sounds at the same time and so implemented code based on examples
I'd seen at juhara.com, in particular this article;

http://juhara.com/article-en-9-Playing-WAV-and-MIDI-files-using-DirectX-
Audio-with-Delphi.html


But no matter what I did I just didn't get any sound out of the speaker
even though no errors were raised and stepping through the code raised no
issues. Here's the test code I was using in it's entirety;



unit DirectSoundTests;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms,
Dialogs,DirectSound,DirectMusic,ActiveX;

type
TForm1 = class(TForm)
procedure FormShow(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
FPerformance : IDirectMusicPerformance8;
FSoundSegment : IDirectMusicSegment8;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormDestroy(Sender: TObject);
begin
FSoundSegment.Unload(FPerformance);
FPerformance.CloseDown();
FPerformance:=nil;
end;

procedure TForm1.FormShow(Sender: TObject);
var
FLoader : IDirectMusicLoader8;
FAudioPath : IDirectMusicAudioPath8;
FSegmentState : PIDirectMusicSegmentState;
begin
// inialise directx audio system, first initialise com (in activex)
CoInitialize(nil);
// initialise performance (normally one per application)
CoCreateInstance
(CLSID_DirectMusicPerformance,nil,CLSCTX_INPROC,IID_IDirectMusicPerformance
8,FPerformance);
if (FPerformance<>nil) then // all went ok ? initialise
performance
FPerformance.InitAudio
(nil,nil,Handle,DMUS_APATH_SHARED_STEREOPLUSREVERB,64,DMUS_AUDIOF_ALL,nil);
// next initialise loader object
CoCreateInstance
(CLSID_DirectMusicLoader,nil,CLSCTX_INPROC,IID_IDirectMusicLoader8,FLoader)
;
// Load sound sample in loader object, all samples go into loader and can
be later reference by FSoundSegment
FLoader.LoadObjectFromFile
(CLSID_DirectMusicSegment,IID_IDirectMusicSegment8,'C:\Sounds
\Intro.wav',FSoundSegment);
// download the sound segment into performance
FSoundSegment.Download(FPerformance);
// create an auido path (channle 64) to allow us to play more than one
sound at once
FPerformance.CreateStandardAudioPath
(DMUS_APATH_SHARED_STEREOPLUSREVERB,64,TRUE,FAudioPath);
// end of directx sound system initilisation

// play sample with mixing with any other sounds
FPerformance.PlaySegmentEx
(FSoundSegment,nil,nil,DMUS_SEGF_SECONDARY,0,FSegmentState,nil,FAudioPath);

end;

end.

All looks OK doesn't it, but no sound happened. I then discovered that
setting FSegmentState to nil before passing it to
FPerformance.PlaySegmentEx solved the issue and the sound played fine. So
adding the line

FSegmentState:=nil;

anywhere before calling FPerformance.PlaySegmentEx enables the sound to
play. I re-tested by taking the line out and the sound just refuses to
play.

I note that the examples on the page I've linked to are based on DirectX 8
and I currently have DirectX9c, which may be the reason, perhaps directX 9
requires the pointer to be nil before entry ? I also had to change the
IDirectMusicSegmentState8 type in the examples on that page to
PIDirectMusicSegmentState.

Hope this helps anyone with a similar problem.
.



Relevant Pages

  • RE: Sound device problem
    ... "Mark L. Ferguson" wrote: ... Download details DirectX 9.0c End-User Runtime: ... The audio files do not open or play without sound; ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Bad DirectSound driver (88780078)
    ... but I seriously wanna figure out the ... My sound devices are definitely hooked up, ... You can reinstall DirectX as much ... installing DirectX 7 on top of DirectX 9, ...
    (microsoft.public.windowsxp.general)
  • Re: Pinball flashes
    ... lets go ahead and update those sound drivers as well ... || Your nVidia driver is outdated.. ...
    (microsoft.public.windowsxp.games)
  • Re: Problem with onboard sound (CMI 8738)
    ... > I can play audio CDs but only directly from the CD drive, ... > pushing on the CD-ROM drive play button. ... Did sound work before and suddenly stopped working? ... Try running msinfo32 and select DirectX from the Tools drop ...
    (alt.comp.hardware.pc-homebuilt)
  • Re: new to audio programming
    ... They have a C# version of the API, and it is very easy to work with. ... believe it is more powerful than straight DirectX at this point. ... > direct sound. ... There also seems to be a limit of 1 instance of each type of filter, ...
    (microsoft.public.win32.programmer.directx.audio)