Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Add MP3 capabilities to Java Sound with SPI

The Service Provider Interface adds functionality to Java Sound without recoding

  • Print
  • Feedback
The digital audio world has changed rapidly over the last ten years, introducing all sorts of new and exciting audio file formats: AU, AIF, MIDI, and WAV, to name a few. The recent arrival of the MP3 file format has set the music world on fire, and the trend shows no sign of slowing as new, better-sounding, and more-compact audio formats replace older, less-efficient ones. How is a computer subsystem such as the Java Sound audio system able to cope with those changes?

Thanks to a new feature in Java 2 1.3 -- the Java Service Provider Interface (SPI) -- the JVM provides audio subsystem information at runtime. Java Sound uses the SPI at runtime to provide sound mixers, file readers and writers, and format conversion utilities to a Java sound program. That allows older Java programs, even Java 1.02 programs, to take advantage of the newly added functions with no changes and no recompiling. Indeed, more functions can be added to Java Sound to take advantage of new file formats, popular compression methods, or even hardware-based sound processors.

In this article, we'll look at the SPI illustrated with a real world example: Java Sound extended to read, convert, and play MP3 sound files.

Note: To download the complete source code for this article, see Resources.

To understand the Service Provider Interface (SPI), it helps to think of a JVM as a provider of services to a Java program -- the consumer of those services. The consumer uses a known interface to request a JVM-provided service. For instance, with Java Sound the Java program requests to play an audio file with one of the public sound methods. In Java 2 version 1.3, the AudioSystem queries itself to see if it can handle the given sound file type. If it can, the sound is played. If it cannot, an exception is thrown, typically the sun.audio.InvalidAudioException for older Java audio programs that use the sun.audio or java.applet packages. In contrast, newer Java Sound programs that use the javax.sound package typically throw the javax.sound.sampled.UnsupportedAudioException. Either way, the JVM is telling you it cannot provide the requested service.

In Java 2 version 1.2, the sound subsystem was enhanced to handle audio files of many types: WAV, AIFF, MIDI, and most AU types. With that enhancement -- as if by magic -- the older programs that use the sun.audio or java.applet packages were able to handle new audio file types. That development represented a blessing to Java audio users, but it still did not allow users to extend the JVM. Java audio programs were still limited to the audio file types provided by the JVM maker.

With Java 2 version 1.3's SPI, we see an architected method of extending the JVM. Java Sound knows how to query those service providers and, when presented with an audio file, one of the service providers may indicate that it knows how to read the audio file type or knows how to convert it. Then the sound subsystem uses that service provider to play the sound.

Next, we examine how to add new service providers to take advantage of one popular audio file type, the MP3 or MPEG Layer 3 audio type developed in the Motion Picture Expert Group ISO standard released several years ago.

  • Print
  • Feedback

Resources
  • Download the complete implementation of the Service Provider Interface in zip format. The zip file includes the MP3.jar archive to add to the Java 2 version 1.3 classpath as well as all the source code to make the jar file
    http://www.javaworld.com/jw-11-2000/mp3/jw-1103-mp3.zip
  • Additionally, to play MP3 files with Java, you will need to download the JavaLayer MP3 decoder package and add it to your classpath
    http://www.javazoom.net/javalayer/javalayer.html
  • Matthias Pfisterer developed the Tritonus Java Sound implementation, released under the GNU Library General Public License. I derived some of the auxiliary files for this article (TCircularBuffer and DecodedMpegAudioInputStream for example) from that code, but they are provided here to help make the absolute smallest implementation of an MP3 service provider. A regular contributor to the Java Sound list group, Matthias has helped advanced the knowledge of the Java Sound community tremendously
    http://tritonus.org/
  • Matthias Pfisterer's Website also boasts an entire tutorial of Java Sound examples, which you'll find at
    http://rupert.informatik.uni-stuttgart.de/~pfistere/jsexamples
  • A full explanation of the Service Provider Interface is contained in Part III, Chapter 13 of the Java 2 version 1.3 "Sound API Programmer's Guide," which can be found online on the Java Sound homepage
    http://java.sun.com/products/java-media/sound
  • A more complete implementation of Java Sound, one that decodes multichannel movie soundtracks as well as MP3 sound files, is on Tritonus, located at
    http://tritonus.org/