How to run multiple sinOscs in class objects

edited May 2016 in Library Questions

Hi all, new to Processing and would appreciate some help!

I'm trying to make a class where each instance of it has a sinOsc playing at a different frequency, with a slowly decreasing (and then increasing) volume for each. I can't find much in the way of in-depth tutorials on this object, and am stuck! here's the code:

PImage img;
import processing.sound.*;
SinOsc sine;


Sphere s1;
Sphere s2;

void setup() {
  size(665, 800);
  img = loadImage("MK.png");
  s1 = new Sphere(random(0, (img.height/2)), random(0, (img.width/2)), 255, 10);
  s2 = new Sphere(random(0, (img.height/2)), random(0, (img.width/2)), 255, 17);
  sine = new SinOsc(this);
  image (img, 0, 0, img.width/2, img.height/2);

  s1.bells();
  s2.bells();
}

//variables
int time = millis();  


void draw() {
  s1.run();
  s2.run();


}

`

and for the class (Sphere) itself:

class Sphere {

  //GLOBAL
  float xpos = 0;//set variables for the class, default for all of this type
  float ypos = 0;
  int col1 = 255;
  int decay = 8;
  int timer;
  Float amp = 0.5;
  Float freq = (random(80, 400));
  int trans = 255;



//CONSTRUCTOR
    Sphere(float _xpos, float _ypos, int _col1, int _decay){
      xpos = _xpos;//args for each instantiation can then alter the global vars
      ypos = _ypos;
      col1 = _col1;
      decay = _decay;
    }


void run(){
  display();

}

void display(){
fill(col1, 0, 0, trans);
  ellipse(xpos, ypos, 10, 10);//global vars now altered to reflect creation args

  if (millis() - timer >= (decay * 10)){
     trans = trans - 10;
     amp = amp - 0.1;
     timer = millis();
   }

  if (trans <= 0){
     trans = 255;
   }
}

void bells(){

  sine.play();
  sine.freq(freq);
  sine.amp(amp);

}
}

Any help would be appreciated!

Tagged:

Answers

  • Highlight code, press ctrl-o

  • edited May 2016 Answer ✓

    Warning: avoid using Float. Stick w/ float as much as possible! :-S

    SinOsc isn't working in my laptop here. Nonetheless, I've made some tweaks for ya: O:-)

    // forum.Processing.org/two/discussion/16625/
    // how-to-run-multiple-sinoscs-in-class-objects
    
    // _Spaces & GoToLoop (2016-May-16)
    
    import processing.sound.SinOsc;
    
    static final int SPHERES = 2;
    final Sphere[] spheres = new Sphere[SPHERES];
    
    PImage img;
    
    void setup() {
      size(800, 665);
      smooth(4);
    
      //img = createImage(width, height, RGB);
      (img = loadImage("MK.png")).resize(img.width>>1, img.height>>1);
    
      surface.setSize(img.width, img.height);
    
      for (int i = 0; i < SPHERES; ++i)  spheres[i] = new Sphere(
        new SinOsc(this), random(img.width), random(img.height), -1, i*7 + 10);
    }
    
    void draw() {
      background(img);
      for (final Sphere s : spheres)  s.display();
    }
    
    class Sphere {
      final SinOsc sine;
    
      final float x, y;
      final color c;
    
      int decay, timer, opac = 255;
      float amp = .5, freq = random(80, 400);
    
      Sphere(SinOsc _sine, float _x, float _y, color _c, int _decay) {
        sine = _sine;
        x = _x;
        y = _y;
        c = _c;
        decay = 10 * _decay;
    
        bells();
      }
    
      void run() {
        display();
      }
    
      void display() {
        fill(c, opac);
        ellipse(x, y, 10, 10);
    
        if (millis() - timer >= decay) {
          timer = millis();
          if ((opac -= 10) <= 0)  opac = 255;
          if ((amp  -= .1) <= 0)  amp = .5;
        }
      }
    
      void bells() {
        sine.play();
        sine.freq(freq);
        sine.amp(amp);
      }
    }
    
  • @GoToLoop wow, many thanks! This looks very thorough, some new approaches to get my head around too. I'll have fun playing with this, cheers!

Sign In or Register to comment.