diff --git a/nq/include/sound.h b/nq/include/sound.h index 92a32aba4..4204cab0d 100644 --- a/nq/include/sound.h +++ b/nq/include/sound.h @@ -104,6 +104,7 @@ typedef struct } wavinfo_t; void S_Init (void); +void S_Init_Cvars (void); void S_Startup (void); void S_Shutdown (void); void S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, float attenuation); diff --git a/nq/source/host.c b/nq/source/host.c index 61694dbff..24903a059 100644 --- a/nq/source/host.c +++ b/nq/source/host.c @@ -927,6 +927,7 @@ Host_Init (quakeparms_t *parms) Cbuf_Execute_Sets (); V_Init (); + S_Init_Cvars (); SCR_InitCvars (); VID_Init_Cvars (); COM_Init (); diff --git a/nq/source/snd_alsa_0_5.c b/nq/source/snd_alsa_0_5.c index 99da854d8..a59fdd9cc 100644 --- a/nq/source/snd_alsa_0_5.c +++ b/nq/source/snd_alsa_0_5.c @@ -1,4 +1,3 @@ - /* snd_alsa_0_5.c @@ -29,19 +28,14 @@ */ #ifdef HAVE_CONFIG_H -# include +# include "config.h" +#endif +#ifdef HAVE_UNISTD_H +# include #endif - -#include -#include -#include -#include #include #include -#ifdef HAVE_UNISTD_H -#include -#endif #include #include #ifdef HAVE_SYS_IOCTL_H @@ -60,6 +54,10 @@ #include +#include "QF/console.h" +#include "QF/qargs.h" +#include "sound.h" + #ifndef MAP_FAILED # define MAP_FAILED ((void*)-1) #endif @@ -271,7 +269,6 @@ SNDDMA_Init (void) shm->samples = setup.buf.block.frags * setup.buf.block.frag_size / (shm->samplebits / 8); // mono // - // // samples // in // buffer @@ -316,11 +313,9 @@ SNDDMA_Shutdown (void) } /* -============== -SNDDMA_Submit + SNDDMA_Submit -Send sound to device if buffer isn't really the dma buffer -=============== + Send sound to device if buffer isn't really the dma buffer */ void SNDDMA_Submit (void) @@ -337,24 +332,27 @@ SNDDMA_Submit (void) mmap_control->fragments[i % setup.buf.block.frags].data = 1; switch (mmap_control->status.status) { case SND_PCM_STATUS_PREPARED: - if ((rc = snd_pcm_channel_go (pcm_handle, SND_PCM_CHANNEL_PLAYBACK)) < - 0) { - fprintf (stderr, "unable to start playback. %s\n", - snd_strerror (rc)); - exit (1); - } - break; + if ((rc = snd_pcm_channel_go (pcm_handle, SND_PCM_CHANNEL_PLAYBACK)) + < 0) { + fprintf (stderr, "unable to start playback. %s\n", + snd_strerror (rc)); + exit (1); + } + break; case SND_PCM_STATUS_RUNNING: - break; + break; case SND_PCM_STATUS_UNDERRUN: - if ((rc = snd_pcm_plugin_prepare (pcm_handle, SND_PCM_CHANNEL_PLAYBACK)) - < 0) { - fprintf (stderr, "underrun: playback channel prepare error. %s\n", - snd_strerror (rc)); - exit (1); - } - break; + if ( + (rc = + snd_pcm_plugin_prepare (pcm_handle, + SND_PCM_CHANNEL_PLAYBACK)) < 0) { + fprintf (stderr, + "underrun: playback channel prepare error. %s\n", + snd_strerror (rc)); + exit (1); + } + break; default: - break; + break; } } diff --git a/nq/source/snd_alsa_0_9.c b/nq/source/snd_alsa_0_9.c index b289b1d05..8f33c54e0 100644 --- a/nq/source/snd_alsa_0_9.c +++ b/nq/source/snd_alsa_0_9.c @@ -89,56 +89,57 @@ SNDDMA_Init (void) switch (rate) { case -1: - if (snd_pcm_hw_params_set_rate_near (pcm, hw, 44100, 0) >= 0) { - frag_size = 256; /* assuming stereo 8 bit */ - rate = 44100; - } else if (snd_pcm_hw_params_set_rate_near (pcm, hw, 22050, 0) >= 0) { - frag_size = 128; /* assuming stereo 8 bit */ - rate = 22050; - } else if (snd_pcm_hw_params_set_rate_near (pcm, hw, 11025, 0) >= 0) { - frag_size = 64; /* assuming stereo 8 bit */ - rate = 11025; - } else { - Con_Printf ("ALSA: no useable rates\n"); - goto error; - } - break; + if (snd_pcm_hw_params_set_rate_near (pcm, hw, 44100, 0) >= 0) { + frag_size = 256; /* assuming stereo 8 bit */ + rate = 44100; + } else if (snd_pcm_hw_params_set_rate_near (pcm, hw, 22050, 0) >= 0) { + frag_size = 128; /* assuming stereo 8 bit */ + rate = 22050; + } else if (snd_pcm_hw_params_set_rate_near (pcm, hw, 11025, 0) >= 0) { + frag_size = 64; /* assuming stereo 8 bit */ + rate = 11025; + } else { + Con_Printf ("ALSA: no useable rates\n"); + goto error; + } + break; case 11025: case 22050: case 44100: - if (snd_pcm_hw_params_set_rate_near (pcm, hw, rate, 0) >= 0) { - frag_size = 64 * rate / 11025; /* assuming stereo 8 bit */ - break; - } - /* Fall through */ + if (snd_pcm_hw_params_set_rate_near (pcm, hw, rate, 0) >= 0) { + frag_size = 64 * rate / 11025; /* assuming stereo 8 bit */ + break; + } + /* Fall through */ default: - Con_Printf ("ALSA: desired rate not supported\n"); - goto error; + Con_Printf ("ALSA: desired rate not supported\n"); + goto error; } switch (bps) { case -1: - if (snd_pcm_hw_params_set_format (pcm, hw, SND_PCM_FORMAT_S16_LE) >= 0) { - bps = 16; - } else if (snd_pcm_hw_params_set_format (pcm, hw, SND_PCM_FORMAT_U8) >= - 0) { - bps = 8; - } else { - Con_Printf ("ALSA: no useable formats\n"); - goto error; - } - break; + if (snd_pcm_hw_params_set_format (pcm, hw, SND_PCM_FORMAT_S16_LE) >= + 0) { + bps = 16; + } else if (snd_pcm_hw_params_set_format (pcm, hw, SND_PCM_FORMAT_U8) + >= 0) { + bps = 8; + } else { + Con_Printf ("ALSA: no useable formats\n"); + goto error; + } + break; case 8: case 16: - if (snd_pcm_hw_params_set_format (pcm, hw, - bps == 8 ? SND_PCM_FORMAT_U8 : - SND_PCM_FORMAT_S16) >= 0) { - break; - } - /* Fall through */ + if (snd_pcm_hw_params_set_format (pcm, hw, + bps == 8 ? SND_PCM_FORMAT_U8 : + SND_PCM_FORMAT_S16) >= 0) { + break; + } + /* Fall through */ default: - Con_Printf ("ALSA: desired format not supported\n"); - goto error; + Con_Printf ("ALSA: desired format not supported\n"); + goto error; } if (snd_pcm_hw_params_set_access (pcm, hw, @@ -149,23 +150,23 @@ SNDDMA_Init (void) switch (stereo) { case -1: - if (snd_pcm_hw_params_set_channels (pcm, hw, 2) >= 0) { - stereo = 1; - } else if (snd_pcm_hw_params_set_channels (pcm, hw, 1) >= 0) { - stereo = 0; - } else { - Con_Printf ("ALSA: no useable channels\n"); - goto error; - } - break; + if (snd_pcm_hw_params_set_channels (pcm, hw, 2) >= 0) { + stereo = 1; + } else if (snd_pcm_hw_params_set_channels (pcm, hw, 1) >= 0) { + stereo = 0; + } else { + Con_Printf ("ALSA: no useable channels\n"); + goto error; + } + break; case 0: case 1: - if (snd_pcm_hw_params_set_channels (pcm, hw, stereo ? 2 : 1) >= 0) - break; - /* Fall through */ + if (snd_pcm_hw_params_set_channels (pcm, hw, stereo ? 2 : 1) >= 0) + break; + /* Fall through */ default: - Con_Printf ("ALSA: desired channels not supported\n"); - goto error; + Con_Printf ("ALSA: desired channels not supported\n"); + goto error; } snd_pcm_hw_params_set_period_size_near (pcm, hw, frag_size, 0); @@ -193,13 +194,11 @@ SNDDMA_Init (void) shm->splitbuffer = 0; shm->channels = stereo + 1; shm->submission_chunk = snd_pcm_hw_params_get_period_size (hw, 0); // don't - // - // - // mix - // less - // than - // this - // # + // mix + // less + // than + // this + // # shm->samplepos = 0; // in mono samples shm->samplebits = bps; buffer_size = snd_pcm_hw_params_get_buffer_size (hw); @@ -283,32 +282,32 @@ SNDDMA_Submit (void) switch (state) { case SND_PCM_STATE_PREPARED: - snd_pcm_mmap_forward (pcm, count); - snd_pcm_start (pcm); - break; - case SND_PCM_STATE_RUNNING: - hw_ptr = get_hw_ptr (); - missed = hw_ptr - shm->samplepos / shm->channels; - if (missed < 0) - missed += buffer_size; - count -= missed; - offset = snd_pcm_mmap_offset (pcm); - if (offset > hw_ptr) - count -= (offset - hw_ptr); - else - count -= (buffer_size - hw_ptr + offset); - if (count < 0) { - snd_pcm_rewind (pcm, -count); - } else { - avail = snd_pcm_avail_update (pcm); - if (avail < 0) - avail = buffer_size; - if (count > avail) - count = avail; snd_pcm_mmap_forward (pcm, count); - } - break; + snd_pcm_start (pcm); + break; + case SND_PCM_STATE_RUNNING: + hw_ptr = get_hw_ptr (); + missed = hw_ptr - shm->samplepos / shm->channels; + if (missed < 0) + missed += buffer_size; + count -= missed; + offset = snd_pcm_mmap_offset (pcm); + if (offset > hw_ptr) + count -= (offset - hw_ptr); + else + count -= (buffer_size - hw_ptr + offset); + if (count < 0) { + snd_pcm_rewind (pcm, -count); + } else { + avail = snd_pcm_avail_update (pcm); + if (avail < 0) + avail = buffer_size; + if (count > avail) + count = avail; + snd_pcm_mmap_forward (pcm, count); + } + break; default: - break; + break; } } diff --git a/nq/source/snd_dma.c b/nq/source/snd_dma.c index ad1c753ef..a7ed5e66e 100644 --- a/nq/source/snd_dma.c +++ b/nq/source/snd_dma.c @@ -29,29 +29,39 @@ #ifdef HAVE_CONFIG_H # include "config.h" #endif +#ifdef HAVE_STRING_H +# include +#endif +#ifdef HAVE_STRINGS_H +# include +#endif -#include #include -#include "QF/sys.h" -#include "sound.h" +#include "client.h" #include "QF/cmd.h" #include "QF/console.h" -#include "client.h" -#include "QF/qargs.h" #include "host.h" +#include "model.h" +#include "QF/qargs.h" +#include "QF/sys.h" +#include "sound.h" #ifdef _WIN32 #include "winquake.h" +#include "in_win.h" #endif void S_Play (void); void S_PlayVol (void); void S_SoundList (void); -void S_Update_ (); +void S_Update_ (void); void S_StopAllSounds (qboolean clear); void S_StopAllSoundsC (void); +// QuakeWorld hack... +//#define viewentity playernum+1 + // ======================================================================= // Internal sound data & structures // ======================================================================= @@ -98,7 +108,6 @@ cvar_t *snd_stereo; cvar_t *nosound; cvar_t *precache; cvar_t *loadas8bit; -cvar_t *bgmbuffer; cvar_t *ambient_level; cvar_t *ambient_fade; cvar_t *snd_noextraupdate; @@ -152,15 +161,13 @@ S_SoundInfo_f (void) Con_Printf ("%5d samplebits\n", shm->samplebits); Con_Printf ("%5d submission_chunk\n", shm->submission_chunk); Con_Printf ("%5d speed\n", shm->speed); - Con_Printf ("0x%x dma buffer\n", (int) shm->buffer); + Con_Printf ("0x%lx dma buffer\n", (unsigned long) shm->buffer); Con_Printf ("%5d total_channels\n", total_channels); } /* -================ -S_Startup -================ + S_Startup */ void @@ -188,9 +195,7 @@ S_Startup (void) /* -================ -S_Init -================ + S_Init */ void S_Init (void) @@ -198,40 +203,16 @@ S_Init (void) Con_Printf ("\nSound Initialization\n"); - Cmd_AddCommand ("play", S_Play, "No Description"); - Cmd_AddCommand ("playvol", S_PlayVol, "No Description"); - Cmd_AddCommand ("stopsound", S_StopAllSoundsC, "No Description"); - Cmd_AddCommand ("soundlist", S_SoundList, "No Description"); - Cmd_AddCommand ("soundinfo", S_SoundInfo_f, "No Description"); - - snd_device = Cvar_Get ("snd_device", "", CVAR_ROM, - "sound device. \"\" is system default"); - snd_rate = Cvar_Get ("snd_rate", "0", CVAR_ROM, - "sound playback rate. 0 is system default"); - snd_bits = Cvar_Get ("snd_bits", "0", CVAR_ROM, - "sound sample depth. 0 is system default"); - snd_stereo = Cvar_Get ("snd_stereo", "1", CVAR_ROM, - "sound stereo output"); - nosound = Cvar_Get ("nosound", "0", CVAR_NONE, "None"); - volume = Cvar_Get ("volume", "0.7", CVAR_ARCHIVE, "None"); - precache = Cvar_Get ("precache", "1", CVAR_NONE, "None"); - loadas8bit = Cvar_Get ("loadas8bit", "0", CVAR_NONE, "None"); - bgmvolume = Cvar_Get ("bgmvolume", "1", CVAR_ARCHIVE, "None"); - bgmbuffer = Cvar_Get ("bgmbuffer", "4096", CVAR_NONE, "None"); - ambient_level = Cvar_Get ("ambient_level", "0.3", CVAR_NONE, "None"); - ambient_fade = Cvar_Get ("ambient_fade", "100", CVAR_NONE, "None"); - snd_noextraupdate = Cvar_Get ("snd_noextraupdate", "0", CVAR_NONE, "None"); - snd_show = Cvar_Get ("snd_show", "0", CVAR_NONE, "None"); - snd_interp = - Cvar_Get ("snd_interp", "1", CVAR_ARCHIVE, - "control sample interpolation"); - snd_phasesep = - Cvar_Get ("snd_phasesep", "0.0", CVAR_ARCHIVE, - "max stereo phase separation in ms. 0.6 is for 20cm head"); - snd_volumesep = - Cvar_Get ("snd_volumesep", "1.0", CVAR_ARCHIVE, - "max stereo volume separation in ms. 1.0 is max"); - _snd_mixahead = Cvar_Get ("_snd_mixahead", "0.1", CVAR_ARCHIVE, "None"); + Cmd_AddCommand ("play", S_Play, + "Play selected sound effect (play pathto/sound.wav)"); + Cmd_AddCommand ("playvol", S_PlayVol, + "Play selected sound effect at selected volume (playvol pathto/sound.wav num"); + Cmd_AddCommand ("stopsound", S_StopAllSoundsC, + "Stops all sounds currently being played"); + Cmd_AddCommand ("soundlist", S_SoundList, + "Reports a list of sounds in the cache"); + Cmd_AddCommand ("soundinfo", S_SoundInfo_f, + "Report information on the sound system"); if (COM_CheckParm ("-nosound")) return; @@ -245,7 +226,6 @@ S_Init (void) } - snd_initialized = true; S_Startup (); @@ -274,8 +254,7 @@ S_Init (void) shm->submission_chunk = 1; shm->buffer = Hunk_AllocName (1 << 16, "shmbuf"); } - - Con_Printf ("Sound sampling rate: %i\n", shm->speed); +// Con_Printf ("Sound sampling rate: %i\n", shm->speed); // provides a tick sound until washed clean @@ -288,6 +267,52 @@ S_Init (void) S_StopAllSounds (true); } +void +S_Init_Cvars (void) +{ + snd_device = Cvar_Get ("snd_device", "", CVAR_ROM, + "sound device. \"\" is system default"); + snd_rate = Cvar_Get ("snd_rate", "0", CVAR_ROM, + "sound playback rate. 0 is system default"); + snd_bits = Cvar_Get ("snd_bits", "0", CVAR_ROM, + "sound sample depth. 0 is system default"); + snd_stereo = Cvar_Get ("snd_stereo", "1", CVAR_ROM, + "sound stereo output"); + nosound = Cvar_Get ("nosound", "0", CVAR_NONE, "Set to turn sound off"); + volume = + Cvar_Get ("volume", "0.7", CVAR_ARCHIVE, + "Set the volume for sound playback"); + precache = + Cvar_Get ("precache", "1", CVAR_NONE, "Toggle the use of a precache"); + loadas8bit = + Cvar_Get ("loadas8bit", "0", CVAR_NONE, + "Toggles if sounds are loaded as 8-bit samples"); + bgmvolume = Cvar_Get ("bgmvolume", "1", CVAR_ARCHIVE, "Volume of CD music"); + ambient_level = + Cvar_Get ("ambient_level", "0.3", CVAR_NONE, "Ambient sounds' volume"); + ambient_fade = + Cvar_Get ("ambient_fade", "100", CVAR_NONE, + "How quickly ambient sounds fade in or out"); + snd_noextraupdate = + Cvar_Get ("snd_noextraupdate", "0", CVAR_NONE, + "Toggles the correct value display in host_speeds. Usually messes up sound playback when in effect"); + snd_show = + Cvar_Get ("snd_show", "0", CVAR_NONE, + "Toggles the display of sounds currently being played"); + snd_interp = + Cvar_Get ("snd_interp", "1", CVAR_ARCHIVE, + "control sample interpolation"); + snd_phasesep = + Cvar_Get ("snd_phasesep", "0.0", CVAR_ARCHIVE, + "max stereo phase separation in ms. 0.6 is for 20cm head"); + snd_volumesep = + Cvar_Get ("snd_volumesep", "1.0", CVAR_ARCHIVE, + "max stereo volume separation in ms. 1.0 is max"); + _snd_mixahead = + Cvar_Get ("_snd_mixahead", "0.1", CVAR_ARCHIVE, + "Delay time for sounds"); +} + // ======================================================================= // Shutdown sound engine @@ -317,10 +342,7 @@ S_Shutdown (void) // ======================================================================= /* -================== -S_FindName - -================== + S_FindName */ sfx_t * S_FindName (char *name) @@ -353,10 +375,7 @@ S_FindName (char *name) /* -================== -S_TouchSound - -================== + S_TouchSound */ void S_TouchSound (char *name) @@ -371,10 +390,7 @@ S_TouchSound (char *name) } /* -================== -S_PrecacheSound - -================== + S_PrecacheSound */ sfx_t * S_PrecacheSound (char *name) @@ -397,9 +413,7 @@ S_PrecacheSound (char *name) //============================================================================= /* -================= -SND_PickChannel -================= + SND_PickChannel */ channel_t * SND_PickChannel (int entnum, int entchannel) @@ -415,14 +429,8 @@ SND_PickChannel (int entnum, int entchannel) ch_idx++) { if (entchannel != 0 // channel 0 never overrides && channels[ch_idx].entnum == entnum - && (channels[ch_idx].entchannel == entchannel || entchannel == -1)) { // always - // - // - // override - // sound - // from - // same - // entity + && (channels[ch_idx].entchannel == entchannel || entchannel == -1)) { + // always override sound from same entity first_to_die = ch_idx; break; } @@ -447,9 +455,7 @@ SND_PickChannel (int entnum, int entchannel) } /* -================= -SND_Spatialize -================= + SND_Spatialize */ void SND_Spatialize (channel_t *ch) @@ -507,8 +513,8 @@ SND_Spatialize (channel_t *ch) // ======================================================================= void -S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, - float fvol, float attenuation) +S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, + float attenuation) { channel_t *target_chan, *check; sfxcache_t *sc; @@ -540,7 +546,6 @@ S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, target_chan->entnum = entnum; target_chan->entchannel = entchannel; SND_Spatialize (target_chan); - target_chan->oldphase = target_chan->phase; if (!target_chan->leftvol && !target_chan->rightvol) return; // not audible at all @@ -634,36 +639,9 @@ S_ClearBuffer (void) clear = 0; #ifdef _WIN32 - if (pDSBuf) { - DWORD dwSize; - DWORD *pData; - int reps; - HRESULT hresult; - - reps = 0; - - while ( - (hresult = - pDSBuf->lpVtbl->Lock (pDSBuf, 0, gSndBufSize, &pData, &dwSize, - NULL, NULL, 0)) != DS_OK) { - if (hresult != DSERR_BUFFERLOST) { - Con_Printf ("S_ClearBuffer: DS::Lock Sound Buffer Failed\n"); - S_Shutdown (); - return; - } - - if (++reps > 10000) { - Con_Printf ("S_ClearBuffer: DS: couldn't restore buffer\n"); - S_Shutdown (); - return; - } - } - - memset (pData, clear, shm->samples * shm->samplebits / 8); - - pDSBuf->lpVtbl->Unlock (pDSBuf, pData, dwSize, NULL, 0); - - } else + if (pDSBuf) + DSOUND_ClearBuffer (clear); + else #endif { memset (shm->buffer, clear, shm->samples * shm->samplebits / 8); @@ -672,9 +650,7 @@ S_ClearBuffer (void) /* -================= -S_StaticSound -================= + S_StaticSound */ void S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation) @@ -716,9 +692,7 @@ S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation) //============================================================================= /* -=================== -S_UpdateAmbientSounds -=================== + S_UpdateAmbientSounds */ void S_UpdateAmbientSounds (void) @@ -768,11 +742,9 @@ S_UpdateAmbientSounds (void) /* -============ -S_Update + S_Update -Called once each time through the main loop -============ + Called once each time through the main loop */ void S_Update (vec3_t origin, vec3_t forward, vec3_t right, vec3_t up) @@ -801,7 +773,7 @@ S_Update (vec3_t origin, vec3_t forward, vec3_t right, vec3_t up) if (!ch->sfx) continue; ch->oldphase = ch->phase; // prepare to lerp from prev to next - // phase + // phase SND_Spatialize (ch); // respatialize channel if (!ch->leftvol && !ch->rightvol) continue; @@ -869,12 +841,8 @@ GetSoundtime (void) // it is possible to miscount buffers if it has wrapped twice between // calls to S_Update. Oh well. -#ifdef __sun__ - soundtime = SNDDMA_GetSamples (); -#else samplepos = SNDDMA_GetDMAPos (); - if (samplepos < oldsamplepos) { buffers++; // buffer wrapped @@ -888,7 +856,6 @@ GetSoundtime (void) oldsamplepos = samplepos; soundtime = buffers * fullsamples + samplepos / shm->channels; -#endif } void @@ -904,10 +871,12 @@ S_ExtraUpdate (void) S_Update_ (); } + + void S_Update_ (void) { - unsigned endtime; + unsigned int endtime; int samps; if (!sound_started || (snd_blocked > 0)) @@ -928,21 +897,8 @@ S_Update_ (void) endtime = soundtime + samps; #ifdef _WIN32 -// if the buffer was lost or stopped, restore it and/or restart it - { - DWORD dwStatus; - - if (pDSBuf) { - if (pDSBuf->lpVtbl->GetStatus (pDSBuf, &dwStatus) != DD_OK) - Con_Printf ("Couldn't get sound buffer status\n"); - - if (dwStatus & DSBSTATUS_BUFFERLOST) - pDSBuf->lpVtbl->Restore (pDSBuf); - - if (!(dwStatus & DSBSTATUS_PLAYING)) - pDSBuf->lpVtbl->Play (pDSBuf, 0, 0, DSBPLAY_LOOPING); - } - } + if (pDSBuf) + DSOUND_Restore (); #endif S_PaintChannels (endtime); @@ -951,11 +907,7 @@ S_Update_ (void) } /* -=============================================================================== - -console functions - -=============================================================================== + console functions */ void @@ -970,7 +922,7 @@ S_Play (void) while (i < Cmd_Argc ()) { if (!strrchr (Cmd_Argv (i), '.')) { strcpy (name, Cmd_Argv (i)); - strcat (name, ".wav"); + strncat (name, ".wav", sizeof (name) - strlen (name)); } else strcpy (name, Cmd_Argv (i)); sfx = S_PrecacheSound (name); @@ -992,7 +944,7 @@ S_PlayVol (void) while (i < Cmd_Argc ()) { if (!strrchr (Cmd_Argv (i), '.')) { strcpy (name, Cmd_Argv (i)); - strcat (name, ".wav"); + strncat (name, ".wav", sizeof (name) - strlen (name)); } else strcpy (name, Cmd_Argv (i)); sfx = S_PrecacheSound (name); diff --git a/nq/source/snd_mem.c b/nq/source/snd_mem.c index d13c65f15..03f70162b 100644 --- a/nq/source/snd_mem.c +++ b/nq/source/snd_mem.c @@ -1,7 +1,7 @@ /* snd_mem.c - @description@ + sound caching Copyright (C) 1996-1997 Id Software, Inc. @@ -29,23 +29,25 @@ #ifdef HAVE_CONFIG_H # include "config.h" #endif +#ifdef HAVE_STRING_H +# include +#endif +#ifdef HAVE_STRINGS_H +# include +#endif -#include - -#include "QF/sys.h" -#include "sound.h" +#include "QF/console.h" #include "QF/qendian.h" #include "QF/quakefs.h" -#include "QF/console.h" +#include "sound.h" +#include "QF/sys.h" int cache_full_cycle; byte *S_Alloc (int size); /* -================ -ResampleSfx -================ + ResampleSfx */ void ResampleSfx (sfx_t *sfx, int inrate, int inwidth, byte * data) @@ -70,7 +72,6 @@ ResampleSfx (sfx_t *sfx, int inrate, int inwidth, byte * data) stepscale = (float) inrate / shm->speed; // this is usually 0.5, 1, or // - // // 2 outcount = sc->length / stepscale; @@ -162,9 +163,7 @@ ResampleSfx (sfx_t *sfx, int inrate, int inwidth, byte * data) //============================================================================= /* -============== -S_LoadSound -============== + S_LoadSound */ sfxcache_t * S_LoadSound (sfx_t *s) @@ -185,7 +184,7 @@ S_LoadSound (sfx_t *s) //Con_Printf ("S_LoadSound: %x\n", (int)stackbuf); // load it in strcpy (namebuffer, "sound/"); - strcat (namebuffer, s->name); + strncat (namebuffer, s->name, sizeof (namebuffer) - strlen (namebuffer)); // Con_Printf ("loading %s\n",namebuffer); @@ -230,11 +229,7 @@ S_LoadSound (sfx_t *s) /* -=============================================================================== - -WAV loading - -=============================================================================== + WAV loading */ @@ -320,9 +315,7 @@ DumpChunks (void) } /* -============ -GetWavinfo -============ + GetWavinfo */ wavinfo_t GetWavinfo (char *name, byte * wav, int wavlength) diff --git a/nq/source/snd_mix.c b/nq/source/snd_mix.c index 6b2f0dc5e..010f2623e 100644 --- a/nq/source/snd_mix.c +++ b/nq/source/snd_mix.c @@ -1,7 +1,7 @@ /* snd_mix.c - @description@ + portable code to mix sounds for snd_dma.c Copyright (C) 1996-1997 Id Software, Inc. @@ -29,14 +29,20 @@ #ifdef HAVE_CONFIG_H # include "config.h" #endif +#ifdef HAVE_STRING_H +# include +#endif +#ifdef HAVE_STRINGS_H +# include +#endif +#include "QF/console.h" #include "sound.h" -#include "QF/compat.h" #ifdef _WIN32 -#include "winquake.h" +# include "winquake.h" #else -#define DWORD unsigned long +# define DWORD unsigned long #endif #define PAINTBUFFER_SIZE 512 @@ -50,7 +56,7 @@ short *snd_out; void Snd_WriteLinearBlastStereo16 (void); -#ifndef USE_INTEL_ASM +#ifndef USE_INTEL_ASM void Snd_WriteLinearBlastStereo16 (void) { @@ -84,13 +90,6 @@ S_TransferStereo16 (int endtime) int lpaintedtime; DWORD *pbuf; -#ifdef _WIN32 - int reps; - DWORD dwSize, dwSize2; - DWORD *pbuf2; - HRESULT hresult; -#endif - snd_vol = volume->value * 256; snd_p = (int *) paintbuffer; @@ -98,27 +97,10 @@ S_TransferStereo16 (int endtime) #ifdef _WIN32 if (pDSBuf) { - reps = 0; - - while ( - (hresult = - pDSBuf->lpVtbl->Lock (pDSBuf, 0, gSndBufSize, &pbuf, &dwSize, - &pbuf2, &dwSize2, 0)) != DS_OK) { - if (hresult != DSERR_BUFFERLOST) { - Con_Printf - ("S_TransferStereo16: DS::Lock Sound Buffer Failed\n"); - S_Shutdown (); - S_Startup (); - return; - } - - if (++reps > 10000) { - Con_Printf - ("S_TransferStereo16: DS: couldn't restore buffer\n"); - S_Shutdown (); - S_Startup (); - return; - } + pbuf = DSOUND_LockBuffer (true); + if (!pbuf) { + Con_Printf ("DSOUND_LockBuffer fails!\n"); + return; } } else #endif @@ -147,7 +129,7 @@ S_TransferStereo16 (int endtime) #ifdef _WIN32 if (pDSBuf) - pDSBuf->lpVtbl->Unlock (pDSBuf, pbuf, dwSize, NULL, 0); + DSOUND_LockBuffer (false); #endif } @@ -163,13 +145,6 @@ S_TransferPaintBuffer (int endtime) int snd_vol; DWORD *pbuf; -#ifdef _WIN32 - int reps; - DWORD dwSize, dwSize2; - DWORD *pbuf2; - HRESULT hresult; -#endif - if (shm->samplebits == 16 && shm->channels == 2) { S_TransferStereo16 (endtime); return; @@ -184,27 +159,10 @@ S_TransferPaintBuffer (int endtime) #ifdef _WIN32 if (pDSBuf) { - reps = 0; - - while ( - (hresult = - pDSBuf->lpVtbl->Lock (pDSBuf, 0, gSndBufSize, &pbuf, &dwSize, - &pbuf2, &dwSize2, 0)) != DS_OK) { - if (hresult != DSERR_BUFFERLOST) { - Con_Printf - ("S_TransferPaintBuffer: DS::Lock Sound Buffer Failed\n"); - S_Shutdown (); - S_Startup (); - return; - } - - if (++reps > 10000) { - Con_Printf - ("S_TransferPaintBuffer: DS: couldn't restore buffer\n"); - S_Shutdown (); - S_Startup (); - return; - } + pbuf = DSOUND_LockBuffer (true); + if (!pbuf) { + Con_Printf ("DSOUND_LockBuffer fails!\n"); + return; } } else #endif @@ -240,30 +198,14 @@ S_TransferPaintBuffer (int endtime) } } #ifdef _WIN32 - if (pDSBuf) { - DWORD dwNewpos, dwWrite; - int il = paintedtime; - int ir = endtime - paintedtime; - - ir += il; - - pDSBuf->lpVtbl->Unlock (pDSBuf, pbuf, dwSize, NULL, 0); - - pDSBuf->lpVtbl->GetCurrentPosition (pDSBuf, &dwNewpos, &dwWrite); - -// if ((dwNewpos >= il) && (dwNewpos <= ir)) -// Con_Printf("%d-%d p %d c\n", il, ir, dwNewpos); - } + if (pDSBuf) + DSOUND_LockBuffer (false); #endif } /* -=============================================================================== - -CHANNEL MIXING - -=============================================================================== + CHANNEL MIXING */ void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int endtime); @@ -285,8 +227,8 @@ S_PaintChannels (int endtime) end = paintedtime + PAINTBUFFER_SIZE; // clear the paint buffer - // memset(paintbuffer, 0, (end - paintedtime) * - // sizeof(portable_samplepair_t)); +// memset (paintbuffer, 0, +// (end - paintedtime) * sizeof (portable_samplepair_t)); max_overpaint = 0; // paint in the channels. @@ -302,8 +244,7 @@ S_PaintChannels (int endtime) ltime = paintedtime; - while (ltime < end) { - // paint up to end + while (ltime < end) { // paint up to end if (ch->end < end) count = ch->end - ltime; else @@ -334,12 +275,10 @@ S_PaintChannels (int endtime) // transfer out according to DMA format S_TransferPaintBuffer (end); - memcpy (paintbuffer, - paintbuffer + end - paintedtime, - max_overpaint * sizeof (paintbuffer[0])); - memset (paintbuffer + max_overpaint, - 0, - sizeof (paintbuffer) - max_overpaint * sizeof (paintbuffer[0])); + memmove (paintbuffer, paintbuffer + end - paintedtime, + max_overpaint * sizeof (paintbuffer[0])); + memset (paintbuffer + max_overpaint, 0, sizeof (paintbuffer) + - max_overpaint * sizeof (paintbuffer[0])); paintedtime = end; } @@ -356,7 +295,7 @@ SND_InitScaletable (void) } -#ifndef USE_INTEL_ASM +#ifndef USE_INTEL_ASM void SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int count) @@ -384,16 +323,18 @@ SND_PaintChannelFrom8 (channel_t *ch, sfxcache_t *sc, int count) ch->pos += count; } -#endif // USE_INTEL_ASM +#endif // !USE_INTEL_ASM void SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int count) { + int data; + int left, right; int leftvol, rightvol; signed short *sfx; unsigned int i = 0; - unsigned int left_phase, right_phase; // never allowed < 0 anyway + unsigned int left_phase, right_phase; // Never allowed < 0 anyway leftvol = ch->leftvol; rightvol = ch->rightvol; @@ -424,7 +365,6 @@ SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int count) old_phase_left = 0; old_phase_right = -ch->oldphase; } - new_phase_left = left_phase; new_phase_right = right_phase; @@ -478,11 +418,10 @@ SND_PaintChannelFrom16 (channel_t *ch, sfxcache_t *sc, int count) } } - for (; count; count--, i++) { - int data = sfx[i]; - int left = (data * leftvol) >> 8; - int right = (data * rightvol) >> 8; - + for (i = 0; i < count; i++) { + data = sfx[i]; + left = (data * leftvol) >> 8; + right = (data * rightvol) >> 8; paintbuffer[i + left_phase].left += left; paintbuffer[i + right_phase].right += right; } diff --git a/nq/source/snd_null.c b/nq/source/snd_null.c index 30f43847d..34b09da93 100644 --- a/nq/source/snd_null.c +++ b/nq/source/snd_null.c @@ -1,10 +1,12 @@ - /* snd_null.c - @description@ + include this instead of all the other snd_* files to have no sound + code whatsoever Copyright (C) 1996-1997 Id Software, Inc. + Copyright (C) 1999,2000 contributors of the QuakeForge project + Please see the file "AUTHORS" for a list of contributors This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -27,9 +29,18 @@ $Id$ */ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif +#include "QF/qtypes.h" +#include "sound.h" + +// ======================================================================= +// Various variables also defined in snd_dma.c +// FIXME - should be put in one place +// ======================================================================= +channel_t channels[MAX_CHANNELS]; +int total_channels; +volatile dma_t *shm = 0; +cvar_t *loadas8bit; +int paintedtime; // sample PAIRS cvar_t *bgmvolume; @@ -39,6 +50,16 @@ cvar_t *volume; void S_Init (void) { + S_Init_Cvars (); +} + +void +S_Init_Cvars (void) +{ + volume = Cvar_Get ("volume", "0.7", CVAR_ARCHIVE, "Volume level of sounds"); + loadas8bit = + Cvar_Get ("loadas8bit", "0", CVAR_NONE, "Load samples as 8-bit"); + bgmvolume = Cvar_Get ("bgmvolume", "1", CVAR_ARCHIVE, "CD music volume"); } void @@ -72,8 +93,8 @@ S_StaticSound (sfx_t *sfx, vec3_t origin, float vol, float attenuation) } void -S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, - float fvol, float attenuation) +S_StartSound (int entnum, int entchannel, sfx_t *sfx, vec3_t origin, float fvol, + float attenuation) { } diff --git a/nq/source/snd_oss.c b/nq/source/snd_oss.c index e423e1096..03dd17335 100644 --- a/nq/source/snd_oss.c +++ b/nq/source/snd_oss.c @@ -1,7 +1,7 @@ /* snd_oss.c - @description@ + (description) Copyright (C) 1996-1997 Id Software, Inc. @@ -34,49 +34,75 @@ #include #include #include -#include -#include #include #include -#include +#include #include -#include "qtypes.h" -#include "console.h" + +#ifdef HAVE_SYS_IOCTL_H +# include +#endif + +#ifdef HAVE_SYS_MMAN_H +# include +#endif + +#if defined HAVE_SYS_SOUNDCARD_H +# include +#elif defined HAVE_LINUX_SOUNDCARD_H +# include +#elif HAVE_MACHINE_SOUNDCARD_H +# include +#endif + +#include "QF/cmd.h" +#include "QF/console.h" +#include "QF/qargs.h" #include "sound.h" -#include "qargs.h" + +#ifndef MAP_FAILED +# define MAP_FAILED ((void *) -1) +#endif static int audio_fd; static int snd_inited; static char *snd_dev = "/dev/dsp"; -static int tryrates[] = { 11025, 22051, 44100, 8000 }; +static int tryrates[] = { 11025, 22050, 22051, 44100, 8000 }; qboolean SNDDMA_Init (void) { - int rc; int fmt; int tmp; int i; struct audio_buf_info info; int caps; + int retries = 3; snd_inited = 0; -// open /dev/dsp, confirm capability to mmap, and get size of dma buffer + // open snd_dev, confirm capability to mmap, and get size of dma buffer if (snd_device->string[0]) snd_dev = snd_device->string; audio_fd = open (snd_dev, O_RDWR); - if (audio_fd < 0) { - perror (snd_dev); - Con_Printf ("Could not open %s\n", snd_dev); - return 0; + if (audio_fd < 0) { // Failed open, retry up to 3 times + // if it's busy + while ((audio_fd < 0) && retries-- && + ((errno == EAGAIN) || (errno == EBUSY))) { + sleep (1); + audio_fd = open (snd_dev, O_RDWR); + } + if (audio_fd < 0) { + perror (snd_dev); + Con_Printf ("Could not open %s\n", snd_dev); + return 0; + } } - rc = ioctl (audio_fd, SNDCTL_DSP_RESET, 0); - if (rc < 0) { + if ((rc = ioctl (audio_fd, SNDCTL_DSP_RESET, 0)) < 0) { perror (snd_dev); Con_Printf ("Could not reset %s\n", snd_dev); close (audio_fd); @@ -91,7 +117,7 @@ SNDDMA_Init (void) } if (!(caps & DSP_CAP_TRIGGER) || !(caps & DSP_CAP_MMAP)) { - Con_Printf ("Sorry but your soundcard can't do this\n"); + Con_Printf ("Sound device can't do memory-mapped I/O.\n"); close (audio_fd); return 0; } @@ -106,42 +132,46 @@ SNDDMA_Init (void) shm = &sn; shm->splitbuffer = 0; -// set sample bits & speed + // set sample bits & speed + shm->samplebits = snd_bits->int_val; - if (snd_bits->int_val) - shm->samplebits = snd_bits->int_val; if (shm->samplebits != 16 && shm->samplebits != 8) { ioctl (audio_fd, SNDCTL_DSP_GETFMTS, &fmt); - if (fmt & AFMT_S16_LE) + + if (fmt & AFMT_S16_LE) { // little-endian 16-bit signed shm->samplebits = 16; - else if (fmt & AFMT_U8) - shm->samplebits = 8; + } else { + if (fmt & AFMT_U8) { // unsigned 8-bit ulaw + shm->samplebits = 8; + } + } } - if (snd_rate->int_val) + if (snd_rate->int_val) { shm->speed = snd_rate->int_val; - else { - for (i = 0; i < sizeof (tryrates) / 4; i++) + } else { + for (i = 0; i < (sizeof (tryrates) / 4); i++) if (!ioctl (audio_fd, SNDCTL_DSP_SPEED, &tryrates[i])) break; shm->speed = tryrates[i]; } - if (!snd_stereo->int_val) + if (!snd_stereo->int_val) { shm->channels = 1; - else + } else { shm->channels = 2; + } shm->samples = info.fragstotal * info.fragsize / (shm->samplebits / 8); shm->submission_chunk = 1; -// memory map the dma buffer - + // memory map the dma buffer shm->buffer = (unsigned char *) mmap (NULL, info.fragstotal * info.fragsize, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, audio_fd, 0); - if (!shm->buffer || shm->buffer == MAP_FAILED) { + + if (shm->buffer == MAP_FAILED) { perror (snd_dev); Con_Printf ("Could not mmap %s\n", snd_dev); close (audio_fd); @@ -158,6 +188,7 @@ SNDDMA_Init (void) close (audio_fd); return 0; } + if (tmp) shm->channels = 2; else @@ -239,7 +270,7 @@ SNDDMA_GetDMAPos (void) return 0; } // shm->samplepos = (count.bytes / (shm->samplebits / 8)) & (shm->samples-1); -// fprintf(stderr, "%d \r", count.ptr); +// fprintf(stderr, "%d \r", count.ptr); shm->samplepos = count.ptr / (shm->samplebits / 8); return shm->samplepos; @@ -256,11 +287,9 @@ SNDDMA_Shutdown (void) } /* -============== -SNDDMA_Submit + SNDDMA_Submit -Send sound to device if buffer isn't really the dma buffer -=============== + Send sound to device if buffer isn't really the dma buffer */ void SNDDMA_Submit (void) diff --git a/nq/source/snd_sdl.c b/nq/source/snd_sdl.c index 885bef24e..a33fc6ddb 100644 --- a/nq/source/snd_sdl.c +++ b/nq/source/snd_sdl.c @@ -1,4 +1,3 @@ - /* snd_sdl.c @@ -28,15 +27,16 @@ */ #ifdef HAVE_CONFIG_H -# include +# include "config.h" #endif + #include #include -#include "console.h" -#include "qargs.h" +#include "QF/cmd.h" +#include "QF/console.h" +#include "QF/qargs.h" #include "sound.h" -#include "cmd.h" static dma_t the_shm; static int snd_inited; @@ -66,17 +66,17 @@ SNDDMA_Init (void) desired.freq = desired_speed; switch (desired_bits) { case 8: - desired.format = AUDIO_U8; - break; + desired.format = AUDIO_U8; + break; case 16: - if (SDL_BYTEORDER == SDL_BIG_ENDIAN) - desired.format = AUDIO_S16MSB; - else - desired.format = AUDIO_S16LSB; - break; + if (SDL_BYTEORDER == SDL_BIG_ENDIAN) + desired.format = AUDIO_S16MSB; + else + desired.format = AUDIO_S16LSB; + break; default: - Con_Printf ("Unknown number of audio bits: %d\n", desired_bits); - return 0; + Con_Printf ("Unknown number of audio bits: %d\n", desired_bits); + return 0; } desired.channels = 2; desired.samples = 512; @@ -91,27 +91,27 @@ SNDDMA_Init (void) /* Make sure we can support the audio format */ switch (obtained.format) { case AUDIO_U8: - /* Supported */ - break; - case AUDIO_S16LSB: - case AUDIO_S16MSB: - if (((obtained.format == AUDIO_S16LSB) && - (SDL_BYTEORDER == SDL_LIL_ENDIAN)) || - ((obtained.format == AUDIO_S16MSB) && - (SDL_BYTEORDER == SDL_BIG_ENDIAN))) { /* Supported */ break; - } - /* Unsupported, fall through */ ; + case AUDIO_S16LSB: + case AUDIO_S16MSB: + if (((obtained.format == AUDIO_S16LSB) && + (SDL_BYTEORDER == SDL_LIL_ENDIAN)) || + ((obtained.format == AUDIO_S16MSB) && + (SDL_BYTEORDER == SDL_BIG_ENDIAN))) { + /* Supported */ + break; + } + /* Unsupported, fall through */ ; default: - /* Not supported -- force SDL to do our bidding */ - SDL_CloseAudio (); - if (SDL_OpenAudio (&desired, NULL) < 0) { - Con_Printf ("Couldn't open SDL audio: %s\n", SDL_GetError ()); - return 0; - } - memcpy (&obtained, &desired, sizeof (desired)); - break; + /* Not supported -- force SDL to do our bidding */ + SDL_CloseAudio (); + if (SDL_OpenAudio (&desired, NULL) < 0) { + Con_Printf ("Couldn't open SDL audio: %s\n", SDL_GetError ()); + return 0; + } + memcpy (&obtained, &desired, sizeof (desired)); + break; } SDL_PauseAudio (0); diff --git a/nq/source/snd_sun.c b/nq/source/snd_sun.c index d2c638871..2a2602b65 100644 --- a/nq/source/snd_sun.c +++ b/nq/source/snd_sun.c @@ -1,10 +1,11 @@ - /* snd_sun.c - @description@ + (description) Copyright (C) 1996-1997 Id Software, Inc. + Copyright (C) 1999,2000 contributors of the QuakeForge project + Please see the file "AUTHORS" for a list of contributors This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License @@ -30,8 +31,10 @@ #ifdef HAVE_CONFIG_H # include "config.h" #endif +#ifdef HAVE_UNISTD_H +# include +#endif -#include #include #include #include @@ -43,10 +46,14 @@ #include #include +#include "QF/qtypes.h" +#include "sound.h" +#include "QF/qargs.h" +#include "QF/console.h" + int audio_fd; int snd_inited; -static int bufpos; static int wbufp; static audio_info_t info; @@ -56,21 +63,12 @@ unsigned char dma_buffer[BUFFER_SIZE]; unsigned char pend_buffer[BUFFER_SIZE]; int pending; -static int lastwrite = 0; - qboolean SNDDMA_Init (void) { - int rc; - int fmt; - int tmp; - int i; - char *s; - int caps; - if (snd_inited) { printf ("Sound already init'd\n"); - return; + return 0; } shm = &sn; @@ -186,23 +184,19 @@ SNDDMA_Shutdown (void) } /* -============== -SNDDMA_Submit + SNDDMA_Submit -Send sound to device if buffer isn't really the dma buffer -=============== + Send sound to device if buffer isn't really the dma buffer */ void SNDDMA_Submit (void) { - int samps; int bsize; int bytes, b; static unsigned char writebuf[1024]; unsigned char *p; int idx; int stop = paintedtime; - extern int soundtime; if (paintedtime < wbufp) wbufp = 0; // reset diff --git a/nq/source/snd_win.c b/nq/source/snd_win.c index ffaf95d27..1b2bb086f 100644 --- a/nq/source/snd_win.c +++ b/nq/source/snd_win.c @@ -1,7 +1,7 @@ /* snd_win.c - @description@ + (description) Copyright (C) 1996-1997 Id Software, Inc. @@ -30,7 +30,12 @@ # include "config.h" #endif +#define CINTERFACE + #include "winquake.h" +#include "QF/qargs.h" +#include "QF/console.h" +#include "sound.h" #define iDirectSoundCreate(a,b,c) pDirectSoundCreate(a,b,c) @@ -55,7 +60,6 @@ static qboolean primary_format_set; static int sample16; static int snd_sent, snd_completed; - /* * Global variables. Must be visible to window-procedure function * so it can unlock and free the data block after it has been played. @@ -80,14 +84,12 @@ LPDIRECTSOUNDBUFFER pDSBuf, pDSPBuf; HINSTANCE hInstDS; -qboolean SNDDMA_InitDirect (void); +sndinitstat SNDDMA_InitDirect (void); qboolean SNDDMA_InitWav (void); /* -================== -S_BlockSound -================== + S_BlockSound */ void S_BlockSound (void) @@ -97,17 +99,14 @@ S_BlockSound (void) if (snd_iswave) { snd_blocked++; - if (snd_blocked == 1) { + if (snd_blocked == 1) waveOutReset (hWaveOut); - } } } /* -================== -S_UnblockSound -================== + S_UnblockSound */ void S_UnblockSound (void) @@ -121,9 +120,7 @@ S_UnblockSound (void) /* -================== -FreeSound -================== + FreeSound */ void FreeSound (void) @@ -131,17 +128,17 @@ FreeSound (void) int i; if (pDSBuf) { - pDSBuf->lpVtbl->Stop (pDSBuf); - pDSBuf->lpVtbl->Release (pDSBuf); + IDirectSoundBuffer_Stop (pDSBuf); + IDirectSound_Release (pDSBuf); } // only release primary buffer if it's not also the mixing buffer we just released if (pDSPBuf && (pDSBuf != pDSPBuf)) { - pDSPBuf->lpVtbl->Release (pDSPBuf); + IDirectSound_Release (pDSPBuf); } if (pDS) { - pDS->lpVtbl->SetCooperativeLevel (pDS, mainwindow, DSSCL_NORMAL); - pDS->lpVtbl->Release (pDS); + IDirectSound_SetCooperativeLevel (pDS, mainwindow, DSSCL_NORMAL); + IDirectSound_Release (pDS); } if (hWaveOut) { @@ -181,14 +178,11 @@ FreeSound (void) /* -================== -SNDDMA_InitDirect + SNDDMA_InitDirect -Direct-Sound support -================== + Direct-Sound support */ -sndinitstat -SNDDMA_InitDirect (void) +sndinitstat SNDDMA_InitDirect (void) { DSBUFFERDESC dsbuf; DSBCAPS dsbcaps; @@ -219,7 +213,7 @@ SNDDMA_InitDirect (void) hInstDS = LoadLibrary ("dsound.dll"); if (hInstDS == NULL) { - Con_SafePrintf ("Couldn't load dsound.dll\n"); + Con_Printf ("Couldn't load dsound.dll\n"); return SIS_FAILURE; } @@ -227,44 +221,35 @@ SNDDMA_InitDirect (void) (void *) GetProcAddress (hInstDS, "DirectSoundCreate"); if (!pDirectSoundCreate) { - Con_SafePrintf ("Couldn't get DS proc addr\n"); + Con_Printf ("Couldn't get DS proc addr\n"); return SIS_FAILURE; } } while ((hresult = iDirectSoundCreate (NULL, &pDS, NULL)) != DS_OK) { if (hresult != DSERR_ALLOCATED) { - Con_SafePrintf ("DirectSound create failed\n"); + Con_Printf ("DirectSound create failed\n"); return SIS_FAILURE; } - - if (MessageBox (NULL, - "The sound hardware is in use by another app.\n\n" - "Select Retry to try to start sound again or Cancel to run Quake with no sound.", - "Sound not available", - MB_RETRYCANCEL | MB_SETFOREGROUND | MB_ICONEXCLAMATION) - != IDRETRY) { - Con_SafePrintf ("DirectSoundCreate failure\n" - " hardware already in use\n"); - return SIS_NOTAVAIL; - } + Con_Printf ("DirectSoundCreate failure\n" + " hardware already in use\n"); + return SIS_NOTAVAIL; } dscaps.dwSize = sizeof (dscaps); - - if (DS_OK != pDS->lpVtbl->GetCaps (pDS, &dscaps)) { - Con_SafePrintf ("Couldn't get DS caps\n"); + if (DS_OK != IDirectSound_GetCaps (pDS, &dscaps)) { + Con_Printf ("Couldn't get DS caps\n"); } if (dscaps.dwFlags & DSCAPS_EMULDRIVER) { - Con_SafePrintf ("No DirectSound driver installed\n"); + Con_Printf ("No DirectSound driver installed\n"); FreeSound (); return SIS_FAILURE; } if (DS_OK != - pDS->lpVtbl->SetCooperativeLevel (pDS, mainwindow, DSSCL_EXCLUSIVE)) { - Con_SafePrintf ("Set coop level failed\n"); + IDirectSound_SetCooperativeLevel (pDS, mainwindow, DSSCL_EXCLUSIVE)) { + Con_Printf ("Set coop level failed\n"); FreeSound (); return SIS_FAILURE; } @@ -282,18 +267,12 @@ SNDDMA_InitDirect (void) if (!COM_CheckParm ("-snoforceformat")) { if (DS_OK == - pDS->lpVtbl->CreateSoundBuffer (pDS, &dsbuf, &pDSPBuf, NULL)) { + IDirectSound_CreateSoundBuffer (pDS, &dsbuf, &pDSPBuf, NULL)) { pformat = format; - if (DS_OK != pDSPBuf->lpVtbl->SetFormat (pDSPBuf, &pformat)) { - if (snd_firsttime) - Con_SafePrintf ("Set primary sound buffer format: no\n"); - } else { - if (snd_firsttime) - Con_SafePrintf ("Set primary sound buffer format: yes\n"); - + if (DS_OK != IDirectSoundBuffer_SetFormat (pDSPBuf, &pformat)) { + } else primary_format_set = true; - } } } @@ -309,8 +288,8 @@ SNDDMA_InitDirect (void) dsbcaps.dwSize = sizeof (dsbcaps); if (DS_OK != - pDS->lpVtbl->CreateSoundBuffer (pDS, &dsbuf, &pDSBuf, NULL)) { - Con_SafePrintf ("DS:CreateSoundBuffer Failed"); + IDirectSound_CreateSoundBuffer (pDS, &dsbuf, &pDSBuf, NULL)) { + Con_Printf ("DS:CreateSoundBuffer Failed"); FreeSound (); return SIS_FAILURE; } @@ -319,59 +298,47 @@ SNDDMA_InitDirect (void) shm->samplebits = format.wBitsPerSample; shm->speed = format.nSamplesPerSec; - if (DS_OK != pDSBuf->lpVtbl->GetCaps (pDSBuf, &dsbcaps)) { - Con_SafePrintf ("DS:GetCaps failed\n"); + if (DS_OK != IDirectSound_GetCaps (pDSBuf, &dsbcaps)) { + Con_Printf ("DS:GetCaps failed\n"); FreeSound (); return SIS_FAILURE; } - - if (snd_firsttime) - Con_SafePrintf ("Using secondary sound buffer\n"); } else { if (DS_OK != - pDS->lpVtbl->SetCooperativeLevel (pDS, mainwindow, + IDirectSound_SetCooperativeLevel (pDS, mainwindow, DSSCL_WRITEPRIMARY)) { - Con_SafePrintf ("Set coop level failed\n"); + Con_Printf ("Set coop level failed\n"); FreeSound (); return SIS_FAILURE; } - if (DS_OK != pDSPBuf->lpVtbl->GetCaps (pDSPBuf, &dsbcaps)) { + if (DS_OK != IDirectSound_GetCaps (pDSPBuf, &dsbcaps)) { Con_Printf ("DS:GetCaps failed\n"); return SIS_FAILURE; } pDSBuf = pDSPBuf; - Con_SafePrintf ("Using primary sound buffer\n"); } // Make sure mixer is active - pDSBuf->lpVtbl->Play (pDSBuf, 0, 0, DSBPLAY_LOOPING); - - if (snd_firsttime) - Con_SafePrintf (" %d channel(s)\n" - " %d bits/sample\n" - " %d bytes/sec\n", - shm->channels, shm->samplebits, shm->speed); + IDirectSoundBuffer_Play (pDSBuf, 0, 0, DSBPLAY_LOOPING); gSndBufSize = dsbcaps.dwBufferBytes; // initialize the buffer reps = 0; - while ( - (hresult = - pDSBuf->lpVtbl->Lock (pDSBuf, 0, gSndBufSize, &lpData, &dwSize, - NULL, NULL, 0)) != DS_OK) { + while ((hresult = IDirectSoundBuffer_Lock (pDSBuf, 0, gSndBufSize, + (LPVOID *) & lpData, &dwSize, + NULL, NULL, 0)) != DS_OK) { if (hresult != DSERR_BUFFERLOST) { - Con_SafePrintf - ("SNDDMA_InitDirect: DS::Lock Sound Buffer Failed\n"); + Con_Printf ("SNDDMA_InitDirect: DS::Lock Sound Buffer Failed\n"); FreeSound (); return SIS_FAILURE; } if (++reps > 10000) { - Con_SafePrintf ("SNDDMA_InitDirect: DS: couldn't restore buffer\n"); + Con_Printf ("SNDDMA_InitDirect: DS: couldn't restore buffer\n"); FreeSound (); return SIS_FAILURE; } @@ -381,16 +348,16 @@ SNDDMA_InitDirect (void) memset (lpData, 0, dwSize); // lpData[4] = lpData[5] = 0x7f; // force a pop for debugging - pDSBuf->lpVtbl->Unlock (pDSBuf, lpData, dwSize, NULL, 0); + IDirectSoundBuffer_Unlock (pDSBuf, lpData, dwSize, NULL, 0); /* we don't want anyone to access the buffer directly w/o locking it first. */ lpData = NULL; - pDSBuf->lpVtbl->Stop (pDSBuf); - pDSBuf->lpVtbl->GetCurrentPosition (pDSBuf, &mmstarttime.u.sample, - &dwWrite); - pDSBuf->lpVtbl->Play (pDSBuf, 0, 0, DSBPLAY_LOOPING); + IDirectSoundBuffer_Stop (pDSBuf); + IDirectSoundBuffer_GetCurrentPosition (pDSBuf, &mmstarttime.u.sample, + &dwWrite); + IDirectSoundBuffer_Play (pDSBuf, 0, 0, DSBPLAY_LOOPING); shm->soundalive = true; shm->splitbuffer = false; @@ -407,11 +374,9 @@ SNDDMA_InitDirect (void) /* -================== -SNDDM_InitWav + SNDDM_InitWav -Crappy windows multimedia base -================== + Crappy windows multimedia base */ qboolean SNDDMA_InitWav (void) @@ -440,23 +405,14 @@ SNDDMA_InitWav (void) /* Open a waveform device for output using window callback. */ while ((hr = waveOutOpen ((LPHWAVEOUT) & hWaveOut, WAVE_MAPPER, - &format, - 0, 0L, CALLBACK_NULL)) != MMSYSERR_NOERROR) { + &format, 0, 0L, + CALLBACK_NULL)) != MMSYSERR_NOERROR) { if (hr != MMSYSERR_ALLOCATED) { - Con_SafePrintf ("waveOutOpen failed\n"); - return false; - } - - if (MessageBox (NULL, - "The sound hardware is in use by another app.\n\n" - "Select Retry to try to start sound again or Cancel to run Quake with no sound.", - "Sound not available", - MB_RETRYCANCEL | MB_SETFOREGROUND | MB_ICONEXCLAMATION) - != IDRETRY) { - Con_SafePrintf ("waveOutOpen failure;\n" - " hardware already in use\n"); + Con_Printf ("waveOutOpen failed\n"); return false; } + Con_Printf ("waveOutOpen failure;\n" " hardware already in use\n"); + return false; } /* @@ -468,13 +424,13 @@ SNDDMA_InitWav (void) gSndBufSize = WAV_BUFFERS * WAV_BUFFER_SIZE; hData = GlobalAlloc (GMEM_MOVEABLE | GMEM_SHARE, gSndBufSize); if (!hData) { - Con_SafePrintf ("Sound: Out of memory.\n"); + Con_Printf ("Sound: Out of memory.\n"); FreeSound (); return false; } lpData = GlobalLock (hData); if (!lpData) { - Con_SafePrintf ("Sound: Failed to lock.\n"); + Con_Printf ("Sound: Failed to lock.\n"); FreeSound (); return false; } @@ -489,7 +445,7 @@ SNDDMA_InitWav (void) (DWORD) sizeof (WAVEHDR) * WAV_BUFFERS); if (hWaveHdr == NULL) { - Con_SafePrintf ("Sound: Failed to Alloc header.\n"); + Con_Printf ("Sound: Failed to Alloc header.\n"); FreeSound (); return false; } @@ -497,7 +453,7 @@ SNDDMA_InitWav (void) lpWaveHdr = (LPWAVEHDR) GlobalLock (hWaveHdr); if (lpWaveHdr == NULL) { - Con_SafePrintf ("Sound: Failed to lock header.\n"); + Con_Printf ("Sound: Failed to lock header.\n"); FreeSound (); return false; } @@ -511,7 +467,7 @@ SNDDMA_InitWav (void) if (waveOutPrepareHeader (hWaveOut, lpWaveHdr + i, sizeof (WAVEHDR)) != MMSYSERR_NOERROR) { - Con_SafePrintf ("Sound: failed to prepare wave headers\n"); + Con_Printf ("Sound: failed to prepare wave headers\n"); FreeSound (); return false; } @@ -531,15 +487,13 @@ SNDDMA_InitWav (void) } /* -================== -SNDDMA_Init + SNDDMA_Init -Try to find a sound device to mix for. -Returns false if nothing is found. -================== + Try to find a sound device to mix for. + Returns false if nothing is found. */ -int +qboolean SNDDMA_Init (void) { sndinitstat stat; @@ -561,10 +515,10 @@ SNDDMA_Init (void) snd_isdirect = true; if (snd_firsttime) - Con_SafePrintf ("DirectSound initialized\n"); + Con_Printf ("DirectSound initialized\n"); } else { snd_isdirect = false; - Con_SafePrintf ("DirectSound failed to init\n"); + Con_Printf ("DirectSound failed to init\n"); } } } @@ -579,9 +533,9 @@ SNDDMA_Init (void) if (snd_iswave) { if (snd_firsttime) - Con_SafePrintf ("Wave sound initialized\n"); + Con_Printf ("Wave sound initialized\n"); } else { - Con_SafePrintf ("Wave sound failed to init\n"); + Con_Printf ("Wave sound failed to init\n"); } } } @@ -590,7 +544,7 @@ SNDDMA_Init (void) if (!dsound_init && !wav_init) { if (snd_firsttime) - Con_SafePrintf ("No sound device initialized\n"); + Con_Printf ("No sound device initialized\n"); return 0; } @@ -599,24 +553,23 @@ SNDDMA_Init (void) } /* -============== -SNDDMA_GetDMAPos + SNDDMA_GetDMAPos -return the current sample position (in mono samples read) -inside the recirculating dma buffer, so the mixing code will know -how many sample are required to fill it up. -=============== + return the current sample position (in mono samples read) + inside the recirculating dma buffer, so the mixing code will know + how many sample are required to fill it up. */ int SNDDMA_GetDMAPos (void) { MMTIME mmtime; - int s; + int s = 0; DWORD dwWrite; if (dsound_init) { mmtime.wType = TIME_SAMPLES; - pDSBuf->lpVtbl->GetCurrentPosition (pDSBuf, &mmtime.u.sample, &dwWrite); + IDirectSoundBuffer_GetCurrentPosition (pDSBuf, &mmtime.u.sample, + &dwWrite); s = mmtime.u.sample - mmstarttime.u.sample; } else if (wav_init) { s = snd_sent * WAV_BUFFER_SIZE; @@ -631,11 +584,9 @@ SNDDMA_GetDMAPos (void) } /* -============== -SNDDMA_Submit + SNDDMA_Submit -Send sound to device if buffer isn't really the dma buffer -=============== + Send sound to device if buffer isn't really the dma buffer */ void SNDDMA_Submit (void) @@ -677,7 +628,7 @@ SNDDMA_Submit (void) wResult = waveOutWrite (hWaveOut, h, sizeof (WAVEHDR)); if (wResult != MMSYSERR_NOERROR) { - Con_SafePrintf ("Failed to write block to device\n"); + Con_Printf ("Failed to write block to device\n"); FreeSound (); return; } @@ -685,14 +636,90 @@ SNDDMA_Submit (void) } /* -============== -SNDDMA_Shutdown + SNDDMA_Shutdown -Reset the sound device for exiting -=============== + Reset the sound device for exiting */ void SNDDMA_Shutdown (void) { FreeSound (); } + +DWORD * +DSOUND_LockBuffer (qboolean lockit) +{ + int reps; + + static DWORD dwSize; + static DWORD dwSize2; + static DWORD *pbuf1; + static DWORD *pbuf2; + HRESULT hresult; + + if (!pDSBuf) + return NULL; + + if (lockit) { + reps = 0; + while ((hresult = IDirectSoundBuffer_Lock (pDSBuf, 0, gSndBufSize, + (LPVOID *) & pbuf1, &dwSize, + (LPVOID *) & pbuf2, &dwSize2, + 0)) != DS_OK) { + if (hresult != DSERR_BUFFERLOST) { + Con_Printf + ("S_TransferStereo16: DS::Lock Sound Buffer Failed\n"); + S_Shutdown (); + S_Startup (); + return NULL; + } + + if (++reps > 10000) { + Con_Printf + ("S_TransferStereo16: DS: couldn't restore buffer\n"); + S_Shutdown (); + S_Startup (); + return NULL; + } + } + } else { + IDirectSoundBuffer_Unlock (pDSBuf, pbuf1, dwSize, NULL, 0); + pbuf1 = NULL; + pbuf2 = NULL; + dwSize = 0; + dwSize2 = 0; + } + return (pbuf1); +} + +void +DSOUND_ClearBuffer (int clear) +{ + DWORD *pData; + +// FIXME: this should be called with 2nd pbuf2 = NULL, dwsize =0 + pData = DSOUND_LockBuffer (true); + memset (pData, clear, shm->samples * shm->samplebits / 8); + DSOUND_LockBuffer (false); +} + +void +DSOUND_Restore (void) +{ +// if the buffer was lost or stopped, restore it and/or restart it + DWORD dwStatus; + + if (!pDSBuf) + return; + + if (IDirectSoundBuffer_GetStatus (pDSBuf, &dwStatus) != DD_OK) + Con_Printf ("Couldn't get sound buffer status\n"); + + if (dwStatus & DSBSTATUS_BUFFERLOST) + IDirectSoundBuffer_Restore (pDSBuf); + + if (!(dwStatus & DSBSTATUS_PLAYING)) + IDirectSoundBuffer_Play (pDSBuf, 0, 0, DSBPLAY_LOOPING); + + return; +} diff --git a/qw/include/client.h b/qw/include/client.h index 80c194fc9..f4051ee6d 100644 --- a/qw/include/client.h +++ b/qw/include/client.h @@ -285,6 +285,7 @@ typedef struct char levelname[40]; // for display on solo scoreboard int playernum; + int viewentity; int stdver; // refresh related state diff --git a/qw/source/cl_parse.c b/qw/source/cl_parse.c index 9c52b4a0a..c52e3fd7d 100644 --- a/qw/source/cl_parse.c +++ b/qw/source/cl_parse.c @@ -656,6 +656,10 @@ CL_ParseServerData (void) cl.spectator = true; cl.playernum &= ~128; } + + // evil hack so NQ and QW can share sound code + cl.viewentity = cl.playernum + 1; + // get the full level name str = MSG_ReadString (net_message); strncpy (cl.levelname, str, sizeof (cl.levelname) - 1); diff --git a/qw/source/snd_alsa_0_5.c b/qw/source/snd_alsa_0_5.c index eac53d574..a59fdd9cc 100644 --- a/qw/source/snd_alsa_0_5.c +++ b/qw/source/snd_alsa_0_5.c @@ -54,8 +54,8 @@ #include -#include "console.h" -#include "qargs.h" +#include "QF/console.h" +#include "QF/qargs.h" #include "sound.h" #ifndef MAP_FAILED diff --git a/qw/source/snd_disk.c b/qw/source/snd_disk.c index 67da0aef8..e45b22d9a 100644 --- a/qw/source/snd_disk.c +++ b/qw/source/snd_disk.c @@ -39,9 +39,9 @@ #include #include -#include "console.h" +#include "QF/console.h" #include "sound.h" -#include "qargs.h" +#include "QF/qargs.h" static int snd_inited; QFile *snd_file; diff --git a/qw/source/snd_dma.c b/qw/source/snd_dma.c index 8007df824..a7ed5e66e 100644 --- a/qw/source/snd_dma.c +++ b/qw/source/snd_dma.c @@ -60,7 +60,7 @@ void S_StopAllSounds (qboolean clear); void S_StopAllSoundsC (void); // QuakeWorld hack... -#define viewentity playernum+1 +//#define viewentity playernum+1 // ======================================================================= // Internal sound data & structures diff --git a/qw/source/snd_null.c b/qw/source/snd_null.c index 45f533e19..34b09da93 100644 --- a/qw/source/snd_null.c +++ b/qw/source/snd_null.c @@ -29,7 +29,7 @@ $Id$ */ -#include "qtypes.h" +#include "QF/qtypes.h" #include "sound.h" // ======================================================================= diff --git a/qw/source/snd_oss.c b/qw/source/snd_oss.c index 4c2390bbf..03dd17335 100644 --- a/qw/source/snd_oss.c +++ b/qw/source/snd_oss.c @@ -55,9 +55,9 @@ # include #endif -#include "cmd.h" -#include "console.h" -#include "qargs.h" +#include "QF/cmd.h" +#include "QF/console.h" +#include "QF/qargs.h" #include "sound.h" #ifndef MAP_FAILED diff --git a/qw/source/snd_sdl.c b/qw/source/snd_sdl.c index 4bbdc1197..a33fc6ddb 100644 --- a/qw/source/snd_sdl.c +++ b/qw/source/snd_sdl.c @@ -33,9 +33,9 @@ #include #include -#include "cmd.h" -#include "console.h" -#include "qargs.h" +#include "QF/cmd.h" +#include "QF/console.h" +#include "QF/qargs.h" #include "sound.h" static dma_t the_shm; diff --git a/qw/source/snd_sgi.c b/qw/source/snd_sgi.c index 62cbadb0a..789388761 100644 --- a/qw/source/snd_sgi.c +++ b/qw/source/snd_sgi.c @@ -33,9 +33,9 @@ #include #include -#include "console.h" -#include "qtypes.h" -#include "qargs.h" +#include "QF/console.h" +#include "QF/qtypes.h" +#include "QF/qargs.h" #include "sound.h" static int snd_inited = 0; diff --git a/qw/source/snd_sun.c b/qw/source/snd_sun.c index 0d8e11193..2a2602b65 100644 --- a/qw/source/snd_sun.c +++ b/qw/source/snd_sun.c @@ -46,10 +46,10 @@ #include #include -#include "qtypes.h" +#include "QF/qtypes.h" #include "sound.h" -#include "qargs.h" -#include "console.h" +#include "QF/qargs.h" +#include "QF/console.h" int audio_fd; int snd_inited; diff --git a/qw/source/snd_win.c b/qw/source/snd_win.c index b91798a71..1b2bb086f 100644 --- a/qw/source/snd_win.c +++ b/qw/source/snd_win.c @@ -33,8 +33,8 @@ #define CINTERFACE #include "winquake.h" -#include "qargs.h" -#include "console.h" +#include "QF/qargs.h" +#include "QF/console.h" #include "sound.h" #define iDirectSoundCreate(a,b,c) pDirectSoundCreate(a,b,c)