From 517b630d7cb98d46136486d7be88a77e91ef03e4 Mon Sep 17 00:00:00 2001 From: pelya Date: Thu, 18 Nov 2010 19:45:26 +0200 Subject: [PATCH] Fixed MIDI music in OpenTTD --- .gitignore | 2 +- buildQuick.sh | 6 +- .../openttd/AndroidAppSettings.cfg | 4 +- .../openttd/openttd-1.0.4-android.patch | 33 ------- .../openttd/openttd-trunk-android.patch | 88 +++++++++++++++++++ project/jni/application/src | 2 +- project/jni/sdl_main/sdl_main.c | 7 +- project/jni/timidity/Android.mk | 2 +- project/jni/timidity/src/resample.c | 3 + project/jni/timidity/src/timidity.c | 11 ++- project/jni/timidity/src/timidity_internal.h | 6 +- 11 files changed, 114 insertions(+), 50 deletions(-) delete mode 100644 project/jni/application/openttd/openttd-1.0.4-android.patch diff --git a/.gitignore b/.gitignore index 32cfb4173..9ea989787 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,4 @@ project/src project/res/values* project/AndroidManifest.xml project/jni/Settings.mk - +libapplication.so diff --git a/buildQuick.sh b/buildQuick.sh index 527434516..fac6fc5b0 100755 --- a/buildQuick.sh +++ b/buildQuick.sh @@ -4,15 +4,15 @@ # export PATH=$PATH:~/src/endless_space/android-ndk-r4b # Set environment to CrystaX NDK with RTTI and exceptions instead of original NDK # export PATH=$PATH:~/src/endless_space/android-ndk-r4-crystax/ndk-build -NDKBUILD=ndk-build +NDKBUILDPATH=$PATH if grep "AppUseCrystaXToolchain=y" AndroidAppSettings.cfg > /dev/null ; then - NDKBUILD=`which ndk-build | sed 's@/[^/]*/ndk-build@/android-ndk-r4-crystax@'`/ndk-build + NDKBUILDPATH=`which ndk-build | sed 's@/[^/]*/ndk-build@/android-ndk-r4-crystax@'`:$PATH fi [ -e project/bin/lib ] || ln -s ../libs project/bin/lib cd project && \ -$NDKBUILD -j2 V=1 && \ +env PATH=$NDKBUILDPATH ndk-build -j2 V=1 && \ cd bin && \ rm -rf DemoActivity-debug-unaligned.apk && \ cp DemoActivity.ap_ DemoActivity-debug-unaligned.apk && \ diff --git a/project/jni/application/openttd/AndroidAppSettings.cfg b/project/jni/application/openttd/AndroidAppSettings.cfg index 142735bfb..5390d4528 100644 --- a/project/jni/application/openttd/AndroidAppSettings.cfg +++ b/project/jni/application/openttd/AndroidAppSettings.cfg @@ -5,7 +5,7 @@ AppName="OpenTTD" AppFullName=org.openttd.sdl ScreenOrientation=h InhibitSuspend=n -AppDataDownloadUrl="Data files size is 19 Mb|http://sites.google.com/site/andgamdev/openttd-1.0.4-data.zip?attredirects=0&d=1^OpenTTD fonts 4MB|http://sites.google.com/site/andgamdev/openttd-1.0.4-fonts-data.zip?attredirects=0&d=1^MIDI music support (18 Mb)|http://sourceforge.net/projects/libsdl-android/files/timidity.zip/download" +AppDataDownloadUrl="Data files - 20 Mb|http://sourceforge.net/projects/libsdl-android/files/OpenTTD/openttd-data-2.zip/download^Localization files and fonts (7 MB)|http://sourceforge.net/projects/libsdl-android/files/OpenTTD/openttd-lang-2.zip/download^MIDI music support (18 Mb)|http://sourceforge.net/projects/libsdl-android/files/timidity.zip/download" SdlVideoResize=y SdlVideoResizeKeepAspect=n NeedDepthBuffer=n @@ -29,5 +29,5 @@ AppCflags='' AppLdflags='' AppSubdirsBuild='' AppUseCrystaXToolchain=y -AppCmdline='' +AppCmdline='openttd' ReadmeText='^You may press "Home" now - the data will be downloaded in background' diff --git a/project/jni/application/openttd/openttd-1.0.4-android.patch b/project/jni/application/openttd/openttd-1.0.4-android.patch deleted file mode 100644 index c98fb0e37..000000000 --- a/project/jni/application/openttd/openttd-1.0.4-android.patch +++ /dev/null @@ -1,33 +0,0 @@ -diff -ru orig/openttd-1.0.4/src/network/core/os_abstraction.h openttd-1.0.4/src/network/core/os_abstraction.h ---- orig/openttd-1.0.4/src/network/core/os_abstraction.h 2010-05-18 17:49:59.000000000 -0400 -+++ openttd-1.0.4/src/network/core/os_abstraction.h 2010-11-13 16:35:45.000000000 -0500 -@@ -159,7 +159,7 @@ - # include - /* According to glibc/NEWS, appeared in glibc-2.3. */ - # if !defined(__sgi__) && !defined(SUNOS) && !defined(__MORPHOS__) && !defined(__BEOS__) && !defined(__HAIKU__) && !defined(__INNOTEK_LIBC__) \ -- && !(defined(__GLIBC__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 2)) && !defined(__dietlibc__) && !defined(HPUX) -+ && !(defined(__GLIBC__) && (__GLIBC__ <= 2) && (__GLIBC_MINOR__ <= 2)) && !defined(__dietlibc__) && !defined(HPUX) && !defined(ANDROID) - /* If for any reason ifaddrs.h does not exist on your system, comment out - * the following two lines and an alternative way will be used to fetch - * the list of IPs from the system. */ -diff -ru orig/openttd-1.0.4/src/os/unix/unix.cpp openttd-1.0.4/src/os/unix/unix.cpp ---- orig/openttd-1.0.4/src/os/unix/unix.cpp 2010-05-18 17:44:47.000000000 -0400 -+++ openttd-1.0.4/src/os/unix/unix.cpp 2010-11-13 18:06:14.000000000 -0500 -@@ -25,7 +25,7 @@ - #ifdef __APPLE__ - #include - #elif (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) || defined(__GLIBC__) -- #define HAS_STATVFS -+// #define HAS_STATVFS - #endif - - #ifdef HAS_STATVFS -@@ -239,6 +239,8 @@ - void cocoaReleaseAutoreleasePool(); - #endif - -+#include -+extern "C" int CDECL main(int, char *[]); - int CDECL main(int argc, char *argv[]) - { - int ret; diff --git a/project/jni/application/openttd/openttd-trunk-android.patch b/project/jni/application/openttd/openttd-trunk-android.patch index 6a5897f06..d1e19ffb9 100644 --- a/project/jni/application/openttd/openttd-trunk-android.patch +++ b/project/jni/application/openttd/openttd-trunk-android.patch @@ -1,3 +1,67 @@ +Index: src/sound/sdl_s.cpp +=================================================================== +--- src/sound/sdl_s.cpp (revision 21200) ++++ src/sound/sdl_s.cpp (working copy) +@@ -20,9 +20,16 @@ + + static FSoundDriver_SDL iFSoundDriver_SDL; + ++#ifdef ANDROID ++extern void Android_MidiMixMusic(Sint16 *stream, int len); ++#endif ++ + static void CDECL fill_sound_buffer(void *userdata, Uint8 *stream, int len) + { + MxMixSamples(stream, len / 4); ++#ifdef ANDROID ++ Android_MidiMixMusic((Sint16 *)stream, len / 2); ++#endif + } + + const char *SoundDriver_SDL::Start(const char * const *parm) +Index: src/music/libtimidity.cpp +=================================================================== +--- src/music/libtimidity.cpp (revision 21200) ++++ src/music/libtimidity.cpp (working copy) +@@ -22,10 +22,13 @@ + #include + #include + #include ++#include ++ + #if defined(PSP) + #include + #endif /* PSP */ + ++ + enum MidiState { + MIDI_STOPPED = 0, + MIDI_PLAYING = 1, +@@ -50,6 +53,24 @@ + } + } + #endif /* PSP */ ++#ifdef ANDROID ++#define MIN(X ,Y) ((X) < (Y) ? (X) : (Y)) ++void Android_MidiMixMusic(Sint16 *stream, int len) ++{ ++ if (_midi.status == MIDI_PLAYING) { ++ Sint16 buf[16384]; ++ while( len > 0 ) ++ { ++ int minlen = MIN(sizeof(buf), len); ++ mid_song_read_wave(_midi.song, stream, MIN(sizeof(buf), len*2)); ++ for( Uint16 i = 0; i < minlen; i++ ) ++ stream[i] += buf[i]; ++ stream += minlen; ++ len -= minlen; ++ } ++ } ++} ++#endif + + static FMusicDriver_LibTimidity iFMusicDriver_LibTimidity; + Index: src/os/unix/unix.cpp =================================================================== --- src/os/unix/unix.cpp (revision 21200) @@ -36,3 +100,27 @@ Index: src/network/core/os_abstraction.h /* If for any reason ifaddrs.h does not exist on your system, comment out * the following two lines and an alternative way will be used to fetch * the list of IPs from the system. */ +Index: src/debug.cpp +=================================================================== +--- src/debug.cpp (revision 21200) ++++ src/debug.cpp (working copy) +@@ -16,6 +16,9 @@ + #include "string_func.h" + #include "fileio_func.h" + #include "settings_type.h" ++#ifdef ANDROID ++#include ++#endif + + #include + +@@ -76,6 +79,9 @@ + */ + static void debug_print(const char *dbg, const char *buf) + { ++#ifdef ANDROID ++ __android_log_print(ANDROID_LOG_INFO, "OpenTTD", "[%s] %s", dbg, buf); ++#endif + #if defined(ENABLE_NETWORK) + if (_debug_socket != INVALID_SOCKET) { + char buf2[1024 + 32]; diff --git a/project/jni/application/src b/project/jni/application/src index f36c75d23..550b7b622 120000 --- a/project/jni/application/src +++ b/project/jni/application/src @@ -1 +1 @@ -scummvm \ No newline at end of file +openttd \ No newline at end of file diff --git a/project/jni/sdl_main/sdl_main.c b/project/jni/sdl_main/sdl_main.c index 0f8fc0cdc..d6e8a545b 100644 --- a/project/jni/sdl_main/sdl_main.c +++ b/project/jni/sdl_main/sdl_main.c @@ -83,11 +83,12 @@ JAVA_EXPORT_NAME(DemoRenderer_nativeInit) ( JNIEnv* env, jobject thiz, jstring } } + __android_log_print(ANDROID_LOG_INFO, "libSDL", "Calling SDL_main(\"%s\")", str); + (*env)->ReleaseStringUTFChars(env, cmdline, jstr); - __android_log_print(ANDROID_LOG_INFO, "libSDL", "Calling SDL_main(\"%s\")", str); - //for( i = 0; i < argc; i++ ) - // __android_log_print(ANDROID_LOG_INFO, "libSDL", "param %d = \"%s\"", i, argv[i]); + for( i = 0; i < argc; i++ ) + __android_log_print(ANDROID_LOG_INFO, "libSDL", "param %d = \"%s\"", i, argv[i]); main( argc, argv ); diff --git a/project/jni/timidity/Android.mk b/project/jni/timidity/Android.mk index 757f967cb..f759f9726 100644 --- a/project/jni/timidity/Android.mk +++ b/project/jni/timidity/Android.mk @@ -19,6 +19,6 @@ LOCAL_SHARED_LIBRARIES := LOCAL_STATIC_LIBRARIES := -LOCAL_LDLIBS := +LOCAL_LDLIBS := -llog include $(BUILD_SHARED_LIBRARY) diff --git a/project/jni/timidity/src/resample.c b/project/jni/timidity/src/resample.c index 20a5ada3e..a700bfb88 100644 --- a/project/jni/timidity/src/resample.c +++ b/project/jni/timidity/src/resample.c @@ -548,6 +548,7 @@ void pre_resample(MidSong *song, MidSample *sp) sint32 incr, ofs, newlen, count; sint16 *newdata, *dest, *src = (sint16 *) sp->data; sint16 v1, v2, v3, v4, *vptr; +/* #ifdef DEBUG_CHATTER static const char note_name[12][3] = { @@ -555,9 +556,11 @@ void pre_resample(MidSong *song, MidSample *sp) }; #endif + DEBUG_MSG(" * pre-resampling for note %d (%s%d)\n", sp->note_to_use, note_name[sp->note_to_use % 12], (sp->note_to_use & 0x7F) / 12); +*/ a = ((double) (sp->sample_rate) * freq_table[(int) (sp->note_to_use)]) / ((double) (sp->root_freq) * song->rate); diff --git a/project/jni/timidity/src/timidity.c b/project/jni/timidity/src/timidity.c index 0653e0be7..9f184a1c6 100644 --- a/project/jni/timidity/src/timidity.c +++ b/project/jni/timidity/src/timidity.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "timidity.h" #include "timidity_internal.h" @@ -90,7 +91,11 @@ static int read_config_file(char *name) } if (!(fp=open_file(name))) + { + __android_log_print(ANDROID_LOG_INFO, "libSDL", "libTimidity: read_config_file(%s) failure", name); return -1; + } + __android_log_print(ANDROID_LOG_INFO, "libSDL", "libTimidity: read_config_file(%s) opened config success", name); while (__fgets(tmp, sizeof(tmp), fp)) { @@ -391,6 +396,9 @@ static int read_config_file(char *name) } } fclose(fp); + + __android_log_print(ANDROID_LOG_INFO, "libSDL", "libTimidity: read_config_file(%s) success", name); + return 0; } @@ -422,8 +430,7 @@ int mid_init(char *config_file) #ifdef WIN32 add_to_pathlist("\\TIMIDITY"); #else - add_to_pathlist("/usr/local/lib/timidity"); - add_to_pathlist("/etc"); + add_to_pathlist("timidity"); #endif mid_init_no_config(); diff --git a/project/jni/timidity/src/timidity_internal.h b/project/jni/timidity/src/timidity_internal.h index 9237465eb..cb22c07c4 100644 --- a/project/jni/timidity/src/timidity_internal.h +++ b/project/jni/timidity/src/timidity_internal.h @@ -1,6 +1,7 @@ #ifndef TIMIDITY_INTERNAL_H #define TIMIDITY_INTERNAL_H +#include #include "timidity.h" #if defined(__i386__) || defined(__ia64__) || defined(WIN32) || \ @@ -49,11 +50,8 @@ #define SWAPLE32(x) XCHG_LONG(x) #endif -#ifdef DEBUG -#define DEBUG_MSG(...) fprintf(stderr, __VA_ARGS__) -#else +//#define DEBUG_MSG(...) __android_log_print(ANDROID_LOG_INFO, "libSDL", __VA_ARGS__) #define DEBUG_MSG(...) -#endif #define MID_VIBRATO_SAMPLE_INCREMENTS 32