FMOD comes in two styles - an interface for C and one for C++
Here is an example of how to call the routines using the C system (taken from my
FightTune game) :
/*
* BASS.c
* BASS
*
* Created by Nicholas Kingsley on 20/07/2007.
* Copyright 2007 __MyCompanyName__. All rights reserved.
*
*/
//#define WIN32
#define MACOS
//#define LINUX
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"
#define FFT_SIZE 1024
#define LEFT_BIT 1
#define UP_BIT 2
#define RIGHT_BIT 4
#define DOWN_BIT 8
#ifdef WIN32
// Windows
#include "windows.h"
#include "C:\Program Files\FMOD SoundSystem\FMOD Programmers API Win32\api\inc\fmod.h"
#endif
#ifdef MACOS
// OSX Version
#include "/Developer/FMOD Programmers API/api/inc/fmod.h"
#endif
#ifdef LINUX
// Linx
#include "/home/nicholas/Desktop/fmodapi40623linux/api/inc/fmod.h"
#endif
FMOD_SYSTEM *FMODSystem=NULL;
FMOD_CREATESOUNDEXINFO extraInfo;
char diff[4];
//----------System
void FMOD_ClearDiff(void)
{
memset(&diff,(char) 0,sizeof(diff));
srand((unsigned)time(0));
}
char FMOD_Initialise(void)
{
FMOD_RESULT result;
result = FMOD_System_Create(&FMODSystem);
if (result==FMOD_OK)
{
result = FMOD_System_Init(FMODSystem,1024, FMOD_INIT_NORMAL, 0);
if (result==FMOD_OK)
{
}
}
return (result==FMOD_OK ? (char) 1 : (char) 0);
}
void FMOD_Close(void)
{
FMOD_System_Close(FMODSystem);
FMODSystem=NULL;
}
void FMOD_Release(void)
{
FMOD_System_Release(FMODSystem);
FMODSystem=NULL;
}
int FMOD_CreateStream(char *fileName,int mode)
{
FMOD_SOUND *FMODSound;
if (FMOD_System_CreateStream(FMODSystem,fileName,mode,NULL,&FMODSound)==FMOD_OK) // For some reason, getting the extra information generates a 37 error
{
return (int) FMODSound;
}
return 0;
}
int FMOD_PlaySound(int channelID,int handle,char paused)
{
FMOD_CHANNEL *FMODChannel;
if (FMOD_System_PlaySound(FMODSystem,channelID,(FMOD_SOUND *) handle,paused,&FMODChannel)==FMOD_OK)
{
return (int) FMODChannel;
}
return 0;
}
int FMOD_GetChannelPosition(int channel,int type)
{
unsigned int pos;
if (FMOD_Channel_GetPosition((FMOD_CHANNEL *) channel,&pos,type)==FMOD_OK)
{
return pos;
}
return 0;
}
char FMOD_ChannelSetPosition(int channel,int position,int type)
{
return FMOD_Channel_SetPosition((FMOD_CHANNEL *) channel,position,type);
}
char FMOD_ChannelIsPlaying(int channel)
{
int playing;
if (FMOD_Channel_IsPlaying(channel,&playing)==FMOD_OK)
{
return (playing ? 1 : 0);
}
return 0;
}
char FMOD_SetVolume(int channel,int volume)
{
return FMOD_Channel_SetVolume(channel,(float) ((float) volume/(float) 100.0));
}
int FMOD_CreateSound(char *fileName,int mode)
{
FMOD_SOUND *sound;
int error;
error=FMOD_System_CreateSound(FMODSystem,fileName,mode,NULL,&sound);
if (error==FMOD_OK)
{
return (int) sound;
}
return 0;
}
char FMOD_Stop(int channel)
{
return FMOD_Channel_Stop(channel);
}
char FMOD_Pause(int channel,char paused)
{
return FMOD_Channel_SetPaused(channel,paused);
}
int FMOD_GetLength(int sound,int timeLength)
{
unsigned int length;
if (FMOD_Sound_GetLength((FMOD_SOUND *) sound,&length,timeLength)==FMOD_OK)
{
return (int) length;
}
return 0;
}
char FMOD_SetStreamBufferSize(int bufferSize,int sizeType)
{
return FMOD_System_SetStreamBufferSize(FMODSystem,bufferSize,sizeType);
}
char FMOD_Update()
{
return FMOD_System_Update(FMODSystem);
}
int FMOD_GetFrequency(int channel)
{
float freq;
if (FMOD_Channel_GetFrequency((FMOD_CHANNEL *) channel,&freq)!=FMOD_OK)
{
freq=(float) 0.0;
}
return (int) freq;
}
int FMOD_SetFrequency(int channel,int freq)
{
return FMOD_Channel_SetFrequency((FMOD_CHANNEL *) channel,(float) freq);
}
int FMOD_Spectrograph5(int channel,int difficultyLevel,char *storeSpec,int SPECHEIGHT,int BANDS,
int NIGHTMARE)
{
register int b0,x,sc,b1,loop;
register char y;
float fft[3][FFT_SIZE];
float sum;
int result;
result=0;
if (FMOD_Channel_GetSpectrum((FMOD_CHANNEL *) channel,&fft[0][0],FFT_SIZE,0,FMOD_DSP_FFT_WINDOW_TRIANGLE)==FMOD_OK &&
FMOD_Channel_GetSpectrum((FMOD_CHANNEL *) channel,&fft[1][0],FFT_SIZE,1,FMOD_DSP_FFT_WINDOW_TRIANGLE)==FMOD_OK)
{
for (x=0; x<FFT_SIZE; x++)
{
fft[2][x]=(fft[0][x]+fft[1][x])/2.0;
}
b0=0;
for (x=0;x<BANDS;x++)
{
sum=0.0;
b1=pow(2,x*10.0/(BANDS-1));
if (b1>=FFT_SIZE)
{
b1=FFT_SIZE-1;
}
if (b1<=b0)
{
b1=b0+1; // make sure it uses at least 1 FFT bin
}
sc=10+b1-b0;
for (;b0<b1;b0++)
{
sum+=fft[2][1+b0];
}
y=(char) ((sqrt(sum/log10(sc))*1.7*SPECHEIGHT)-4.0); // scale it
y=(y>SPECHEIGHT ? SPECHEIGHT : \
y<0 ? 0 : y);
*(storeSpec+x)=(char) y;
if (diff[x]!=y)
{
diff[x]=y;
result=(x==0 ? result | LEFT_BIT : \
x==1 ? result | UP_BIT : \
x==2 ? result | RIGHT_BIT : result | DOWN_BIT);
}
}
if (difficultyLevel!=NIGHTMARE)
{
int tempResult;
tempResult=0;
if (result & (LEFT_BIT | RIGHT_BIT))
{
if ((rand() % 15)<=7)
{
tempResult|=LEFT_BIT;
}
else
{
tempResult|=RIGHT_BIT;
}
}
if (result & (UP_BIT | DOWN_BIT))
{
if ((rand() % 15)<=7)
{
tempResult|=UP_BIT;
}
else
{
tempResult|=DOWN_BIT;
}
}
result=tempResult;
}
}
return result;
}
void FMOD_Spectrograph1(int channel,int SPECWIDTH,int SPECHEIGHT,char *specBuff)
{
register int c,x,v,y,loop;
int channels;
float *buf;
float fft[FFT_SIZE];
if (specBuff==NULL) return;
memset(specBuff,(char) 0,SPECWIDTH*SPECHEIGHT);
if (FMOD_System_GetSoftwareFormat(FMODSystem,NULL,NULL,&channels,NULL,NULL,NULL)==FMOD_OK)
{
for (loop=0; loop<2; loop++)
{
if (FMOD_System_GetWaveData(FMODSystem,&fft,FFT_SIZE,loop)==FMOD_OK)
{
for (x=0; x<SPECWIDTH; x++)
{
v=(1-fft[x])*SPECHEIGHT/2;
if (v<0)
{
v=0;
}
else
if (v>SPECHEIGHT)
{
v=SPECHEIGHT;
}
if (!x)
{
y=v;
}
do { // draw line from previous sample...
if (y<v)
{
y++;
}
else
if (y>v)
{
y--;
}
specBuff[y*SPECWIDTH+x]=(loop==0 ? 127 : 1);
} while (y!=v);
}
}
}
}
}