/*
Copyright (c) 2006, Geomatics and Cartographic Research Centre, Carleton 
University All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions are met:

 - Redistributions of source code must retain the above copyright notice, 
   this list of conditions and the following disclaimer.
 - Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
 - Neither the name of the Geomatics and Cartographic Research Centre, 
   Carleton University nor the names of its contributors may be used to 
   endorse or promote products derived from this software without specific 
   prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.

$Id$
*/
package ca.carleton.gcrc.atlas;

import java.net.MalformedURLException;
import java.net.URL;

import javax.sound.sampled.*;


/**
 * @author amoshayes
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SoundTest implements LineListener {
	
	Clip m_currentClip;
    Thread thread;
	
	public static void main(String[] args) {
		
		SoundTest soundtest = new SoundTest();
		URL url;
		try {
			url = new URL("http://devel0.gcrc.carleton.ca/~amoshayes/test.au");
			System.out.println("Loading Sound...");
			soundtest.loadSound(url);
			System.out.println("Sound Loaded.");
			soundtest.playSound();
			System.out.println("Sound played.");
		} catch (MalformedURLException e) {
			System.out.println("Error.");
			e.printStackTrace();
		}
	}
	
	public boolean loadSound(URL URL_) {
		
		double duration;
		AudioInputStream currentSound;
		String currentName;
		
		try {
			currentSound = AudioSystem.getAudioInputStream(URL_);
		} catch (Exception ex) { 
			System.out.println("Unsupported audio file.");
			ex.printStackTrace(); 
			currentSound = null;
			return false;
		}
		
		

		
		
		try {
			AudioInputStream stream = currentSound;
			AudioFormat format = stream.getFormat();
			
			/**
			 * we can't yet open the device for ALAW/ULAW playback,
			 * convert ALAW/ULAW to PCM
			 */
			if ((format.getEncoding() == AudioFormat.Encoding.ULAW) ||
					(format.getEncoding() == AudioFormat.Encoding.ALAW)) 
			{
				AudioFormat tmp = new AudioFormat(
						AudioFormat.Encoding.PCM_SIGNED, 
						format.getSampleRate(),
						format.getSampleSizeInBits() * 2,
						format.getChannels(),
						format.getFrameSize() * 2,
						format.getFrameRate(),
						true);
				stream = AudioSystem.getAudioInputStream(tmp, stream);
				format = tmp;
			}
			DataLine.Info info = new DataLine.Info(
					Clip.class, 
					stream.getFormat(), 
					((int) stream.getFrameLength() *
							format.getFrameSize()));
			
			Clip clip = (Clip) AudioSystem.getLine(info);
			clip.addLineListener(this);
			clip.open(stream);
			m_currentClip = clip;
		} catch (Exception ex) { 
			ex.printStackTrace(); 
			currentSound = null;
			return false;
		}
		
		return true;
	}
	
	 public void playSound() {
       //setGain();
            Clip clip = m_currentClip;
            clip.start();
            while (clip.isActive()) {
                try { Thread.sleep(99); } catch (Exception e) {break;}
            }
            clip.stop();
            clip.close();
        m_currentClip = null;
    }


	/* (non-Javadoc)
	 * @see javax.sound.sampled.LineListener#update(javax.sound.sampled.LineEvent)
	 */
	public void update(LineEvent arg0) {
		
	}
}