whiteshepherd
Posts: 78
Joined: Thu Nov 03, 2011 7:59 pm

Educational Java Program

Mon Apr 01, 2013 1:00 am

I would like to run a really great java educational program for designing robots/electronics for my niece called Droidquest ( www.droidquest.com ). The file is: http://mysite.verizon.net/thomasfoote/D ... /DQ2.7.zip

I installed "sudo apt-get install openjdk-7-jdk" and tried the "java -jar ./DQ.jar" But the CPU just pegs and does nothing else. Any other ideas or am I out of luck?

Gene

User avatar
brox
Posts: 19
Joined: Sat Mar 02, 2013 11:24 pm
Location: Betelgeuse

Re: Educational Java Program

Mon Apr 01, 2013 2:13 pm

Any console output?
So long, and thanks for all the fish!

User avatar
xranby
Posts: 540
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: Educational Java Program

Tue Apr 02, 2013 10:31 am

The DroidQuest source code is available here:
http://mysite.verizon.net/thomasfoote/DQ/id78.htm

I took a quick look and notice that DroidQuest make use of *midi* to play sound.
Its actually the midi initialization that takes a *really-long-time-to-complete* the reason why is that Gervill the midi implementation inside Oracle JDK and OpenJDK is generating an, several megabytes large, "emergency"-sound-bank on start-up for use by the Gervill software-synthesizer on devices without a hardware midi implementation.

Gervill is written by Karl Helgason, you may watch his presentation from FOSDEM 2009 where he describe the Gervill generation of the emergency sound-bank. According to Karl there is a way to insert a pre-generated high quality "SoundFonts" soundbank into Gervill and this will prevent the generation of the emergency soundbank at startup and give higher quality midi sounds.
The Emergency soundbank is described 7min:20sec into the talk
http://archive.org/details/fosdem_2009_ ... ynthesizer

The quick ways to fix DroidQuest initialization is to
A) figure out how to download and give gervill an "free" SoundFonts soundbank to prevent the *long* startup delay. (there is no known opensource SoundFont soundbank thus therefore we can not bundle any pre-generated sound bank with the OpenJDK packages.)
B) remove all use of midi from the DroidQuest source and recompile it.
C) improve the gervill implementation start-up inside the OpenJDK source code


Edit: I was dead wrong about the midi start-up penalty theory mentioned above.
DroidQuest did not use any midi despite importing
import javax.sound.midi.*;
sorry for pointing towards the wrong direction..

Compiling DroidQuest manually is easy
javac DQ.java
and then run it using
java DQ

You may try recompiling DQ.java into native code using GCJ if you have the gcj-jdk package installed, native compilation using GCJ takes quite a while so be patient.
gcj DQ.java --main=DQ -o DroidQuest
you may then run the binary using
./DroidQuest

When i test DroidQuest on my dektop PC i find it starts-up in about
3 second when compiled using GCJ
10 seconds using OpenJDK hotspot
and up to some minutes for less optimised JVM's and interpreters, thus even on traditional desktops this game is quite heavy on logic.

On the raspberry pi execution times are expected to be much longer compared to a desktop pc.
I need to profile this game further to check if there is some shortcuts that can be done to get this educational game startup in reasonable time on the Pi.
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

User avatar
xranby
Posts: 540
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: Educational Java Program

Tue Apr 02, 2013 11:05 pm

To fix the startup issue open the QD.java source file and go to line 1559
and comment out or remove all code inside the
InitSounds and PlaySound functions.
then recompile.
javac QD.java

The game will now start much faster.
java QD
java -jamvm QD
java -cacao QD
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

whiteshepherd
Posts: 78
Joined: Thu Nov 03, 2011 7:59 pm

Re: Educational Java Program

Mon Apr 29, 2013 7:04 pm

Thanks so much for looking into this I will test it now! My niece will be thrilled if we get this working! :)

whiteshepherd
Posts: 78
Joined: Thu Nov 03, 2011 7:59 pm

Re: Educational Java Program

Wed May 01, 2013 4:21 pm

With the sound removed the game starts right up. With the sound it pegs the cpu and locks up. This is a game I used to play in the 80s as a kid and have fond memories. Is there any way to play with sound on the Pi or is that a lost cause do to limitations?

User avatar
xranby
Posts: 540
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: Educational Java Program

Wed May 01, 2013 6:08 pm

whiteshepherd wrote:With the sound removed the game starts right up. With the sound it pegs the cpu and locks up. This is a game I used to play in the 80s as a kid and have fond memories. Is there any way to play with sound on the Pi or is that a lost cause do to limitations?
I consider this to be a bug that needs to be investigated and fixed.
This game can serve as a test-case to improve the generic java sound back-ends used by the JRE to be more raspberry-pi friendly.

The short term fix for the game may be to rewrite the game to use a third party java sound library.
https://gist.github.com/xranby/4160432 - JOAL raspberry pi test example

Ideally we want to use a Java sound library that make use of OpenMAX IL similar to the sound playback examples inside /opt/vc/src for best hardware accelerated sound playback on the device. Unfortunally no OpenMAX IL sound library exist for java yet (except on android).
Last edited by xranby on Wed May 01, 2013 10:56 pm, edited 1 time in total.
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

User avatar
xranby
Posts: 540
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: Educational Java Program

Wed May 01, 2013 10:56 pm

Jan Newmarch: Programming and Using Linux Sound - in depth Version 0.10 (alpha), 2 April, 2013 http://jan.newmarch.name/LinuxSound/index.html

This e-book covers many raspberry pi details like OpenMAX IL and java-sound on the raspberry pi
http://jan.newmarch.name/LinuxSound/Mis ... spberryPi/

I guess you may find most answers in here how to fix the audio stack for the droid quest game.
Highly recommended!
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

whiteshepherd
Posts: 78
Joined: Thu Nov 03, 2011 7:59 pm

Re: Educational Java Program

Wed May 01, 2013 11:03 pm

I've coded for pascal. But I have never coded java before. I can read that link and give it a try. However you said OpenMax is not working? Plus there is a bug in the current sound setup? Should I try and look for examples in JOAL? Just looking for a little direction to start?

User avatar
xranby
Posts: 540
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: Educational Java Program

Wed May 01, 2013 11:37 pm

whiteshepherd wrote:I've coded for pascal. But I have never coded java before. I can read that link and give it a try. However you said OpenMax is not working? Plus there is a bug in the current sound setup? Should I try and look for examples in JOAL? Just looking for a little direction to start?
first step, fix the game:
You may start by test run the JOAL examples and see if they playback sound for you ok.
JOAL uses the OpenAL-soft sound library that in turn passes the sound samples down to ALSA or PulseAudio. If JOAL playback ok for you then you may start look at the JOAL example source code and then change the DQ.java source to use the same logic.
https://gist.github.com/xranby/4160432 - JOAL raspberry pi test example

second step, help improve hardware acceleration on the pi:
OpenAL-soft is a suitable library to improve, if we add a OpenMAX IL binding to OpenAL-soft then all applications using openal will be able to take advantage of hardware acceleration for sound playback.

final step, help debug the current java sound setup:
I belive there is some bug somewhere in the audio stack that gets exposed when running droid quest that cause the application to lock up, its hard to say if it is in the ALSA driver or inside OpenJDK itself. My feeling is that it is a multithreading issue that triggering a deadlock and the cpu keeps spinning waiting for the lock to clear. Finding the root cause may be tricky. Fixing this will help all java users to playback sound swiftly on the pi.

alternative final step to fix java sound, implement a JOAL back-end:
Java sound is modular, its configuration is loaded from the sound.properties stored inside the JRE/JDK. On the Debian/Rasbian system this configuration file is stored in /etc/java-7-openjdk/sound.properties writing a new back-end starts by providing a new class that implements the interfaces used by javax.sound.sampled.*
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

whiteshepherd
Posts: 78
Joined: Thu Nov 03, 2011 7:59 pm

Re: Educational Java Program

Sun Sep 29, 2013 6:19 pm

I've noticed that the Oracle version of java works with my Raspberry Pi model A. It takes about 3-4 min to load game segments. On looking at the Pi with " free" there is a lot of disk swapping taking place. I was wondering can anyone confirm if the game will run any better on the model B? I was hoping the extra memory would help but checking with someone who has one?

Return to “Java”