Max type functions

Hello,
I’ve been an oF user for about the past 6 months. However, I keep coming back to the need for functions that are similar to those used in max/msp that I just can’t figure out how to accomplish in openFrameworks. A basic example of this would be to create a function that is a ‘counter’ that counts 1 through 4 and ‘bangs’ a random number every time the ‘counter’ is equal to ‘1’, similar to a sequencer. The problem is that when you say generate a random number every time the counter gets back to ‘1’ it generates as many random numbers as frames that are passed while the counter is equal to’1’. I think what I’m looking for is a way to create a ‘bang’ and can’t really understand what a ‘bang’ actually would be written out in c++. Thanks in advance.

you could do something like this. This “Bangs” after a certain number of audio samples.


class Bang {
public:
    Bang(int bangInterval){
        currentSample = 0;
        this->bangInterval = bangInterval;
    }
    bool update(int samples){
        currentSample+=samples;
        if (currentSample >= bangInterval) {
            currentSample = 0;
            return true;
        }else{
            return false;
        }
    }

public:
    int bangInterval;
    int currentSample;
    
    
};
void ofApp::audioOut(float *output, int bufferSize, int nChannels){
  if(bang.update(bufferSize)){
     // genRandomNumber
 }  
   
}

Hmmm … I get where you’re coming from here. it makes sense to use the audioOut because it’s on a different thread correct? I don’t seem to be getting my numbers generated randomly in my code which you can find here. I think I may be using audioOut incorrectly.

The bang interval is in samples not seconds. if you want to bang every 4 seconds you set the bang interval to 176400 samples. Yes if you want precise timing in sync with audio you need to update the bang in audioOut

If anyone out there came from a Max background as I did and would like some similar functions in OF, I built out a small addon that includes some such as a counter, metro, line. Hope someone can get some use out of it.

ofxControlUtils https://github.com/nightshining/ofxControlUtils

Here is a video made by Mizutani Kirin