Commit a760a147 authored by Abdu Mohamdy's avatar Abdu Mohamdy
Browse files

Added playQueue, moveInQueue(), and Playlist::getPlaylistLength() -- UNTESTED

parent f388b4bb
package engine;
import com.google.gson.JsonArray;
import model.FlightModel;
......@@ -7,6 +8,7 @@ import model.FlyerConfig;
import java.io.File;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
......@@ -17,9 +19,9 @@ import heronarts.lx.pattern.LXPattern;
import heronarts.lx.clip.LXClip;
/**
* Central controller for a FLIGHT installation, responsible for
* loading and running playlists, loading the available Patterns,
* and configuring output to generate Python code.
* Central controller for a FLIGHT installation, responsible for loading and
* running playlists, loading the available Patterns, and configuring output to
* generate Python code.
*
* @author Philip Levis <pal@cs.stanford.edu>
* @author Moses Swai
......@@ -52,6 +54,13 @@ public abstract class FlightEngine implements FlightPlaylist {
// having to poke at a channel.
final private ArrayList<LXPattern> patterns;
// The queue of playlists to be played
// TODO: Sync up with Ashley to change the name to what she has.
final private ArrayList<Playlist> playQueue;
// Indices indicating which playlist is playing and which is selected.
final private int playIndex;
// Helper class that generates Python code to control FLIGHT based
// on the state of the FlightModel.
private Generator generator;
......@@ -63,7 +72,8 @@ public abstract class FlightEngine implements FlightPlaylist {
this.lx = lx;
this.patterns = new ArrayList<LXPattern>();
this.channels = new ArrayList<LXChannel>();
this.playQueue = new ArrayList<Playlist>();
/* All engine configuration */
configureGeneratorOutput();
configureChannels();
......@@ -78,39 +88,37 @@ public abstract class FlightEngine implements FlightPlaylist {
* @return The FlightModel
*/
public FlightModel getModel() {
return model;
return model;
}
/**
* Returns an array with all the flyer configurations
* This array can be used to save the current flyer states
* to a .json file using the IO class
* Returns an array with all the flyer configurations This array can be used to
* save the current flyer states to a .json file using the IO class
*
* @return A FlyerConfig array
*/
public FlyerConfig[] getFlyerConfigs() {
return model.getFlyerConfigs();
return model.getFlyerConfigs();
}
/**
* Returns the number of patterns in each channel.
* This assumes that every channel has the same patterns
* Returns the number of patterns in each channel. This assumes that every
* channel has the same patterns
*
* @return The number of patterns
*/
public int numPatterns() {
return this.patterns.size();
return this.patterns.size();
}
/**
* Returns the number of channels in the LXMixerEngine
* NOTE: This does not count the master channel
* Returns the number of channels in the LXMixerEngine NOTE: This does not count
* the master channel
*
* @return The number of channels
*/
public int numChannels() {
return this.lx.engine.mixer.channels.size();
return this.lx.engine.mixer.channels.size();
}
/**
......@@ -123,8 +131,8 @@ public abstract class FlightEngine implements FlightPlaylist {
}
/**
* Returns the loaded channels in the LX engine.
* NOTE: This does not include the master channel
* Returns the loaded channels in the LX engine. NOTE: This does not include the
* master channel
*
* @return A list of LXAbstractChannel
*/
......@@ -133,20 +141,19 @@ public abstract class FlightEngine implements FlightPlaylist {
}
/**
* Returns the IO class object for saving and loading
* flyer configurations and playlist
* Returns the IO class object for saving and loading flyer configurations and
* playlist
*
* @return The IO class object
*/
public IO getIO(){
public IO getIO() {
return io;
}
/**
* Sets up and starts recording a playlist:
* Arms all the channels in the mixer to a recording state and
* triggers all the clips at index 0 (each channel is configured
* to have one clip only
* Sets up and starts recording a playlist: Arms all the channels in the mixer
* to a recording state and triggers all the clips at index 0 (each channel is
* configured to have one clip only
*/
public void startRecording() {
lx.engine.mixer.masterBus.arm.setValue(true);
......@@ -166,11 +173,9 @@ public abstract class FlightEngine implements FlightPlaylist {
}
}
/**
* Starts playback of a loaded playlist:
* Disarms all the channels in the mixer to a playback state
* and triggers all the clips at index 0 (each channel is
* Starts playback of a loaded playlist: Disarms all the channels in the mixer
* to a playback state and triggers all the clips at index 0 (each channel is
* configured to have one clip only
*/
public void playbackPlaylist() {
......@@ -182,85 +187,97 @@ public abstract class FlightEngine implements FlightPlaylist {
}
/**
* Stops recording or playback of a playlist:
* Stops all clips in the mixer. This will stop recording if
* the channels were in a recording state or stop playback if
* the channels were in a playback state
* Stops recording or playback of a playlist: Stops all clips in the mixer. This
* will stop recording if the channels were in a recording state or stop
* playback if the channels were in a playback state
*/
public void stopPlaylist() {
getMixerEngine().stopClips();
}
/**
* Loads a playlist from a file:
* Creates a Playlist object from a JsonArray that is loaded
* by the IO class. It then calls the Playlist class function
* that loads the playlist array onto the channels
* Loads a playlist from a file: Creates a Playlist object from a JsonArray that
* is loaded by the IO class. It then calls the Playlist class function that
* loads the playlist array onto the channels
*
* @param filename The relative path of the playlist file to be loaded
*/
public void loadPlaylist(String filename){
try {
JsonArray file = io.loadPlaylistFile(filename);
Playlist playlist = new Playlist(file);
playlist.loadPlaylist(lx);
}
catch (Exception e) {
System.err.println("Error loading playlist from " + filename);
e.printStackTrace();
}
public void loadPlaylist(String filename) {
try {
JsonArray file = io.loadPlaylistFile(filename);
Playlist playlist = new Playlist(file);
playlist.loadPlaylist(lx);
} catch (Exception e) {
System.err.println("Error loading playlist from " + filename);
e.printStackTrace();
}
}
/**
* Loads a playlist from a file:
* Creates a Playlist object from a JsonArray that is loaded
* by the IO class. It then calls the Playlist class function
* that loads the playlist array onto the channels
* Loads a playlist from a file: Creates a Playlist object from a JsonArray that
* is loaded by the IO class. It then calls the Playlist class function that
* loads the playlist array onto the channels
*
* @param file The file object of the playlist file to be loaded
*/
public void loadPlaylist(File file){
public void loadPlaylist(File file) {
Playlist playlist = new Playlist(io.loadPlaylistFile(file));
playlist.loadPlaylist(lx);
}
/**
* Saves a loaded or recorded playlist onto a file:
* Creates a Playlist object from a the channels in the mixer
* and then save the resulting JsonArray through the IO class
* Saves a loaded or recorded playlist onto a file: Creates a Playlist object
* from a the channels in the mixer and then save the resulting JsonArray
* through the IO class
*
* @param filename The relative path of the playlist file to be saved
*/
public void savePlaylist(String filename){
public void savePlaylist(String filename) {
Playlist playlist = new Playlist(lx);
io.savePlaylistFile(playlist.getPlaylistArray(), filename);
}
/**
* Saves a loaded or recorded playlist onto a file:
* Creates a Playlist object from a the channels in the mixer
* and then save the resulting JsonArray through the IO class
* Saves a loaded or recorded playlist onto a file: Creates a Playlist object
* from a the channels in the mixer and then save the resulting JsonArray
* through the IO class
*
* @param file The file object of the playlist file to be saved
*/
public void savePlaylist(File file){
public void savePlaylist(File file) {
Playlist playlist = new Playlist(lx);
io.savePlaylistFile(playlist.getPlaylistArray(), file);
}
/**
* Sets whether the current playlist should run once or loop.
*/
public void setPlaylistLooping(boolean looping) {
for (LXChannel channel: this.channels) {
for (LXChannel channel : this.channels) {
LXClip clip = channel.getClip(0);
clip.loop.setValue(looping);
}
}
/**
* Moves playlists up and down @param playQueue
*
* @param index : Index of playList to move
* @param up : True: move up. False: move down
*/
public void moveInQueue(int index, boolean up) {
if ((index == 0 && up) || (index == playQueue.size() - 1 && !up))
return;
Collections.swap(this.playQueue, index, index + (up ? -1 : 1));
}
// PRIVATE FUNCTIONS
/* Adds patterns to the pattern list passed as parameter.
* This pattern is useful because each channel has its own
* list of patterns, since each instance of a pattern might
* have different settings.
/*
* Adds patterns to the pattern list passed as parameter. This pattern is useful
* because each channel has its own list of patterns, since each instance of a
* pattern might have different settings.
*/
private void addPatterns(ArrayList<LXPattern> patterns) {
patterns.add(new ColorSweep(lx));
......@@ -269,18 +286,17 @@ public abstract class FlightEngine implements FlightPlaylist {
patterns.add(new ColorWheel(lx));
patterns.add(new RaspberryTrip(lx));
}
/* Create an array with a new instance of each available Pattern. */
private LXPattern[] createPatternArray() {
ArrayList<LXPattern> list = new ArrayList<LXPattern>();
addPatterns(list);
return list.toArray(new LXPattern[patterns.size()]);
}
/* Creates all of the channels for the mixer, giving each its
* own copy of each available pattern and giving it a clip for
* later recording or playback.
/*
* Creates all of the channels for the mixer, giving each its own copy of each
* available pattern and giving it a clip for later recording or playback.
*/
private void configureChannels() {
addPatterns(this.patterns);
......@@ -291,7 +307,7 @@ public abstract class FlightEngine implements FlightPlaylist {
LXChannel channel = mixer.addChannel(c, createPatternArray());
channels.add(channel);
// By default channel 1 is on and others are off.
if (c == 0) {
if (c == 0) {
channel.fader.setValue(1);
} else {
channel.fader.setValue(0);
......@@ -302,7 +318,7 @@ public abstract class FlightEngine implements FlightPlaylist {
mixer.selectChannel(mixer.getChannel(0));
mixer.masterBus.addClip();
}
/* Configures the GeneratorOutput */
private void configureGeneratorOutput() {
PrintStream[] outputSockets = new PrintStream[76];
......@@ -312,11 +328,9 @@ public abstract class FlightEngine implements FlightPlaylist {
generator = new Generator(lx, model, null, outputSockets);
lx.addOutput(generator);
} catch (Exception ex) {
System.out.println("IN generator");
System.out.println("IN generator");
System.out.println(ex);
}
}
}
package engine;
import java.util.List;
import java.util.Collections;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
......@@ -106,6 +107,33 @@ public class Playlist{
}
}
public double getPlaylistLength(LX lx) {
// Return length of MasterClip
return playlistArray
.get(0)
.getAsJasonObject()
.getAsJasonObject("parameters")
.get("length");
// // Return maximum length in all channel clips in playListArray
// return Collections.max(
// playlistArray,
// new Comparator<JsonElement>() {
// @Override
// public int compare(JsonElement e1, JsonElement e2) {
// return e1
// .getAsJasonObject()
// .getAsJasonObject("parameters")
// .get("length") >
// e2
// .getAsJasonObject()
// .getAsJasonObject("parameters")
// .get("length");
// }
// }
// ).getAsJasonObject().getAsJasonObject("parameter").get("length");
}
/*
Modifies the JsonObject for each clip in the channel. The function adds
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment