changeset 1:7e0a19a538d4

added preset manager (untested)
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 10 Oct 2014 13:20:55 +0100
parents a223551fdc1f
children 851833072cf1
files PDSynthWrapper.h eventLogger.h presetManager.h presetManager.mm
diffstat 4 files changed, 312 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/PDSynthWrapper.h	Fri Oct 10 11:46:42 2014 +0100
+++ b/PDSynthWrapper.h	Fri Oct 10 13:20:55 2014 +0100
@@ -15,7 +15,7 @@
 #include "boost/bind.hpp"
 #include "boost/function.hpp"
 #include "SynthParam.h"
-
+#include "presetManager.h"
 
 //---------------------------------------------------------------------
 //---------------------------------------------------------------------
@@ -74,6 +74,7 @@
             
         }
         cout << "ERROR ERROR getParamValueForID not found" << endl;
+        return -1;
     }
     const int getMappingIDForName(string name) const{
         
@@ -87,6 +88,7 @@
             
         }
         cout << "ERROR ERROR getMappingIDForName not found" << endl;
+        return -1;
     };
     const string getNameForMappingID(int pid) const{
         
@@ -100,6 +102,7 @@
             
         }
         cout << "ERROR ERROR getNameForMappingID not found" << endl;
+        return "error";
     };
     vector<int> getMappingIDForIndices(vector<int> idx){
         vector<int> result;
@@ -189,6 +192,7 @@
             
         }
         cout << "ERROR ERROR: getParamValueFromName name not found" << endl;
+        return -1;
     };
     
     const int getNumParams(){
@@ -217,10 +221,14 @@
         }
         return pl;
     }
+    
+    void storeParamsAsPreset(string name){
+        presetManager.savePreset(name, getAllParamValues());
+    }
 private:
     string synthPrefix;
     AppCore* core;
-    
+    PresetManager presetManager; // TODO not here
     vector<SynthParam> timbreParams; // array of everything in synth
   
 };
--- a/eventLogger.h	Fri Oct 10 11:46:42 2014 +0100
+++ b/eventLogger.h	Fri Oct 10 13:20:55 2014 +0100
@@ -66,6 +66,7 @@
                 CRAP_TEST,                  // eliminate these coords somehow ???
                 EMPTY_EVENT,
                 SPEED_CHANGED,              // 22 ms between sounds
+                SAVE_PRESET                 // 23 save a preset
     
 };
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/presetManager.h	Fri Oct 10 13:20:55 2014 +0100
@@ -0,0 +1,118 @@
+//
+//  presetManager.h
+//  oscSenderExample
+//
+//  Created by Robert Tubb on 07/11/2012.
+//
+//
+
+// defines:
+//      PresetManager
+// and Preset
+
+#ifndef __oscSenderExample__presetManager__
+#define __oscSenderExample__presetManager__
+
+#define PRESET_FILENAME "presets.json"
+
+#include <iostream>
+#include <string>
+#include "ofMain.h"
+#include "ofxiPhone.h"
+#include "ofxiPhoneExtras.h"
+#include "eventLogger.h"
+#include "json.h"
+#include <ctime>
+
+
+//---------------------------------------------------------------------------
+class Preset{
+public:
+    // important details - these saved to file (uploaded?)
+    string          creatorUserName;
+    unsigned int    creatorDeviceID; // unique user device ID
+    string          name; // name of preset
+    unsigned long long       creationTime; // datetime that preset was created milliseconds
+
+    vector<int> CCValues; // the actual data
+    
+    // from save button press
+    Preset(vector<int> aCCValues, string aname, int aID, string un, unsigned int uid){
+        CCValues = aCCValues;
+
+        name = aname;
+        creatorUserName = un;
+        creatorDeviceID = uid;
+        double timemsd = [NSDate timeIntervalSinceReferenceDate];
+        creationTime = (unsigned long long)(timemsd*1000);
+
+        cout << "Create preset sys time: " << creationTime << "\n";
+        
+        //TODO color / texture?
+    };
+    // contruct from json value
+    Preset(Json::Value jval){
+
+        name = jval["name"].asString();
+        creatorUserName = jval["creatorUserName"].asString();
+        creatorDeviceID = jval["creatorDeviceID"].asUInt();
+
+
+        creationTime = jval["creationTime"].asLargestInt(); // dodgy?
+
+        
+        Json::Value JArray = jval["CCValues"];
+        if(JArray.size() < 1){
+            cout << "No Presets";
+        }
+        for ( unsigned int i = 0; i < JArray.size(); i++ )
+        {
+            CCValues.push_back(JArray[i].asInt());
+        }
+
+
+    }
+    
+    void draw();
+    Json::Value presetToJson();
+ 
+    string displayTextDescription(){ // eg: for grid hover
+        // objC to C to C++ bleurgh
+        NSDate *ocdate = [NSDate dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)(creationTime/1000.0)];
+        NSString *dateText = [ocdate description];
+        const char *date_str = [dateText cStringUsingEncoding:NSASCIIStringEncoding];
+        stringstream ss;
+        ss << "Name: \t" << name << "\nCreation time: \t" << date_str << "\nCreator: \t" << creatorUserName << '\n';
+        return ss.str();
+        
+    }
+};
+
+//---------------------------------------------------------------------------
+class PresetManager{
+public:
+    int nextID;
+    int timesOpened;
+    bool presetAlertShowing;
+
+    vector<Preset *> thePresets; 
+    
+    int savePreset(string name, vector<int> stuff);
+  
+    TwoVector recallPreset(int presetID); // by name ? id?
+    TwoVector recallPreset(string name); // by name ? id?
+
+    void printAll();
+    
+    void startLoadAll(); // load everything from the json file
+    void exitAndSaveAll(); // save stuff to the json file
+    void clearAll();
+    Json::Value  allPresetsToJson();
+    void readJsonToPresets(const string &jsonFile);
+    void showNameDialog();
+    PresetManager();
+};
+
+
+    
+#endif /* defined(__oscSenderExample__presetManager__) */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/presetManager.mm	Fri Oct 10 13:20:55 2014 +0100
@@ -0,0 +1,183 @@
+//
+//  presetManager.mm
+//  oscSenderExample
+//
+//  Created by Robert Tubb on 07/11/2012.
+//
+//
+
+#include "presetManager.h"
+
+//---------------------------------------------------------------------------
+
+PresetManager presetManager;
+extern EventLogger eventLogger;
+
+//---------------------------------------------------------------------------
+void Preset::draw(){
+    // probably just hint values on sliders
+};
+//---------------------------------------------------------------------------
+Json::Value Preset::presetToJson(){
+    // create the string for this instance of Preset object
+    
+    Json::Value presetVal;
+    
+    presetVal["creatorUserName"] = creatorUserName;
+    presetVal["creatorDeviceID"] = creatorDeviceID;
+    presetVal["creationTime"] =     creationTime;
+    presetVal["name"] =             name;
+    
+    for(vector<int>::iterator i; i < CCValues.end(); i++){
+        presetVal["CCValues"].append(*i);
+    }
+
+    return presetVal;
+}
+//---------------------------------------------------------------------------
+PresetManager::PresetManager(){
+    timesOpened = 0;
+    nextID = 0;
+   
+    string ts = ofGetTimestampString();
+    
+    presetAlertShowing = false;
+
+    
+    cout << "ofGetTimestampString: " << ts << '\n';
+}
+//---------------------------------------------------------------------------
+Json::Value PresetManager::allPresetsToJson(){
+    Json::Value root;
+ 
+    // use jsoncpp
+    vector<Preset *>::iterator presetIter;
+    
+    int i = 0;
+    for(presetIter = thePresets.begin(); presetIter < thePresets.end(); presetIter++){
+        root["presets"][i] = (*presetIter)->presetToJson();
+        i++;
+    }
+
+    return root;
+}
+//---------------------------------------------------------------------------
+void PresetManager::readJsonToPresets(const string &jsonFile){
+    Json::Value root;
+    Json::Reader reader;
+
+    
+    ifstream theFile(jsonFile.c_str());
+    stringstream fileText;
+    string line;
+    if(!theFile){
+        cout<<"can't find preset file \n";
+        return;
+    }else{
+        
+        while(theFile){
+            theFile >> line;
+            // cout << line << "\n"; // lots?
+            fileText << line;
+            
+        }
+        
+        theFile.close();
+    }
+    
+    bool parsingSuccessful = reader.parse( fileText.str(), root );
+
+    if ( !parsingSuccessful )
+    {
+        // report to the user the failure and their locations in the document.
+        std::cout  << "Failed to parse preset JSON: \n"
+        << reader.getFormattedErrorMessages();
+        return;
+    }
+
+    // now put into variables
+    const Json::Value jpresets = root["presets"];
+    
+    for ( int index = 0; index < jpresets.size(); ++index ) thePresets.push_back(new Preset(jpresets[index]));
+    
+    //printAll();
+     
+}
+//---------------------------------------------------------------------------
+void PresetManager::printAll(){
+    cout << "----------------ALL PRESETS-------------: \n";
+    cout << allPresetsToJson() << "\n";
+}
+//---------------------------------------------------------------------------
+void PresetManager::showNameDialog(){
+//    if(!presetAlertViewController.alertShowing){ // this is to stop wierd infinite loop in ios5 (simulator)
+//        presetAlertShowing = true;
+//        [presetAlertViewController showPresetNamePrompt];
+//        
+//    }
+    
+}
+//---------------------------------------------------------------------------
+// when save button pressed
+int PresetManager::savePreset(const string name, vector<int> stuff){
+
+    presetAlertShowing = false;
+    // check for same name
+    vector<Preset *>::iterator iter;
+    /*
+    for(iter = thePresets.begin(); iter < thePresets.end(); iter++){
+        if ((*iter)->name == name){
+            cout << " Preset by that name exists\n";
+            
+            // use exceptions!
+            return -1;
+        }
+    }
+     
+    if(name == ""){
+        cout << "Please name preset\n";
+        return -2;
+        
+    }
+     */
+
+    thePresets.push_back(new Preset(stuff, name, nextID, eventLogger.userName, eventLogger.deviceID));
+    eventLogger.logEvent(SAVE_PRESET); // TODO need to log details?
+
+    return nextID++;
+}
+
+
+//----------------------------------------------cu-----------------------------
+void PresetManager::startLoadAll(){
+    // get stuff from file
+    // load file
+
+    string fname = ofxiPhoneGetDocumentsDirectory() + PRESET_FILENAME;
+    
+    readJsonToPresets(fname);
+    
+    timesOpened++;
+}
+
+//---------------------------------------------------------------------------
+void PresetManager::exitAndSaveAll(){
+    ofFile presetFile(ofxiPhoneGetDocumentsDirectory() +PRESET_FILENAME,ofFile::WriteOnly);
+    
+    // stick all the stuff in a json value
+    Json::Value root = allPresetsToJson();
+    
+    cout << root;
+    presetFile << root;
+
+}
+
+//---------------------------------------------------------------------------
+void PresetManager::clearAll(){
+    thePresets.clear();
+}
+//---------------------------------------------------------------------------
+//---------------------------------------------------------------------------
+//---------------------------------------------------------------------------
+//---------------------------------------------------------------------------
+//---------------------------------------------------------------------------
\ No newline at end of file