• Advertisement
Sign in to follow this  

DirectSound is driving me insane! Please Help...

This topic is 4149 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there all, I am beginning my education in C++ and DirectX, and have developed my first Pong Game. It's in Direct3D, I managed to build it from scratch, no tutorials or anything (Other than the direct3d standard triangle tutorials). So now I have a working D3D Pong like game that works great. The only problem is I want a little .wav to play when the ball touches the paddles. For over a week now I have been pouring over code after code, tutorial after tutorial (including each directsound tutorial on this site) and nothing will work. All of them pretty much lead me to setup directSound the same way, and when I do... I get an unhandled exception error when directsound init's. From what very very little I know about this error, I believe it is generated from a null value being passed. The error is as follows: Unhandled exception at 0x00411472 in soundtrials.exe: 0xC0000005: Access violation reading location 0x00000000. -g_pPerformance 0x00000000 IDirectMusicPerformance8 * -IDirectMusicPerformance {...} IDirectMusicPerformance -IUnknown {...} IUnknown __vfptr CXX0030: Error: expression cannot be evaluated I made a seperate project to simplify my trial and error of the sound war. I have copied the code verbatim from the Microsoft website. Here is the code:
#define INITGUID
#include <dmusici.h>


IDirectMusicLoader8*  g_pLoader           = NULL;
IDirectMusicPerformance8* g_pPerformance  = NULL;
IDirectMusicSegment8*   g_pSegment        = NULL;


INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, 
  LPSTR pCmdLine, INT nCmdShow )
{
  CoInitialize(NULL);
  
  CoCreateInstance(CLSID_DirectMusicLoader, NULL, 
       CLSCTX_INPROC, IID_IDirectMusicLoader8,
       (void**)&g_pLoader);

  CoCreateInstance(CLSID_DirectMusicPerformance, NULL,
       CLSCTX_INPROC, IID_IDirectMusicPerformance8,
       (void**)&g_pPerformance );

  g_pPerformance->InitAudio( 
    NULL,              // IDirectMusic interface not needed.
    NULL,              // IDirectSound interface not needed.
    NULL,              // Window handle.
    DMUS_APATH_SHARED_STEREOPLUSREVERB,  // Default audiopath type.
    64,                // Number of performance channels.
    DMUS_AUDIOF_ALL,   // Features on synthesizer.
    NULL               // Audio parameters; use defaults.
  );

    // Find the Windows media directory.
 
  CHAR strPath[512];
  if( GetWindowsDirectory( strPath, MAX_PATH+1 ) == 0 )
        return 0;
  strcat( strPath, "\\media" );
 
 // Convert to Unicode.
 
  WCHAR wstrSearchPath[MAX_PATH + 1];
  MultiByteToWideChar( CP_ACP, 0, strPath, -1, 
         wstrSearchPath, MAX_PATH );
  wstrSearchPath[MAX_PATH] = 0;

  // Set the search directory.
 
  g_pLoader->SetSearchDirectory( 
    GUID_DirectMusicAllTypes, // Types of files sought.
    wstrSearchPath,           // Where to look.
    FALSE                     // Don't clear object data.
  );

  WCHAR wstrFileName[MAX_PATH] = L"ding.wav";
 
  if (FAILED(g_pLoader->LoadObjectFromFile(
    CLSID_DirectMusicSegment, // Class identifier.
    IID_IDirectMusicSegment8, // ID of desired interface.
    wstrFileName,             // Filename.
    (LPVOID*) &g_pSegment     // Pointer that receives interface.
  )))
  {
    MessageBox( NULL, "Media not found, sample will now quit.", 
          "DirectMusic Tutorial", MB_OK );
    g_pPerformance->CloseDown();
    g_pLoader->Release(); 
    g_pPerformance->Release();
    CoUninitialize();
    return 0;
  }

    g_pSegment->Download( g_pPerformance );

  g_pPerformance->PlaySegmentEx(
      g_pSegment,  // Segment to play.
      NULL,        // Not used.
      NULL,        // For transitions. 
      0,           // Flags.
      0,           // Start time; 0 is immediate.
      NULL,        // Pointer that receives segment state.
      NULL,        // Object to stop.
      NULL         // Audiopath, if not default.
  );  
  MessageBox( NULL, "Click OK to Exit.", "Play Audio", MB_OK );
 
    g_pPerformance->Stop(
      NULL, // Stop all segments.
      NULL, // Stop all segment states.
      0,    // Do it immediately.
      0     // Flags.
  );
 
  g_pSegment->Unload(g_pPerformance);
  g_pPerformance->CloseDown();
 
  g_pLoader->Release(); 
  g_pPerformance->Release();
  g_pSegment->Release();
 
  CoUninitialize();
    
  return 0;  // Return value for WinMain.
}      // End of WinMain.


Can anyone PLEASE help me with this... It's driving me crazy! :) cheers ~ Az [Edited by - AzrielCross on October 14, 2006 9:42:03 AM]

Share this post


Link to post
Share on other sites
Advertisement
That's not DirectSound, it's DirectMusic. Anyway my advice is to forget about DirectSound or DirectMusic altogether. D3D and DirectInput might be great, but the other libraries are really lacking. Just look at all the code it took to setup and play a WAV file, I could read and parse a WAV in less.

If you still want to stick with just DirectX, find a recent DirectSound(NOT DirectMusic) and use the built-in WAV loading function. Otherwise I suggest FMOD.

(Actually after looking through the DXSDK, it would appear that the C++ version of DirectSound can not directly load WAVs. Strange since the C# version can.)

Edit: Oh yeah, you might want to edit your post so the code is between [source][/source] tags. It's in the FAQ.

Share this post


Link to post
Share on other sites
You are accessing a NULL pointer without initializing it. It looks as if CoCreateInstance() is not creating your performance instance. Place the following code after the CoCreateInstance() calls:


if (!g_pPerformance)
{
MessageBox(NULL, "Could not create performance!!", "Debug Test", 48);
return 0;
}



IIRC, you need a window in order to play the sounds also.

Share this post


Link to post
Share on other sites
Thanks Scet,

Again, forgive my ignorance as I am learning, but if I don't use DirectSound, what other choices do I have? I don't care what happens as long as two things are met. 1) A sound plays. 2) I don't want to use someone else's wrapper because I want to learn this the first time I do it.

I don't understand how playing a wav file can be so tedious lol. It seems like I am going to need to go to college for a year to learn how to play "ding.wav" on command in C++!

I see all these tetris and asteroids and space shooter games playing a sound file when something triggers it, but I can't do it for pong... ::screams::. There must be a way to manually code without using someone's wrapper, a sound into this game.

Thanks again, Az

Share this post


Link to post
Share on other sites
Quote:
Original post by paradoxnj
You are accessing a NULL pointer without initializing it.


That's what I derived from googling for several hours. But to my primitive programming brain that equates to:

information = useless;

I'm doing my best to research null pointers and what not, and how to debug code to locate null pointers through step procedures, but in the meantime, do you know which pointer is NULL and how I can fix it?

cheers, Az

Share this post


Link to post
Share on other sites
First off, Thank you again!

Finally seems like a light at the end of the tunnel... You have no idea how crazy this has been driving me.. thank you so much.


I implemented the code that you gave me and sure enough the mBox popped upon compiling... so there is the null pointer... but how do I fix that?

Share this post


Link to post
Share on other sites
CoCreateInstance() gives you an HRESULT as a return value. You might want to somehow print that data to the screen, then look at the documentation for CoCreateInstance() to see which error code you got. Couple of questions:

1. What OS are you running and do you have the latest DirectX runtime installed?

2. What version of the DirectX Software Development Kit are you using?

Share this post


Link to post
Share on other sites
Quote:
Original post by paradoxnj
CoCreateInstance() gives you an HRESULT as a return value. You might want to somehow print that data to the screen, then look at the documentation for CoCreateInstance() to see which error code you got. Couple of questions:

1. What OS are you running and do you have the latest DirectX runtime installed?

2. What version of the DirectX Software Development Kit are you using?


I'm not sure how to do that lol, but I will give it my best shot :)

FROM DXDIAG::

Time of this report: 10/14/2006, 15:46:19
Operating System: Windows Vista™ Ultimate (6.0, Build 5600) (5600.vista_rc1.060829-2230)
System Manufacturer: Hewlett-Packard
System Model: Pavilion ZV6000 (PX343UA#ABA)
Processor: AMD Athlon(tm) 64 , ~2.0GHz
DirectX Version: DirectX 10
DX Setup Parameters: Not found
DxDiag Version: 6.00.5600.16384 32bit Unicode

I am using the latest EndUser Runtime, and am using the October 2006 release of the DirectX SDK

Share this post


Link to post
Share on other sites
Quote:
Original post by Programmer16
Your code compiled and ran fine for me.
I'm on Windows XP, so maybe Vista doesn't support DirectSound; I'm not sure though. Or maybe it has to do with Direct3D 10; again not sure though.

Good luck!


lol well that's bleak :) So what are my alternatives to using DirectMusic? Surely there must be some work around... I can't believe there will be no more audio in games after vista and dx10 become mainstream lol :)

Share this post


Link to post
Share on other sites
Quote:
Original post by AzrielCross
Quote:
Original post by Programmer16
Your code compiled and ran fine for me.
I'm on Windows XP, so maybe Vista doesn't support DirectSound; I'm not sure though. Or maybe it has to do with Direct3D 10; again not sure though.

Good luck!


lol well that's bleak :) So what are my alternatives to using DirectMusic? Surely there must be some work around... I can't believe there will be no more audio in games after vista and dx10 become mainstream lol :)


I just mentioned it since I believe that both are still in the beta stage (DX10 may not be anymore; not sure though.)

There are tons of options: XAct, OpenAL, audiere, and FMOD.

Try googling "sound library" + "C++" or something similar.

Share this post


Link to post
Share on other sites
I can assure you that both DirectMusic and DirectSound work perfectly fine on Vista. DirectMusic after all is just an interface that wraps the DirectSound interface, and there are a ton of games that use DirectSound, and they still work in Vista.

Share this post


Link to post
Share on other sites
Quote:
Original post by Programmer16
I just mentioned it since I believe that both are still in the beta stage (DX10 may not be anymore; not sure though.)

There are tons of options: XAct, OpenAL, audiere, and FMOD.

Try googling "sound library" + "C++" or something similar.


lol, yeah I was kidding about the future of vista not having sound :) DirectSound is a real pain to get a wav file to play however. I fiddled with this all day and still never managed to get that performance to init correctly. Like you said, it seems I am doing everything correctly, it just isn't working on my system for some reason. As far as the other options, I was definately interested in XACT and had originally gone that route for the sound setup, however, in all their genius Microsoft released the latest DirectX SDK (with DX10) for Vista compatibility, which is the entire reason I went ahead and upgraded to Vista in the first place, then afterwards I noted that XACT is *not* compatible with Vista yet. FUN!! lol. So I'll be waiting for an update to this in the future for playing with XACT.

The other options are developed around wrappers and I'm trying to stay away from that. I feel I will learn more if I don't use a wrapper. I'm in college right now majoring in Game Development (Only my first semester) so I'm sure I'll be expected to write my own wrappers and not use anyone elses. I don't want to rely on that now and then have to go back and relearn how they did it in the first place.

However, in the end I did manage to resolve the whole thing in one nice little line of code:

PlaySound("ball.wav",NULL,SND_ASYNC|SND_FILENAME);


So now my little pong ball makes a nice DOINK! when it hits the paddles :) Next it's on to particle effects... hehehe.

Thanks everyone for your help. This is my learning project. It's hopefully the stick figure that will turn into the mona lisa type of thing in my game career. As I learn more I will continually updgrade this game into something more, and create new projects that better use the skills I learn, and I'm sure once I'm done with my degree I'll look back at this whole thing and go... "Wow... I had trouble with *THAT??!?!*"

Thanks again, and Cheers!

Az

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement