Skip to content

Commit

Permalink
Bundle is now done through file, OSC message has 1 argument of filename
Browse files Browse the repository at this point in the history
  • Loading branch information
fiebrink1 committed Aug 27, 2015
1 parent 6c09a3f commit 204847a
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 141 deletions.
21 changes: 11 additions & 10 deletions src/wekimini/DtwLearningManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void notifyInputError() {
}

@Override
public void updateBundle(List<Object> values) {
public void updateBundle(List<List<Double>> values) {
updateInputBundle(values);
}
});
Expand Down Expand Up @@ -240,20 +240,21 @@ private void updateInputs(double[] vals) {
}
}

private void updateInputBundle(List<Object> values) {
int numPoints = (Integer) values.get(0);
int numInputs = w.getInputManager().getNumInputs();
private void updateInputBundle(List<List<Double>> values) {
// int numPoints = (Integer) values.get(0);
// int numInputs = w.getInputManager().getNumInputs();
if (model.getRecordingState() == DtwModel.RecordingState.RECORDING) {
int currentVal = 1; //starts at 1
for (int i = 0; i < numPoints; i++) {
double[] theseVals = new double[numInputs];
for (int j = 0; j < numInputs; j++) {
theseVals[j] = (Float)values.get(currentVal++);
//int currentVal = 1; //starts at 1
for (List<Double> thisValue : values) {
//for (int i = 0; i < numPoints; i++) {
double[] theseVals = new double[thisValue.size()];
for (int j = 0; j < thisValue.size(); j++) {
theseVals[j] = thisValue.get(j);
}
model.getData().addTrainingVector(theseVals);
}
} else if (runningState == RunningState.RUNNING) {
double[][] allOutputs = model.runOnBundle(numPoints, w.getInputManager().getNumInputs(), values);
double[][] allOutputs = model.runOnBundle(values);
try {
w.getOSCSender().sendOutputBundleValuesMessage(w.getOutputManager().getOutputGroup().getOscMessage(), allOutputs);
} catch (IOException ex) {
Expand Down
219 changes: 123 additions & 96 deletions src/wekimini/InputManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.EventListener;
import java.util.LinkedList;
Expand Down Expand Up @@ -38,36 +43,35 @@ public class InputManager {
private transient final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
private double[] currentValues = new double[0];
private static final Logger logger = Logger.getLogger(InputManager.class.getName());



public double[] getInputValues() {
if (inputGroup == null) {
return new double[0];
} else {
return currentValues;
}
}

public int getNumInputs() {
if (inputGroup == null) {
return 0;
} else {
return inputGroup.getNumInputs();
}
}

public String[] getInputNames() {
if (inputGroup == null) {
return new String[0];
} else {
return inputGroup.getInputNames();
}
}

public boolean hasValidInputs() {
return (inputGroup != null);
}

/**
* Add PropertyChangeListener.
*
Expand Down Expand Up @@ -118,22 +122,21 @@ public void setOSCInputGroup(OSCInputGroup newG) {
//inputs.add(g);
OSCInputGroup oldGroup = inputGroup;
inputGroup = newG;
if (oldGroup == null || !oldGroup.getOscMessage().equals(newG.getOscMessage())) {
//Update OSC istener for new(?) message
addOSCInputListener();
}

if (oldGroup == null || !oldGroup.getOscMessage().equals(newG.getOscMessage())) {
//Update OSC istener for new(?) message
addOSCInputListener();
}
if (inputGroup != null) {
currentValues = new double[inputGroup.getNumInputs()];
}
propertyChangeSupport.firePropertyChange(PROP_INPUTGROUP, oldGroup, inputGroup);
}

public OSCInputGroup getOSCInputGroup() {
return inputGroup;
}


private void addOSCInputListener() {
if (inputGroup != null) {
addOSCInputListener(inputGroup);
Expand All @@ -157,112 +160,134 @@ public void acceptMessage(Date date, OSCMessage oscm) {
messageArrived(g.getOscMessage(), oscm);
}
};



OSCListener groupl = new OSCListener() {
@Override
public void acceptMessage(Date date, OSCMessage oscm) {
bundleArrived(g.getOscMessage(), oscm);
}


};

w.getOSCReceiver().addOSCListener(g.getOscMessage(), l);
w.getOSCReceiver().addOSCListener("/wek/inputs/bundle", groupl) ;
w.getOSCReceiver().addOSCListener("/wek/inputs/bundle", groupl);
}

private void messageArrived(String messageName, OSCMessage m) {
//TODO: CHeck if enabled before doing anything
//System.out.println("Received " + name);
if (inputGroup != null && messageName.equals(inputGroup.getOscMessage())) {
List<Object> o = m.getArguments();
double d[] = new double[o.size()];
for (int i = 0; i < o.size(); i++) {
if (o.get(i) instanceof Float) {
d[i] = ((Float) o.get(i));
} else {
Logger.getLogger(InputManager.class.getName()).log(Level.WARNING, "Received feature is not a float");
}
}
if (d.length == currentValues.length) {
notifyListeners(d);
System.arraycopy(d, 0, currentValues, 0, d.length);
double d[] = new double[o.size()];
for (int i = 0; i < o.size(); i++) {
if (o.get(i) instanceof Float) {
d[i] = ((Float) o.get(i));
} else {
String msg = "Mismatch in input length: "
+ "Expected " + currentValues.length + ", received " + o.size();
w.getStatusUpdateCenter().warn(this, msg);
notifyListenersOfError();
Logger.getLogger(InputManager.class.getName()).log(Level.WARNING, "Received feature is not a float");
}
}
if (d.length == currentValues.length) {
notifyListeners(d);
System.arraycopy(d, 0, currentValues, 0, d.length);
} else {
String msg = "Mismatch in input length: "
+ "Expected " + currentValues.length + ", received " + o.size();
w.getStatusUpdateCenter().warn(this, msg);
notifyListenersOfError();
}
}
//Not sure if we need to store this array within this class, too
}

private void bundleArrived(String messageName, OSCMessage m) {
//TODO

//TODO: CHeck if enabled before doing anything
//System.out.println("Received " + name);
if (inputGroup != null) { //&& messageName.equals(makeBundleMessage(inputGroup.getOscMessage()))) {
if (inputGroup != null) { try {
//&& messageName.equals(makeBundleMessage(inputGroup.getOscMessage()))) {
List<Object> o = m.getArguments();
if (o == null || o.size() < 1) {
String msg = "Unexpected bundle message; require at least 1 message";
w.getStatusUpdateCenter().warn(this, msg);
notifyListenersOfError();
return;
if (o == null || o.size() < 1 || !(o.get(0) instanceof String)) {
String msg = "Unexpected bundle message; require 1 string containing filename";
w.getStatusUpdateCenter().warn(this, msg);
notifyListenersOfError();
return;
}
int numDatapoints = 0;
String filename = (String) o.get(0);
BufferedReader br;
String line = "";
String cvsSplitBy = ",";
LinkedList<List<Double>> data = new LinkedList<>();
br = new BufferedReader(new FileReader(filename));
while ((line = br.readLine()) != null) {
String[] thisLine = line.split(cvsSplitBy);
ArrayList<Double> thisData = new ArrayList<>(thisLine.length);
for (String thisLine1 : thisLine) {
thisData.add(Double.parseDouble(thisLine1));
}
data.add(thisData);
}
notifyBundleListeners(data);
for (int i = 0; i < currentValues.length; i++) {
currentValues[i] = data.getLast().get(i);
}


/* int numDatapoints = 0;
if (o.get(0) instanceof Integer) {
numDatapoints = (Integer) o.get(0);
numDatapoints = (Integer) o.get(0);
} else {
String msg = "Unexpected bundle message; require at least 1 datapoint";
w.getStatusUpdateCenter().warn(this, msg);
notifyListenersOfError();
return;
String msg = "Unexpected bundle message; require at least 1 datapoint";
w.getStatusUpdateCenter().warn(this, msg);
notifyListenersOfError();
return;
}
if (o.size() != (numDatapoints * currentValues.length + 1)) {
String msg = "Unexpected bundle length: Expected " + numDatapoints + " points";
w.getStatusUpdateCenter().warn(this, msg);
notifyListenersOfError();
return;
String msg = "Unexpected bundle length: Expected " + numDatapoints + " points";
w.getStatusUpdateCenter().warn(this, msg);
notifyListenersOfError();
return;
}
if (numDatapoints > 0) {
//o.remove(0);
notifyBundleListeners(o);
//update currentValues
for (int i = 0; i < currentValues.length; i++) {
currentValues[i] = (Float)o.get(o.size()-currentValues.length + i);
}
//o.remove(0);
notifyBundleListeners(o);
//update currentValues
for (int i = 0; i < currentValues.length; i++) {
currentValues[i] = (Float)o.get(o.size()-currentValues.length + i);
}
/* int dataIndex = 1;
} */
/* int dataIndex = 1;
for (int i = 0; i < numDatapoints; i++) {
double[] d = new double[currentValues.length];
for (int j = 0; j < currentValues.length; j++) {
if (o.get(dataIndex) instanceof Float) {
d[j] = ((Float) o.get(dataIndex++));
} else {
String msg = "Received feature is not a float";
w.getStatusUpdateCenter().warn(this, msg);
notifyListenersOfError();
return;
}
}
notifyListeners(d);
System.arraycopy(d, 0, currentValues, 0, d.length);
double[] d = new double[currentValues.length];
for (int j = 0; j < currentValues.length; j++) {
if (o.get(dataIndex) instanceof Float) {
d[j] = ((Float) o.get(dataIndex++));
} else {
String msg = "Received feature is not a float";
w.getStatusUpdateCenter().warn(this, msg);
notifyListenersOfError();
return;
}
}
notifyListeners(d);
System.arraycopy(d, 0, currentValues, 0, d.length);
} */

} catch (FileNotFoundException ex) {
logger.log(Level.WARNING, "File not found");
} catch (IOException ex) {
Logger.getLogger(InputManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
}

private void notifyListeners(double[] data) {
for(InputListener l : inputValueListeners) {
for (InputListener l : inputValueListeners) {
l.update(data);
}
}

private void notifyListenersOfError() {
for(InputListener l : inputValueListeners) {
for (InputListener l : inputValueListeners) {
l.notifyInputError();
}
}
Expand All @@ -271,40 +296,42 @@ private String makeBundleMessage(String oscMessage) {
return new StringBuilder(oscMessage).append("/bundle").toString();
}

private void notifyBundleListeners(List<Object> o) {
for(InputListener l : inputValueListeners) {
l.updateBundle(o);
private void notifyBundleListeners(List<List<Double>> inputs) {
for (InputListener l : inputValueListeners) {
l.updateBundle(inputs);
}
}

public interface InputListener extends EventListener {

public void update(double[] vals);
public void updateBundle(List<Object> values);

public void updateBundle(List<List<Double>> inputs);

public void notifyInputError();
}

/*public interface InputGroupChangeListener extends EventListener {
void inputGroupChange(InputGroupChangeEvent evt);
}*/
void inputGroupChange(InputGroupChangeEvent evt);
}*/

/*public class InputGroupChangeEvent {
private final String name;
private final InputGroupChangeType changeType;
private final String name;
private final InputGroupChangeType changeType;
public InputGroupChangeEvent(final String name, final InputGroupChangeType changeType) {
this.name = name;
this.changeType = changeType;
}
public String getName() {
return name;
}
public InputGroupChangeEvent(final String name, final InputGroupChangeType changeType) {
this.name = name;
this.changeType = changeType;
}
public InputGroupChangeType getChangeType() {
return changeType;
}
} */
public String getName() {
return name;
}
public InputGroupChangeType getChangeType() {
return changeType;
}
} */
}
Loading

0 comments on commit 204847a

Please sign in to comment.