MuXM SPI

MuXM SPI is an implementation of the JavaSound Service Provider Interface based on MuXM which allows to add MOD/XM playing support to all Java applications without having to deal with MuXM API.

Example

You can find below a simple class that plays audio with JavaSound SPI (Service Provider Interface) from given file names.

Note that this class can also be used to play MP3, Ogg Vorbis, FLAC, etc (provided you add the appropriate SPI libraries to the classpath)

Code from ONJava article: Extend JavaSound to Play MP3, Ogg Vorbis, and More

public class SpiPlayerExample {

    /** Plays audio from given file names. */
    public static void main(String[] args) {
        // Check for given sound file names.
        if (args.length < 1) {
            System.out.println("SpiModulePlayerExample usage:");
            System.out.println("\tjava SpiModulePlayerExample <sound file names>*");
            System.exit(0);
        }
         
        SpiPlayerExample player = new SpiPlayerExample();

        try {
            // Process arguments.
            for (int i = 0; i < args.length; i++ ) {
                player.play( args[ i ] );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // Must exit explicitly since audio creates non-daemon threads.
        System.exit(0);
    } // main

    public void play(String fileName) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
        File file = new File(fileName);
        AudioInputStream in = AudioSystem.getAudioInputStream(file);
        AudioInputStream ain = null;
        AudioFormat baseFormat = in.getFormat();
        AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, baseFormat.getSampleRate(), 16,
                baseFormat.getChannels(), baseFormat.getChannels() * 2, baseFormat.getSampleRate(), false);
        ain = AudioSystem.getAudioInputStream(decodedFormat, in);
        // Play now.
        rawplay(decodedFormat, ain);
        in.close();
    }

    private void rawplay(AudioFormat targetFormat, AudioInputStream din) throws IOException, LineUnavailableException {
        byte[] data = new byte[4096];
        SourceDataLine line = getLine(targetFormat);
        if (line != null) {
            // Start
            line.start();
            int nBytesRead = 0, nBytesWritten = 0;
            while (nBytesRead != -1) {
                nBytesRead = din.read(data, 0, data.length);
                if (nBytesRead != -1)
                    nBytesWritten = line.write(data, 0, nBytesRead);
            }
            // Stop
            line.drain();
            line.stop();
            line.close();
            din.close();
        }
    }

    private SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException {
        SourceDataLine res = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        res = (SourceDataLine) AudioSystem.getLine(info);
        res.open(audioFormat);
        return res;
    }
}