changeset 10:df1d4beb6994

Save working, but hacky. Midi? Algorave.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Wed, 15 May 2013 17:43:23 +0100
parents 0e03760cf2d9
children 274864620848
files mesh.h mesh.mm testApp.h testApp.mm
diffstat 4 files changed, 191 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/mesh.h	Tue May 14 18:05:08 2013 +0100
+++ b/mesh.h	Wed May 15 17:43:23 2013 +0100
@@ -50,7 +50,7 @@
     // MEMBER FUNCTIONS
 	Mesh();
     Mesh(Json::Value& savedMesh); // contructor using saved mesh
-    
+    void setReasonableDefaults();
 	virtual ~Mesh();
 
     void draw();
@@ -122,6 +122,11 @@
 // save stuff
     Json::Value convertToJsonForSaving();
     Json::Value saveConnectionsAsJson();
+    // UTILS
+    TwoVector calculateCentre();
+	void connect(int springnum,int lumpnum);
+    void connect(int springnum,int lumpnum,int alumpnum2);
+	int getNearestLump(double ax,double ay);
 protected:
 
     int prevLump;
@@ -139,11 +144,7 @@
 	void makeConnectionsFromJson(Json::Value connections);
 	void makeScanPathFromJson(Json::Value scanPathElements);
     void constrainFromJson(Json::Value constrainedLumps);
-    // UTILS    
-    TwoVector calculateCentre();
-	void connect(int springnum,int lumpnum);
-    void connect(int springnum,int lumpnum,int alumpnum2);
-	int getNearestLump(double ax,double ay);	
+
 	
 };
 //---------------------------------------------------------
--- a/mesh.mm	Tue May 14 18:05:08 2013 +0100
+++ b/mesh.mm	Wed May 15 17:43:23 2013 +0100
@@ -18,39 +18,42 @@
 //--------------------------------------------------------------
 Mesh::Mesh(){
 	// construct an empty structure
-	numSprings = 0;
+    setReasonableDefaults();
+
+    cout << "constructed mesh base" << endl;
+}
+
+
+Mesh::~Mesh(){
+	cout << "destroying mesh" << endl;
+
+
+}
+//--------------------------------------------------------------
+void Mesh::setReasonableDefaults(){
+    numSprings = 0;
 	numLumps = 0;
-
+    
 	syrup = false;
 	GRAB_RANGE = 0.040;
     propagationSpeed = 0.5; // this will be changed by adjusting k/m
-	cout << "constructed mesh base" << endl;
+	
     
     m = 123.0;
     k = 0.1;
     f = 0.999;
-
+    
     prevLump = -1;
     
     // some reasonable defaults
     setSpringConstant(0.8);
     setMass(1);
     setFriction(0.999991);
-
-
 }
-
-
-Mesh::~Mesh(){
-	cout << "destroying mesh" << endl;
-
-
-}
-
 //--------------------------------------------------------------
 //--------------------------------------------------------------
 Mesh::Mesh(Json::Value& jmesh){
-    //Mesh(); // sensible?
+    setReasonableDefaults(); // sensible?
     
     cout << jmesh;
     
@@ -557,11 +560,14 @@
     
 	// find closest
     int nearest = getNearestLump(ax,ay);
-    if(nearest = -1) return; // there are no lumps!
+    //if(nearest = -1) return; // there are no lumps!
     diff.setCoord(lumps[nearest].position.x - ax, lumps[nearest].position.y - ay);
     //cout << "diff " << diff.x << "," << diff.y << endl;
+    cout << GRAB_RANGE << endl;
     if (diff.norm() < GRAB_RANGE){
+        
         cout << "grabbing lump " << nearest << endl;
+        
         // move this lump to mouse pos
         
         lumps[nearest].grab(touchID);
--- a/testApp.h	Tue May 14 18:05:08 2013 +0100
+++ b/testApp.h	Wed May 15 17:43:23 2013 +0100
@@ -18,11 +18,12 @@
 
 #include "ofxUI.h"
 #include "ofxOsc.h"
+#include "ofxMidi.h"
 #include "globalForces.h"
 #include "globalUI.h"
 
 #include "json.h"
-
+#import "PresetAlertViewController.h"
 #define SAMPLE_RATE 44100
 // listen on port 12345
 #define INPORT 12345
@@ -35,14 +36,14 @@
 
 
 
-class testApp : public ofxiPhoneApp, public ofxiPhoneExternalDisplay{
+class testApp : public ofxiPhoneApp, public ofxiPhoneExternalDisplay, public ofxMidiListener, public ofxMidiConnectionListener {
 	
 public:
     int timesOpened;
 	bool audioOn;
 	bool drawingOn;
     bool controlsShowing;
-	
+	int presetSaveNumber;
 	bool paused;
     //bool inscribeScanPathMode;
     
@@ -130,7 +131,34 @@
     void drawSidePanels();
     
     ofxUILabel *counter;
-     
+    
+    //---------------------------------
+    // MIDI STUFF
+    // message
+    
+    int midiChannel; // what channel are we recieving on?
+    
+    void addMessage(string msg);
+	
+	// midi message callback
+	void newMidiMessage(ofxMidiMessage& msg);
+	
+	// midi device (dis)connection event callbacks
+	void midiInputAdded(string name, bool isNetwork);
+	void midiInputRemoved(string name, bool isNetwork);
+	
+	void midiOutputAdded(string nam, bool isNetwork);
+	void midiOutputRemoved(string name, bool isNetwork);
+    
+	vector<ofxMidiIn*> inputs;
+	vector<ofxMidiOut*> outputs;
+    
+	deque<string> messages;
+	int maxMessages;
+	
+	int note, ctl;
+	vector<unsigned char> bytes;
+    
 };	
 
 
--- a/testApp.mm	Tue May 14 18:05:08 2013 +0100
+++ b/testApp.mm	Wed May 15 17:43:23 2013 +0100
@@ -48,6 +48,7 @@
 extern GlobalForces globalForces;
 extern GlobalUI globalUI;
 extern ScanPath scanPath;
+extern PresetAlertViewController *presetAlertViewController;
 //--------------------------------------------------------------
 void testApp::setup(){
     ofxiPhoneSetOrientation( OFXIPHONE_ORIENTATION_LANDSCAPE_RIGHT );
@@ -107,7 +108,7 @@
     [ofxiPhoneGetGLView() updateDimensions];
     
     
-    
+    presetSaveNumber =
 }
 //--------------------------------------------------------------
 Json::Value testApp::convertToJsonForSaving(){
@@ -122,8 +123,17 @@
     
     return root;
 }
+//
+void testApp::showPresetNameDialog(){
+    if(!presetAlertViewController.alertShowing){ // this is to stop wierd infinite loop in ios5 (simulator)
+        [presetAlertViewController showPresetNamePrompt];
+        
+    }
+    
+}
 //--------------------------------------------------------------
-void testApp::savePreset(){
+void testApp::savePreset(const string name ){
+    presetNumber ++;
     Json::Value jpreset = convertToJsonForSaving();
     //save json to file
     string fname = ofxiPhoneGetDocumentsDirectory() + "presetFile";
@@ -182,10 +192,10 @@
     // make saved scanpath
     // recall global settings
     pitch = presetJson["pitch"].asDouble();
-
-    globalForces.setFromJson(presetJson["globalForces"]);
     
     theMesh = new Mesh(presetJson["mesh"]);
+    
+    globalForces.setFromJson(presetJson["globalForces"]);
 }
 void testApp::loadPreset(){
     if(audioAccessFlag) return; // or rather wait !
@@ -307,7 +317,7 @@
         }
     }else if(type % numTypes ==2){
         
-        theMesh = new SquareCrossMesh(16,16);
+        theMesh = new LineMesh(4096);
         
     }else if(type % numTypes ==3){
         theMesh = new SquareCrossMesh(28,28);
@@ -1526,3 +1536,118 @@
  
 }
 */
+
+//-------------------------------------------------------------------------
+//--------------------------------------------------------------
+
+#pragma mark MIDI
+void testApp::addMessage(string msg) {
+	cout << msg << endl;
+	messages.push_back(msg);
+	while(messages.size() > maxMessages)
+		messages.pop_front();
+}
+
+//--------------------------------------------------------------
+void testApp::newMidiMessage(ofxMidiMessage& msg) {
+	// recieved one
+    static int p; // remembers last note on
+    int v;
+    // look at what it is
+    
+    if(msg.status == MIDI_CONTROL_CHANGE){
+        int ctl_num = msg.control;
+        int ctl_val = msg.value;
+        // TODO route control change message here
+    }
+    
+    if(msg.status == MIDI_NOTE_ON){
+        p = msg.pitch;
+        v = msg.velocity;
+
+        // TODO handle note on here
+        if(v > 0){
+            cout << "note pitch = " << p << endl;
+            pitch = 440.0/pow(2.0,((69.0 - double(p))/12.0));
+            theMesh->hit(0.5,0.5,v,globalForces.excitationType,globalForces.excitationShape);
+        }else{
+            //theMesh->damp();
+        }
+    }
+    if(msg.status == MIDI_NOTE_OFF){
+        if(msg.pitch == p){
+            //theMesh->damp();
+        }
+        // otherwise ignore it, it's not the currenttly playing note
+    }
+    
+}
+
+//--------------------------------------------------------------
+void testApp::midiInputAdded(string name, bool isNetwork) {
+	stringstream msg;
+	msg << "ofxMidi: input added: " << name << " network: " << isNetwork;
+    cout << msg.str();
+	addMessage(msg.str());
+	
+	// create and open a new input port
+	ofxMidiIn * newInput = new ofxMidiIn;
+	newInput->openPort(name);
+	newInput->addListener(this);
+	inputs.push_back(newInput);
+}
+
+//--------------------------------------------------------------
+void testApp::midiInputRemoved(string name, bool isNetwork) {
+	stringstream msg;
+	msg << "ofxMidi: input removed: " << name << " network: " << isNetwork << endl;
+    cout << msg.str();
+	addMessage(msg.str());
+	
+	// close and remove input port
+	vector<ofxMidiIn*>::iterator iter;
+	for(iter = inputs.begin(); iter != inputs.end(); ++iter) {
+		ofxMidiIn * input = (*iter);
+		if(input->getName() == name) {
+			input->closePort();
+			input->removeListener(this);
+			delete input;
+			inputs.erase(iter);
+			break;
+		}
+	}
+}
+
+//--------------------------------------------------------------
+void testApp::midiOutputAdded(string name, bool isNetwork) {
+	stringstream msg;
+	msg << "ofxMidi: output added: " << name << " network: " << isNetwork << endl;
+    cout << msg.str();
+	addMessage(msg.str());
+	
+	// create and open new output port
+	ofxMidiOut * newOutput = new ofxMidiOut;
+	newOutput->openPort(name);
+	outputs.push_back(newOutput);
+}
+
+//--------------------------------------------------------------
+void testApp::midiOutputRemoved(string name, bool isNetwork) {
+	stringstream msg;
+	msg << "ofxMidi: output removed: " << name << " network: " << isNetwork << endl;
+    cout << msg.str();
+	addMessage(msg.str());
+	
+	// close and remove output port
+	vector<ofxMidiOut*>::iterator iter;
+	for(iter = outputs.begin(); iter != outputs.end(); ++iter) {
+		ofxMidiOut * output = (*iter);
+		if(output->getName() == name) {
+			output->closePort();
+			delete output;
+			outputs.erase(iter);
+			break;
+		}
+	}
+}
+//--------------------------------------------------------------