package de.toman.milight;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.Mixer.Info;

/**
 * With this class you can visualize your music using some group of lights. The
 * lights will change their color and brightness as the music does.
 * 
 * @author Stefan Toman (toman@tum.de)
 */
public class MusicVisualizer {
	/**
	 * The group of lights controlled by this visualizer
	 */
	private Lights lights;

	/**
	 * The line to read the music input from.
	 */
	private TargetDataLine line;

	/**
	 * This constructor creates a new visualizer.
	 * 
	 * @param lights
	 *            the group of lights to be controlled
	 * @param line
	 *            the line to read the music input from
	 */
	public MusicVisualizer(Lights lights, TargetDataLine line) {
		super();
		this.lights = lights;
		this.line = line;
	}

	/**
	 * This function returns the group of lights controlled by this visualizer.
	 * 
	 * @return the group of lights controlled by this visualizer
	 */
	public Lights getLights() {
		return lights;
	}

	/**
	 * This function returns the line to read the music input from.
	 * 
	 * @return the line to read the music input from
	 */
	public TargetDataLine getLine() {
		return line;
	}

	/**
	 * This function returns the default line to listen on. It is searching for
	 * a mixer called "Primary Sound Capture Driver" or returns some line if
	 * none is found with this none. If there is no available line at all it
	 * will return null.
	 * 
	 * @return the line or null if no line is available
	 */
	public static TargetDataLine getDefaultLine() {
		// search for "Primary Sound Capture Driver"
		for (Info mixerInfo : AudioSystem.getMixerInfo()) {
			javax.sound.sampled.Line.Info[] lineInfos = AudioSystem.getMixer(
					mixerInfo).getTargetLineInfo();
			try {
				if (lineInfos.length > 0
						&& mixerInfo.getName().equals(
								"Primary Sound Capture Driver")) {
					return (TargetDataLine) AudioSystem.getLine(lineInfos[0]);
				}
			} catch (LineUnavailableException e) {
				// line unavailable -> ignore mixer
			}
		}

		// search for some other mixer
		for (Info mixerInfo : AudioSystem.getMixerInfo()) {
			javax.sound.sampled.Line.Info[] lineInfos = AudioSystem.getMixer(
					mixerInfo).getTargetLineInfo();
			try {
				if (lineInfos.length > 0) {
					return (TargetDataLine) AudioSystem.getLine(lineInfos[0]);
				}
			} catch (LineUnavailableException e) {
				// line unavailable -> ignore mixer
			}
		}

		// no line found at all
		return null;
	}
}

@@ -20,16 +20,86 @@
 light;%0A%0A
+import java.math.BigInteger;%0A%0Aimport javax.sound.sampled.AudioFormat;%0A
 import j
@@ -210,30 +210,26 @@
 sampled.
-TargetDataLine
+Mixer.Info
 ;%0Aimport
@@ -249,26 +249,30 @@
 sampled.
-Mixer.Info
+TargetDataLine
 ;%0A%0A/**%0A 
@@ -2669,11 +2669,773 @@
 null;%0A%09%7D
+%0A%0A%09/**%0A%09 * This function converts a raw array of bytes to an integer value. It will%0A%09 * be converted in the corresponding way to the%0A%09 * %7B@link AudioFormat#isBigEndian()%7D value of the%0A%09 * %7B@link MusicVisualizer#line%7D attribute.%0A%09 * %0A%09 * @param data%0A%09 *            is the array of values to converted%0A%09 * @return is the integer represented by the byte array%0A%09 */%0A%09public BigInteger convertByteArrayToInteger(byte%5B%5D data) %7B%0A%09%09// create variables%0A%09%09BigInteger ret = BigInteger.ZERO;%0A%09%09BigInteger power = BigInteger.valueOf(256);%0A%0A%09%09// read all parts%0A%09%09for (int i = 0; i %3C data.length; i++) %7B%0A%09%09%09ret = ret.multiply(power).add(%0A%09%09%09%09%09BigInteger.valueOf(data%5Bline.getFormat().isBigEndian() ? i%0A%09%09%09%09%09%09%09: (data.length - 1 - i)%5D & 0xff));%0A%09%09%7D%0A%0A%09%09// return%0A%09%09return ret;%0A%09%7D
 %0A%7D%0A

add function to convert little- resp. big-endian arrays