import java.util.concurrent.Semaphore;
import java.util.concurrent.CopyOnWriteArrayList;
+// TODO: REMOVE THIS
+import java.util.Random;
+import java.io.File;
public class IHome implements Speaker {
public static final long RTP_TIMESTAMP_INCREMENT_VALUE = 352L;
public static final long SOURCE_ID = 1326796157;
public static final long SEQUENCE_ID = 0x86b27741;
+ // TODO: REMOVE THIS
+ public static final String MUSIC_FILE_DIRECTORY = "./music/"; // file that the music files are in
private IoTDeviceAddress tcpAddress = null;
private IoTDeviceAddress myAddress = null;
@config private IoTSet<IoTDeviceAddress> speakerAddresses;
+ public IHome(IoTSet<IoTDeviceAddress> devAddresses) {
+ this();
+ speakerAddresses = devAddresses;
+ }
+
public IHome() {
didInit.set(false);
playbackAboutToStart.set(false);
if (keyValue.length == 2) {
if (keyValue[0].equals("server_port")) {
serverPort = Integer.parseInt(keyValue[1]);
+ System.out.println("DEBUG: Server port: " + serverPort);
} else if (keyValue[0].equals("control_port")) {
controlPort = Integer.parseInt(keyValue[1]);
+ System.out.println("DEBUG: Control port: " + controlPort);
} else if (keyValue[0].equals("timing_port")) {
timingPort = Integer.parseInt(keyValue[1]);
+ System.out.println("DEBUG: Timing port: " + timingPort);
}
}
long nanotime = System.nanoTime();
return nanotime;
}
+
+
+ // TODO: REMOVE THIS
+ /**
+ * Prepare the speakers for a new song to start playing
+ */
+ private void prepareNextSong(IHome ih) {
+ System.out.println("Starting Music Prep");
+
+ System.out.println("Stopping all device playback");
+ // stop all devices that are still playing and clear their buffers
+ // they are about to end playback anyways
+ try {
+
+ if (ih.getPlaybackState()) {
+ ih.stopPlayback();
+ }
+ ih.clearData();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ // get the music file names that are in the music files directory
+ File musicFolder = new File(MUSIC_FILE_DIRECTORY);
+ File[] audioFiles = musicFolder.listFiles();
+ List<String> audioFileNames = new ArrayList<String>();
+
+ // put all names in a list
+ for (int i = 0; i < audioFiles.length; i++) {
+ if (audioFiles[i].isFile()) {
+ try {
+ audioFileNames.add(audioFiles[i].getCanonicalPath());
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+ }
+
+ // pick a random file to play
+ Random rand = new Random(System.nanoTime());
+ String audioFilename = audioFileNames.get(rand.nextInt(audioFileNames.size()));
+
+ System.out.println("Going to load audio file");
+ System.out.println(audioFilename);
+
+ // decode the mp3 file
+ System.out.println("Starting Decode");
+ MP3Decoder dec = new MP3Decoder(audioFilename);
+ List<short[]> dat = dec.getDecodedFrames();
+ System.out.println("Ending Decode");
+
+ // count the number of samples
+ int count = 0;
+ for (short[] d : dat) {
+ count += d.length;
+ }
+
+ // make into a single large buffer for 1 large RMI call
+ short[] compressedArray = new short[count];
+ count = 0;
+ for (short[] d : dat) {
+ for (short s : d) {
+ compressedArray[count] = s;
+ count++;
+ }
+ }
+
+ System.out.println("Loading Speakers");
+ // send the new data to all the speakers
+ ih.loadData(compressedArray, 0, compressedArray.length);
+ System.out.println("Done loading a single speaker with data");
+ System.out.println("All Speakers done loading");
+
+ // Attack the speaker
+ System.out.println("Trying to play the song on the speaker");
+ ih.startPlayback();
+ ih.setPosition(0);
+ }
+
+ /* TODO: Uncomment this part to do speaker test
+ public static void main(String[] args) throws Exception {
+
+ System.out.println("Executing main function!");
+ //IoTDeviceAddress iotDevAdd1 = new IoTDeviceAddress("192.168.0.93", 60000, 1024, false, false);
+ //IoTDeviceAddress iotDevAdd2 = new IoTDeviceAddress("192.168.0.90", 60001, -1, false, false);
+ //IoTDeviceAddress iotDevAdd3 = new IoTDeviceAddress("192.168.0.93", 60010, -1, false, true);
+ //IoTDeviceAddress iotDevAdd4 = new IoTDeviceAddress("192.168.0.93", 60011, -1, false, true);
+ //IoTDeviceAddress iotDevAdd5 = new IoTDeviceAddress("192.168.0.93", 60012, -1, false, true);
+ IoTDeviceAddress iotDevAdd1 = new IoTDeviceAddress(args[0], 60000, 1024, false, false);
+ IoTDeviceAddress iotDevAdd2 = new IoTDeviceAddress(args[1], 60001, -1, false, false);
+ IoTDeviceAddress iotDevAdd3 = new IoTDeviceAddress(args[0], 60010, -1, false, true);
+ IoTDeviceAddress iotDevAdd4 = new IoTDeviceAddress(args[0], 60011, -1, false, true);
+ IoTDeviceAddress iotDevAdd5 = new IoTDeviceAddress(args[0], 60012, -1, false, true);
+ Set<IoTDeviceAddress> set = new HashSet<IoTDeviceAddress>();
+ set.add(iotDevAdd1);
+ set.add(iotDevAdd2);
+ set.add(iotDevAdd3);
+ set.add(iotDevAdd4);
+ set.add(iotDevAdd5);
+ IoTSet<IoTDeviceAddress> iotset = new IoTSet<IoTDeviceAddress>(set);
+ IHome ih = new IHome(iotset);
+ ih.init();
+ ih.prepareNextSong(ih);
+ }*/
}