/* MuhmuDevice.java - An audiodevice which uses MuhmuAudio
 * Copyright (C) 2000 Fredrik Ehnbom
 *
 * This library 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 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package silence.devices;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Hashtable;

import org.komplex.audio.AudioConstants;
import org.komplex.audio.AudioOutDevice;
import org.komplex.audio.AudioOutDeviceFactory;
import org.komplex.audio.PullAudioSource;

import silence.AudioException;
import silence.format.AudioFormat;

/**
 * An audiodevice which uses MuhmuAudio by Jarno Heikkinen
 * &lt;<a href="mailto:jarnoh@komplex.org">jarnoh@komplex.org</a>&gt;.<br>
 * For more information about MuhmuAudio please visit
 * <a href="http://muhmuaudio.sourceforge.net">http://muhmuaudio.sourceforge.net</a>
 * @author Fredrik Ehnbom
 * @version $Id: MuhmuDevice.java,v 1.4 2000/07/21 09:40:00 quarn Exp $
 */
public class MuhmuDevice extends AudioDevice {

	private AudioOutDevice  device  = null;
	private AudioFormat 	     format = null;

	/**
	 * Create a new MuhmuDevice
	 */
	public MuhmuDevice() {
	}

	/**
	 * Returns the name of this device
	 */
	public String getName() {
		return "MuhmuAudio";
	}

	/**
	 * Returns the home url for this device
	 */
	public String getUrl() {
		 return "http://muhmuaudio.sourceforge.net";
	}

	/**
	 * Init the AudioDevice
	 * @param sound If we should play the sound or if we are in nosound mode
	 */
	public void init(boolean sound) throws AudioException {
		AudioOutDeviceFactory factory = new AudioOutDeviceFactory();

		if (sound == false) {
			String[] args = {"-nosound"};
			factory.initArgs(args);
		}

		while (device == null) {
			try {
				device = factory.getAudioOutDevice();

				if (device == null) {
					break;
				}

				// directsound requires a component reference
				Hashtable hash = new Hashtable();
				hash.put(AudioConstants.PROP_COMPONENT, this);
				device.setProperties(hash);

				device.init(device.FORMAT_PCM44K16S);

				break;
			} catch(Throwable e) {
				factory.disableDevice(device);
				device = null;
			}
		}

		if (device == null) {
			throw new AudioException("Could not find a device to use...");
		}
	}

	/**
	 * Start playing the file
	 * @param file The file to play
	 * @param loop Wheter to loop or not
	 */
	public void play(String file, boolean loop) throws AudioException {
		if (file.indexOf(".") == -1) throw new AudioException(file + " can not be played by this device");

		String end = file.substring(file.lastIndexOf("."), file.length());

		// get the AudioFormat for this file
		format = AudioFormat.getFormat(end);

		if (format == null) throw new AudioException(file + " can not be played by this device");

		// load the file
		try {
			format.load(file);
		} catch (Exception e) {
			throw new AudioException(e.toString());
		}

		device.setPullSource(format);

		try {
			device.start();
		} catch (org.komplex.audio.AudioException e) {
			throw new AudioException(e.toString());
		}
	}

	/**
	 * Stop playing the file
	 */
	public void stop() {
		device.stop();
	}

	/**
	 * Pause the playing of the file
	 */
	public void pause() {}

	/**
	 * Sets the volume
	 * @param volume The new volume
	 */
	public void setVolume(int volume) {}

	/**
	 * Close and cleanup
	 */
	public void close() {}
}
/*
 * ChangeLog:
 * $Log: MuhmuDevice.java,v $
 * Revision 1.4  2000/07/21 09:40:00  quarn
 * now uses the AudioFormat system
 *
 * Revision 1.3  2000/06/25 18:39:08  quarn
 * removed the sync method
 *
 * Revision 1.2  2000/06/20 23:22:05  quarn
 * no need to throw an exception on pause
 *
 */

@@ -1551,29 +1551,29 @@
 v 1.
-4
+5
  2000/0
-7/21 09:40:00
+8/20 17:55:48
  qua
@@ -1849,15 +1849,16 @@
 ing 
-getName
+toString
 () %7B
@@ -2883,21 +2883,13 @@
 %09 * 
-Start playing
+Loads
  the
@@ -2926,50 +2926,12 @@
  to 
-play%0A%09 * @param loop Wheter to loop or not
+load
 %0A%09 *
@@ -2945,20 +2945,20 @@
 ic void 
-play
+load
 (String 
@@ -2965,22 +2965,8 @@
 file
-, boolean loop
 ) th
@@ -3327,24 +3327,73 @@
  device%22);%0A%0A
+%09%09format.setSampleRate(device.getSampleRate());%0A%0A
 %09%09// load th
@@ -3535,16 +3535,150 @@
 ormat);%0A
+%09%7D%0A%0A%09/**%0A%09 * Start playing the file%0A%09 * @param loop Wheter to loop or not%0A%09 */%0A%09public void play(boolean loop) throws AudioException %7B
 %0A%09%09try %7B
@@ -4152,16 +4152,108 @@
 ava,v $%0A
+ * Revision 1.5  2000/08/20 17:55:48  quarn%0A * split play into play and load, some fixes%0A *%0A
  * Revis

split play into play and load, some fixes