2013年10月18日金曜日

How to embed pd-for-android in your libGDX project

libGDX supports cross-platform game development.
It is very convenient in the case of using your PC as a test environment of a mobile game application.

When you set up cross-platform project using gdx-setup-ui.jar,
following project folders will be made.
  • my-gdx-project : core project, includes main sources, common libraries and interfaces
    • your sources are connected to libGDX
  • my-gdx-project-android : Android project, includes only Android specific implementations and native libraries
    • libGDX is connected to Android: OpenGL ES, touch input, etc
  • my-gdx-project-desktop : Desktop project, includes only Win/Mac specific implementations and native libraries 
    • libGDX is connected to Win/Mac: DirectX, OpenGL, mouse input, etc
libpd is a library to embed the Data-Flow programming language Pd(Pure Data) in our application programs. Additionally, in Android systems, libpd supports OpenSL ES audio API expected to reduce audio latency after Jelly Bean.

libpd needs platform-specific implementations. So if you want to use libpd in the cross-platform projects, it would be better to trace the same way as above libGDX connection.


Step 0: Prepare libGDX projects

For Android and Desktop.
Import them to your workspace.
You will immediately be able to run the project templates both at these platforms.


Step 1: Add pd-for-android to the Android project

pd-for-android is an integration kit of the libpd(Android build) and some Android-specific implementations  especially includes an "audio glue" between Android audio and Pd process.
(An O'Reilly book "Making Musical Apps" - written by Peter Brinkmann, a developer of libpd - also uses the audio glue of pd-for-android.)

Now, download pd-for-android git clone, then import all project files to your workspace.





Some errors will occur. Most causes of them are mismatches of Android SDK version.
About the all imported projects, open "project.properties" and change a target SDK number to your already installed version.. for example, Jelly Bean(4.1) is "16".


Update the dependencies. Fire F5 key to each project!




If an error is remained at "PdCore", open its properties and make a reference to "Android Midi".




After resolving all errors, an awaited pd-for-android library file "PdCore" is generated to:
pd-for-android/src/bin/pdcore.jar

OK, register the pdcore.jar file to the references library of your Android project.



This will make available libpd at only the Android project.

Step 2: Install PortAudio and Build libpd-portaudio

PortAudio is a cross-platform audio programming library which wraps platform-specific API: DirectSound, ASIO, CoreAudio and so on.

A Github libpd repository has a branch named "portaudio".
This branch's libpd implements an audio processing with PortAudio, and furthermore, its usage is similar to libpd Android build with OpenSL ES.

But libpd-portaudio is a build not for Android, therefore it doesn't include any PdCore functions of pd-for-android.
It means the pd-for-android can be simulated on desktop project by imitating necessary PdCore implementations on libpd-portaudio.

At first, install PortAudio. Dowload a stable version, extract it, and
$ ./configure
$ make
$ sudo make install
(If you have not agreed with a license of Xcode in Terminal, execute this before ./configure:)
$ sudo xcodebuild -license
$ sudo xcode-select --install

Next, get the libpd-portaudio.
$ git clone https://github.com/libpd/libpd.git
$ cd libpd
$ git submodule init
$ git submodule update
$ git checkout portaudio
(or download directly from the website of portaudio-branch)

and
$ make clean javalib

Then "libpd.jar" is generated to the destination:
libpd/lib/libpd.jar
 I renamed it to "libpd-portaudio.jar".
Add this to the desktop project, libpd will just be included to your desktop project, but other pd-for-android functions are not on it. Let's make them below.
I prepared another reference of pd-for-android, includes dummy imitations of Android-specific functions.
Download a "PdCore-peripheral_desktop_beta.jar" from here.
Then register said two JAR files to the references library of your core project.


Step 3: Extract Sources from pd-for-android, and refer it to the core project

pd-for-android runs on only Android projects, so for your coding on the core project , you must prepare a reference(dummy) of pd-for-android to the core project (make a connection of your sources to libpd).

At first, select PdCore sources below, then select "Export..." on right-click menu.



Select "JAR file" on the next window, and click "Next >".



Check "Export generated class files and resources" and "Export Java source files and resources", and set an output destination folder and JAR file name as you like(I set a name "PdCore-source.jar"), then click "Finish".


OK, the generated JAR file doesn't include any platform-specific part or internal Pd itself, so I call it a dummy of pd-for-android.
Register the "PdCore-source.jar" and "PdCore-peripheral_desktop_beta.jar" that you downloaded at Step 2 to the references library of your core project.



This step will make you able to write libpd codes with referencing the pd-for-android class or methods.

Step 4: Make Interfaces of pd-for-android

Next, make interfaces and its implement for each platform.
For example, When you want to "start" a Pd processing on your project,  the "start" should act differently on each platform. Interface is used to resolve problems like this.

Download files from said location and import to the packages of each project.

PdInterface.java -> my-gdx-game/src/com.me.mygdxgame/
pdImpleAndroid.java -> my-gdx-game-android/src/com.me.mygdxgame/
pdImeleDesktop.java -> my-gdx-game-desktop/src/com.me.mygdxgame/




Add codes to each main java file like this:



Step 5: Write a common source code

Download "MyGdxGame.java" from said location and add(replace) to the core project:
my-gdx-game/src/com.me.mygdxgame/MyGdxGame.java

And download "testforblog.pd" and add to the assets directory:
my-gdx-game-android/assets/



I prepared a "Hello-World"-like libpd sample, check it out.

Conclusion:

Both at the desktop and the Android, not only libGDX but libpd works by the same source codes!

0 件のコメント :

コメントを投稿