Sophie

Sophie

distrib > Fedora > 13 > i386 > by-pkgid > 2cb68dd07908498b6b79003d5a0cc526 > files > 15

jmod-0.9-2.fc12.i686.rpm

/*
 *  (c) 2006 by VWP / Oxy
 *  (c) 2005 by Torkjel Hongve
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Library General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *----------------------------------------------------------------------
 */

package examples.cli;

import com.vwp.sound.mod.modplay.ThreadedPlayer;
import com.vwp.sound.mod.sound.output.JavaSoundOutput;
import com.vwp.sound.mod.sound.output.Output;
import com.vwp.sound.mod.sound.output.SoundDataFormat;

/**
 * This example demonstrates how a module player can be created in the least possible amount of
 * code. This is very similar to the {@link com.vwp.sound.mod.modplay.example.cli.SimplePlayer} (take a look
 * at that one first, it's better documented), but uses the asynchrounus version of the player
 * interface ({@link com.vwp.sound.mod.modplay.ThreadedPlayer}).
 * @author torkjel
 */
public class SimpleThreadedPlayer {

    public static final int BITS = 16;
    public static final int RATE = 44100;
    public static final int CHANNELS = 2;
    public static final boolean INTERPOLATE = true;
    public static final int BUFFERSIZE = 500;

    /**
     * main method. The player is expecting to be invoked with one argument on the command line,
     * the filename of the module to play
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        if (args.length == 0) {
            System.out.println("Usage:\n\tjava -cp jmod-<version>.jar " +
                    "examples.cli.SimpleThreadedPlayer <module file>");
            System.exit(1);
        }

        String module = args[0];

        SoundDataFormat format = new SoundDataFormat(BITS, RATE, CHANNELS);

        Output out = new JavaSoundOutput(format, BUFFERSIZE);

        ThreadedPlayer player = new ThreadedPlayer();

        player.init(out, INTERPOLATE);

        player.load(module);

        player.start();

        while (player.isRunning()) {
            // do usefull stuff here or just sleep a bit.
            try { Thread.sleep(1); } catch (InterruptedException e) { }

            // if you decide you've had enough, call player.stop() to stop the music (don't
            // forget to call player.close() also...) :
            /*
             * if (something) {
             *     player.stop();
             *     break;
             * }
             */
        }

        if (player.hasFailed())
            player.getFailiureCause().printStackTrace();

        player.close();
    }
}