DirectSound is driving me insane! Please Help...

Started by
13 comments, last by AzrielCross 17 years, 6 months ago
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]
I must have generated alot of errors when I was compiled... ~Azriel Cross
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.
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.
Anthony Rufrano
RealityFactory 2 Programmer
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
I must have generated alot of errors when I was compiled... ~Azriel Cross
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
I must have generated alot of errors when I was compiled... ~Azriel Cross
I modified my post. It seems you were quicker than me. :)
Anthony Rufrano
RealityFactory 2 Programmer
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?
I must have generated alot of errors when I was compiled... ~Azriel Cross
I can't figure this out... Anyone?
I must have generated alot of errors when I was compiled... ~Azriel Cross
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?
Anthony Rufrano
RealityFactory 2 Programmer
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
I must have generated alot of errors when I was compiled... ~Azriel Cross

This topic is closed to new replies.

Advertisement