Welcome, Guest! Log In | Create Account

ShortTutorial

From codesounding

Jump to: navigation, search

This is a short step by step ANT example to show how "sonify" your application. I''ll use BSD licensed Sun files from $JAVA_HOME/demo/jfc/Java2D/src folder (*). I like this example because it is multithread and is fun to play with. The live demo is also available as a one-click Java Web Start application.

Here the complete ANT build file

Let:

<property name="src"  value="${basedir}/java2Demo/src"/>
<property name="srcRewritten"  value="${basedir}/java2Demo/srcRewritten"/>
<property name="build"  value="${basedir}/java2Demo/bin"/>
<property name="lib"  value="${basedir}/dist"/>
<property name="manifest"  value="${basedir}/java2demo/manifest"/>
<property name="dist"  value="${basedir}/java2Demo/dist"/>

Where 'cp.main' contains codesounding-embedded.jar:

<path id="cp.main">
    <pathelement location="${lib}/codesounding-embedded.jar"/>
</path>
  • The first step is rewriting your source files, to add sonification instruction; all rewritten files go in ${srcRewritten} folder:
<taskdef name="sonify" classpathref="cp.main" classname="codesounding.ant.TaskTemplateApplier"/>
<sonify destDir="${srcRewritten}">
   <fileset dir="${src}" includes="**/*.java" />
</sonify>
  • Second, compile your source files
<javac
   srcdir="${srcRewritten}"
   destdir="${build}"
   classpathref="cp.main"
/>
  • Third, maybe you'd like to pakage all in a jar:
  <jar destfile="${dist}\java2demo.jar" manifest="${manifest}/MANIFEST.MF">
      <fileset dir="${build}"/>
      <fileset dir="${src}" >
          <include name="codesounding.properties"/>
      </fileset>
      <zipfileset src="${lib}/codesounding-embedded.jar"/>
  </jar>
Note I'm including also codesounding-embedded.jar content files.
===Important: sonification configuration is into codesounding.properties file (see the file format doc). It must be either in the current running directory or into the classpath.===
  • Finally, run the application and hear your code play; here three ways to start it:
<target name="run.embedded" depends="sounding.java2demo" >
    <!-- this works only if java2demo.jar contains also codesounding.* classes -->
    <java fork="true" jar="${dist}\java2demo.jar"/>
</target>

<target name="run.jvmarg" depends="sounding.java2demo" >
    <!-- tricky way to inject a classpath to a 'java -jar' command -->
    <java fork="true" jar="${dist}\java2demo.jar">
        <jvmarg value="-Xbootclasspath/a:${lib}/codesounding-embedded.jar"/>
    </java>
</target>

<target name="run.class" depends="sounding.java2demo" >
    <!-- 'java classname' standard way -->
    <java
        fork="true"
        classpath="${dist}/java2demo.jar:${lib}/codesounding-embedded.jar"
        classname="java2d.Java2Demo">
    </java>
</target>

(*) Since I'm publishing the demo also as a Java Web Start application and to avoid a ClassCastException on the classloader (JWS uses com.sun.jnlp.JNLPClassLoader class), I made two changes to Sun original sources; they are on file DemoImages.java, where in the following lines I use a generic ClassLoader (and its method 'getResource') instead of a URLClassLoader ('findResource' method):

ClassLoader urlLoader = cmp.getClass().getClassLoader();
URL fileLoc = urlLoader.getResource("images/" + name);