FMODsound for GTest

  by Bob Lawrence

boblaw@ourfun.com

FMODsound (http://www.fmod.org/)

 

 This is a simple implementation for adding the FMOD dll to your project.

I have added some comments to trigger your attention to the possibilities of using this.

 

This will basically add a sound entity to your level that will begin playing on load.

This should be plenty to get you started on using FMOD, check the license agreement for using FMOD in your project.

Note: This now assumes you are using pak files.

Here are the FMOD.c and FMOD.h zippped:Files

 

 

 

Start by adding the #include “FMOD.h “ to Gmain.c and GameMgr.c

 

Now add fmodvc.lib (found in FMOD\api\lib\ in the FMOD API) to your link settings. And the fmod.dll in your exe path.

 

Now add in GameMgr_SetWorld in GameMgr.c:

 

//FMOD Background Sound

            if (!PlayFMOD_Spawn(GameMgr_GetWorld(GMgr),GameMgr_GetEngine(GMgr)))

            GenVS_Error("GameMgr_SetWorld: FMODsound_Spawn failed.\n");

 

 

And in Gmain.c in UpdatePlayerContents add:

 

FMOD_Start += 1000;

           

            if (FMODlength >= FMOD_Start)

            {

                        if ((FMODchannel = FSOUND_Stream_Play(FSOUND_STREAMABLE | FSOUND_FREE, FMODSample)) == -1)

                       

                        {

                       

                        }

            }

 

            else

            {

                        FSOUND_Stream_Close(FMODSample);

            }

           

 

 

Here is the FMOD.c file:

 

//*********

//FMOD.c Sound Interface

//

//Bob Lawrence 8/17/01

//*********

#include <windows.h>

#include <math.h>

#include <assert.h>

#include <stdio.h>

#include <string.h>

#include "genesis.h"

#include "ErrorLog.h"

#include "FMOD.h"

 

extern geVFile *VFSWAV;

void  GenVS_Error(const char *Msg, ...);

 

geBoolean PlayFMOD_Spawn(geWorld *World, geEngine *Engine)

{

geEntity_EntitySet * Set;

geEntity * Entity;

FMODsound * Fsound;

geVFile *            fp2;

 

 

float pos[3] = { -10.0f, -0.0f, 0.0f };

float vel[3] = { 0,0,0 };

// Note: see the FMOD documents about using the pos/vel above

// you can use these for 3D sound in your game shell, I do not use them here

// but left these declared as a reminder.

 

int                     length;

char *   data;

 

 

long LastPosition;

 

// INITIALIZE

 

FSOUND_SetOutput(FSOUND_OUTPUT_DSOUND);

 

FSOUND_SetDriver(0);

 

if (!FSOUND_Init(44100, 32, 0))

            {

                        FSOUND_Close();

                        return GE_FALSE;

            }

 

 

// Find the FMODSound entity in the World

Set = geWorld_GetEntitySet(World, "FMODsound");

if (Set == NULL)

return GE_TRUE;

 

for (Entity= geEntity_EntitySetGetNextEntity(Set, NULL); Entity;

Entity= geEntity_EntitySetGetNextEntity(Set, Entity)) {

 

// Get data from the FMODSound entity

Fsound = geEntity_GetUserData(Entity);

 

//Open the sound file specified in the entity

fp2 = geVFile_Open(VFSWAV, Fsound->TheFile,GE_VFILE_OPEN_READONLY);

 

 

if (!fp2)

            {

                        FSOUND_Close();

                        return GE_FALSE;

            }

           

 

                        geVFile_Seek(fp2, 0, GE_VFILE_SEEKEND);

                        length = geVFile_Tell(fp2,&LastPosition);

 

                        //check for valid file length (curruption check)

                                    if (length < 1)

                        {

 

                                    geVFile_Close(fp2);

                                    GenVS_Error("Failed FMOD File Length");

                        }

 

                        geVFile_Seek(fp2, 0, GE_VFILE_SEEKSET);

                        data = (char *)malloc(LastPosition);

                        geVFile_Read(fp2,data,LastPosition);

                        geVFile_Close(fp2);

 

 

                        //Open the file with FMOD

            FMODSample = FSOUND_Stream_OpenFile(data, FSOUND_NORMAL | FSOUND_LOADMEMORY,LastPosition);

 

 

            //In case you need to know this data in the shell

            FMODlength = FSOUND_Stream_GetLengthMs(FMODSample);

 

            //Set the pan (see above note)

            FSOUND_SetPan(FMODchannel, FSOUND_STEREOPAN);

 

            //Set Paused to false

            FSOUND_Stream_SetPaused(FMODSample, GE_FALSE);

 

           

           

 

           

 

return GE_TRUE;

 

}

 

 

return GE_TRUE;

 

}

 

 

Here is the FMOD.h file:

 

//*********

//FMOD.h Sound Interface

//

//Bob Lawrence 8/17/01

//*********

 

#ifndef FMODsound_H

#define FMODsound_H

 

#ifdef __cplusplus

extern "C" {

#endif

 

#include "Genesis.h"

#include "FMOD/api/inc/fmod.h"

#include "FMOD/api/inc/fmod_errors.h"            // optional

 

 

 

#pragma warning( disable : 4068 )

 

#pragma GE_Type("FMOD.bmp")

 

typedef struct FMODsound

{

#pragma GE_Published  

 

char            *TheFile;

geVec3d Origin; // Where the entity is located

 

#pragma GE_DefaultValue(TheFile, "spookywind.ogg")

           

#pragma GE_Origin(Origin)

 

} FMODsound;

 

 

 

 

 

geBoolean PlayFMOD_Spawn(geWorld *World, geEngine *Engine);

 

int FMODchannel;

long FMODlength, FMOD_Start;

 

 

FSOUND_STREAM *FMODSample;

 

#ifdef __cplusplus

}

#endif

 

#endif