I have the mouse colliding with an ellipse, which will generate a tone. If I hold it there the tone would just play over and over again. How could I get it to play only once until the next time I touch it again?
Anyways, any references, links, or feedback will be appreciated, thanks!
import ddf.minim.analysis.*;
import ddf.minim.*;
import ddf.minim.signals.*;
Minim minim;
AudioOutput out;
SineWave mySine;
MyNote newNote;
void setup() {
size(600, 600);
stroke(255);
minim = new Minim(this);
out = minim.getLineOut(Minim.STEREO);
}
void draw() {
background(0);
ellipse(mouseX, mouseY, 50, 50);
circleStuff();
}
void circleStuff() {
int cX = 100;
int cY = 150;
int cRadius = 50;
float cDist = dist( cX, cY, mouseX, mouseY);
float pitch = 0;
if ( cDist < cRadius ) {
fill(255, 0, 0);
pitch = 494;
} else {
fill(0);
}
if (pitch > 0) {
newNote = new MyNote(pitch, 0.2);
}
ellipse( cX, cY, cRadius*2, cRadius*2 );
}
void stop()
{
out.close();
minim.stop();
super.stop();
}
class MyNote implements AudioSignal
{
private float freq;
private float level;
private float alph;
private SineWave sine;
MyNote(float pitch, float amplitude)
{
freq = pitch;
level = amplitude;
sine = new SineWave(freq, level, out.sampleRate());
alph = 0.9;
out.addSignal(this);
}
void updateLevel()
{
level = level * alph;
sine.setAmp(level);
if (level < 0.01) {
out.removeSignal(this);
}
}
void generate(float [] samp)
{
sine.generate(samp);
updateLevel();
}
void generate(float [] sampL, float [] sampR)
{
sine.generate(sampL, sampR);
updateLevel();
}
}