From b332742d6f8c2b3275489e4086bfdcd79105b45f Mon Sep 17 00:00:00 2001 From: Spoike Date: Sun, 26 Apr 2020 01:32:02 +0000 Subject: [PATCH] Fix the web port's numerous audio issues. git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@5681 fc73d0e0-1445-4013-8a0c-d673dee63da5 --- build_wip.sh | 25 +++++++++++++++++++++++++ engine/client/snd_al.c | 2 +- engine/client/snd_dma.c | 10 +++++++++- engine/client/snd_mem.c | 22 +++++++++++++++++++++- engine/web/ftejslib.js | 18 +++++++++--------- 5 files changed, 65 insertions(+), 12 deletions(-) diff --git a/build_wip.sh b/build_wip.sh index 224d2f8df..6912505d6 100755 --- a/build_wip.sh +++ b/build_wip.sh @@ -60,6 +60,10 @@ do echo " -r VER Specifies the SVN revision to update to" echo " -j THREADS Specifies how many jobs to make with" echo " --help This text" + echo " --noupdate Don't do svn updates" + echo " --unclean Don't do make clean, for faster rebuilds" + echo " --web Build web target (excluding all others)" + echo " --droid Build android target (excluding others)" exit 0 ;; -build|--build) @@ -69,6 +73,27 @@ do --noupdate) NOUPDATE="y" ;; + --unclean) + BUILD_CLEAN="n" + ;; + --web) + BUILD_LINUXx86="n" + BUILD_LINUXx64="n" + BUILD_LINUXarmhf="n" + BUILD_WIN32="n" + BUILD_WIN64="n" + BUILD_ANDROID="n" + BUILD_WEB="y" + ;; + --droid) + BUILD_LINUXx86="n" + BUILD_LINUXx64="n" + BUILD_LINUXarmhf="n" + BUILD_WIN32="n" + BUILD_WIN64="n" + BUILD_ANDROID="y" + BUILD_WEB="n" + ;; *) echo "Unknown option $1" ;; diff --git a/engine/client/snd_al.c b/engine/client/snd_al.c index 7d513808c..a781a9276 100644 --- a/engine/client/snd_al.c +++ b/engine/client/snd_al.c @@ -527,7 +527,7 @@ static qboolean OpenAL_LoadCache(oalinfo_t *oali, unsigned int *bufptr, sfxcache float *tmp = malloc(size); float *src = (float*)sc->data; int i; - for (i = 0; i < (size>>1); i++) + for (i = 0; i < (size>>2); i++) { tmp[i] = src[i]*volume; //signed. oversaturation isn't my problem } diff --git a/engine/client/snd_dma.c b/engine/client/snd_dma.c index fc4d3ee4d..d6c6c705b 100644 --- a/engine/client/snd_dma.c +++ b/engine/client/snd_dma.c @@ -84,7 +84,14 @@ cvar_t precache = CVARAF( "s_precache", "1", "precache", 0); cvar_t loadas8bit = CVARAFD( "s_loadas8bit", "0", "loadas8bit", CVAR_ARCHIVE, - "Downsample sounds on load as lower quality 8-bit sound."); + "Downsample sounds on load as lower quality 8-bit sound, to save memory."); +#ifdef FTE_TARGET_WEB +cvar_t snd_loadasstereo = CVARD( "snd_loadasstereo", "1", + "Force mono sounds to load as if stereo ones, to waste memory. Used to work around stupid browser bugs."); +#else +cvar_t snd_loadasstereo = CVARD( "snd_loadasstereo", "0", + "Force mono sounds to load as if stereo ones, to waste memory. Not normally useful."); +#endif cvar_t ambient_level = CVARAFD( "s_ambientlevel", "0.3", "ambient_level", CVAR_ARCHIVE, "This controls the volume levels of automatic area-based sounds (like water or sky), and is quite annoying. If you're playing deathmatch you'll definitely want this OFF."); @@ -2294,6 +2301,7 @@ void S_Init (void) Cvar_Register(&volume, "Sound controls"); Cvar_Register(&precache, "Sound controls"); Cvar_Register(&loadas8bit, "Sound controls"); + Cvar_Register(&snd_loadasstereo, "Sound controls"); Cvar_Register(&bgmvolume, "Sound controls"); Cvar_Register(&snd_nominaldistance, "Sound controls"); Cvar_Register(&ambient_level, "Sound controls"); diff --git a/engine/client/snd_mem.c b/engine/client/snd_mem.c index 8ad12a5c0..99a309a00 100644 --- a/engine/client/snd_mem.c +++ b/engine/client/snd_mem.c @@ -559,6 +559,7 @@ ResampleSfx static qboolean ResampleSfx (sfx_t *sfx, int inrate, int inchannels, int inwidth, int insamps, int inloopstart, qbyte *data) { extern cvar_t snd_linearresample; + extern cvar_t snd_loadasstereo; double scale; sfxcache_t *sc; int outsamps; @@ -575,7 +576,7 @@ static qboolean ResampleSfx (sfx_t *sfx, int inrate, int inchannels, int inwidth outwidth = inwidth; len = outsamps * outwidth * inchannels; - sfx->decoder.buf = sc = BZ_Malloc(len + sizeof(sfxcache_t)); + sfx->decoder.buf = sc = BZ_Malloc(sizeof(sfxcache_t) + len); if (!sc) { return false; @@ -603,6 +604,25 @@ static qboolean ResampleSfx (sfx_t *sfx, int inrate, int inchannels, int inwidth sc->numchannels, snd_linearresample.ival); + if (inchannels == 1 && snd_loadasstereo.ival) + { //I'm implementing this to work around what looks like a firefox bug, where mono buffers don't get played (but stereo works just fine despite all the spacialisation issues associated with that). + sfxcache_t *nc = sfx->decoder.buf = BZ_Malloc(sizeof(sfxcache_t) + len*2); + *nc = *sc; + nc->data = (qbyte*)(nc+1); + SND_ResampleStream (sc->data, + sc->speed, + sc->width, + sc->numchannels, + outsamps, + nc->data, + nc->speed*2, + nc->width, + nc->numchannels, + false); + nc->numchannels *= 2; + BZ_Free(sc); + } + return true; } diff --git a/engine/web/ftejslib.js b/engine/web/ftejslib.js index c6fb6cc43..c3a2f736e 100644 --- a/engine/web/ftejslib.js +++ b/engine/web/ftejslib.js @@ -1083,27 +1083,28 @@ console.log("onerror: " + _url); emscriptenfte_al_loadaudiofile : function(buf, dataptr, datasize) { - var ctx = AL.currentContext || AL.currentCtx; + var ctx = AL; //match emscripten's openal support. if (!buf) return; - var albuf = ctx.buffers[buf]; - ctx.buffers[buf] = null; //alIsBuffer will report it as invalid now + var albuf = AL.buffers[buf]; + AL.buffers[buf] = null; //alIsBuffer will report it as invalid now try { - //its async, so it needs its own copy of an arraybuffer + //its async, so it needs its own copy of an arraybuffer, not just a view. var abuf = new ArrayBuffer(datasize); - Uint8Array(abuf).set(HEAPU8.subarray(dataptr, dataptr+datasize)); - AL.currentContext.ctx.decodeAudioData(abuf, + var rbuf = new Uint8Array(abuf); + rbuf.set(HEAPU8.subarray(dataptr, dataptr+datasize)); + AL.currentCtx.audioCtx.decodeAudioData(abuf, function(buffer) { //Warning: This depends upon emscripten's specific implementation of alBufferData albuf.bytesPerSample = 2; albuf.channels = 1; - albuf.length = 1; - albuf.frequency = 11025; + albuf.length = buffer.length; + albuf.frequency = buffer.sampleRate; albuf.audioBuf = buffer; ctx.buffers[buf] = albuf; //and its valid again! @@ -1121,7 +1122,6 @@ console.log("onerror: " + _url); console.log(e); ctx.buffers[buf] = albuf; } - return id; }, emscriptenfte_gl_loadtexturefile : function(texid, widthptr, heightptr, dataptr, datasize, fname)