Archived

This topic is now archived and is closed to further replies.

LucidDreamer

DirectX 8 Audio (Lag and Overlap)

Recommended Posts

LucidDreamer    122
Okay, I''ve just been delving into sound and music with DirectX 8 and I''m running across some annoying problems. One thing is that I can''t find any decent tutorials/manuals that explain it, so I don''t know where to begin looking for how to solve my problems. But here are the two problems I''m having: 1) The sound lags. That is, a sound doesn''t start playing until about half a second after the function that tells it to play is called. 2) I can''t figure out how to play two sounds simultaneously. I want background music, but I also want game sounds, and I don''t know how to make them play at the same time. Specifically the problem is I have background music playing (as a midi file), and I play a game sound (as a wave file) and the sound plays, but the background midi playback stops at the same time. Okay, so anyway, these may be relatively common problems that someone knows the solution to right off hand, but if not, here''s basically how my program works (I''ve stripped a lot of error handling code and other code that isn''t necessary for understanding the sound part): First, I do some setup: // Create the music music loader object CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC, IID_IDirectMusicLoader8, (void**)&noiseLoader); // Create the performance object CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC, IID_IDirectMusicPerformance8, (void**)&performance); // Initialize the audio performance->InitAudio(NULL, NULL, wnd, DMUS_APATH_SHARED_STEREOPLUSREVERB, 64, DMUS_AUDIOF_ALL, NULL); // Set the search directory to the program''s audio directory noiseLoader->SetSearchDirectory(GUID_DirectMusicAllTypes, L".\\audio", FALSE); Then, I load some wave and midi files using the following code (this is actually a function and the variable "segment" here is actaully just part of an array of segments variables elsewhere in the program): // Load the sound noiseLoader->LoadObjectFromFile(CLSID_DirectMusicSegment, IID_IDirectMusicSegment8, filename, (void**)&segment); // Special stuff for midi files if (isMidi) { // Tell it that it''s a standard midi file segment->SetParam(GUID_StandardMIDIFile, 0xFFFFFFFF, DMUS_SEG_ALLTRACKS, 0, NULL); } // Download the instruments segment->Download(performance); Finally, I play back the sounds using the following code: if (repeat) { segment->SetRepeats(DMUS_SEG_REPEAT_INFINITE); } else { segment->SetRepeats(0); } performance->PlaySegment(segment, DMUS_SEGF_AFTERPREPARETIME, 0, NULL); And it all kind of works, except for the couple problems above.

Share this post


Link to post
Share on other sites
dansteph    122
1-read the directsound doc
2-Wonder if it *really* talk about sound
3-make some test, take an aspirin
4-Download FMOD (www.fmod.org)
5-Enjoy music s3m mod , mp3, 3d sound in few ligne of code without headeach (low cost)
6-Smile, the life is wonderful

Dan

Share this post


Link to post
Share on other sites
Jim Adams    440
I had the exact same problem on lag when starting - couldn''t find a solution. As far as I can tell, DM starts sounds playing on the beat, not immediately. Somebody had previously mentioned that DM is either using a debug library or that the drivers are not certified. Either way, I dropped it and used DS instead for sound effects.

As for for playing multiple sounds, use the following to play music:

performance->PlaySegment(segment, DMUS_SEGF_AFTERPREPARETIME, 0, NULL);

For playing sounds, use:
performance->PlaySegment(segment, DMUS_SEGF_SECONDARY, 0, NULL);

Jim Adams

Share this post


Link to post
Share on other sites
LucidDreamer    122
I looked up PlaySegment() and found out where the lag is coming from. That second parameter, DMUS_SEGF_AFTERPREPARETIME, causes the sound, by default, to wait 1000ms before playing. This is to give time for, I quote, "the message to be processed by tools." I''m not sure what this means. Actually, I really have no idea what it means. But in any case, I just told the background midi music to play as DMUS_SEGF_SECONDARY, just like the foreground wave sounds, and that eliminated the lag. And it all worked fine with multiple sounds playing at once and all.

Share this post


Link to post
Share on other sites