Convert sound files to native Java code to avoid the tedium of importing them!
If you like this, you'll probably also like ImageToClass.
This is the sound version of ImageToClass. I started this project thinking, "How much harder can sound be than images?" The answer was a lot. If you haven't dealt with the Java Sound API before, take my advice and don't. In an effort to support many devices and formats, Sun has thrown simplicity totally out the window. If you don't believe me, take a look at what is required just to play a simple sound file. I think the Java community will find that this tool greatly simplifies the process of dealing with sound in Java.
SoundToClass converts files from an audio format, like .WAV, into a Java code file, with a .JAVA extension, which implements java.applet.AudioClip. To do this, it simply reads in the sound data as an array of bytes and hard-codes those bytes into the generated Java file, which then uses them in its constructor.
In fairness to Sun, presenting audio is more complex than presenting images. Firstly, audio occurs over time, and secondly audio capabilities differ greatly from machine to machine. SoundToClass attempts to convert all sound to the widely used PCM encoding, but even this may not play on all machines. If not, the code generated by SoundToClass will fail gracefully and simply do nothing when you attempt to play it on an unsupported machine.
Download the tool here: SoundToClass
This tool has a graphical interface which can be accessed by double-clicking SoundToClass.jar (an executable JAR file). It can also be used from the command line, with the following syntax:
java -jar SoundToClass.jar source/path/and/name.ext [destination/path/and/name.java]
".ext" above must be the extension of a supported audio format. Which formats are supported will vary from computer to computer, but right now on my computer, the list is .WAV, .AU, and .TIF.
If no destination file is provided, it defaults to the same path and name as the source file.
The .JAVA files generated by SoundToClass have a simple interface in accordance with java.applet.AudioClip. In summary, here are their five methods (the last two are not part of the AudioClip interface, but you may still find them useful):
// Plays the sound from the beginning, even if it is already playing or looping. public void play(); // Plays the sound continuously until stopped. public void loop(); // Stops play and looping of the sound. public void stop(); // Tests if the sound is currently playing or looping. public boolean isPlaying(); // Tests if the sound is currently looping. public boolean isLooping();
The source is available, already zipped up as an Eclipse project file for your convenience: SoundToClass.zip
This software is freeware. Use it, abuse it, change it, whatever. I'd appreciate it if you kept my name somewhere on the GUI though.
In case you were wondering, yes, the sound that plays when you start the GUI version was made using this very tool.
The downside to this tool is that the .JAVA files it generates are significantly larger than the audio files from which they came. Some of the files I tested this on ended up nearly 300 times larger by the time they were in .CLASS format. The reason for this is twofold. First, the data must be represented as arrays of int's, and this takes up a lot more space. Secondly, the PCM encoding which SoundToClass uses for the sake of inter-system compatibility is not a compressed format, so compressed sound files may become much larger when converted to raw code because they must first be uncompressed. You should weight the pro's and con's of using this tool in your application.
Without the work of Matthias Pfisterer, the internet's Java Sound wiz, this tool would not have been possible. AudioPlayer was especially helpful.
As always, they go to email@example.com.
If I get really bored or if this tool gets a lot of positive feedback, I may extend the generated code to implement the more modern javax.sound.sampled.Clip interface. This is free and open software, so anyone else is welcome to do it. If you do, please let me know and I will link to your software from here.