*** empty log message ***

git-svn-id: svn://svn.code.sf.net/p/actiongame/code/trunk/ac@38 e016b1c5-ed68-44aa-a2f7-31cac88cffde
This commit is contained in:
driAn
2005-10-13 22:12:47 +00:00
parent 5d7a290652
commit f3f6bf158c
16 changed files with 498 additions and 456 deletions
+106 -42
View File
@@ -2,27 +2,39 @@
#include "cube.h"
#ifndef WIN32
//#ifndef WIN32 // NOTE: fmod not being supported for the moment as it does not allow stereo pan/vol updating during playback
#define USE_MIXER
#endif
//#endif
VAR(soundvol, 0, 255, 255);
VAR(musicvol, 0, 128, 255);
bool nosound = false;
#ifdef USE_MIXER
#define MAXCHAN 32
#define SOUNDFREQ 22050
struct soundloc { vec loc; bool inuse; } soundlocs[MAXCHAN];
#ifdef USE_MIXER
#include "SDL_mixer.h"
#define MAXVOL MIX_MAX_VOLUME
Mix_Music *mod = NULL;
void *stream = NULL; // TODO
void *stream = NULL;
#else
#include "fmod.h"
#define MAXVOL 255
FMUSIC_MODULE *mod = NULL;
FSOUND_STREAM *stream = NULL;
#endif
#endif
#ifdef USE_MIXER
VARFP(musicvol, 0, 128, 255, Mix_VolumeMusic((musicvol*MAXVOL)/255));
#else
VARP(musicvol, 0, 128, 255);
#endif
VARP(soundvol, 0, 255, 255);
void stopsound()
{
if(nosound) return;
if(mod)
{
#ifdef USE_MIXER
@@ -42,36 +54,31 @@ void stopsound()
};
};
void cleansound()
{
stopsound();
#ifdef USE_MIXER
Mix_CloseAudio();
#else
FSOUND_Close();
#endif
};
VAR(soundbufferlen, 128, 1024, 4096);
void initsound()
{
memset(soundlocs, 0, sizeof(soundloc)*MAXCHAN);
#ifdef USE_MIXER
if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 512)<0)
if(Mix_OpenAudio(SOUNDFREQ, MIX_DEFAULT_FORMAT, 2, soundbufferlen)<0)
{
conoutf("sound init failed (SDL_mixer): %s", (int) Mix_GetError());
soundvol = 0;
};
conoutf("sound init failed (SDL_mixer): %s", (size_t)Mix_GetError());
nosound = true;
};
Mix_AllocateChannels(MAXCHAN);
#else
if(FSOUND_GetVersion()<FMOD_VERSION) fatal("old FMOD dll");
if(!FSOUND_Init(22050, 32, FSOUND_INIT_GLOBALFOCUS))
if(!FSOUND_Init(SOUNDFREQ, MAXCHAN, FSOUND_INIT_GLOBALFOCUS))
{
conoutf("sound init failed (FMOD): %d", FSOUND_GetError());
soundvol = 0;
nosound = true;
};
#endif
};
void music(char *name)
{
if(nosound) return;
stopsound();
if(soundvol && musicvol)
{
@@ -90,14 +97,14 @@ void music(char *name)
FMUSIC_PlaySong(mod);
FMUSIC_SetMasterVolume(mod, musicvol);
}
else if(stream = FSOUND_Stream_OpenFile(path(sn), FSOUND_LOOP_NORMAL, 0))
else if(stream = FSOUND_Stream_Open(path(sn), FSOUND_LOOP_NORMAL, 0, 0))
{
int chan = FSOUND_Stream_Play(FSOUND_FREE, stream);
if(chan>=0) { FSOUND_SetVolume(chan, (musicvol*MAXVOL)/255); FSOUND_SetPaused(chan, false); };
}
else
{
conoutf("could not play music: %s", (int)&sn);
conoutf("could not play music: %s", sn);
};
#endif
};
@@ -123,12 +130,73 @@ int registersound(char *name)
COMMAND(registersound, ARG_1EST);
void cleansound()
{
if(nosound) return;
stopsound();
#ifdef USE_MIXER
Mix_CloseAudio();
#else
FSOUND_Close();
#endif
};
VAR(stereo, 0, 1, 1);
void updatechanvol(int chan, vec *loc)
{
int vol = soundvol, pan = 255/2;
if(loc)
{
vdist(dist, v, *loc, player1->o);
vol -= (int)(dist*3*soundvol/255); // simple mono distance attenuation
if(stereo && (v.x != 0 || v.y != 0))
{
float yaw = -atan2(v.x, v.y) - player1->yaw*(PI / 180.0f); // relative angle of sound along X-Y axis
pan = int(255.9f*(0.5*sin(yaw)+0.5f)); // range is from 0 (left) to 255 (right)
};
};
vol = (vol*MAXVOL)/255;
#ifdef USE_MIXER
Mix_Volume(chan, vol);
Mix_SetPanning(chan, 255-pan, pan);
#else
FSOUND_SetVolume(chan, vol);
FSOUND_SetPan(chan, pan);
#endif
};
void newsoundloc(int chan, vec *loc)
{
assert(chan>=0 && chan<MAXCHAN);
soundlocs[chan].loc = *loc;
soundlocs[chan].inuse = true;
};
void updatevol()
{
if(nosound) return;
loopi(MAXCHAN) if(soundlocs[i].inuse)
{
#ifdef USE_MIXER
if(Mix_Playing(i))
#else
if(FSOUND_IsPlaying(i))
#endif
updatechanvol(i, &soundlocs[i].loc);
else soundlocs[i].inuse = false;
};
};
COMMAND(updatevol, ARG_NONE);
void playsoundc(int n) { addmsg(0, 2, SV_SOUND, n); playsound(n); };
int soundsatonce = 0, lastsoundmillis = 0;
void playsound(int n, vec *loc)
{
if(nosound) return;
if(!soundvol) return;
if(lastmillis==lastsoundmillis) soundsatonce++; else soundsatonce = 1;
lastsoundmillis = lastmillis;
@@ -142,28 +210,24 @@ void playsound(int n, vec *loc)
#ifdef USE_MIXER
samples[n] = Mix_LoadWAV(path(buf));
#else
samples[n] = FSOUND_Sample_Load(n, path(buf), 0, 0);
samples[n] = FSOUND_Sample_Load(n, path(buf), FSOUND_LOOP_OFF, 0, 0);
#endif
if(!samples[n]) { conoutf("failed to load sample: %s", (int)&buf); return; };
if(!samples[n]) { conoutf("failed to load sample: %s", buf); return; };
};
int vol = soundvol;
if(loc)
{
vdist(d, v, *loc, player1->o);
vol -= (int)(d*3*soundvol/255); // simple mono distance attenuation
}
if(vol<=0) return;
#ifdef USE_MIXER
int chan = Mix_PlayChannel(-1, samples[n], 0);
if(chan>=0) Mix_Volume(chan, (vol*MAXVOL)/255);
#else
int chan = FSOUND_PlaySoundEx(FSOUND_FREE, samples[n], NULL, true);
if(chan>=0) { FSOUND_SetVolume(chan, (vol*MAXVOL)/255); FSOUND_SetPaused(chan, false); };
#endif
#ifdef USE_MIXER
int chan = Mix_PlayChannel(-1, samples[n], 0);
#else
int chan = FSOUND_PlaySoundEx(FSOUND_FREE, samples[n], NULL, true);
#endif
if(chan<0) return;
if(loc) newsoundloc(chan, loc);
updatechanvol(chan, loc);
#ifndef USE_MIXER
FSOUND_SetPaused(chan, false);
#endif
};
void sound(int n) { playsound(n, NULL); };
COMMAND(sound, ARG_1INT);