view SequenceController.h @ 13:ab3e0e980c82

Sequence conrtollrer FINALLY works.
author Robert Tubb <rt300@eecs.qmul.ac.uk>
date Mon, 20 Oct 2014 19:36:39 +0100
parents af71bf84660f
children f83635861187
line wrap: on
line source
//
//  SequenceController.h
//  riftathon
//
//  Created by Robert Tubb on 17/10/2014.
//
//

#ifndef __riftathon__SequenceController__
#define __riftathon__SequenceController__

#include <iostream>
#include "presetManager.h"

#define MAX_TARGETS_IN_SEQUENCE 2
#define MIN_TEMPO   80
#define MAX_TEMPO   200
#define NUM_TEMPO_STEPS 3
#define NUM_PRESETS 16

//===============================================================================


class Sequence{
public:
    Sequence(vector<int> si, int t, bool pv){
        tempo = t;
        presetSequence =  si;
        isSequencePreview = pv;
        curStep = 0;
    };
    bool moveToNextStep(){
        curStep++;
        
        if (curStep >= presetSequence.size()){
            return false;
        }else{
            return true;
        }
    };
    int getCurrentPresetIndex(){
        cout << "step : " << curStep << endl;
        if (curStep >= presetSequence.size()){
            cout << "ERROR: off end of seq" << endl;
            return 0;
        }
        int i = presetSequence[curStep];
        if(i < 0){
            cout << "ERROR: negative index" << endl;
        }
        return i;

    };
    int getStepNo(){
        return curStep;
    }
    float tempo;
    
    bool isSequencePreview; // true if demoing the seq to the user, false if user is matching a seq
    vector<int> presetSequence;
    int curStep;
    
};

//===============================================================================

class Run{
public:
    Run(vector<Sequence> vs, int sl){
        theSequences = vs;
        sequenceLength = sl;
        curSeq = 0;
    };
    bool moveToNextStep(){
        if (curSeq >= theSequences.size()){
            return false;
        }
        
        bool stepFound = theSequences[curSeq].moveToNextStep();
        if (stepFound){
            return true;
        }else{
            curSeq++;
            if (curSeq >= theSequences.size()){
                return false;
                
            }else{
                return true;
            }
        }
        
        return false;
    };
    int getCurrentPresetIndex(){
        cout << "seq : " << curSeq << endl;
        if (curSeq >= theSequences.size()){
            cout << "ERROR: get preset" << endl;
            return 0;
        }
        return theSequences[curSeq].getCurrentPresetIndex();
        
    };
    int getSequenceNo(){
        return curSeq;
    }
    
    vector<Sequence> theSequences;
    int curSeq;
    int sequenceLength;
    
};
//===============================================================================

class SequenceController{
public:
  
    SequenceController(){
        makeSequences();
        tempoInc = float(MAX_TEMPO - MIN_TEMPO) / float(NUM_TEMPO_STEPS);
        curRun = 0;
    };

    int stepForward(){
        cout << "run : " << curRun << endl;
        if (curRun >= theRuns.size()){
            return -1;
        }
            
        bool stepFound = theRuns[curRun].moveToNextStep();
        if (stepFound){
            return curRun;
        }else{
            curRun++;
            if (curRun >= theRuns.size()){
                cout << " END OF RUNS " << endl;
                return -1;
                
            }else{
                return -2; // in order to stop the metro, next tick will just start thjings agaaain
            }
        }
        
        return false;
        
    }

    int getCurrentPresetIndex(){
        
        // if end of sequence return something else so we can do something
        // if end of tempo ramp return something else
        if (curRun >= theRuns.size()){
            cout << "DAAAAHHH" << endl;
            return -1;
        }
        int nextIndex = theRuns[curRun].getCurrentPresetIndex();

        return nextIndex;
    };
    vector<int> getCurrentRunSeqAndStep(){
        vector<int> rss;
//        int r = curRun - theRuns.begin();
//        int sq = (*curRun).getSequenceNo();
//        int s = 4534534;
//        rss.push_back(r);
//        rss.push_back(sq);
//        rss.push_back(s);
        return rss;
        
    }
protected:
    void makeSequences(){
        srand (time(NULL));
        

        for(int numInSequence = 1; numInSequence <= MAX_TARGETS_IN_SEQUENCE; numInSequence++){
            float curTempo = MIN_TEMPO;
            vector<Sequence> seqsForRun;
            
            for(int tempoLevel = 0; tempoLevel < NUM_TEMPO_STEPS; tempoLevel++){
                
                vector<int> stepsForSequence;
                
                // repeat the same tests for xtra practice?
                for(int n=0; n < numInSequence; n++){
                    int next = getRandomButNot(NUM_PRESETS,stepsForSequence);
                    stepsForSequence.push_back(next);
                    
                    cout << next << ",";
                }
                seqsForRun.push_back(Sequence(stepsForSequence, curTempo, true));
                seqsForRun.push_back(Sequence(stepsForSequence, curTempo, false));
                curTempo += tempoInc;
                cout << endl;
                
            }
            
            theRuns.push_back( Run(seqsForRun,numInSequence) );
            
            cout << "---" << endl;
        }
    };

    int getRandomButNot(int max, vector<int> notThese){
        
        bool there = true;
        int randomInt = rand() % max;
        
        if (notThese.size()){
        while(there){
            randomInt = rand() % max;
            vector<int>::iterator result = std::find(notThese.begin(), notThese.end(), randomInt);
            there =  (result != notThese.end());
        }
        }
        return randomInt;
        
    };

    
private:
   
    vector<Run> theRuns;
    int curRun;
    
    float tempoInc;
};
//===============================================================================

#endif /* defined(__riftathon__SequenceController__) */