+
+
+ // 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);
+ }*/