Small tip for playing sounds with DirectX
- From: Steve <Notfor@xxxxxxxx>
- Date: Wed, 13 May 2009 10:10:33 +0100
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.
.
- Prev by Date: Re: re Thread probrem
- Next by Date: Re: tTreeview Delphi 2009 Pro
- Previous by thread: tTreeview Delphi 2009 Pro
- Next by thread: Form1 not showing in delphi editor
- Index(es):
Relevant Pages
|