r662 - trunk

black at icculus.org black at icculus.org
Mon Mar 6 18:01:25 EST 2006


Author: black
Date: 2006-03-06 18:01:25 -0500 (Mon, 06 Mar 2006)
New Revision: 662

Modified:
   trunk/lhsound.c
   trunk/lhsound.h
   trunk/sound.c
   trunk/system.c
   trunk/system.h
   trunk/todo
Log:
Some minor changes, mostly code reviewing and fixing possible bugs.


Modified: trunk/lhsound.c
===================================================================
--- trunk/lhsound.c	2006-03-06 18:46:24 UTC (rev 661)
+++ trunk/lhsound.c	2006-03-06 23:01:25 UTC (rev 662)
@@ -33,9 +33,9 @@
 lhsound_point_t;
 
 // FIXME: allocate someday
-#define LHSOUND_MAX_SAMPLES 65536
+#define LHSOUND_MAX_SAMPLES 65536U
 // FIXME: make configurable
-#define LHSOUND_MAX_MIXBUFFERLENGTH 8192
+#define LHSOUND_MAX_MIXBUFFERLENGTH 65535U
 
 static struct lhsound_state_s
 {
@@ -70,7 +70,7 @@
 	return n;
 }
 
-static void *lhsoundAllocateMemory(int size)
+static void *lhsoundAllocateMemory(unsigned int size)
 {
 	return malloc(size);
 }
@@ -80,14 +80,14 @@
 	free(memory);
 }
 
-static void lhsoundClearMemory(void *memory, int fill, int size)
+static void lhsoundClearMemory(void *memory, int fill, unsigned int size)
 {
 	memset(memory, fill, size);
 }
 
-static void lhsoundExpandSamplesArray(int number)
+static void lhsoundExpandSamplesArray(unsigned int number)
 {
-	// FIXME: this crashes if number is >= lhsound_state.maxsamples
+	LHSOUNDASSERT(number < lhsound_state.maxsamples, LHSOUND_ERROR_OUT_OF_MEMORY);
 	while (number >= lhsound_state.numsamples)
 	{
 		lhsoundClearMemory(&lhsound_state.samples[lhsound_state.numsamples], 0, sizeof(lhsound_sample_t));
@@ -111,24 +111,27 @@
 	}
 }
 
-void lhsoundGenSamples(int count, int *array)
+void lhsoundGenSamples(unsigned int count, unsigned int *array)
 {
-	int i;
+	unsigned int i;
 	LHSOUNDASSERT(count > 0, LHSOUND_ERROR_INVALID_VALUE);
 	// TODO: add a firstunused optimization
-	for (i = 1;count > 0;count--)
+	for (i = 1;i < LHSOUND_MAX_SAMPLES;i++)
 	{
-		LHSOUNDASSERT(i < LHSOUND_MAX_SAMPLES, LHSOUND_ERROR_OUT_OF_MEMORY);
 		lhsoundExpandSamplesArray(i);
 		if (!lhsound_state.samples[i].used)
 		{
 			lhsound_state.samples[i].used = LHSOUND_TRUE;
-			*array++ = i++;
+			*array++ = i;
+			if( --count == 0 ) {
+				return;
+			}
 		}
 	}
+	LHSOUNDERROR(LHSOUND_ERROR_OUT_OF_MEMORY);
 }
 
-void lhsoundDeleteSamples(unsigned int count, int *array)
+void lhsoundDeleteSamples(unsigned int count, unsigned int *array)
 {
 	unsigned int i;
 	for (i = 0;i < count;i++)
@@ -146,7 +149,7 @@
 	}
 }
 
-void lhsoundSample(unsigned int length, lhsound_format_t providedformat, const void *data, unsigned channelcount)
+void lhsoundSample(unsigned int length, lhsound_format_t providedformat, unsigned int channelcount, const void *data)
 {
 	unsigned int i;
 	unsigned int bytes;
@@ -258,7 +261,7 @@
 			const double *in;
 			float *out;
 			for (i = 0, in = data, out = lhsound_state.boundsample->data;i < length;i++, in+=channelcount, out++)
-				*out = *in;
+				*out = (float) *in;
 			out[0] = out[-1]; // for LINEAR resample
 		}
 		break;
@@ -276,6 +279,7 @@
 		{
 		case LHSOUND_REPEAT_CLAMP:
 		case LHSOUND_REPEAT_WRAP:
+		case LHSOUND_REPEAT_REPEATSECTION:
 			lhsound_state.boundsample->repeatmode = value;
 			break;
 		default:
@@ -284,6 +288,7 @@
 		}
 		break;
 	case LHSOUND_SAMPLE_REPEATPOSITION:
+		// FIXME: this should actually passed as clamped float since lhSoundPoint takes the time in the 0..1 range, too
 		LHSOUNDASSERT(value >= 0 && value < lhsound_state.boundsample->length, LHSOUND_ERROR_INVALID_VALUE);
 		lhsound_state.boundsample->repeatposition = value;
 		break;
@@ -428,8 +433,10 @@
 
 	lhsound_state.maxsamples = sizeof(lhsound_state.samples) / sizeof(lhsound_sample_t);
 	lhsound_state.numsamples = 1;
+
 	lhsound_state.boundsampleindex = 0;
 	lhsound_state.boundsample = lhsound_state.samples;
+	// init the default sample with our default states
 	lhsound_state.boundsample->used = LHSOUND_TRUE;
 	lhsound_state.boundsample->internalformat = LHSOUND_SIGNED_BYTE;
 	lhsound_state.boundsample->repeatmode = LHSOUND_REPEAT_CLAMP;
@@ -444,11 +451,15 @@
 
 	lhsound_state.errornum = LHSOUND_ERROR_NONE;
 
+	lhsound_state.mixbeginstate = LHSOUND_FALSE;
 	lhsound_state.mixbufferlength = 0;
 }
 
 void lhsoundShutdown(void)
 {
+	if( lhsound_state.mixbuffer != LHSOUND_NULL )
+		lhsoundFreeMemory(lhsound_state.mixbuffer);
+
 	while (lhsound_state.numsamples > 0)
 	{
 		int number = --lhsound_state.numsamples;
@@ -466,25 +477,28 @@
 	}
 }
 
-void lhsoundMixBegin(int bufferlength)
+void lhsoundMixBegin(unsigned int bufferlength)
 {
 	LHSOUNDASSERT(lhsound_state.mixbeginstate == LHSOUND_FALSE, LHSOUND_ERROR_INVALID_OPERATION);
-	LHSOUNDASSERT(bufferlength >= 1 && bufferlength < 65536, LHSOUND_ERROR_INVALID_VALUE);
+	LHSOUNDASSERT(bufferlength >= 1 && bufferlength < LHSOUND_MAX_MIXBUFFERLENGTH, LHSOUND_ERROR_INVALID_VALUE);
 	lhsound_state.mixbeginstate = LHSOUND_TRUE;
 	if (lhsound_state.mixbufferlength != bufferlength)
 	{
+		// free the old buffer (if existing)
+		if (lhsound_state.mixbuffer != LHSOUND_NULL)
+			lhsoundFreeMemory(lhsound_state.mixbuffer);
+
 		lhsound_state.mixbufferlength = bufferlength;
 		lhsound_state.mixbuffer = lhsoundAllocateMemory(lhsound_state.mixbufferlength * sizeof(*lhsound_state.mixbuffer));
 	}
 	lhsoundClearMemory(lhsound_state.mixbuffer, 0, lhsound_state.mixbufferlength * sizeof(*lhsound_state.mixbuffer));
 }
 
-void lhsoundMixEnd(lhsound_format_t format, size_t stride, void *output, int bufferlength)
+void lhsoundMixEnd(unsigned int bufferlength, lhsound_format_t format, unsigned int stride, void *output)
 {
-	int i;
-	int ival;
-	float fval;
+	unsigned int i;
 	unsigned char *out;
+	// the swaptest is fast even if its bloats the code a bit, so lets keep it here..
 	int swap;
 	union swaptest_u
 	{
@@ -496,6 +510,7 @@
 	LHSOUNDASSERT(bufferlength == lhsound_state.mixbufferlength, LHSOUND_ERROR_INVALID_VALUE);
 	LHSOUNDASSERT(stride != 0, LHSOUND_ERROR_INVALID_VALUE);
 	lhsound_state.mixbeginstate = LHSOUND_FALSE;
+	
 	swaptest.s = 1;
 	switch (format)
 	{
@@ -527,6 +542,7 @@
 	case LHSOUND_UNSIGNED_BYTE:
 		for (i = 0;i < bufferlength;i++, out += stride)
 		{
+			int ival;
 			ival = (int)floor(lhsound_state.mixbuffer[i] * 127.5f + 128.0f);
 			ival = ival >= 0 ? (ival <= 255 ? ival : 255) : 0;
 			*((unsigned char *)out) = (unsigned char)ival;
@@ -535,6 +551,7 @@
 	case LHSOUND_SIGNED_BYTE:
 		for (i = 0;i < bufferlength;i++, out += stride)
 		{
+			int ival;
 			ival = (int)floor(lhsound_state.mixbuffer[i] * 127.5f);
 			ival = ival >= -128 ? (ival <= 127 ? ival : 127) : -128;
 			*((signed char *)out) = (signed char)ival;
@@ -543,6 +560,7 @@
 	case LHSOUND_UNSIGNED_SHORT:
 		for (i = 0;i < bufferlength;i++, out += stride)
 		{
+			int ival;
 			ival = (int)floor(lhsound_state.mixbuffer[i] * 32767.5f + 32768.0f);
 			ival = ival >= 0 ? (ival <= 65535 ? ival : 65535) : 0;
 			*((unsigned short *)out) = (unsigned short)ival;
@@ -551,6 +569,7 @@
 	case LHSOUND_SIGNED_SHORT:
 		for (i = 0;i < bufferlength;i++, out += stride)
 		{
+			int ival;
 			ival = (int)floor(lhsound_state.mixbuffer[i] * 32767.5f);
 			ival = ival >= -32768 ? (ival <= 32767 ? ival : 32767) : -32768;
 			*((signed short *)out) = (signed short)ival;
@@ -559,6 +578,7 @@
 	case LHSOUND_UNSIGNED_INT:
 		for (i = 0;i < bufferlength;i++, out += stride)
 		{
+			float fval;
 			fval = lhsound_state.mixbuffer[i];
 			fval = fval >= -1.0f ? (fval <= 1.0f ? fval : 1.0f) : -1.0f;
 			*((unsigned int *)out) = (unsigned int)floor(fval * (65536.0 * 32768.0 - 0.5) + (65536.0 * 32768.0));
@@ -567,6 +587,7 @@
 	case LHSOUND_SIGNED_INT:
 		for (i = 0;i < bufferlength;i++, out += stride)
 		{
+			float fval;
 			fval = lhsound_state.mixbuffer[i];
 			fval = fval >= -1.0f ? (fval <= 1.0f ? fval : 1.0f) : -1.0f;
 			*((signed int *)out) = (signed int)floor(fval * (65536.0 * 32768.0 - 0.5));
@@ -582,7 +603,6 @@
 	}
 	if (swap)
 	{
-		unsigned char a, b;
 		out = (unsigned char *)output;
 		switch(format)
 		{
@@ -590,6 +610,8 @@
 		case LHSOUND_SIGNED_SHORT:
 			for (i = 0;i < bufferlength;i++, out += stride)
 			{
+				unsigned char a;
+
 				a = out[0];
 				out[0] = out[1];
 				out[1] = a;
@@ -600,6 +622,8 @@
 		case LHSOUND_FLOAT:
 			for (i = 0;i < bufferlength;i++, out += stride)
 			{
+				unsigned char a, b;
+
 				a = out[0];
 				b = out[1];
 				out[0] = out[3];

Modified: trunk/lhsound.h
===================================================================
--- trunk/lhsound.h	2006-03-06 18:46:24 UTC (rev 661)
+++ trunk/lhsound.h	2006-03-06 23:01:25 UTC (rev 662)
@@ -103,12 +103,12 @@
 void lhsoundGeti(lhsound_queryname_t pname, int *output);
 
 void lhsoundBindSample(unsigned int number);
-void lhsoundGenSamples(int count, int *array);
-void lhsoundDeleteSamples(unsigned int count, int *array);
+void lhsoundGenSamples(unsigned int count, unsigned int *array);
+void lhsoundDeleteSamples(unsigned int count, unsigned int *array);
 
 // expects the data to be tightly packed, channelcount specifies the stride (actual stride = sizeof(providedformat)*channelcount)
 // note that a sample can only contain one channel, so you have to create a sample for each channel of e.g. a stereo wave
-void lhsoundSample(unsigned int length, lhsound_format_t providedformat, const void *data, unsigned channelcount);
+void lhsoundSample(unsigned int length, lhsound_format_t providedformat, unsigned int channelcount, const void *data);
 void lhsoundSampleParami(lhsound_sampleparam_t parameter, int value);
 
 void lhsoundBegin(lhsound_rendermode_t mode);
@@ -118,7 +118,7 @@
 void lhsoundInit(void);
 void lhsoundShutdown(void);
 
-void lhsoundMixBegin(int bufferlength);
-void lhsoundMixEnd(lhsound_format_t format, size_t stride, void *output, int bufferlength);
+void lhsoundMixBegin(unsigned int bufferlength);
+void lhsoundMixEnd(unsigned int bufferlength, lhsound_format_t format, unsigned int stride, void *output);
 
 #endif

Modified: trunk/sound.c
===================================================================
--- trunk/sound.c	2006-03-06 18:46:24 UTC (rev 661)
+++ trunk/sound.c	2006-03-06 23:01:25 UTC (rev 662)
@@ -215,7 +215,7 @@
 		void *channelStart = (NUint8*) info.data + channel * info.bytespersample; 
 
 		lhsoundBindSample(samplenumber[channel]);
-		lhsoundSample(info.length, info.lhsoundformat, channelStart, info.channels);
+		lhsoundSample(info.length, info.lhsoundformat, info.channels, channelStart);
 		// TODO: add mipmaps and use LHSOUND_LINEAR_MIPMAP_LINEAR
 		lhsoundSampleParami(LHSOUND_SAMPLE_MINFILTER, LHSOUND_LINEAR);
 		lhsoundSampleParami(LHSOUND_SAMPLE_MAGFILTER, LHSOUND_LINEAR);

Modified: trunk/system.c
===================================================================
--- trunk/system.c	2006-03-06 18:46:24 UTC (rev 661)
+++ trunk/system.c	2006-03-06 23:01:25 UTC (rev 662)
@@ -102,14 +102,14 @@
 
 static void Audio_Callback(void *userdata, NUint8 *stream, int len)
 {
-	int speakerindex;
-	int bufferlength = len / Audio.bytespersampleframe;
+	NUint speakerindex;
+	NUint bufferlength = len / Audio.bytesperframe;
 	Audio.callbackcount++;
 	for (speakerindex = 0;speakerindex < Audio.speakers;speakerindex++)
 	{
 		lhsoundMixBegin(bufferlength);
 		G_AudioMix(bufferlength);
-		lhsoundMixEnd(Audio.lhsoundformat, Audio.bytespersampleframe, (void *)(stream + speakerindex * Audio.bytespersample), bufferlength);
+		lhsoundMixEnd(bufferlength, Audio.lhsoundformat, Audio.bytesperframe, (void *)(stream + speakerindex * Audio.bytespersample));
 	}
 }
 
@@ -159,7 +159,7 @@
 		Audio.speakers = speakers;
 		Audio.lhsoundformat = LHSOUND_SIGNED_SHORT;
 		Audio.bytespersample = sizeof(short);
-		Audio.bytespersampleframe = speakers * Audio.bytespersample;
+		Audio.bytesperframe = speakers * Audio.bytespersample;
 		SDL_PauseAudio(false);
 	}
 	else
@@ -176,6 +176,16 @@
 	Audio.framecount++;
 }
 
+static void Audio_Init(void)
+{
+	lhsoundInit();
+}
+
+static void Audio_Quit(void)
+{
+	lhsoundShutdown();
+}
+
 // input section
 
 void Input_SetMode(Nbool grabmouse)
@@ -316,6 +326,7 @@
 	File_Init();
 	File_Start(".", "~/.darkwar/");
 	File_AddPath("base/");
+	Audio_Init();
 	// FIXME: check system memory to decide how much resource memory to use
 	// 128mb memory for resources
 	Resource_Init(128 * (1 << 20));
@@ -323,7 +334,7 @@
 	G_Main();
 	Util_Quit();
 	Resource_Quit();
-	S_Quit();
+	Audio_Quit();
 	R_Quit();
 	File_Quit();
 	DynGL_CloseLibrary ();

Modified: trunk/system.h
===================================================================
--- trunk/system.h	2006-03-06 18:46:24 UTC (rev 661)
+++ trunk/system.h	2006-03-06 23:01:25 UTC (rev 662)
@@ -30,7 +30,7 @@
 	NUint speakers;
 	NUint lhsoundformat;
 	NUint bytespersample;
-	NUint bytespersampleframe;
+	NUint bytesperframe;
 }
 AudioState;
 

Modified: trunk/todo
===================================================================
--- trunk/todo	2006-03-06 18:46:24 UTC (rev 661)
+++ trunk/todo	2006-03-06 23:01:25 UTC (rev 662)
@@ -7,6 +7,7 @@
 0 feature darkwar console: add mousewheel scrolling of console
 0 feature darkwar console: condump cvar to dump console history (Kazashi)
 0 feature darkwar input: add a mouse inversion cvar (Sajt)
+0 fixme darkwar sound: figure out whether we need s_main.c/h at all and what do with Audio_Init/Shutdown
 1 change darkwar filesystem: use shGetSpecialFolderPath with CSIDL_PERSONAL to get the My Documents directory path, and use My Documents/DarkWar as the user path (Black)
 2 feature darkwar console: console_logfile cvar (Kazashi)
 4 change darkwar drawview: redesign G_DrawView to allow different scope flags on each surface of an entity




More information about the neither-commits mailing list