shithub: qk2

Download patch

ref: e685f6329cbe2e8ec1bb5df5769e240b1ad06b98
parent: 0e1d3a54adfb5fae7edcba12b46ce3756ca464b9
author: qwx <>
date: Fri Jun 1 08:02:08 EDT 2018

snd: simplify somewhat

--- a/cl_cin.c
+++ b/cl_cin.c
@@ -168,7 +168,7 @@
 	if (cin.restart_sound)
 	{
 		cin.restart_sound = false;
-		CL_Snd_Restart_f ();
+		restartsnd();
 	}
 
 }
@@ -623,7 +623,7 @@
 	{
 		cin.restart_sound = true;
 		Cvar_SetValue ("s_khz", cin.s_rate/1000);
-		CL_Snd_Restart_f ();
+		restartsnd();
 		Cvar_SetValue ("s_khz", old_khz);
 	}
 
--- a/cl_main.c
+++ b/cl_main.c
@@ -1032,21 +1032,6 @@
 	Info_Print (Cvar_Userinfo());
 }
 
-/*
-=================
-CL_Snd_Restart_f
-
-Restart the sound subsystem so it can pick up
-new parameters and flush all sounds
-=================
-*/
-void CL_Snd_Restart_f (void)
-{
-	S_Shutdown ();
-	S_Init ();
-	CL_RegisterSounds ();
-}
-
 int precache_check; // for autodownload of precache items
 int precache_spawncount;
 int precache_tex;
@@ -1430,7 +1415,6 @@
 	Cmd_AddCommand ("skins", CL_Skins_f);
 
 	Cmd_AddCommand ("userinfo", CL_Userinfo_f);
-	Cmd_AddCommand ("snd_restart", CL_Snd_Restart_f);
 
 	Cmd_AddCommand ("changing", CL_Changing_f);
 	Cmd_AddCommand ("disconnect", CL_Disconnect_f);
@@ -1721,7 +1705,7 @@
 	// all archived variables will now be loaded
 
 	Con_Init ();
-	S_Init ();	
+	initsnd();	
 	VID_Init ();
 	
 	V_Init ();
@@ -1765,7 +1749,7 @@
 	CL_WriteConfiguration (); 
 
 	CDAudio_Shutdown ();
-	S_Shutdown();
+	shutsnd();
 	IN_Shutdown ();
 	VID_Shutdown();
 }
--- a/dat.h
+++ b/dat.h
@@ -2255,12 +2255,16 @@
 extern cvar_t *s_volume;
 extern cvar_t *s_loadas8bit;
 extern cvar_t *s_khz;
-extern cvar_t *s_show;
 extern cvar_t *s_mixahead;
-extern cvar_t *s_testsound;
-extern cvar_t *s_primary;
+extern int soundtime;
+extern int sound_started, num_sfx;
+extern sfx_t known_sfx[];
 
 enum{
+	Nsamp = 4096,
+	Sampsz = 2,
+	Rate = 44100,
+	Nsbuf = Sampsz * Nsamp,
 	MAX_CHANNELS = 32,
 	MAX_RAW_SAMPLES = 8192
 };
--- a/files.c
+++ b/files.c
@@ -555,11 +555,7 @@
 		fs_searchpaths = next;
 	}
 
-	//
-	// flush all data, so it will be forced to reload
-	//
-	if (dedicated && !dedicated->value)
-		Cbuf_AddText ("vid_restart\nsnd_restart\n");
+	restartsnd();
 
 	Com_sprintf (fs_gamedir, sizeof(fs_gamedir), "%s/%s", fs_basedir->string, dir);
 
--- a/fns.h
+++ b/fns.h
@@ -1,4 +1,8 @@
+void	shutsnd(void);
+void	restartsnd(void);
+void	initsnd(void);
 int	mkdir(char*);
+void*	emalloc(ulong);
 
 void	Qcommon_Init(int, char **);
 void	Qcommon_Frame(int);
@@ -239,7 +243,6 @@
 void	CL_Disconnect_f(void);
 void	CL_GetChallengePacket(void);
 void	CL_PingServers_f(void);
-void	CL_Snd_Restart_f(void);
 void	CL_RequestNextDownload(void);
 void	CL_InitInput(void);
 void	CL_SendCmd(void);
--- a/menu.c
+++ b/menu.c
@@ -1011,8 +1011,6 @@
 static menuslider_t		s_options_sfxvolume_slider;
 static menulist_t		s_options_joystick_box;
 static menulist_t		s_options_cdvolume_box;
-static menulist_t		s_options_quality_list;
-static menulist_t		s_options_compatibility_list;
 static menulist_t		s_options_console_action;
 
 static void CrosshairFunc( void * )
@@ -1061,7 +1059,6 @@
 {
 	s_options_sfxvolume_slider.curvalue		= Cvar_VariableValue( "s_volume" ) * 10;
 	s_options_cdvolume_box.curvalue 		= !Cvar_VariableValue("cd_nocd");
-	s_options_quality_list.curvalue			= !Cvar_VariableValue( "s_loadas8bit" );
 	s_options_sensitivity_slider.curvalue	= ( sensitivity->value ) * 2;
 
 	Cvar_SetValue( "cl_run", ClampCvar( 0, 1, cl_run->value ) );
@@ -1147,32 +1144,6 @@
 	cls.key_dest = key_console;
 }
 
-static void UpdateSoundQualityFunc( void * )
-{
-	if ( s_options_quality_list.curvalue )
-	{
-		Cvar_SetValue( "s_khz", 22 );
-		Cvar_SetValue( "s_loadas8bit", false );
-	}
-	else
-	{
-		Cvar_SetValue( "s_khz", 11 );
-		Cvar_SetValue( "s_loadas8bit", true );
-	}
-	
-	Cvar_SetValue( "s_primary", s_options_compatibility_list.curvalue );
-
-	M_DrawTextBox( 8, 120 - 48, 36, 3 );
-	M_Print( 16 + 16, 120 - 48 + 8,  "Restarting the sound system. This" );
-	M_Print( 16 + 16, 120 - 48 + 16, "could take up to a minute, so" );
-	M_Print( 16 + 16, 120 - 48 + 24, "please be patient." );
-
-	// the text box won't show up unless we do a buffer swap
-	re.EndFrame();
-
-	CL_Snd_Restart_f();
-}
-
 void Options_MenuInit( void )
 {
 	static char *cd_music_items[] =
@@ -1233,22 +1204,6 @@
 	s_options_cdvolume_box.itemnames		= cd_music_items;
 	s_options_cdvolume_box.curvalue 		= !Cvar_VariableValue("cd_nocd");
 
-	s_options_quality_list.generic.type	= MTYPE_SPINCONTROL;
-	s_options_quality_list.generic.x		= 0;
-	s_options_quality_list.generic.y		= 20;;
-	s_options_quality_list.generic.name		= "sound quality";
-	s_options_quality_list.generic.callback = UpdateSoundQualityFunc;
-	s_options_quality_list.itemnames		= quality_items;
-	s_options_quality_list.curvalue			= !Cvar_VariableValue( "s_loadas8bit" );
-
-	s_options_compatibility_list.generic.type	= MTYPE_SPINCONTROL;
-	s_options_compatibility_list.generic.x		= 0;
-	s_options_compatibility_list.generic.y		= 30;
-	s_options_compatibility_list.generic.name	= "sound compatibility";
-	s_options_compatibility_list.generic.callback = UpdateSoundQualityFunc;
-	s_options_compatibility_list.itemnames		= compatibility_items;
-	s_options_compatibility_list.curvalue		= Cvar_VariableValue( "s_primary" );
-
 	s_options_sensitivity_slider.generic.type	= MTYPE_SLIDER;
 	s_options_sensitivity_slider.generic.x		= 0;
 	s_options_sensitivity_slider.generic.y		= 50;
@@ -1335,8 +1290,6 @@
 
 	Menu_AddItem( &s_options_menu, ( void * ) &s_options_sfxvolume_slider );
 	Menu_AddItem( &s_options_menu, ( void * ) &s_options_cdvolume_box );
-	Menu_AddItem( &s_options_menu, ( void * ) &s_options_quality_list );
-	Menu_AddItem( &s_options_menu, ( void * ) &s_options_compatibility_list );
 	Menu_AddItem( &s_options_menu, ( void * ) &s_options_sensitivity_slider );
 	Menu_AddItem( &s_options_menu, ( void * ) &s_options_alwaysrun_box );
 	Menu_AddItem( &s_options_menu, ( void * ) &s_options_invertmouse_box );
--- a/snd.c
+++ b/snd.c
@@ -10,120 +10,113 @@
 static cvar_t *sndchannels;
 static cvar_t *snddev;
 
-static int afd, sndon, wpos;
+static int afd = -1, wpos;
 enum{
-	Nbuf	= 32
+	Nbuf = 4,
 };
 static Channel *schan;
 static QLock sndlock;
 
-
 static void
 sproc(void *)
 {
 	int n;
 
-	threadsetgrp(THsnd);
-
 	for(;;){
-		if(recv(schan, nil) < 0){
-			fprint(2, "sproc:recv %r\n");
+		if(recv(schan, nil) < 0)
 			break;
-		}
-		if((n = write(afd, dma.buffer, dma.samplebits/8 * dma.samples)) < 0){
-			fprint(2, "sproc:write %r\n");
+		if((n = write(afd, dma.buffer, Nsbuf)) != Nsbuf)
 			break;
-		}
 		qlock(&sndlock);
 		wpos += n;
 		qunlock(&sndlock);
 	}
-	fprint(2, "sproc %d: %r\n", threadpid(threadid()));
 }
 
-qboolean
-SNDDMA_Init(void)
-{
-	if(sndon)
-		return false;
-
-	if(COM_CheckParm("-nosound"))
-		return false;
-
-	if(snddev == nil){
-		sndbits = Cvar_Get("sndbits", "16", CVAR_ARCHIVE);
-		sndspeed = Cvar_Get("sndspeed", "44100", CVAR_ARCHIVE);
-		sndchannels = Cvar_Get("sndchannels", "2", CVAR_ARCHIVE);
-		snddev = Cvar_Get("snddev", "/dev/audio", CVAR_ARCHIVE);
-	}
-
-	if((afd = open(snddev->string, OWRITE)) < 0){
-		fprint(2, "SNDDMA_Init:open %r\n");
-		return false;
-	}
-
-	dma.samplebits = (int)sndbits->value;
-	if(dma.samplebits != 16 && dma.samplebits != 8)
-		dma.samplebits = 16;
-	dma.speed = (int)sndspeed->value;
-	if(dma.speed != 44100)
-		dma.speed = 44100;
-	dma.channels = (int)sndchannels->value;
-	if(dma.channels < 1 || dma.channels > 2)
-		dma.channels = 2;
-	dma.samples = 8192;
-	dma.submission_chunk = 1;
-	if((dma.buffer = mallocz(dma.samplebits/8 * dma.samples, 1)) == nil)
-		sysfatal("SNDDMA_Init:mallocz: %r\n");
-	dma.samplepos = 0;
-	sndon = 1;
-	wpos = 0;
-
-	schan = chancreate(sizeof(int), Nbuf);
-	if(proccreate(sproc, nil, 8192) < 0){
-		SNDDMA_Shutdown();
-		sysfatal("SNDDMA_Init:proccreate: %r\n");
-	}
-	return true;
-}
-
 int
 SNDDMA_GetDMAPos(void)
 {
-	if(!sndon)
+	if(afd < 0)
 		return 0;
 	qlock(&sndlock);
-	dma.samplepos = wpos / (dma.samplebits/8);
+	dma.samplepos = wpos / Sampsz;
 	qunlock(&sndlock);
 	return dma.samplepos;
 }
 
 void
-SNDDMA_Shutdown(void)
+SNDDMA_Submit(void)
 {
-	if(!sndon)
-		return;
+	if(nbsend(schan, nil) < 0){
+		fprint(2, "SNDDMA_Submit: %r\n");
+		shutsnd();
+	}
+}
 
-	threadkillgrp(THsnd);
+void
+SNDDMA_BeginPainting(void)
+{
+}
+
+void
+shutsnd(void)
+{
+	if(afd < 0)
+		return;
+	afd = -1;
 	close(afd);
-	if(schan != nil){
-		chanfree(schan);
-		schan = nil;
-	}
-	free(dma.buffer);
-	sndon = 0;
+	chanfree(schan);
+	sound_started = 0;
+	num_sfx = 0;
 }
 
 void
-SNDDMA_Submit(void)
+restartsnd(void)
 {
-	if(nbsend(schan, nil) < 0){
-		fprint(2, "SNDDMA_Submit:nbsend: %r\n");
-		SNDDMA_Shutdown();
+	sfx_t *s;
+
+	if(afd < 0)
+		return;
+	for(s=known_sfx+num_sfx; s<known_sfx+num_sfx*2; s++){
+		if(!s->name[0])
+			continue;
+		if(s->cache)
+			Z_Free(s->cache);
+		memset(s, 0, sizeof*s);
 	}
+	num_sfx = 0;
+	soundtime = 0;
+	paintedtime = 0;
+	S_StopAllSounds();
+	CL_RegisterSounds();
 }
 
 void
-SNDDMA_BeginPainting(void)
+initsnd(void)
 {
+	if((afd = open("/dev/audio", OWRITE)) < 0){
+		fprint(2, "initsnd %r\n");
+		return;
+	}
+	dma.samplebits = Sampsz * 8;
+	dma.speed = Rate;
+	dma.channels = 2;
+	dma.samples = Nsamp;
+	dma.submission_chunk = 1;
+	dma.buffer = emalloc(Nsamp * Sampsz);
+	dma.samplepos = 0;
+	wpos = 0;
+	schan = chancreate(sizeof(int), Nbuf);
+	if(proccreate(sproc, nil, 8192) < 0)
+		sysfatal("proccreate: %r\n");
+	s_volume = Cvar_Get("s_volume", "0.7", CVAR_ARCHIVE);
+	s_khz = Cvar_Get("s_khz", "22", CVAR_ARCHIVE);
+	s_loadas8bit = Cvar_Get("s_loadas8bit", "0", CVAR_ARCHIVE);
+	s_mixahead = Cvar_Get("s_mixahead", "0.2", CVAR_ARCHIVE);
+	S_InitScaletable();
+	sound_started = 1;
+	num_sfx = 0;
+	soundtime = 0;
+	paintedtime = 0;
+	S_StopAllSounds();
 }
--- a/snd_dma.c
+++ b/snd_dma.c
@@ -1,5 +1,3 @@
-// snd_dma.c -- main control for any streaming sound output device
-
 #include <u.h>
 #include <libc.h>
 #include <stdio.h>
@@ -6,10 +4,7 @@
 #include "dat.h"
 #include "fns.h"
 
-void S_Play(void);
-void S_SoundList(void);
 void S_Update_(void);
-void S_StopAllSounds(void);
 
 
 // =======================================================================
@@ -56,127 +51,15 @@
 int			s_beginofs;
 
 cvar_t		*s_volume;
-cvar_t		*s_testsound;
 cvar_t		*s_loadas8bit;
 cvar_t		*s_khz;
-cvar_t		*s_show;
 cvar_t		*s_mixahead;
-cvar_t		*s_primary;
 
 
 int		s_rawend;
 portable_samplepair_t	s_rawsamples[MAX_RAW_SAMPLES];
 
-
-// ====================================================================
-// User-setable variables
-// ====================================================================
-
-
-void S_SoundInfo_f(void)
-{
-	if (!sound_started)
-	{
-		Com_Printf ("sound system not started\n");
-		return;
-	}
-	
-    Com_Printf("%5d stereo\n", dma.channels - 1);
-    Com_Printf("%5d samples\n", dma.samples);
-    Com_Printf("%5d samplepos\n", dma.samplepos);
-    Com_Printf("%5d samplebits\n", dma.samplebits);
-    Com_Printf("%5d submission_chunk\n", dma.submission_chunk);
-    Com_Printf("%5d speed\n", dma.speed);
-    Com_Printf("0x%x dma buffer\n", dma.buffer);
-}
-
-
-
-/*
-================
-S_Init
-================
-*/
-void S_Init (void)
-{
-	cvar_t	*cv;
-
-	Com_Printf("\n------- sound initialization -------\n");
-
-	cv = Cvar_Get ("s_initsound", "1", 0);
-	if (!cv->value)
-		Com_Printf ("not initializing.\n");
-	else
-	{
-		s_volume = Cvar_Get ("s_volume", "0.7", CVAR_ARCHIVE);
-		s_khz = Cvar_Get ("s_khz", "11", CVAR_ARCHIVE);
-		s_loadas8bit = Cvar_Get ("s_loadas8bit", "1", CVAR_ARCHIVE);
-		s_mixahead = Cvar_Get ("s_mixahead", "0.2", CVAR_ARCHIVE);
-		s_show = Cvar_Get ("s_show", "0", 0);
-		s_testsound = Cvar_Get ("s_testsound", "0", 0);
-		s_primary = Cvar_Get ("s_primary", "0", CVAR_ARCHIVE);	// win32 specific
-
-		Cmd_AddCommand("play", S_Play);
-		Cmd_AddCommand("stopsound", S_StopAllSounds);
-		Cmd_AddCommand("soundlist", S_SoundList);
-		Cmd_AddCommand("soundinfo", S_SoundInfo_f);
-
-		if (!SNDDMA_Init())
-			return;
-
-		S_InitScaletable ();
-
-		sound_started = 1;
-		num_sfx = 0;
-
-		soundtime = 0;
-		paintedtime = 0;
-
-		Com_Printf ("sound sampling rate: %i\n", dma.speed);
-
-		S_StopAllSounds ();
-	}
-
-	Com_Printf("------------------------------------\n");
-}
-
-
 // =======================================================================
-// Shutdown sound engine
-// =======================================================================
-
-void S_Shutdown(void)
-{
-	int		i;
-	sfx_t	*sfx;
-
-	if (!sound_started)
-		return;
-
-	SNDDMA_Shutdown();
-
-	sound_started = 0;
-
-	Cmd_RemoveCommand("play");
-	Cmd_RemoveCommand("stopsound");
-	Cmd_RemoveCommand("soundlist");
-	Cmd_RemoveCommand("soundinfo");
-
-	// free all sounds
-	for (i=0, sfx=known_sfx ; i < num_sfx ; i++,sfx++)
-	{
-		if (!sfx->name[0])
-			continue;
-		if (sfx->cache)
-			Z_Free (sfx->cache);
-		memset (sfx, 0, sizeof(*sfx));
-	}
-
-	num_sfx = 0;
-}
-
-
-// =======================================================================
 // Load a sound
 // =======================================================================
 
@@ -536,8 +419,6 @@
 	channel_t	*ch;
 	sfxcache_t	*sc;
 
-	if (s_show->value)
-		Com_Printf ("Issue %i\n", ps->begin);
 	// pick a channel to play on
 	ch = S_PickChannel(ps->entnum, ps->entchannel);
 	if (!ch)
@@ -994,7 +875,6 @@
 void S_Update(vec3_t origin, vec3_t forward, vec3_t right, vec3_t up)
 {
 	int			i;
-	int			total;
 	channel_t	*ch;
 
 	if (!sound_started)
@@ -1040,23 +920,6 @@
 	// add loopsounds
 	S_AddLoopSounds ();
 
-	//
-	// debugging output
-	//
-	if (s_show->value)
-	{
-		total = 0;
-		ch = channels;
-		for (i=0 ; i<MAX_CHANNELS; i++, ch++)
-			if (ch->sfx && (ch->leftvol || ch->rightvol) )
-			{
-				Com_Printf ("%3i %3i %s\n", ch->leftvol, ch->rightvol, ch->sfx->name);
-				total++;
-			}
-		
-		Com_Printf ("----(%i)---- painted: %i\n", total, paintedtime);
-	}
-
 // mix some sound
 	S_Update_();
 }
@@ -1129,68 +992,3 @@
 
 	SNDDMA_Submit ();
 }
-
-/*
-===============================================================================
-
-console functions
-
-===============================================================================
-*/
-
-void S_Play(void)
-{
-	int 	i;
-	char name[256];
-	sfx_t	*sfx;
-	
-	i = 1;
-	while (i<Cmd_Argc())
-	{
-		if (!strrchr(Cmd_Argv(i), '.'))
-		{
-			strcpy(name, Cmd_Argv(i));
-			strcat(name, ".wav");
-		}
-		else
-			strcpy(name, Cmd_Argv(i));
-		sfx = S_RegisterSound(name);
-		S_StartSound(NULL, cl.playernum+1, 0, sfx, 1.0, 1.0, 0);
-		i++;
-	}
-}
-
-void S_SoundList(void)
-{
-	int		i;
-	sfx_t	*sfx;
-	sfxcache_t	*sc;
-	int		size, total;
-
-	total = 0;
-	for (sfx=known_sfx, i=0 ; i<num_sfx ; i++, sfx++)
-	{
-		if (!sfx->registration_sequence)
-			continue;
-		sc = sfx->cache;
-		if (sc)
-		{
-			size = sc->length*sc->width*(sc->stereo+1);
-			total += size;
-			if (sc->loopstart >= 0)
-				Com_Printf ("L");
-			else
-				Com_Printf (" ");
-			Com_Printf("(%2db) %6i : %s\n",sc->width*8,  size, sfx->name);
-		}
-		else
-		{
-			if (sfx->name[0] == '*')
-				Com_Printf("  placeholder : %s\n", sfx->name);
-			else
-				Com_Printf("  not loaded  : %s\n", sfx->name);
-		}
-	}
-	Com_Printf ("Total resident: %i\n", total);
-}
-
--- a/snd_mix.c
+++ b/snd_mix.c
@@ -85,18 +85,6 @@
 
 	pbuf = (unsigned long *)dma.buffer;
 
-	if (s_testsound->value)
-	{
-		int		i;
-		int		count;
-
-		// write a fixed sine wave
-		count = (endtime - paintedtime);
-		for (i=0 ; i<count ; i++)
-			paintbuffer[i].left = paintbuffer[i].right = sin((paintedtime+i)*0.1)*20000*256;
-	}
-
-
 	if (dma.samplebits == 16 && dma.channels == 2)
 	{	// optimized case
 		S_TransferStereo16 (pbuf, endtime);
--- a/sys.c
+++ b/sys.c
@@ -385,12 +385,23 @@
 		sysfatal("chancreate tchan: %r");
 }
 
+void *
+emalloc(ulong n)
+{
+	void *p;
+
+	if(p = mallocz(n, 1), p == nil)
+		sysfatal("emalloc %r");
+	setmalloctag(p, getcallerpc(&n));
+	return p;
+}
+
 void
 croak(void *, char *note)
 {
 	if(!strncmp(note, "sys:", 4)){
 		IN_Shutdown();
-		SNDDMA_Shutdown();
+		shutsnd();
 		NET_Shutdown();
 	}
 	noted(NDFLT);