Step 2: Load a File

DirectMusic

Microsoft DirectX 9.0 SDK Update (Summer 2004)

Step 2: Load a File

The DirectMusic performance and synthesizer are now ready to process sound data. To get the data, the loader needs to know where to find it. Although a full path can be provided each time a file is loaded, it is more convenient to establish a default directory. Do this by using the IDirectMusicLoader8::SetSearchDirectory method.

In the sample code, the path to the default Windows media directory is given. You can change the value of wstrSearchPath to get files from a different folder.

The following code is from the WinMain function in the tutorial sample:

  // 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.
  );

In the call to SetSearchDirectory, the fClear parameter is set to FALSE because there is no danger of accidentally reloading objects from the wrong directory. This is likely to happen only if the application is loading identically named objects from different folders.

Now that the loader knows where to look for the file, it can load it as a segment:

  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;
  }

Next: Step 3: Play the File


© 2004 Microsoft Corporation. All rights reserved.