Create a music sequencer clock

edited October 2013 in How To...

Hello!

OK, I've got a fantastic idea for a music sequencer but there are minor details (read essential requirements) that I'm wondering how to achieve.

First of all, I need to know how to create a clock, something that ticks at regular intervals, so that my objects could be synchronized. What's the best method to create that clock? I'm a bit dubious about checking millis() at each iteration of the draw loop, is that precise enough? There would be some graphical things happening in the same time, I'm afraid these would lead the clock to behave erratically...

Answers

  • also...

    int lastStored;
    int interval = 1000;
    color c;
    void setup() {
      noStroke();
      c = color (random(255), random(255), random(200));
      lastStored = millis();
    }
    void draw() {
    
      background(255);
      fill(c);
      ellipse(width/2, height/2, 30, 30);
        if (millis() - lastStored > interval) {
          c = color (random(255), random(255), random(200));
          lastStored = millis();
        }
    }
    
  • How about

    if (millis() - lastStored > interval) {
          c = color (random(255), random(255), random(200));
          lastStored = lastStored + interval(); // to prevent offset from increasing
        }
    

    Would that be sufficient? I'm sure there are people who already coded sequencers around here :)

  • edited October 2013

    Modded Sec Counter: >:)

    // forum.processing.org/two/discussion/585/create-a-music-sequencer-clock
    
    final static int DIM = 130, INTERVAL = 1 * 1000;
    
    int cx, cy, lastStored;
    
    void setup() {
      size(170, 170);
      frameRate(60);
      noSmooth();
      noStroke();
    
      cx = width  >> 1;
      cy = height >> 1;
    }
    
    void draw() {
      if (millis() - lastStored < INTERVAL)   return;
    
      frame.setTitle("Secs: " + (lastStored += INTERVAL)/1000);
    
      background(-1);
    
      fill((color) random(#000000));
      ellipse(cx, cy, DIM, DIM);
    }
    
  • _vk_vk
    edited October 2013

    I think millis() is not going to be affected by performance, unless you got really low frame rate where you would not check enough fast for elapsed time. I mean times passes independently of your sketch. So it's performance is going to impact only the frequency you check for elapsed time. In fact time is passing right now... (:

  • _vk_vk
    edited October 2013

    warning, this is going for ever... if you don't stop it (:

    int lastStored;
    int interval = 1000;
    ArrayList<CEllipse> many = new ArrayList<CEllipse>();
    
    void setup() {
      size(300, 300);
      noStroke(); 
      many.add(new CEllipse());
      lastStored = millis();
      frameRate(-1);
    }
    void draw() {
    
      background(255);
      for (CEllipse e:many) {
        e.display();
      }
    
      float elapsed = millis() - lastStored;
      if (elapsed > interval) {
        for (CEllipse e:many) {
          e.update();
        }
    
        lastStored = millis();
        println(int(frameRate) + "fps  " + elapsed + " elapsed since last sample with " + many.size() + " balls.");
      }
      for (int i = 0; i < 10; i++) {
        many.add(new CEllipse());
      }
    }
    
    
    class CEllipse {
      color c;
      float x, y;
    
      CEllipse() {
        c = color (random(255), random(255), random(200));
        x = random(width);
        y = random(height);
      }
    
      void display() {
        fill(c);
        ellipse(x, y, 10, 10 );
      }
    
      void update() {
        c = color (random(255), random(255), random(200));
      }
    }
    
  • Thanks! I will try to build from there. Any suggestion for keeping track of time besides replacing lastStored = millis(); with lastStored = lastStored + interval; ?

  • what do you mean?

  • When you store the millis() value, more than 1 s has passed (about 1.02 s on my machine). So using that average, 60 ticks would last 61.2 seconds.

Sign In or Register to comment.