koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
authornemo
Thu, 25 Jun 2009 10:49:05 +0000
changeset 2194 1597710c6118
parent 2193 4d5d78aa7ca4
child 2195 4ae585fc7183
koda adds threading for fadein/out. Untested under windows, but works beautifully under Linux (and presumably OSX, right koda?)
QTfrontend/SDLs.h
QTfrontend/pages.cpp
cmake_modules/FindOggVorbis.cmake
gameServer/OfficialServer/extdbinterface.hs
hedgewars/uSound.pas
openalbridge/CMakeLists.txt
openalbridge/openalwrap.c
openalbridge/openalwrap.h
openalbridge/wrappers.c
--- a/QTfrontend/SDLs.h	Wed Jun 24 23:53:02 2009 +0000
+++ b/QTfrontend/SDLs.h	Thu Jun 25 10:49:05 2009 +0000
@@ -21,8 +21,6 @@
 
 #include <QStringList>
 
-//#include "SDL_mixer.h"
-
 extern	"C" int		openal_init			(int);
 extern	"C" int		openal_close		(void);
 extern	"C" int		openal_loadfile		(const char*);
--- a/QTfrontend/pages.cpp	Wed Jun 24 23:53:02 2009 +0000
+++ b/QTfrontend/pages.cpp	Thu Jun 25 10:49:05 2009 +0000
@@ -286,18 +286,16 @@
 
 void PageEditTeam::testSound()
 {
+	int sound;
 	QDir tmpdir;
 	tmpdir.cd(datadir->absolutePath());
 	tmpdir.cd("Sounds/voices");
 	tmpdir.cd(CBVoicepack->currentText());
 	QStringList list = tmpdir.entryList(QStringList() << "Illgetyou.ogg" << "Incoming.ogg" << "Stupid.ogg" << "Coward.ogg" << "Firstblood.ogg", QDir::Files);
 	if (list.size()) {
-	//	printf("%s\n", QString(tmpdir.absolutePath() + "/" + list[rand() % list.size()]).toLocal8Bit().constData());
-		int tmp =openal_loadfile(QString(tmpdir.absolutePath() + "/" + list[rand() % list.size()]).toLocal8Bit().constData());
-		openal_playsound(tmp);										   
+		sound = openal_loadfile(QString(tmpdir.absolutePath() + "/" + list[rand() % list.size()]).toLocal8Bit().constData());
+		openal_playsound(sound);										   
 	}
-		
-		//QSound::play(tmpdir.absolutePath() + "/" + list[rand() % list.size()]);
 }
 
 PageMultiplayer::PageMultiplayer(QWidget* parent) :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cmake_modules/FindOggVorbis.cmake	Thu Jun 25 10:49:05 2009 +0000
@@ -0,0 +1,90 @@
+# - Try to find the OggVorbis libraries
+# Once done this will define
+#
+#  OGGVORBIS_FOUND - system has OggVorbis
+#  OGGVORBIS_VERSION - set either to 1 or 2
+#  OGGVORBIS_INCLUDE_DIR - the OggVorbis include directory
+#  OGGVORBIS_LIBRARIES - The libraries needed to use OggVorbis
+#  OGG_LIBRARY         - The Ogg library
+#  VORBIS_LIBRARY      - The Vorbis library
+#  VORBISFILE_LIBRARY  - The VorbisFile library
+#  VORBISENC_LIBRARY   - The VorbisEnc library
+
+# Copyright (c) 2006, Richard Laerkaeng, <richard@goteborg.utfors.se>
+#
+# Redistribution and use is allowed according to the terms of the BSD license.
+# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+
+
+include (CheckLibraryExists)
+
+find_path(VORBIS_INCLUDE_DIR vorbis/vorbisfile.h)
+find_path(OGG_INCLUDE_DIR ogg/ogg.h)
+
+find_library(OGG_LIBRARY NAMES ogg)
+find_library(VORBIS_LIBRARY NAMES vorbis)
+find_library(VORBISFILE_LIBRARY NAMES vorbisfile)
+find_library(VORBISENC_LIBRARY NAMES vorbisenc)
+
+mark_as_advanced(VORBIS_INCLUDE_DIR OGG_INCLUDE_DIR
+                 OGG_LIBRARY VORBIS_LIBRARY VORBISFILE_LIBRARY VORBISENC_LIBRARY)
+
+
+if (VORBIS_INCLUDE_DIR AND VORBIS_LIBRARY AND VORBISFILE_LIBRARY AND VORBISENC_LIBRARY)
+   set(OGGVORBIS_FOUND TRUE)
+
+   set(OGGVORBIS_LIBRARIES ${OGG_LIBRARY} ${VORBIS_LIBRARY} ${VORBISFILE_LIBRARY} ${VORBISENC_LIBRARY})
+
+   set(_CMAKE_REQUIRED_LIBRARIES_TMP ${CMAKE_REQUIRED_LIBRARIES})
+   set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${OGGVORBIS_LIBRARIES})
+   check_library_exists(vorbis vorbis_bitrate_addblock "" HAVE_LIBVORBISENC2)
+   set(CMAKE_REQUIRED_LIBRARIES ${_CMAKE_REQUIRED_LIBRARIES_TMP})
+
+   if (HAVE_LIBVORBISENC2)
+      set (OGGVORBIS_VERSION 2)
+   else (HAVE_LIBVORBISENC2)
+      set (OGGVORBIS_VERSION 1)
+   endif (HAVE_LIBVORBISENC2)
+
+else (VORBIS_INCLUDE_DIR AND VORBIS_LIBRARY AND VORBISFILE_LIBRARY AND VORBISENC_LIBRARY)
+   set (OGGVORBIS_VERSION)
+   set(OGGVORBIS_FOUND FALSE)
+endif (VORBIS_INCLUDE_DIR AND VORBIS_LIBRARY AND VORBISFILE_LIBRARY AND VORBISENC_LIBRARY)
+
+
+if (OGGVORBIS_FOUND)
+   if (NOT OggVorbis_FIND_QUIETLY)
+      message(STATUS "Found OggVorbis: ${OGGVORBIS_LIBRARIES}")
+   endif (NOT OggVorbis_FIND_QUIETLY)
+else (OGGVORBIS_FOUND)
+   if (OggVorbis_FIND_REQUIRED)
+      message(FATAL_ERROR "Could NOT find OggVorbis libraries")
+   endif (OggVorbis_FIND_REQUIRED)
+   if (NOT OggVorbis_FIND_QUITELY)
+      message(STATUS "Could NOT find OggVorbis libraries")
+   endif (NOT OggVorbis_FIND_QUITELY)
+endif (OGGVORBIS_FOUND)
+
+#check_include_files(vorbis/vorbisfile.h HAVE_VORBISFILE_H)
+#check_library_exists(ogg ogg_page_version "" HAVE_LIBOGG)
+#check_library_exists(vorbis vorbis_info_init "" HAVE_LIBVORBIS)
+#check_library_exists(vorbisfile ov_open "" HAVE_LIBVORBISFILE)
+#check_library_exists(vorbisenc vorbis_info_clear "" HAVE_LIBVORBISENC)
+#check_library_exists(vorbis vorbis_bitrate_addblock "" HAVE_LIBVORBISENC2)
+
+#if (HAVE_LIBOGG AND HAVE_VORBISFILE_H AND HAVE_LIBVORBIS AND HAVE_LIBVORBISFILE AND HAVE_LIBVORBISENC)
+#    message(STATUS "Ogg/Vorbis found")
+#    set (VORBIS_LIBS "-lvorbis -logg")
+#    set (VORBISFILE_LIBS "-lvorbisfile")
+#    set (VORBISENC_LIBS "-lvorbisenc")
+#    set (OGGVORBIS_FOUND TRUE)
+#    if (HAVE_LIBVORBISENC2)
+#        set (HAVE_VORBIS 2)
+#    else (HAVE_LIBVORBISENC2)
+#        set (HAVE_VORBIS 1)
+#    endif (HAVE_LIBVORBISENC2)
+#else (HAVE_LIBOGG AND HAVE_VORBISFILE_H AND HAVE_LIBVORBIS AND HAVE_LIBVORBISFILE AND HAVE_LIBVORBISENC)
+#    message(STATUS "Ogg/Vorbis not found")
+#endif (HAVE_LIBOGG AND HAVE_VORBISFILE_H AND HAVE_LIBVORBIS AND HAVE_LIBVORBISFILE AND HAVE_LIBVORBISENC)
+
+
--- a/gameServer/OfficialServer/extdbinterface.hs	Wed Jun 24 23:53:02 2009 +0000
+++ b/gameServer/OfficialServer/extdbinterface.hs	Thu Jun 25 10:49:05 2009 +0000
@@ -48,7 +48,7 @@
 	Control.Exception.handle (\e -> return ()) $ handleSqlError $
 		bracket
 			(connectMySQL mySQLConnectionInfo)
-			(disconnect)
+            (return . const)
 			(dbInteractionLoop)
 
 
--- a/hedgewars/uSound.pas	Wed Jun 24 23:53:02 2009 +0000
+++ b/hedgewars/uSound.pas	Thu Jun 25 10:49:05 2009 +0000
@@ -59,8 +59,8 @@
 function openal_loadfile	(filename: PChar)			: LongInt; cdecl; external OpenALBridge;
 function openal_toggleloop	(index: LongInt)			: boolean; cdecl; external OpenALBridge;
 function openal_setvolume	(index: LongInt; percentage: byte)	: boolean; cdecl; external OpenALBridge; 
-function openal_fadeout		(index: LongInt; quantity: byte)	: boolean; cdecl; external OpenALBridge;
-function openal_fadein		(index: LongInt; quantity: byte)	: boolean; cdecl; external OpenALBridge;
+function openal_fadeout		(index: LongInt; quantity: LongInt)	: boolean; cdecl; external OpenALBridge;
+function openal_fadein		(index: LongInt; quantity: LongInt)	: boolean; cdecl; external OpenALBridge;
 function openal_playsound	(index: LongInt)			: boolean; cdecl; external OpenALBridge;
 function openal_pausesound	(index: LongInt)			: boolean; cdecl; external OpenALBridge;
 function openal_stopsound	(index: LongInt)			: boolean; cdecl; external OpenALBridge;
--- a/openalbridge/CMakeLists.txt	Wed Jun 24 23:53:02 2009 +0000
+++ b/openalbridge/CMakeLists.txt	Thu Jun 25 10:49:05 2009 +0000
@@ -3,21 +3,21 @@
 
 #add_library (openalbridge openalwrap.c loaders.c endianness.c wrappers.c)
 exec_program(${CMAKE_C_COMPILER}
-			ARGS ${arch_to_build} -o ${EXECUTABLE_OUTPUT_PATH}/loaders.o -c ${hedgewars_SOURCE_DIR}/openalbridge/loaders.c -I ${OPENAL_INCLUDE_DIR} -O2
+			ARGS ${arch_to_build} -o ${EXECUTABLE_OUTPUT_PATH}/loaders.o -c ${hedgewars_SOURCE_DIR}/openalbridge/loaders.c -I ${OPENAL_INCLUDE_DIR} -O2 -w
 			OUTPUT_VARIABLE noout
 )
 exec_program(${CMAKE_C_COMPILER}
-			ARGS ${arch_to_build} -o ${EXECUTABLE_OUTPUT_PATH}/endianness.o -c ${hedgewars_SOURCE_DIR}/openalbridge/endianness.c -I ${OPENAL_INCLUDE_DIR} -O2 
+			ARGS ${arch_to_build} -o ${EXECUTABLE_OUTPUT_PATH}/endianness.o -c ${hedgewars_SOURCE_DIR}/openalbridge/endianness.c -I ${OPENAL_INCLUDE_DIR} -O2 -w
 	OUTPUT_VARIABLE noout
 )
 
 exec_program(${CMAKE_C_COMPILER}
-			ARGS ${arch_to_build} -o ${EXECUTABLE_OUTPUT_PATH}/openalwrap.o -c ${hedgewars_SOURCE_DIR}/openalbridge/openalwrap.c -I ${OPENAL_INCLUDE_DIR} -O2 
+			ARGS ${arch_to_build} -o ${EXECUTABLE_OUTPUT_PATH}/openalwrap.o -c ${hedgewars_SOURCE_DIR}/openalbridge/openalwrap.c -I ${OPENAL_INCLUDE_DIR} -O2 -w
 			OUTPUT_VARIABLE noout
 )
 
 exec_program(${CMAKE_C_COMPILER}
-			ARGS ${arch_to_build} -o ${EXECUTABLE_OUTPUT_PATH}/wrappers.o -c ${hedgewars_SOURCE_DIR}/openalbridge/wrappers.c -I ${OPENAL_INCLUDE_DIR} -O2 
+			ARGS ${arch_to_build} -o ${EXECUTABLE_OUTPUT_PATH}/wrappers.o -c ${hedgewars_SOURCE_DIR}/openalbridge/wrappers.c -I ${OPENAL_INCLUDE_DIR} -O2 -w
 			OUTPUT_VARIABLE noout
 			)
 		exec_program(${CMAKE_AR}
--- a/openalbridge/openalwrap.c	Wed Jun 24 23:53:02 2009 +0000
+++ b/openalbridge/openalwrap.c	Thu Jun 25 10:49:05 2009 +0000
@@ -16,19 +16,16 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include "al.h"
-#include "alc.h"
 #include "openalwrap.h"
-#include "loaders.h"
-#include "wrappers.h"
-#include "endianness.h"
 
 #ifdef __CPLUSPLUS
 extern "C" {
 #endif 
+
+	typedef struct _fade_t {
+		int index;
+		unsigned int quantity;
+	} fade_t;
 	
 	// Sources are points emitting sound.
 	ALuint *Sources;
@@ -275,60 +272,135 @@
 		return AL_TRUE;
 	}
 	
+#ifndef _WIN32
+	void *helper_fadeout(void* tmp) {
+#else
+	VOID WINAPI helper_fadeout(LPVOID tmp) {	
+#endif
+		ALfloat gain;
+		fade_t *fade;
+		int index; 
+		unsigned int quantity; 
+		
+		fade = tmp;
+		index = fade->index;
+		quantity = fade->quantity;
+		
+		alGetSourcef(Sources[index], AL_GAIN, &gain);
+		
+		for ( ; gain >= 0.00f; gain -= (float) quantity/10000){
+#ifdef DEBUG
+			fprintf(stderr, "Fade-out: Set gain to %f\n", gain);
+#endif
+			alSourcef(Sources[index], AL_GAIN, gain);
+			usleep(10000);
+		}
+		
+		AlGetError("ERROR %d: Setting fade out volume\n");
+		
+		//stop that sound and reset its gain
+		alSourceStop (Sources[index]);
+		alSourcef (Sources[index], AL_GAIN, 1.0f);	
+		
+#ifndef _WIN32
+		pthread_exit(NULL);
+#else
+		ThreadExit();
+#endif
+	}
 	
 	ALint openal_fadeout(int index, unsigned int quantity) {
-		ALfloat gain;
+#ifndef _WIN32
+		pthread_t thread;
+#else
+		HANDLE Thread;
+		DWORD threadID;
+#endif
+		fade_t fade;
 		
 		if (index >= globalindex) {
 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)", index, globalindex);
 			return AL_FALSE;
 		}
 		
-		alGetSourcef(Sources[index], AL_GAIN, &gain);
-
-		for ( ; gain >= 0.00f; gain -= (float) quantity/10000){
-#ifdef DEBUG
-			fprintf(stderr, "Fade-out: Set gain to: %f\n", gain);
+		fade.index = index;
+		fade.quantity = quantity;
+		
+#ifndef _WIN32
+		pthread_create(&thread, NULL, helper_fadeout, (void*) &fade);
+		pthread_detach(thread);
+#else
+		Thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) helper_fadeout, (void*) &fade, 0, threadID);
+		CloseHandle(Thread);
 #endif
-			alSourcef(Sources[index], AL_GAIN, gain);
-			usleep(10000);
-		}
 		
-		if (AlGetError("ERROR %d: Setting fade out volume\n") != AL_TRUE)
-			return AL_FALSE;
-		
-		//stop that sound and reset its gain
-		alSourceStop (Sources[index]);
-		alSourcef (Sources[index], AL_GAIN, 1.0f);
-
 		alGetError();  /* clear any AL errors beforehand */
 
 		return AL_TRUE;
 	}
-	
-	
+		
+#ifndef _WIN32
+		void *helper_fadein(void* tmp) 
+#else
+		VOID WINAPI helper_fadein(LPVOID tmp) 
+#endif
+		{
+			ALfloat gain;
+			fade_t *fade;
+			int index; 
+			unsigned int quantity; 
+			
+			fade = tmp;
+			index = fade->index;
+			quantity = fade->quantity;
+			
+			gain = 0.0f;
+			alSourcef(Sources[index], AL_GAIN, gain);
+			alSourcePlay(Sources[index]);
+			
+			for ( ; gain <= 1.00f; gain += (float) quantity/10000){
+#ifdef DEBUG
+				fprintf(stderr, "Fade-in: Set gain to: %f\n", gain);
+#endif
+				alSourcef(Sources[index], AL_GAIN, gain);
+				usleep(10000);
+			}
+			
+			if (AlGetError("ERROR %d: Setting fade in volume\n") != AL_TRUE)
+				return AL_FALSE;
+			
+#ifndef _WIN32
+			pthread_exit(NULL);
+#else
+			ThreadExit();
+#endif
+		}
+			
+		
 	ALint openal_fadein(int index, unsigned int quantity) {
-		ALfloat gain;
+#ifndef _WIN32
+		pthread_t thread;
+#else
+		HANDLE Thread;
+		DWORD threadID;
+#endif
+		fade_t fade;
 		
 		if (index >= globalindex) {
 			fprintf(stderr, "ERROR: index out of bounds (got %d, max %d)", index, globalindex);
 			return AL_FALSE;
 		}
 		
-		gain = 0.0f;
-		alSourcef(Sources[index], AL_GAIN, gain);
-		alSourcePlay(Sources[index]);
+		fade.index = index;
+		fade.quantity = quantity;
 		
-		for ( ; gain <= 1.00f; gain += (float) quantity/10000){
-#ifdef DEBUG
-			fprintf(stderr, "Fade-in: Set gain to: %f\n", gain);
+#ifndef _WIN32
+		pthread_create(&thread, NULL, helper_fadein, (void*) &fade);
+		pthread_detach(thread);
+#else
+		Thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) helper_fadein, (void*) &fade, 0, threadID);
+		CloseHandle(Thread);
 #endif
-			alSourcef(Sources[index], AL_GAIN, gain);
-			usleep(10000);
-		}
-		
-		if (AlGetError("ERROR %d: Setting fade in volume\n") != AL_TRUE)
-			return AL_FALSE;
 		
 		alGetError();  /* clear any AL errors beforehand */
 
--- a/openalbridge/openalwrap.h	Wed Jun 24 23:53:02 2009 +0000
+++ b/openalbridge/openalwrap.h	Thu Jun 25 10:49:05 2009 +0000
@@ -16,6 +16,21 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  */
 
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include "al.h"
+#include "alc.h"
+#include "loaders.h"
+#include "wrappers.h"
+#include "endianness.h"
+
+#ifndef _WIN32
+#include <pthread.h>
+#else
+#include <windows.h>
+#endif
+
 #ifndef _SLEEP_H
 #define _SLEEP_H
 /** 1.0 02/03/10 - Defines cross-platform sleep, usleep, etc. * By Wu Yongwei **/
--- a/openalbridge/wrappers.c	Wed Jun 24 23:53:02 2009 +0000
+++ b/openalbridge/wrappers.c	Thu Jun 25 10:49:05 2009 +0000
@@ -16,10 +16,7 @@
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  */
 
-#include <stdio.h>
-#include <stdlib.h>
 #include "wrappers.h"
-#include "al.h"
 
 #ifdef __CPLUSPLUS
 extern "C" {