www.xbdev.net
xbdev - software development
Saturday December 7, 2024
Home | Contact | Support | XBOX Programming.. More than just a toy...
>>
     
 

XBOX Programming..

More than just a toy...

 

Prt9 - Sound

Author: Ben_3D

 

Well where would you be without sound!... it really does bring that edge to your game or application.  Now if you run up the sample code what you'll get is a playing .wav file....one from my collection, and a splash image on the screen...as who wants to steer at a blank screen...so I put a snazzy image on there so you could see a cool picture.

 

All the sound code is wrapped up nicely in the sound.h/sound.cpp files nice and tidy...so you can see what I've done.  This also allows for you to plug in the CSound class easily into some of your projects.  Again there will be later sound tutorials implementing .wma, and .mp3.  But this will give you a taste of sound.

 

Now I'll copy and paste the sound.h file below so you can see how simple it is...agian it looks a lot because there's loads of comments in the code.

 

/***************************************************************************/

/*                                                                         */

/* FileName: sound.cpp                      ***  **  ~~                    */

/*                                          *** * *~                       */

/* Details: Sound! (prt9)                   ****  *    ~~                  */

/*                                          ***   * ~                      */

/* Author: Ben_3D & Wishi                   ****  *  ~~~                   */

/*                                          *** * *   ~~~                  */

/* URL: www.xbdev.net                  ***  ** ~~~                    */

/*                                                                         */

/***************************************************************************/

/*                                                                         */

/* Details: Class to encapsulate the sound, includeds the                  */

/* initialisation and loading of sounds                                    */

/*                                                                         */

/***************************************************************************/

 

#pragma once

 

#include <stdio.h>

 

 

#include <xtl.h>

#include <dmusici.h>

  

 

/***************************************************************************/

/*                                                                         */

/* CSound Class definition                                                 */

/*                                                                         */

/***************************************************************************/

 

class CSound

{

protected:

      IDirectMusicSegment8*     m_pSoundSegment;

 

public:

      void Create(char* filename);  //e.g. "D:\\chomp.wav"

 

      void playsound();

 

      void Release();

 

protected:

      static IDirectMusicLoader8*      m_pLoader;

      static IDirectMusicPerformance8* m_pPerformance;

 

      void LoadSound(char* filename);

 

      void SetupSound();

};

 

/***************************************************************************/

/*                                                                         */

/*  CSound Instructions                                                    */

/*                                                                         */

/***************************************************************************/

//                                                                         //

// How the CSound Class works:                                             //

// Well first you have to initilise DirectX, this can be done in the       //

// graphics stage, e.g. "Direct3DCreate8(D3D_SDK_VERSION);", this is so    //

// that we can use the directX components.                                 //

//                                                                         //

// So you can create as many CSound objects and you like, like this:       //

// CSound Madonna;                                                         //

// CSound MySong;                                                          //

// CSound GameOverTune;                                                    //

//                                                                         //

// Then in the inilisation stage you call the member functions to load,    //

// your tune in                                                            //

// e.g.  GameOverTune.Create("D:\\chomp.wav");                             //

//                                                                         //

// Now once you've loaded your tune in...you can play it simply by calling //

// the member function playsound....                                       //

// This will play the tune until the end....you can add to this simple     //

// class to include 'stop()'...'pause()'...member functions possibly.      //

//                                                                         //

//                                                                         //

// So whenever you want to play the tune, just call the member function.   //

//                                                                         //

// GameOverTune.playsound();                                               //

//                                                                         //

// Simple as that :)                                                       //

//                                                                         //

/***************************************************************************/

 

And well if I've shown you the .h file...I might has well show you the .cpp file...where the actual code taht does the work goes.

 

 

#include "sound.h"

 

 

// Static members of the class... as with directx sound, you only need to

// initilise them once....so you don't need seperate copies for all your

// classes, instead you can share them, by making them static....so a single

// instance of them is shared between all our sound classes.

 

IDirectMusicLoader8*          CSound::m_pLoader       = NULL;

IDirectMusicPerformance8*     CSound::m_pPerformance  = NULL;

 

/////////////////////////////////////////////////////////////////////////////

//                                                                         //

// Create(..) - The name speaks for itself.                                //

// We check if our dx soudn have been initilised...if now we call the      //

// sound setup and setup our seound....only need to do it once.            //

// Them we load our sound file.                                            //

//                                                                         //

/////////////////////////////////////////////////////////////////////////////

 

void CSound::Create(char* filename) //e.g. "D:\\chomp.wav"

{

     

      if( (m_pLoader == NULL) && (m_pPerformance == NULL))

      {

            SetupSound();

      }

 

      LoadSound(filename);

}

 

/////////////////////////////////////////////////////////////////////////////

//                                                                         //

// LoadSound(..) - another description function name :)  We load our sound //

// into our m_pSoundSegment,....which is what holds our .wav.              //

//                                                                         //

/////////////////////////////////////////////////////////////////////////////

 

void CSound::LoadSound(char* filename) //e.g. "D:\\chomp.wav"

{    

      m_pLoader->LoadObjectFromFile( CLSID_DirectMusicSegment, IID_IDirectMusicSegment8,

                                  filename, (VOID**)&m_pSoundSegment);

}

 

/////////////////////////////////////////////////////////////////////////////

//                                                                         //

// Using our m_pSoundSegment, which was loaded using LoadSound(..)...we    //

// then play it using our m_pPerformance dx interface.                     //

//                                                                         //

// You can change parameters to this PlaySegmentEX(..) if you want to      //

// repeat you rsound etc.                                                  //

//                                                                         //

/////////////////////////////////////////////////////////////////////////////

void CSound::playsound()

{

      m_pPerformance->PlaySegmentEx( m_pSoundSegment, NULL, NULL, DMUS_SEGF_SECONDARY,

                                               0, NULL, NULL, NULL );

}

 

/////////////////////////////////////////////////////////////////////////////

//                                                                         //

// If you want to know this code...you'll have to go over it slowly...     //

// its purpose it to inilize or dx sound.                                  //

//                                                                         //

/////////////////////////////////////////////////////////////////////////////

 

void CSound::SetupSound()

{

      // Initialize DMusic

 

    IDirectMusicHeap* pNormalHeap;

    DirectMusicCreateDefaultHeap( &pNormalHeap );

 

    IDirectMusicHeap* pPhysicalHeap;

    DirectMusicCreateDefaultPhysicalHeap( &pPhysicalHeap );

 

    DirectMusicInitializeEx( pNormalHeap, pPhysicalHeap, &DirectMusicDefaultFactory );

 

    pNormalHeap->Release();

    pPhysicalHeap->Release();

     

 

    // Create loader object

    DirectMusicCreateInstance( CLSID_DirectMusicLoader, NULL,

                               IID_IDirectMusicLoader8, (VOID**)&m_pLoader );

 

    // Create performance object

    DirectMusicCreateInstance( CLSID_DirectMusicPerformance, NULL,

                               IID_IDirectMusicPerformance8, (VOID**)&m_pPerformance );

 

   

       m_pPerformance->InitAudioX( DMUS_INITAUDIO_NOTHREADS, 64, 128, 0 );

}

 

/////////////////////////////////////////////////////////////////////////////

//                                                                         //

// Tidy up...                                                              //

//                                                                         //

/////////////////////////////////////////////////////////////////////////////

 

void CSound::Release()

{

      m_pSoundSegment->Release();

     

}

 

And now for the main.cpp...the code which creates and instance of our CSound class and how we would use it...

 

// Main header file for the XDK

#include <xtl.h>

 

/////////////////////////////////////////////////////////////////////////////

//                                                                         //

// All our sound loading code and playing code is all nice and tidy in     //

// sound.h/.cpp...so it should be easy for you to use it and understand    //

// it without getting mixed up with loads of other problem code.           //

//                                                                         //

/////////////////////////////////////////////////////////////////////////////

      //** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **

      //** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **

#include "sound.h" // So we can use CSound!

 

/////////////////////////////////////////////////////////////////////////////

//                                                                         //

// This is just a file I use in the splash screen...its the location of    //

// the jpg file which will display on screen while we listen to our music  //

//                                                                         //

/////////////////////////////////////////////////////////////////////////////

 

#define szTexFile "D:\\media\\xfactordev.jpg"

 

/////////////////////////////////////////////////////////////////////////////

//                                                                         //

// These are used for our directX initilisation and so I can display my    //

// wonderful splash image on the screen.                                   //

//                                                                         //

/////////////////////////////////////////////////////////////////////////////

 

IDirect3D8*       g_pD3D = NULL;                // DirectX Object

IDirect3DDevice8* g_pD3DDevice = NULL;          // Screen Object

 

/////////////////////////////////////////////////////////////////////////////

//                                                                         //

//  Our sound class...well I create a single global instance of it here    //

//  then call Create etc for it later on...and we can play our sound .wav  //

//  file which I have in the media folder!                                 //

//  Don't forget to include the media folder, which as the .wav sound      //

//  file which you need!.. or else it will CRASH!                          //

//                                                                         //

/////////////////////////////////////////////////////////////////////////////

 

      //** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **

      //** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **

CSound g_Madonna;                            // CSound is defined in sound.h

 

 

/////////////////////////////////////////////////////////////////////////////

//                                                                         //

//  Well this is our DirectX initilsation code, we call this once when     //

//  our program starts up.                                                 //

//                                                                         //

/////////////////////////////////////////////////////////////////////////////

 

void InitialiseD3D()

{

    //First of all, create the main D3D object. If it is created successfully we

    //should get a pointer to an IDirect3D8 interface.

    g_pD3D = Direct3DCreate8(D3D_SDK_VERSION);

 

    //Create a structure to hold the settings for our device

    D3DPRESENT_PARAMETERS d3dpp;

    ZeroMemory(&d3dpp, sizeof(d3dpp));

 

    //Fill the structure.

    //Set fullscreen 640x480x32 mode

 

      d3dpp.BackBufferWidth = 640;

      d3dpp.BackBufferHeight = 480;

      d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;

 

 

      // Create one backbuffer and a zbuffer

      d3dpp.BackBufferCount = 1;

 

      // Set up how the backbuffer is "presented" to the frontbuffer each time

      d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

 

    //Create a Direct3D device.

    g_pD3D->CreateDevice(0, D3DDEVTYPE_HAL, NULL,

                                   D3DCREATE_HARDWARE_VERTEXPROCESSING,

                                                   &d3dpp, &g_pD3DDevice);

 

 

}

 

void CleanUp()

{

    g_pD3DDevice->Release();

    g_pD3D->Release();

}

 

 

/////////////////////////////////////////////////////////////////////////////

//                                                                         //

// This little function is nothing to do with the sound!  I just added it  //

// in, so that while your listening to the mega cool .wav sound I gave you //

// the screen would display more than just a blank screen or a triangle.   //

// Its the basic's...and it loads in file which I've specified above as    //

// "D:\\xfactodev.net" with a define (so you can change it later... and    //

// displays it.                                                            //

//                                                                         //

/////////////////////////////////////////////////////////////////////////////

 

 

void XFactorTextureAdvert()

{

     

///.......

<cut out to keep it simple>

///.....

}

 

/////////////////////////////////////////////////////////////////////////////

//                                                                         //

// Our applicatoin entry point!                                            //

// Now note the **NEW** comments...so you can't get mixed up with where    //

// sound code is being initilised and used :)                              //

//                                                                         //

/////////////////////////////////////////////////////////////////////////////

 

//Application entry point

void __cdecl main()

{

 

      InitialiseD3D();

 

      //** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **

      //** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **

      g_Madonna.Create( "D:\\media\\do_it_with_madonna.wav" );

 

      //** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **

      //** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW ** NEW **

      g_Madonna.playsound();

 

      while(true)

      {

 

 

            //Clear the backbuffer to black

            g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 255), 1.0f, 0);

            //Begin the scene

            g_pD3DDevice->BeginScene();

 

                  XFactorTextureAdvert();

 

            //End the scene

            g_pD3DDevice->EndScene();

 

            //Filp the back and front buffers so that whatever has been

            //rendered on the back buffer

            //will now be visible on screen (front buffer).

 

            g_pD3DDevice->Present(NULL, NULL, NULL, NULL);

      }    

 

        g_Madonna.Release();

 

      CleanUp();

}

 

 

Well there you have it....

 

Sound basics.

 

Download Source Code

 

 

 

 

 

 

 

 

 

 

 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.