diff options
| author | xolatile | 2025-08-04 18:17:30 +0200 |
|---|---|---|
| committer | xolatile | 2025-08-04 18:17:30 +0200 |
| commit | a89dca11cf475e0bd1399cf5d5c5135d00495ec3 (patch) | |
| tree | 4d551a7f4193bc89a0b8560e955f4f874101b388 /src/engine/sound.cpp | |
| parent | 9d533fa4d82935a083ff93a09aed95baea9fbfd4 (diff) | |
| download | xolatile-badassbug-a89dca11cf475e0bd1399cf5d5c5135d00495ec3.tar.xz xolatile-badassbug-a89dca11cf475e0bd1399cf5d5c5135d00495ec3.tar.zst | |
Minor changes...
Diffstat (limited to 'src/engine/sound.cpp')
| -rw-r--r-- | src/engine/sound.cpp | 152 |
1 files changed, 17 insertions, 135 deletions
diff --git a/src/engine/sound.cpp b/src/engine/sound.cpp index 38ff025..18cc0ab 100644 --- a/src/engine/sound.cpp +++ b/src/engine/sound.cpp @@ -30,7 +30,7 @@ struct soundconfig bool hasslot(const soundslot *p, const vector<soundslot> &v) const { - return p >= v.getbuf() + slots && p < v.getbuf() + slots+numslots && slots+numslots < v.length(); + return p >= v.getbuf() + slots && p < v.getbuf() + slots+numslots && slots+numslots < v.length(); } int chooseslot(int flags) const @@ -42,12 +42,12 @@ struct soundconfig }; struct soundchannel -{ +{ int id; bool inuse; - vec loc; + vec loc; soundslot *slot; - extentity *ent; + extentity *ent; int radius, volume, pan, flags; bool dirty; @@ -156,11 +156,7 @@ void stopmusic() DELETEP(musicstream); } -#ifdef WIN32 -#define AUDIODRIVER "directsound winmm" -#else #define AUDIODRIVER "" -#endif bool shouldinitaudio = true; SVARF(audiodriver, AUDIODRIVER, { shouldinitaudio = true; initwarning("sound configuration", INIT_RESET, CHANGE_SOUND); }); VARF(usesound, 0, 1, 1, { shouldinitaudio = true; initwarning("sound configuration", INIT_RESET, CHANGE_SOUND); }); @@ -197,11 +193,7 @@ bool initaudio() if(restorefallback) { restorefallback = false; - #ifdef WIN32 - SDL_setenv("SDL_AUDIODRIVER", fallback, 1); - #else unsetenv("SDL_AUDIODRIVER"); - #endif } if(SDL_InitSubSystem(SDL_INIT_AUDIO) >= 0) return true; conoutf(CON_ERROR, "sound init failed: %s", SDL_GetError()); @@ -236,7 +228,7 @@ void initsound() conoutf(CON_ERROR, "sound init failed (SDL_mixer): %s", Mix_GetError()); return; } - Mix_AllocateChannels(soundchans); + Mix_AllocateChannels(soundchans); maxchannels = soundchans; nosound = false; } @@ -263,7 +255,7 @@ Mix_Music *loadmusic(const char *name) if(!musicrw) DELETEP(musicstream); } if(musicrw) music = Mix_LoadMUSType_RW(musicrw, MUS_NONE, 0); - else music = Mix_LoadMUS(findfile(name, "rb")); + else music = Mix_LoadMUS(findfile(name, "rb")); if(!music) { if(musicrw) { SDL_FreeRW(musicrw); musicrw = NULL; } @@ -271,7 +263,7 @@ Mix_Music *loadmusic(const char *name) } return music; } - + void startmusic(char *name, char *cmd) { if(nosound) return; @@ -293,7 +285,7 @@ void startmusic(char *name, char *cmd) else { conoutf(CON_ERROR, "could not play music: %s", file); - intret(0); + intret(0); } } } @@ -582,7 +574,6 @@ void resetchannels() void clear_sound() { - closemumble(); if(nosound) return; stopmusic(); @@ -672,7 +663,7 @@ bool updatechannel(soundchannel &chan) chan.pan = pan; chan.dirty = true; return true; -} +} void reclaimchannels() { @@ -696,7 +687,6 @@ VARP(minimizedsounds, 0, 0, 1); void updatesounds() { - updatemumble(); if(nosound) return; if(minimized && !minimizedsounds) stopsounds(); else @@ -736,7 +726,7 @@ void preloadmapsounds() if(e.type==ET_SOUND) mapsounds.preloadsound(e.attr1); } } - + int playsound(int n, const vec *loc, extentity *ent, int flags, int loops, int fade, int chanid, int radius, int expire) { if(nosound || !soundvol || (minimized && !minimizedsounds)) return -1; @@ -757,7 +747,7 @@ int playsound(int n, const vec *loc, extentity *ent, int flags, int loops, int f Mix_HaltChannel(chanid); freechannel(chanid); } - return -1; + return -1; } } @@ -792,7 +782,7 @@ int playsound(int n, const vec *loc, extentity *ent, int flags, int loops, int f if(!slot.sample->chunk && !slot.sample->load()) return -1; if(dbgsound) conoutf(CON_DEBUG, "sound: %s", slot.sample->name); - + chanid = -1; loopv(channels) if(!channels[i].inuse) { chanid = i; break; } if(chanid < 0 && channels.length() < maxchannels) chanid = channels.length(); @@ -802,13 +792,13 @@ int playsound(int n, const vec *loc, extentity *ent, int flags, int loops, int f soundchannel &chan = newchannel(chanid, &slot, loc, ent, flags, radius); updatechannel(chan); int playing = -1; - if(fade) + if(fade) { Mix_Volume(chanid, chan.volume); playing = expire >= 0 ? Mix_FadeInChannelTimed(chanid, slot.sample->chunk, loops, fade, expire) : Mix_FadeInChannel(chanid, slot.sample->chunk, loops, fade); } else playing = expire >= 0 ? Mix_PlayChannelTimed(chanid, slot.sample->chunk, loops, expire) : Mix_PlayChannel(chanid, slot.sample->chunk, loops); - if(playing >= 0) syncchannel(chan); + if(playing >= 0) syncchannel(chan); else freechannel(chanid); return playing; } @@ -834,8 +824,8 @@ bool stopsound(int n, int chanid, int fade) return true; } -int playsoundname(const char *s, const vec *loc, int vol, int flags, int loops, int fade, int chanid, int radius, int expire) -{ +int playsoundname(const char *s, const vec *loc, int vol, int flags, int loops, int fade, int chanid, int radius, int expire) +{ if(!vol) vol = 100; int id = gamesounds.findsound(s, vol); if(id < 0) id = gamesounds.addsound(s, vol); @@ -847,7 +837,7 @@ ICOMMAND(sound, "i", (int *n), playsound(*n)); void resetsound() { clearchanges(CHANGE_SOUND); - if(!nosound) + if(!nosound) { cleanupsamples(); if(music) @@ -881,111 +871,3 @@ void resetsound() } COMMAND(resetsound, ""); - -#ifdef WIN32 - -#include <wchar.h> - -#else - -#include <unistd.h> - -#ifdef _POSIX_SHARED_MEMORY_OBJECTS -#include <sys/types.h> -#include <sys/stat.h> -#include <sys/mman.h> -#include <fcntl.h> -#include <wchar.h> -#endif - -#endif - -#if defined(WIN32) || defined(_POSIX_SHARED_MEMORY_OBJECTS) -struct MumbleInfo -{ - int version, timestamp; - vec pos, front, top; - wchar_t name[256]; -}; -#endif - -#ifdef WIN32 -static HANDLE mumblelink = NULL; -static MumbleInfo *mumbleinfo = NULL; -#define VALID_MUMBLELINK (mumblelink && mumbleinfo) -#elif defined(_POSIX_SHARED_MEMORY_OBJECTS) -static int mumblelink = -1; -static MumbleInfo *mumbleinfo = (MumbleInfo *)-1; -#define VALID_MUMBLELINK (mumblelink >= 0 && mumbleinfo != (MumbleInfo *)-1) -#endif - -#ifdef VALID_MUMBLELINK -VARFP(mumble, 0, 1, 1, { if(mumble) initmumble(); else closemumble(); }); -#else -VARFP(mumble, 0, 0, 1, { if(mumble) initmumble(); else closemumble(); }); -#endif - -void initmumble() -{ - if(!mumble) return; -#ifdef VALID_MUMBLELINK - if(VALID_MUMBLELINK) return; - - #ifdef WIN32 - mumblelink = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "MumbleLink"); - if(mumblelink) - { - mumbleinfo = (MumbleInfo *)MapViewOfFile(mumblelink, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(MumbleInfo)); - if(mumbleinfo) wcsncpy(mumbleinfo->name, L"Sauerbraten", 256); - } - #elif defined(_POSIX_SHARED_MEMORY_OBJECTS) - defformatstring(shmname, "/MumbleLink.%d", getuid()); - mumblelink = shm_open(shmname, O_RDWR, 0); - if(mumblelink >= 0) - { - mumbleinfo = (MumbleInfo *)mmap(NULL, sizeof(MumbleInfo), PROT_READ|PROT_WRITE, MAP_SHARED, mumblelink, 0); - if(mumbleinfo != (MumbleInfo *)-1) wcsncpy(mumbleinfo->name, L"Sauerbraten", 256); - } - #endif - if(!VALID_MUMBLELINK) closemumble(); -#else - conoutf(CON_ERROR, "Mumble positional audio is not available on this platform."); -#endif -} - -void closemumble() -{ -#ifdef WIN32 - if(mumbleinfo) { UnmapViewOfFile(mumbleinfo); mumbleinfo = NULL; } - if(mumblelink) { CloseHandle(mumblelink); mumblelink = NULL; } -#elif defined(_POSIX_SHARED_MEMORY_OBJECTS) - if(mumbleinfo != (MumbleInfo *)-1) { munmap(mumbleinfo, sizeof(MumbleInfo)); mumbleinfo = (MumbleInfo *)-1; } - if(mumblelink >= 0) { close(mumblelink); mumblelink = -1; } -#endif -} - -static inline vec mumblevec(const vec &v, bool pos = false) -{ - // change from X left, Z up, Y forward to X right, Y up, Z forward - // 8 cube units = 1 meter - vec m(-v.x, v.z, v.y); - if(pos) m.div(8); - return m; -} - -void updatemumble() -{ -#ifdef VALID_MUMBLELINK - if(!VALID_MUMBLELINK) return; - - static int timestamp = 0; - - mumbleinfo->version = 1; - mumbleinfo->timestamp = ++timestamp; - - mumbleinfo->pos = mumblevec(player->o, true); - mumbleinfo->front = mumblevec(vec(RAD*player->yaw, RAD*player->pitch)); - mumbleinfo->top = mumblevec(vec(RAD*player->yaw, RAD*(player->pitch+90))); -#endif -} - |
