(svn r8757) -Add: added libtimidity music driver (it is a nice small library simular to timidity via extmidi)
authortruelight
Fri, 16 Feb 2007 09:50:28 +0000
changeset 6360 37f43b7ce27b
parent 6359 fb2fe9142034
child 6361 8fc35d15114f
(svn r8757) -Add: added libtimidity music driver (it is a nice small library simular to timidity via extmidi)
[PSP] -Add: added PSP specific code for libtimidity. libtimidity code is based on the work of Turulo. Tnx a bunch!
config.lib
configure
source.list
src/driver.cpp
src/music/libtimidity.cpp
src/music/libtimidity.h
--- a/config.lib	Fri Feb 16 09:39:32 2007 +0000
+++ b/config.lib	Fri Feb 16 09:50:28 2007 +0000
@@ -52,11 +52,12 @@
 	with_iconv="1"
 	with_midi=""
 	with_midi_arg=""
+	with_libtimidity="1"
 	with_freetype="1"
 	with_fontconfig="1"
 	with_psp_config="1"
 
-	save_params_array="build host cc_build cc_host cxx_build cxx_host windres strip lipo os revision endian config_log prefix_dir binary_dir data_dir icon_dir personal_dir install_dir custom_lang_dir second_data_dir enable_install enable_debug enable_profiling enable_dedicated enable_network enable_static enable_translator enable_assert enable_strip with_osx_sysroot enable_universal enable_osx_g5 with_application_bundle with_sdl with_cocoa with_zlib with_png with_makedepend with_direct_music with_sort with_iconv with_midi with_midi_arg with_freetype with_fontconfig with_psp_config CC CXX CFLAGS LDFLAGS"
+	save_params_array="build host cc_build cc_host cxx_build cxx_host windres strip lipo os revision endian config_log prefix_dir binary_dir data_dir icon_dir personal_dir install_dir custom_lang_dir second_data_dir enable_install enable_debug enable_profiling enable_dedicated enable_network enable_static enable_translator enable_assert enable_strip with_osx_sysroot enable_universal enable_osx_g5 with_application_bundle with_sdl with_cocoa with_zlib with_png with_makedepend with_direct_music with_sort with_iconv with_midi with_midi_arg with_libtimidity with_freetype with_fontconfig with_psp_config CC CXX CFLAGS LDFLAGS"
 }
 
 detect_params() {
@@ -184,6 +185,10 @@
 			--without-libpng)             with_png="0";;
 			--with-libpng=*)              with_png="$optarg";;
 
+			--with-libtimidity)           with_libtimidity="2";;
+			--without-libtimidity)        with_libtimidity="0";;
+			--with-libtimidity=*)         with_libtimidity="$optarg";;
+
 			--with-freetype)              with_freetype="2";;
 			--without-freetype)           with_freetype="0";;
 			--with-freetype=*)            with_freetype="$optarg";;
@@ -421,6 +426,7 @@
 	detect_fontconfig
 	detect_iconv
 	detect_pspconfig
+	detect_libtimidity
 
 	if [ "$with_direct_music" = "1" ] || [ "$with_direct_music" = "2" ]; then
 		if [ "$os" != "MINGW" ] && [ "$os" != "CYGWIN" ]; then
@@ -776,6 +782,15 @@
 		CFLAGS="$CFLAGS -DWIN32_ENABLE_DIRECTMUSIC_SUPPORT"
 	fi
 
+	if [ "$with_libtimidity" != "0" ]; then
+		if [ "$enable_static" != "0" ]; then
+			LIBS="$LIBS $libtimidity"
+		else
+			LIBS="$LIBS -ltimidity"
+		fi
+		CFLAGS="$CFLAGS -DLIBTIMIDITY"
+	fi
+
 	if [ "$with_iconv" != "0" ]; then
 		CFLAGS="$CFLAGS -DWITH_ICONV"
 		LIBS="$LIBS -liconv"
@@ -1273,65 +1288,78 @@
 	log 1 "checking COCOA... found"
 }
 
-detect_zlib() {
+detect_library() {
+	# $1 - config-param ($with_zlib value)
+	# $2 - library name ('zlib', sets $zlib)
+	# $3 - static library name (libz.a)
+	# $4 - header name (zlib.h)
+
 	# 0 means no, 1 is auto-detect, 2 is force
-	if [ "$with_zlib" = "0" ]; then
-		log 1 "checking zlib... disabled"
+	if [ "$1" = "0" ]; then
+		log 1 "checking $2... disabled"
 
-		zlib=""
+		eval "$2=\"\""
 		return 0
 	fi
 
-	log 2 "detecting zlib"
+	log 2 "detecting $2"
 
-	if [ "$with_zlib" = "1" ] || [ "$with_zlib" = "" ] || [ "$with_zlib" = "2" ]; then
-		zlib=`ls -1 /usr/include/*.h 2>/dev/null | grep "\/zlib.h$"`
-		if [ -z "$zlib" ]; then
-			log 2 "  trying /usr/include/zlib.h... no"
-			zlib=`ls -1 /usr/local/include/*.h 2>/dev/null | grep "\/zlib.h$"`
+	if [ "$1" = "1" ] || [ "$1" = "" ] || [ "$1" = "2" ]; then
+		eval "$2=`ls -1 /usr/include/*.h 2>/dev/null | grep \"\/$4\$\"`"
+		eval "res=\$$2"
+		if [ -z "$res" ]; then
+			log 2 "  trying /usr/include/$4... no"
+			eval "$2=`ls -1 /usr/local/include/*.h 2>/dev/null | grep \"\/$4\$\"`"
 		fi
-		if [ -z "$zlib" ]; then
-			log 2 "  trying /usr/local/include/zlib.h... no"
+		eval "res=\$$2"
+		if [ -z "$res" ]; then
+			log 2 "  trying /usr/local/include/$4... no"
 		fi
 
-		if [ -n "$zlib" ] && [ "$enable_static" != "0" ] && [ "$os" != "OSX" ]; then
-			log 2 "  trying $zlib... found"
+		eval "res=\$$2"
+		if [ -n "$res" ] && [ "$enable_static" != "0" ] && [ "$os" != "OSX" ]; then
+			eval "res=\$$2"
+			log 2 "  trying $res... found"
 			# Now find the static lib, if needed
-			zlib=`ls /lib/*.a 2>/dev/null | grep "\/libz.a$"`
-			if [ -z "$zlib" ]; then
-				log 2 "  trying /lib/libz.a... no"
-				zlib=`ls /usr/lib/*.a 2>/dev/null | grep "\/libz.a$"`
+			eval "$2=`ls /lib/*.a 2>/dev/null | grep \"\/$3\$\"`"
+			eval "res=\$$2"
+			if [ -z "$res" ]; then
+				log 2 "  trying /lib/$3... no"
+				eval "$2=`ls /usr/lib/*.a 2>/dev/null | grep \"\/$3\$\"`"
 			fi
-			if [ -z "$zlib" ]; then
-				log 2 "  trying /usr/lib/libz.a... no"
-				zlib=`ls /usr/local/lib/*.a 2>/dev/null | grep "\/libz.a$"`
+			eval "res=\$$2"
+			if [ -z "$res" ]; then
+				log 2 "  trying /usr/lib/$3... no"
+				eval "$2=`ls /usr/local/lib/*.a 2>/dev/null | grep \"\/$3\$\"`"
 			fi
-			if [ -z "$zlib" ]; then
-				log 2 "  trying /usr/local/lib/libz.a... no"
-				log 1 "configure: error: zlib couldn't be found"
-				log 1 "configure: error: you requested a static link, but I can't find zlib.a"
+			eval "res=\$$2"
+			if [ -z "$res" ]; then
+				log 2 "  trying /usr/local/lib/$3... no"
+				log 1 "configure: error: $2 couldn't be found"
+				log 1 "configure: error: you requested a static link, but I can't find $3"
 
 				exit 1
 			fi
 		fi
 	else
 		# Make sure it exists
-		if [ -f "$with_zlib" ]; then
-			zlib=`ls $with_zlib 2>/dev/null`
+		if [ -f "$1" ]; then
+			eval "$2=`ls $1 2>/dev/null`"
 		else
-			zlib=`ls $with_zlib/libz.a 2>/dev/null`
+			eval "$2=`ls $1/$3 2>/dev/null`"
 		fi
 	fi
 
-	if [ -z "$zlib" ]; then
-		log 1 "checking zlib... not found"
-		if [ "$with_zlib" = "2" ]; then
-			log 1 "configure: error: zlib couldn't be found"
+	eval "res=\$$2"
+	if [ -z "$res" ]; then
+		log 1 "checking $2... not found"
+		if [ "$1" = "2" ]; then
+			log 1 "configure: error: $2 couldn't be found"
 
 			exit 1
-		elif [ "$with_zlib" != "1" ]; then
-			log 1 "configure: error: zlib couldn't be found"
-			log 1 "configure: error: you supplied '$with_zlib', but it seems invalid"
+		elif [ "$1" != "1" ]; then
+			log 1 "configure: error: $2 couldn't be found"
+			log 1 "configure: error: you supplied '$1', but it seems invalid"
 
 			exit 1
 		fi
@@ -1339,9 +1367,18 @@
 		return 0
 	fi
 
-	log 2 "  trying $zlib... found"
+	eval "res=\$$2"
+	log 2 "  trying $res... found"
 
-	log 1 "checking zlib... found"
+	log 1 "checking $2... found"
+}
+
+detect_zlib() {
+	detect_library "$with_zlib" "zlib" "libz.a" "zlib.h"
+}
+
+detect_libtimidity() {
+	detect_library "$with_libtimidity" "libtimidity" "libtimidity.a" "timidity.h"
 }
 
 detect_png() {
--- a/configure	Fri Feb 16 09:39:32 2007 +0000
+++ b/configure	Fri Feb 16 09:50:28 2007 +0000
@@ -82,6 +82,7 @@
 		if ($0 == "WINCE"       && "'$os'" != "WINCE")             { next; }
 		if ($0 == "MSVC"        && "'$os'" != "MSVC")              { next; }
 		if ($0 == "DIRECTMUSIC" && "'$with_direct_music'" == "0")  { next; }
+		if ($0 == "LIBTIMIDITY" && "'$with_libtimidity'" == "0")   { next; }
 
 		skip += 1;
 
--- a/source.list	Fri Feb 16 09:39:32 2007 +0000
+++ b/source.list	Fri Feb 16 09:50:28 2007 +0000
@@ -366,12 +366,18 @@
 #else
 	#if WINCE
 	#else
-		music/extmidi.cpp
+		#if PSP
+		#else
+			music/extmidi.cpp
+		#end
 	#end
 #end
 #if BEOS
  	music/bemidi.cpp
 #end
+#if LIBTIMIDITY
+	music/libtimidity.cpp
+#end
 
 # Sound
 sound/null_s.cpp
--- a/src/driver.cpp	Fri Feb 16 09:39:32 2007 +0000
+++ b/src/driver.cpp	Fri Feb 16 09:50:28 2007 +0000
@@ -15,6 +15,7 @@
 #include "music/os2_m.h"
 #include "music/win32_m.h"
 #include "music/qtmidi.h"
+#include "music/libtimidity.h"
 
 #include "sound/null_s.h"
 #include "sound/sdl_s.h"
@@ -58,7 +59,10 @@
 	M("qt",      "QuickTime MIDI Driver",   &_qtime_music_driver),
 #endif
 #ifdef UNIX
-#if !defined(__MORPHOS__) && !defined(__AMIGA__)
+#if defined(LIBTIMIDITY)
+	M("libtimidity", "LibTimidity MIDI Driver", &_libtimidity_music_driver),
+#endif /* LIBTIMIDITY */
+#if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(PSP)
 	M("extmidi", "External MIDI Driver",    &_extmidi_music_driver),
 #endif
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/music/libtimidity.cpp	Fri Feb 16 09:50:28 2007 +0000
@@ -0,0 +1,143 @@
+/* $Id: extmidi.c 4692 2006-05-02 19:09:49Z peter1138 $ */
+
+#include "../stdafx.h"
+#include "../openttd.h"
+#include "../sound.h"
+#include "../string.h"
+#include "../variables.h"
+#include "../debug.h"
+#include "libtimidity.h"
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+#include <signal.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <timidity.h>
+#if defined(PSP)
+#include <pspaudiolib.h>
+#endif /* PSP */
+
+enum MidiState {
+	MIDI_STOPPED = 0,
+	MIDI_PLAYING = 1,
+};
+
+static struct {
+	MidIStream *stream;
+	MidSongOptions options;
+	MidSong *song;
+
+	MidiState status;
+	uint32 song_length;
+	uint32 song_position;
+} _midi;
+
+#if defined(PSP)
+static void AudioOutCallback(void *buf, unsigned int _reqn, void *userdata)
+{
+	memset(buf, 0, _reqn * PSP_NUM_AUDIO_CHANNELS);
+	if (_midi.status == MIDI_PLAYING) {
+		mid_song_read_wave(_midi.song, buf, _reqn * PSP_NUM_AUDIO_CHANNELS);
+	}
+}
+#endif /* PSP */
+
+static const char *LibtimidityMidiStart(const char *const *param)
+{
+	_midi.status = MIDI_STOPPED;
+
+	if (mid_init(param == NULL ? NULL : (char *)param[0]) < 0) {
+		/* If init fails, it can be because no configuration was found.
+		 *  If it was not forced via param, try to load it without a
+		 *  configuration. Who knows that works. */
+		if (param != NULL || mid_init_no_config() < 0) {
+			DEBUG(driver, 0, "error initializing timidity");
+			return NULL;
+		}
+	}
+	DEBUG(driver, 1, "successfully initialised timidity");
+
+	_midi.options.rate = 44100;
+	_midi.options.format = MID_AUDIO_S16LSB;
+	_midi.options.channels = 2;
+#if defined(PSP)
+	_midi.options.buffer_size = PSP_NUM_AUDIO_SAMPLES;
+#else
+	_midi.options.buffer_size = _midi.options.rate;
+#endif
+
+#if defined(PSP)
+	pspAudioInit();
+	pspAudioSetChannelCallback(_midi.options.channels, &AudioOutCallback, NULL);
+	pspAudioSetVolume(_midi.options.channels, PSP_VOLUME_MAX, PSP_VOLUME_MAX);
+#endif /* PSP */
+
+	return NULL;
+}
+
+static void LibtimidityMidiStop(void)
+{
+	if (_midi.status == MIDI_PLAYING) {
+		_midi.status = MIDI_STOPPED;
+		mid_song_free(_midi.song);
+	}
+	mid_exit();
+}
+
+static void LibtimidityMidiPlaySong(const char *filename)
+{
+	_midi.stream = mid_istream_open_file(filename);
+	if (_midi.stream == NULL) {
+		DEBUG(driver, 0, "Could not open music file");
+		return;
+	}
+
+	_midi.song = mid_song_load(_midi.stream, &_midi.options);
+	mid_istream_close(_midi.stream);
+	_midi.song_length = mid_song_get_total_time(_midi.song);
+
+	if (_midi.song == NULL) {
+		DEBUG(driver, 1, "Invalid MIDI file");
+		return;
+	}
+
+	mid_song_start(_midi.song);
+	_midi.status = MIDI_PLAYING;
+}
+
+static void LibtimidityMidiStopSong(void)
+{
+	_midi.status = MIDI_STOPPED;
+	mid_song_free(_midi.song);
+}
+
+static bool LibtimidityMidiIsPlaying(void)
+{
+	if (_midi.status == MIDI_PLAYING) {
+		_midi.song_position = mid_song_get_time(_midi.song);
+		if (_midi.song_position >= _midi.song_length) {
+			_midi.status = MIDI_STOPPED;
+			_midi.song_position = 0;
+		}
+	}
+
+	return (_midi.status == MIDI_PLAYING);
+}
+
+static void LibtimidityMidiSetVolume(byte vol)
+{
+	if (_midi.song != NULL)
+		mid_song_set_volume(_midi.song, vol);
+}
+
+const HalMusicDriver _libtimidity_music_driver = {
+	LibtimidityMidiStart,
+	LibtimidityMidiStop,
+	LibtimidityMidiPlaySong,
+	LibtimidityMidiStopSong,
+	LibtimidityMidiIsPlaying,
+	LibtimidityMidiSetVolume,
+};
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/music/libtimidity.h	Fri Feb 16 09:50:28 2007 +0000
@@ -0,0 +1,10 @@
+/* $Id: extmidi.h 2704 2005-07-25 07:16:10Z tron $ */
+
+#ifndef MUSIC_LIBTIMIDITY_H
+#define MUSIC_LIBTIMIDITY_H
+
+#include "../hal.h"
+
+extern const HalMusicDriver _libtimidity_music_driver;
+
+#endif /* MUSIC_LIBTIMIDITY_H */