We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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!
Answers
Highlight code, press ctrl-o
Warning: avoid using Float. Stick w/
float
as much as possible! :-SSinOsc isn't working in my laptop here. Nonetheless, I've made some tweaks for ya: O:-)
@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!