//# 0 errors, 149 messages
//#
/*
    //#AudioPlayer.java:1:1: class: com.dmdirc.addons.audio.AudioPlayer$AudioType
    //#AudioPlayer.java:1:1: class: com.dmdirc.addons.audio.AudioPlayer$1
    //#AudioPlayer.java:1:1: class: com.dmdirc.addons.audio.AudioPlayer
    //#AudioPlayer.java:1:1: method: com.dmdirc.addons.audio.AudioPlayer.com.dmdirc.addons.audio.AudioPlayer__static_init
 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.audio;

import java.io.File;
import java.io.IOException;
import java.applet.AudioClip;
import java.applet.Applet;

import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.AudioSystem;

import java.net.MalformedURLException;

/**
 * The AudioPlayer handles the playing of the audio
 *
 * @author Shane "Dataforce" Mc Cormack
 */
public final class AudioPlayer implements Runnable {

    /** The AudioType enum */
	public static enum AudioType { WAV, INVALID; }
    //#AudioPlayer.java:43: method: AudioPlayer$AudioType[] com.dmdirc.addons.audio.AudioPlayer$AudioType.values()
    //#input(AudioPlayer$AudioType[] values()): $VALUES
    //#input(AudioPlayer$AudioType[] values()): $VALUES.length
    //#input(AudioPlayer$AudioType[] values()): $VALUES[0..2]
    //#output(AudioPlayer$AudioType[] values()): new AudioPlayer$AudioType[](values#1) num objects
    //#output(AudioPlayer$AudioType[] values()): return_value.length
    //#output(AudioPlayer$AudioType[] values()): return_value[0..2]
    //#output(AudioPlayer$AudioType[] values()): return_value
    //#new obj(AudioPlayer$AudioType[] values()): new AudioPlayer$AudioType[](values#1)
    //#pre[1] (AudioPlayer$AudioType[] values()): (soft) init'ed($VALUES[0..2])
    //#post(AudioPlayer$AudioType[] values()): return_value == &new AudioPlayer$AudioType[](values#1)
    //#post(AudioPlayer$AudioType[] values()): new AudioPlayer$AudioType[](values#1) num objects == 1
    //#post(AudioPlayer$AudioType[] values()): return_value.length == 2
    //#post(AudioPlayer$AudioType[] values()): return_value[0..2] == One-of{$VALUES[0..2], undefined}
    //#AudioPlayer.java:43: end of method: AudioPlayer$AudioType[] com.dmdirc.addons.audio.AudioPlayer$AudioType.values()
    //#AudioPlayer.java:43: method: AudioPlayer$AudioType com.dmdirc.addons.audio.AudioPlayer$AudioType.valueOf(String)
    //#input(AudioPlayer$AudioType valueOf(String)): __Descendant_Table[com/dmdirc/addons/audio/AudioPlayer$AudioType]
    //#input(AudioPlayer$AudioType valueOf(String)): __Descendant_Table[others]
    //#input(AudioPlayer$AudioType valueOf(String)): name
    //#output(AudioPlayer$AudioType valueOf(String)): return_value
    //#presumption(AudioPlayer$AudioType valueOf(String)): java.lang.Enum:valueOf(...).__Tag@43 == com/dmdirc/addons/audio/AudioPlayer$AudioType
    //#post(AudioPlayer$AudioType valueOf(String)): init'ed(return_value)
    //#AudioPlayer.java:43: end of method: AudioPlayer$AudioType com.dmdirc.addons.audio.AudioPlayer$AudioType.valueOf(String)
    //#AudioPlayer.java:43: method: void com.dmdirc.addons.audio.AudioPlayer$AudioType.com.dmdirc.addons.audio.AudioPlayer$AudioType(String, int)
    //#input(void com.dmdirc.addons.audio.AudioPlayer$AudioType(String, int)): Param_1
    //#input(void com.dmdirc.addons.audio.AudioPlayer$AudioType(String, int)): Param_2
    //#input(void com.dmdirc.addons.audio.AudioPlayer$AudioType(String, int)): this
    //#AudioPlayer.java:43: end of method: void com.dmdirc.addons.audio.AudioPlayer$AudioType.com.dmdirc.addons.audio.AudioPlayer$AudioType(String, int)
    //#AudioPlayer.java:43: method: com.dmdirc.addons.audio.AudioPlayer$AudioType.com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init
    //#output(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): $VALUES
    //#output(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): INVALID
    //#output(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): WAV
    //#output(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): __Descendant_Table[com/dmdirc/addons/audio/AudioPlayer$AudioType]
    //#output(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#1) num objects
    //#output(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): WAV.__Tag
    //#output(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#2) num objects
    //#output(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): INVALID.__Tag
    //#output(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): new AudioPlayer$AudioType[](AudioPlayer$AudioType__static_init#3) num objects
    //#output(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): $VALUES.length
    //#output(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): $VALUES[0]
    //#output(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): $VALUES[1]
    //#new obj(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#1)
    //#new obj(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#2)
    //#new obj(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): new AudioPlayer$AudioType[](AudioPlayer$AudioType__static_init#3)
    //#post(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): $VALUES == &new AudioPlayer$AudioType[](AudioPlayer$AudioType__static_init#3)
    //#post(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): INVALID == &new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#2)
    //#post(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): $VALUES[1] == &new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#2)
    //#post(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): WAV == &new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#1)
    //#post(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): $VALUES[0] == &new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#1)
    //#post(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): __Descendant_Table[com/dmdirc/addons/audio/AudioPlayer$AudioType] == &__Dispatch_Table
    //#post(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#1) num objects == 1
    //#post(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#2) num objects == 1
    //#post(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): new AudioPlayer$AudioType[](AudioPlayer$AudioType__static_init#3) num objects == 1
    //#post(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): WAV.__Tag == com/dmdirc/addons/audio/AudioPlayer$AudioType
    //#post(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): INVALID.__Tag == com/dmdirc/addons/audio/AudioPlayer$AudioType
    //#post(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): $VALUES.length == 2
    //#unanalyzed(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init): Effects-of-calling:java.lang.Enum
    //#AudioPlayer.java:43: end of method: com.dmdirc.addons.audio.AudioPlayer$AudioType.com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init
	
	/** The file object of the file to play */
	final File myFile;
	
	/**
	 * Create the AudioPlayer
	 *
	 * @param file The file to play
	 */
	public AudioPlayer(final File file) {
    //#AudioPlayer.java:53: method: void com.dmdirc.addons.audio.AudioPlayer.com.dmdirc.addons.audio.AudioPlayer(File)
    //#input(void com.dmdirc.addons.audio.AudioPlayer(File)): file
    //#input(void com.dmdirc.addons.audio.AudioPlayer(File)): this
    //#output(void com.dmdirc.addons.audio.AudioPlayer(File)): this.myFile
    //#post(void com.dmdirc.addons.audio.AudioPlayer(File)): this.myFile == file
    //#post(void com.dmdirc.addons.audio.AudioPlayer(File)): init'ed(this.myFile)
		myFile = file;
	}
    //#AudioPlayer.java:55: end of method: void com.dmdirc.addons.audio.AudioPlayer.com.dmdirc.addons.audio.AudioPlayer(File)
	
	/**
	 * Play this AudioPlayer.
	 */
	public void play() {
		new Thread(this).start();
    //#AudioPlayer.java:61: method: void com.dmdirc.addons.audio.AudioPlayer.play()
    //#input(void play()): this
	}
    //#AudioPlayer.java:62: end of method: void com.dmdirc.addons.audio.AudioPlayer.play()
	
	/**
	 * Run this AudioPlayer (Should not be invoked directly).
	 */
    @Override
	public void run() {
		final AudioType type = getAudioType(myFile);
    //#AudioPlayer.java:69: method: void com.dmdirc.addons.audio.AudioPlayer.run()
    //#input(void run()): com.dmdirc.addons.audio.AudioPlayer$1__static_init.new int[](AudioPlayer$1__static_init#1).length
    //#input(void run()): com.dmdirc.addons.audio.AudioPlayer$1__static_init.new int[](AudioPlayer$1__static_init#1)[0..4_294_967_295]
    //#input(void run()): com/dmdirc/addons/audio/AudioPlayer$1.$SwitchMap$com$dmdirc$addons$audio$AudioPlayer$AudioType
    //#input(void run()): com/dmdirc/addons/audio/AudioPlayer$AudioType.INVALID
    //#input(void run()): com/dmdirc/addons/audio/AudioPlayer$AudioType.WAV
    //#input(void run()): this
    //#input(void run()): this.myFile
    //#pre[1] (void run()): (soft) init'ed(com.dmdirc.addons.audio.AudioPlayer$1__static_init.new int[](AudioPlayer$1__static_init#1)[0..4_294_967_295])
    //#pre[3] (void run()): (soft) this.myFile != null
    //#presumption(void run()): com.dmdirc.addons.audio.AudioPlayer_AudioType:ordinal(...)@70 in {0,1}
    //#unanalyzed(void run()): Effects-of-calling:javax.sound.sampled.AudioSystem:getAudioInputStream
    //#unanalyzed(void run()): Effects-of-calling:java.lang.Throwable:__curr_excep_obj
    //#unanalyzed(void run()): Effects-of-calling:java.io.File:toURI
    //#unanalyzed(void run()): Effects-of-calling:java.net.URI:toURL
    //#unanalyzed(void run()): Effects-of-calling:java.applet.Applet:newAudioClip
    //#unanalyzed(void run()): Effects-of-calling:java.applet.AudioClip:play
    //#test_vector(void run()): com.dmdirc.addons.audio.AudioPlayer$1__static_init.new int[](AudioPlayer$1__static_init#1)[0..4_294_967_295]: {1}, {-2_147_483_648..0, 2..4_294_967_295}
		switch (type) {
    //#AudioPlayer.java:70: method: com.dmdirc.addons.audio.AudioPlayer$1.com.dmdirc.addons.audio.AudioPlayer$1__static_init
    //#AudioPlayer.java:70: Warning: method not available - call not analyzed
    //#    call on int com.dmdirc.addons.audio.AudioPlayer$AudioType:ordinal()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.audio.AudioPlayer$1
    //#    method: com.dmdirc.addons.audio.AudioPlayer$1__static_init
    //#    unanalyzed callee: int com.dmdirc.addons.audio.AudioPlayer$AudioType:ordinal()
    //#input(com.dmdirc.addons.audio.AudioPlayer$1__static_init): com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init.new AudioPlayer$AudioType[](AudioPlayer$AudioType__static_init#3).length
    //#input(com.dmdirc.addons.audio.AudioPlayer$1__static_init): com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init.new AudioPlayer$AudioType[](AudioPlayer$AudioType__static_init#3)[0..2]
    //#input(com.dmdirc.addons.audio.AudioPlayer$1__static_init): com/dmdirc/addons/audio/AudioPlayer$AudioType.$VALUES
    //#input(com.dmdirc.addons.audio.AudioPlayer$1__static_init): com/dmdirc/addons/audio/AudioPlayer$AudioType.WAV
    //#output(com.dmdirc.addons.audio.AudioPlayer$1__static_init): $SwitchMap$com$dmdirc$addons$audio$AudioPlayer$AudioType
    //#output(com.dmdirc.addons.audio.AudioPlayer$1__static_init): __Descendant_Table[com/dmdirc/addons/audio/AudioPlayer$1]
    //#output(com.dmdirc.addons.audio.AudioPlayer$1__static_init): new int[](AudioPlayer$1__static_init#1) num objects
    //#output(com.dmdirc.addons.audio.AudioPlayer$1__static_init): $SwitchMap$com$dmdirc$addons$audio$AudioPlayer$AudioType.length
    //#output(com.dmdirc.addons.audio.AudioPlayer$1__static_init): $SwitchMap$com$dmdirc$addons$audio$AudioPlayer$AudioType[0..4_294_967_295]
    //#new obj(com.dmdirc.addons.audio.AudioPlayer$1__static_init): new int[](AudioPlayer$1__static_init#1)
    //#pre[1] (com.dmdirc.addons.audio.AudioPlayer$1__static_init): (soft) init'ed(com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init.new AudioPlayer$AudioType[](AudioPlayer$AudioType__static_init#3)[0..2])
    //#presumption(com.dmdirc.addons.audio.AudioPlayer$1__static_init): com.dmdirc.addons.audio.AudioPlayer_AudioType:ordinal(...)@70 in {0,1}
    //#presumption(com.dmdirc.addons.audio.AudioPlayer$1__static_init): com.dmdirc.addons.audio.AudioPlayer_AudioType:ordinal(...)@70 - values(...).length in range
    //#post(com.dmdirc.addons.audio.AudioPlayer$1__static_init): $SwitchMap$com$dmdirc$addons$audio$AudioPlayer$AudioType == &new int[](AudioPlayer$1__static_init#1)
    //#post(com.dmdirc.addons.audio.AudioPlayer$1__static_init): __Descendant_Table[com/dmdirc/addons/audio/AudioPlayer$1] == &__Dispatch_Table
    //#post(com.dmdirc.addons.audio.AudioPlayer$1__static_init): new int[](AudioPlayer$1__static_init#1) num objects == 1
    //#post(com.dmdirc.addons.audio.AudioPlayer$1__static_init): $SwitchMap$com$dmdirc$addons$audio$AudioPlayer$AudioType.length == 2
    //#post(com.dmdirc.addons.audio.AudioPlayer$1__static_init): $SwitchMap$com$dmdirc$addons$audio$AudioPlayer$AudioType[0..4_294_967_295] in {0,1}, if init'ed
    //#AudioPlayer.java:70: end of method: com.dmdirc.addons.audio.AudioPlayer$1.com.dmdirc.addons.audio.AudioPlayer$1__static_init
    //#AudioPlayer.java:70: Warning: method not available - call not analyzed
    //#    call on int com.dmdirc.addons.audio.AudioPlayer$AudioType:ordinal()
    //#    severity: INFORMATIONAL
    //#    class: com.dmdirc.addons.audio.AudioPlayer
    //#    method: void run()
    //#    unanalyzed callee: int com.dmdirc.addons.audio.AudioPlayer$AudioType:ordinal()
			case WAV:
				playWav();
				break;
			default:
				break;
		}
	}
    //#AudioPlayer.java:77: end of method: void com.dmdirc.addons.audio.AudioPlayer.run()
	
	/**
	 * Check if this File is a supported file type
	 *
	 * @param file the File to check
	 * @return true if playable, else false.
	 */
	public static boolean isValid(final File file) {
		final AudioType type = getAudioType(file);
    //#AudioPlayer.java:86: method: bool com.dmdirc.addons.audio.AudioPlayer.isValid(File)
    //#input(bool isValid(File)): com/dmdirc/addons/audio/AudioPlayer$AudioType.INVALID
    //#input(bool isValid(File)): com/dmdirc/addons/audio/AudioPlayer$AudioType.WAV
    //#input(bool isValid(File)): file
    //#output(bool isValid(File)): return_value
    //#post(bool isValid(File)): init'ed(return_value)
    //#unanalyzed(bool isValid(File)): Effects-of-calling:javax.sound.sampled.AudioSystem:getAudioInputStream
    //#unanalyzed(bool isValid(File)): Effects-of-calling:java.lang.Throwable:__curr_excep_obj
		return (type != AudioType.INVALID);
    //#AudioPlayer.java:87: end of method: bool com.dmdirc.addons.audio.AudioPlayer.isValid(File)
	}
	
	/**
	 * Get the AudioType of a given file
	 *
	 * @param file the File to check
	 * @return AudioType for this file.
	 */
	public static AudioType getAudioType(final File file) {
		AudioType type;
		try {
			AudioSystem.getAudioInputStream(file);
    //#AudioPlayer.java:99: method: AudioPlayer$AudioType com.dmdirc.addons.audio.AudioPlayer.getAudioType(File)
    //#input(AudioPlayer$AudioType getAudioType(File)): com/dmdirc/addons/audio/AudioPlayer$AudioType.INVALID
    //#input(AudioPlayer$AudioType getAudioType(File)): com/dmdirc/addons/audio/AudioPlayer$AudioType.WAV
    //#input(AudioPlayer$AudioType getAudioType(File)): file
    //#output(AudioPlayer$AudioType getAudioType(File)): return_value
    //#post(AudioPlayer$AudioType getAudioType(File)): return_value == One-of{&com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init.new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#1), &com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init.new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#2)}
    //#post(AudioPlayer$AudioType getAudioType(File)): return_value in Addr_Set{&com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init.new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#1),&com.dmdirc.addons.audio.AudioPlayer$AudioType__static_init.new AudioPlayer$AudioType(AudioPlayer$AudioType__static_init#2)}
			type = AudioType.WAV;
		} catch (UnsupportedAudioFileException e) {
			type = AudioType.INVALID;
		} catch (IOException e) {
			type = AudioType.INVALID;
		}
		return type;
    //#AudioPlayer.java:106: end of method: AudioPlayer$AudioType com.dmdirc.addons.audio.AudioPlayer.getAudioType(File)
	}
	
	
	/**
	 * Play the file as a wav file, using the Applet class.
	 * (This code seems to work better than the non-applet version, but can't play
	 * streams)
	 */
	private void playWav() {
		try {
			final AudioClip ac = Applet.newAudioClip(myFile.toURI().toURL());
    //#AudioPlayer.java:117: method: void com.dmdirc.addons.audio.AudioPlayer.playWav()
    //#input(void playWav()): this
    //#input(void playWav()): this.myFile
    //#pre[2] (void playWav()): (soft) this.myFile != null
    //#presumption(void playWav()): java.io.File:toURI(...)@117 != null
    //#test_vector(void playWav()): java.applet.Applet:newAudioClip(...)@117: Addr_Set{null}, Inverse{null}
			if (ac != null) { ac.play(); }
		} catch (MalformedURLException e) { }
	}
    //#AudioPlayer.java:120: end of method: void com.dmdirc.addons.audio.AudioPlayer.playWav()
}
    //#AudioPlayer.java:: end of class: com.dmdirc.addons.audio.AudioPlayer$AudioType
    //#AudioPlayer.java:: end of class: com.dmdirc.addons.audio.AudioPlayer$1
    //#output(com.dmdirc.addons.audio.AudioPlayer__static_init): __Descendant_Table[com/dmdirc/addons/audio/AudioPlayer]
    //#output(com.dmdirc.addons.audio.AudioPlayer__static_init): __Dispatch_Table.play()V
    //#output(com.dmdirc.addons.audio.AudioPlayer__static_init): __Dispatch_Table.playWav()V
    //#output(com.dmdirc.addons.audio.AudioPlayer__static_init): __Dispatch_Table.run()V
    //#post(com.dmdirc.addons.audio.AudioPlayer__static_init): __Descendant_Table[com/dmdirc/addons/audio/AudioPlayer] == &__Dispatch_Table
    //#post(com.dmdirc.addons.audio.AudioPlayer__static_init): __Dispatch_Table.play()V == &play
    //#post(com.dmdirc.addons.audio.AudioPlayer__static_init): __Dispatch_Table.playWav()V == &playWav
    //#post(com.dmdirc.addons.audio.AudioPlayer__static_init): __Dispatch_Table.run()V == &run
    //#AudioPlayer.java:: end of method: com.dmdirc.addons.audio.AudioPlayer.com.dmdirc.addons.audio.AudioPlayer__static_init
    //#AudioPlayer.java:: end of class: com.dmdirc.addons.audio.AudioPlayer
