view PDSynthWrapper.h @ 41:ba426cc4e6e1

better sequence gen , same for easy (1) and hard (many ) seq fixed judder on sliders
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Fri, 05 Dec 2014 18:36:05 +0000
parents fea11c3d1d94
children
line wrap: on
line source
//
//  pdSynthWrapper.h
//  RIFTATHON
//
//  Created by Robert Tubb on 14/01/2014.
//
//


// for riftathon, this is also a wrapper for the sequencer
#pragma once

#include "AppCore.h"
#include "eventLogger.h"
#include "ofxPd.h"
#include <string>
#include "boost/bind.hpp"
#include "boost/function.hpp"
#include "SynthParam.h"
#include "globalVariables.h"
//---------------------------------------------------------------------
//---------------------------------------------------------------------
//---------------------------------------------------------------------

class PDSynthWrapper {
public:
    void init(AppCore* aCore, string sp){
        core = aCore;

        synthPrefix = sp;
        // init all the params, refer to synth in pd patch
        // riftathon_drum_synth
        timbreParams.push_back(SynthParam(64,aCore,"Pitch",sp));
        timbreParams.push_back(SynthParam(64,aCore,"PenvDecay",sp));
        timbreParams.push_back(SynthParam(64,aCore,"DecayFM",sp));
        timbreParams.push_back(SynthParam(64,aCore,"NzFrq1",sp));
        timbreParams.push_back(SynthParam(64,aCore,"NzFrq2",sp));
        timbreParams.push_back(SynthParam(64,aCore,"DecayNz",sp));
        
        //timbreParams.push_back(SynthParam(64,aCore,"SPAM",sp));
        //timbreParams.push_back(SynthParam(64,aCore,"FILTH",sp));
        //timbreParams.push_back(SynthParam(64,aCore,"reson",sp));
        
        if (timbreParams.size() != TOTAL_NUM_PARAMS){
            cout << "ERROR ERROR: WRONG NUM OF timbreParams or TOTAL_NUM_PARAMS" << endl;
        }
        cout << "initialised synth: " << sp << " with " << timbreParams.size() << " params" << endl;
        metronomeRunning = false;
    };
    
// PD SENDERS
    
    void trigger(){
        // play the noise
        List toPD;
        toPD.addSymbol(synthPrefix);
        toPD.addSymbol("playSound");
        toPD.addFloat(1.0); // volume here, just in case??
        
        core->pd.sendList("fromOF", toPD);
        
    };
    
    void sendAllParams(){
        std::vector<SynthParam>::const_iterator psp;
        for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
            psp->sendToPD();
        }
    }
    void sendVolume(int value){
        List toPD;
        toPD.addSymbol(synthPrefix);
        toPD.addSymbol("Volume");
        toPD.addFloat(value); // rounding here??
        
        core->pd.sendList("fromOF", toPD);
    }

    void setNoteLength(int lms){
        List toPD;
        toPD.addSymbol(synthPrefix);
        toPD.addSymbol("noteLength");
        toPD.addFloat(lms); // volume here, just in case??
        
        core->pd.sendList("fromOF", toPD);
    }
    
// GETTERS
    const int getParamValueForID(int pid){
        int v = 0;
        std::vector<SynthParam>::const_iterator psp;
        for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
            if (psp->getID() == pid){
                v = psp->getValue();
                return v;
            }
            
        }
        cout << "ERROR ERROR getParamValueForID not found" << endl;
        return -1;
    }
    const int getMappingIDForName(string name) const{
        
        int rID = -1;
        std::vector<SynthParam>::const_iterator psp;
        for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
            if (psp->getName() == name){
                rID = psp->getID();
                return rID;
            }
            
        }
        cout << "ERROR ERROR getMappingIDForName not found" << endl;
        return -1;
    };
    const string getNameForMappingID(int pid) const{
        
        string rname = "no name";
        std::vector<SynthParam>::const_iterator psp;
        for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
            if (psp->getID() == pid){
                rname = psp->getName();
                return rname;
            }
            
        }
        cout << "ERROR ERROR getNameForMappingID not found" << endl;
        return "error";
    };
    vector<int> getMappingIDForIndices(vector<int> idx){
        vector<int> result;
        
        for(int i = 0 ; i < idx.size(); i++){
            if (idx[i] < timbreParams.size()){
                int mapid = timbreParams[idx[i]].getID();
                
                result.push_back(mapid);
                //cout << " Map id for param no: " << idx[i] << " is  " << *(result.end()-1);
            }else{
                cout << "ERROR ERROR: index bigger than num timbre params" << endl;
                
            }
        }
        return result;
    }
    vector<int> getAllMappingIDs(){
        vector<int> result;
        
        for(int i = 0 ; i < timbreParams.size(); i++){
                int mapid = timbreParams[i].getID();
                result.push_back(mapid);

        }
        return result;
    }
    
    const int getParamValueFromName(string name) const{
        
        int value = -1;
        std::vector<SynthParam>::const_iterator psp;
        for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
            if (psp->getName() == name){
                value = psp->getValue();
                return value;
            }
            
        }
        cout << "ERROR ERROR: getParamValueFromName name not found" << endl;
        return -1;
    };
    
    const int getNumParams(){
        return timbreParams.size();
    };
    
    vector<int> getAllParamValues(){
        vector<int> pl;
        std::vector<SynthParam>::const_iterator psp;
        for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
            pl.push_back(psp->getValue());
        }
        return pl;
    }
    int getIndexForMappingID(int mappingID){
        std::vector<SynthParam>::iterator psp;
        int i = 0;
        for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
            if ( psp->getID() == mappingID){
                return i;
            }
            i++;
        }
    }
    void paramChangeCallback(int mappingID, int value, bool send = true){

        // look for id in params
        std::vector<SynthParam>::iterator psp;
        for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
            if ( psp->getID() == mappingID){
                if (send){
                    psp->setValue(value);
                }else{
                    psp->setValueWithoutSend(value);
                }
                return;
            }
        }
        cout << "ERROR ERROR: paramChangeCallback mappingID not found" << endl;
    };

// SETTERS
    void randomiseParams(){
        
        cout << " randomising" << endl;
        std::vector<SynthParam>::iterator psp;
        for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){

            int value = ofRandom(0,127);
            psp->setValue(value);

        }
        
    };

    void setSameAsOtherSynth(const PDSynthWrapper* otherSynth){
        // loop thru all params and set them to other synth
        std::vector<SynthParam>::iterator psp;
        for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
            psp->setValue(otherSynth->getParamValueFromName(psp->getName()));
    
        }
    };
    
    vector<int> randomiseParamSubset(int howMany){
        vector<int> randomisedIDs;
        for (int i=0;i<howMany;i++){
            
            int value = ofRandom(0,127);
            timbreParams[i].setValue(value);
            randomisedIDs.push_back(i);
            
        }
        return randomisedIDs;
        
    };

    
    void setAllParams(vector<int> params){
        if(params.size() != timbreParams.size()){
            cout << "Error not right number of params in set in synth" << endl;
            return;
        }
        std::vector<SynthParam>::iterator psp;
        int i=0;
        for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
            psp->setValue(params[i]);
            i++;
        }
        

    }
    void setAllParamsWithoutSend(vector<int> params){
        if(params.size() != timbreParams.size()){
            cout << "Error not right number of params in set in synth" << endl;
            return;
        }
        std::vector<SynthParam>::iterator psp;
        int i=0;
        for(psp = timbreParams.begin(); psp < timbreParams.end(); psp++){
            psp->setValueWithoutSend(params[i]);
            i++;
        }
        
        
    }
// METRONOME STUFF
    void registerForTicks(TickListenerFunction callback){

        core->addTickListener(callback);
    }
    void startMetronome(){
        // play the noise
        List toPD;
        core->pd.sendMessage("fromOF", "startMetro", toPD);
        metronomeRunning = true;
    }
    void stopMetronome(){
        // play the noise
        List toPD;
        core->pd.sendMessage("fromOF", "stopMetro", toPD);
        metronomeRunning = false;
    }
    bool isMetronomeRunning(){
        return metronomeRunning;
    }
    void setMetroTime(float t){
        List toPD;
        toPD.addSymbol("setMetroTime");
        toPD.addFloat(t); // rounding here??
        
        core->pd.sendList("fromOF", toPD);
    }
    void playDownbeatBlip(){
        List toPD;
        core->pd.sendMessage("fromOF", "blip", toPD);
    }
    void onTick(int tickNo){
        //
        
    };

private:
    string synthPrefix;
    AppCore* core;
    vector<SynthParam> timbreParams; // array of everything in synth
    bool metronomeRunning;
};

//---------------------------------------------------------------------
//---------------------------------------------------------------------