Skip to content

Commit

Permalink
lots and lots of changes
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelforrest committed Jan 25, 2009
1 parent 7bde5be commit bd86769
Show file tree
Hide file tree
Showing 20 changed files with 500 additions and 97 deletions.
53 changes: 17 additions & 36 deletions src/animata/AnimataPlayback.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,21 @@
import processing.xml.XMLElement;
import rwmidi.MidiInput;
import rwmidi.RWMidi;
import animata.controls.ClockBobber;
import animata.model.Layer;

public class AnimataPlayback {
public class Camera {
public float x;
public float y;
public float z;
public float targetX;
public float targetY;
public float targetZ;
public Camera(PApplet applet) {
x = applet.width/2.0f;
y = applet.height/2.0f;
z = 1500;//(applet.height/2.0f) / PApplet.tan(PApplet.PI*60.0f / 360.0f);
targetX = applet.width/2.0f;
targetY = applet.height/2.0f;
targetZ = 0f;
}
public void zoomBy(float delta) {
z+=delta;
targetZ += delta;

}
public void panXBy(float delta) {
x += delta;
targetX += delta;
}
public void panYBy(float delta) {
y += delta;
targetY += delta;
}
}
public static final float timeDivision = 42f;
public static float gravity = 0;
private static boolean debug;
private PApplet applet;
private Layer root;
private LayerView layersView;
private Controller controller;
private Camera camera;
public Camera camera;
private MidiInput in;
public static Clock clock;

public AnimataPlayback(PApplet applet, String nmtFile){
setup(applet);
Expand All @@ -62,8 +36,12 @@ private void setup(PApplet applet) {
this.applet.hint(PApplet.ENABLE_OPENGL_2X_SMOOTH);
root = new Layer();
in = RWMidi.getInputDevice("IAC Bus 1 <MIn:0> Apple Computer, Inc.").createInput();
clock = new Clock("IAC Bus 2");
new ClockBobber(clock);
new RandomCameraPanner(clock, this);
Controller.init(applet,root,this);
controller = Controller.getInstance();
new GrimoniumInput(applet,root,controller,7111);
}
public void initOSC(int port){
new OSCInput(applet,root,controller,port);
Expand All @@ -82,20 +60,23 @@ public void loadSet(String xml){
XMLElement[] layers = scene.getChildren("layer");
for (int i = 0; i < layers.length; i++) {
XMLElement element = layers[i];
addScene(element.getStringAttribute("nmt"));
new Scene(element,in);
Layer layer = addScene(element.getStringAttribute("nmt"));
layer.visible = true;
layer.alpha = 1;
new Scene(element,in,applet,layer);
}
layersView = new LayerView(root, applet);
}

public void addScene(String xml){
addScene(xml, root);
public Layer addScene(String xml){
return addScene(xml, root);
}
public void addScene(String xml, Layer parent){
public Layer addScene(String xml, Layer parent){
String folder = new File(xml).getParent();
if(folder == null) folder = ".";
XMLElement element = new XMLElement(applet, xml);
parent.addLayers(element.getChildren("layer"), folder);
layersView = new LayerView(root, applet);
return parent.addLayer(folder, element);//element.getChildren("layer"), folder);

}
public void draw(){
applet.camera(camera.x, camera.y, camera.z, camera.targetX, camera.targetY,camera.targetZ, 0f, 1f, 0f);
Expand Down
1 change: 1 addition & 0 deletions src/animata/Animator.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void complete() {
}

public void stop() {
frame = 1;
// engine.deleteObserver(this);
}

Expand Down
119 changes: 119 additions & 0 deletions src/animata/Camera.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package animata;

import java.util.Observable;
import java.util.Observer;

import processing.core.PApplet;

public class Camera implements Observer {
private static final int CAMERA_DISTANCE = 500;
public class CameraShaker implements Observer {
private static final float X_RANGE = (float) Math.PI * 5;
private Animator animator;
public float y = 0;
public float x = 0;

CameraShaker(){
this.animator = new Animator(0,this);
}

public void update(Observable o, Object arg) {
float v = sinWithDecay(animator.currentValue);
this.y = v;
updateWithShake();
}


private float sinWithDecay(float v) {
return (float) Math.sin(v) * 1/(v/X_RANGE);
}

public void shake() {
animator.currentValue = 0;
animator.set(X_RANGE, 20);
}
}
// TODO: find a good standard Java vector class! This is shocking now.
public float x;
public float y;
public float z;
public float targetX;
public float targetY;
public float targetZ;
private Animator animator;
private float endX;
private float endY;
private float endZ;
private float startX;
private float startY;
private float startZ;
private CameraShaker shaker;



public Camera(PApplet applet) {
x = applet.width/2.0f;
y = applet.height/2.0f;
z = CAMERA_DISTANCE;//(applet.height/2.0f) / PApplet.tan(PApplet.PI*60.0f / 360.0f);
targetX = applet.width/2.0f;
targetY = applet.height/2.0f;
targetZ = 0f;
shaker = new CameraShaker();
animator = new Animator(0,this);
}
private void adjustCameraForShake() {
y += shaker.y;

}
public void zoomBy(float delta) {
z+=delta;
targetZ += delta;

}
public void panXBy(float delta) {
x += delta;
targetX += delta;
}

public void panYBy(float delta) {
y += delta;
targetY += delta;
}
public void update(Observable o, Object arg) {
updateFromAnimator();
updateWithShake();
}
private void updateWithShake() {
if(animator.currentValue != 1 && animator.currentValue != 0 ) return;
y += shaker.y;
targetY += shaker.y;

}
private void updateFromAnimator() {
float v = animator.currentValue;
x = startX + (endX-startX) * v;
y = startY + (endY-startY) * v;
z = startZ + (endZ-startZ) * v;
targetX = x;
targetY = y;
targetZ = z - CAMERA_DISTANCE;
}
public void moveTo(float x, float y, float z, int frames){
startX = this.x;
startY = this.y;
startZ = this.z;
endX = x;
endY = y;
endZ = z;
animator.currentValue = 0;
animator.set(1, frames);
}
@Override
public String toString() {
return super.toString() + "<camera x=\"" + x +"\" y=\"" + y +"\" z=\"" + z +"\" />";
}
public void shake() {
shaker.shake();

}
}
77 changes: 77 additions & 0 deletions src/animata/Clock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package animata;

import java.util.Observable;

import javax.sound.midi.*;


public class Clock extends Observable implements Receiver {

public static final Integer TICK = 0;
public static final Integer CROTCHET = 1;
public static final Integer BAR = 2;
private MidiDevice in;
private int tick = 0;

public Clock(String deviceName) {
in = getMidiDevice(deviceName);
if (!in.isOpen()) try {
in.open();
} catch (Exception e) {
e.printStackTrace();
}
Transmitter transmitter;
try {
transmitter = in.getTransmitter();
transmitter.setReceiver(this);
System.out.println("successfully created clock");
} catch (MidiUnavailableException e) {
e.printStackTrace();
}
}

private MidiDevice getMidiDevice(String deviceName) {
javax.sound.midi.MidiDevice.Info infos[] = MidiSystem.getMidiDeviceInfo();
for (javax.sound.midi.MidiDevice.Info info : infos) {
System.out.println("info: " + info.getName());
if (info.getName().matches(deviceName + ".*")) {
try {

return MidiSystem.getMidiDevice(info);
} catch (MidiUnavailableException e) {
e.printStackTrace();
}
}
}
System.out.println("Error - couldn't find " + deviceName);
return null;
}

public void close() {
in.close();
}

public void send(MidiMessage message, long timestamp) {
if(message.getStatus() == ShortMessage.START) tick = 0;
if(message.getStatus() != ShortMessage.TIMING_CLOCK) return;
setChanged();
notifyObservers(TICK);

if(tick == 0){
setChanged();
notifyObservers(BAR);
}

if(tick % 24 == 0){
setChanged();
notifyObservers(CROTCHET);
}

tick ++;
if(tick>=96) {
tick = 0;
}
}


}
12 changes: 12 additions & 0 deletions src/animata/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;

import processing.core.PApplet;
import animata.controls.CameraPosition;
import animata.model.Layer;
import animata.model.Skeleton;
import animata.model.Skeleton.Bone;
Expand All @@ -11,6 +12,7 @@ public class Controller {

private static Controller instance;
private final AnimataPlayback animataPlayback;
public String currentSong;

public Controller(PApplet applet, Layer root, AnimataPlayback animataPlayback) {
this.animataPlayback = animataPlayback;
Expand Down Expand Up @@ -67,4 +69,14 @@ public static void init(PApplet applet, Layer root, AnimataPlayback animataPlayb

}

public void moveCameraTo(CameraPosition p, int frames) {
animataPlayback.camera.moveTo(p.x, p.y,p.z, frames);

}

public void shakeCamera() {
animataPlayback.camera.shake();

}

}
33 changes: 33 additions & 0 deletions src/animata/GrimoniumInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package animata;

import oscP5.OscMessage;
import oscP5.OscP5;
import processing.core.PApplet;
import animata.model.Layer;

public class GrimoniumInput {
private Controller controller;

public GrimoniumInput(PApplet applet, Layer root, Controller controller, int port) {
this.controller = controller;
new OscP5(this, port);

}
public void oscEvent(OscMessage message) {
boolean result = run(message);
if (!result) {
System.out.println("failed to respond to message " + message.addrPattern());
message.print();
}
}
private boolean run(OscMessage message) {
String type = message.addrPattern();
if(type.equals("/visualschanged")) return visualsChanged(message);
return false;
}
private boolean visualsChanged(OscMessage message) {
controller.currentSong = (String) message.arguments()[0];
RandomCameraPanner.moveNow();
return true;
}
}
2 changes: 1 addition & 1 deletion src/animata/LayerView.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private void addChildLayers(ArrayList<Layer> layers) {
}

public void draw() {
if(!layer.visible) return;
//if(!layer.visible) return;
applet.pushMatrix();
doTransformation();
applet.pushMatrix();
Expand Down
10 changes: 8 additions & 2 deletions src/animata/NoteParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ public static Integer getNote(XMLElement xml) throws BadNoteFormatException{
return getNote(noteName);
}

public static Integer getNote(String noteName) throws BadNoteFormatException{
public static Integer getNote(String noteName){
try {
return Integer.parseInt(noteName);

} catch (NumberFormatException e) {
} catch (NumberFormatException e) {}

try {
return convertStringToNoteNumber(noteName);
} catch (BadNoteFormatException e) {
e.printStackTrace();
}
System.out.println("Note xml - error parsing " + noteName);
return null;
}
/*
* C0 = 0
Expand Down
Loading

0 comments on commit bd86769

Please sign in to comment.