Stop a sine wave synth in minim
in
Core Library Questions
•
2 years ago
This is my first real project with Processing so forgive me if this is a really stupid question.
I have 3 sine waves playing various note frequencies mapped to RGB color codes. As you drag the mouse over a pixel it gets the RGB values and plays the frequency associated with it. I only want this to happen when the mouse button is down. So how do I get the sound to stop on mouse button release? Currently I am just setting the sine to freq 0 but it is still outputing sound. Here is my code. Be kind
I have 3 sine waves playing various note frequencies mapped to RGB color codes. As you drag the mouse over a pixel it gets the RGB values and plays the frequency associated with it. I only want this to happen when the mouse button is down. So how do I get the sound to stop on mouse button release? Currently I am just setting the sine to freq 0 but it is still outputing sound. Here is my code. Be kind

- import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
/*
Allow the image to also be played by holding the left mouse
button down and dragging over the image getting the pixel
infomation and just playing the freq ignoring the duration and
pan values.
Include a button to allow user to upload an image file.
*/
float[] color_to_freq = new float[256];
float[] freq = new float[26];
Minim minim;
AudioOutput out;
SineWave sine_red;
SineWave sine_green;
SineWave sine_blue;
PImage b;
boolean soundOut = false;
void setup() {
int w = 500;
int h = 500;
size(w, h);
Build_Freq();
minim = new Minim(this);
//create line out
out = minim.getLineOut(Minim.MONO);
//create RGB sine waves
sine_red = new SineWave(0, 0.5, out.sampleRate());
sine_green = new SineWave(0, 0.5, out.sampleRate());
sine_blue = new SineWave(0, 0.5, out.sampleRate());
//Send RGB sine waves to out
out.addSignal(sine_red);
out.addSignal(sine_green);
out.addSignal(sine_blue);
//load image - for now.
b = loadImage("1.jpg");
image(b, 0, 0, w, h);
}
void draw() {
if (mousePressed && (mouseButton == LEFT))
{
//get color info
color pc = get(mouseX, mouseY);
//get individual RGB values
float rc = red(pc);
float gc = green(pc);
float bc = blue(pc);
//Convert individual RGB values to ints for array use
int push_red = int(rc);
int push_green = int(gc);
int push_blue = int(bc);
//Set sine waves to the RGB values
sine_red.setFreq(color_to_freq[push_red]);
sine_green.setFreq(color_to_freq[push_green]);
sine_blue.setFreq(color_to_freq[push_blue]);
soundOut = true;
}
else
{
soundOut = false;
sine_red.setFreq(0);
sine_green.setFreq(0);
sine_blue.setFreq(0);
}
}
void stop() {
out.close();
minim.stop();
super.stop();
}
void Build_Freq() {
freq[0] = 220.00; //a
freq[1] = 233.08; //a#,bb
freq[2] = 246.94; //b
freq[3] = 261.63; //c
freq[4] = 277.18; //c#,db
freq[5] = 293.66; //d
freq[6] = 311.13; //d#.eb
freq[7] = 329.63; //e
freq[8] = 349.23; //f
freq[9] = 369.99; //f#,gb
freq[10] = 392.00; //g
freq[11] = 415.30; //g#,ab
freq[12] = 440.00; //a
freq[13] = 466.16; //a#,bb
freq[14] = 493.88; //b
freq[15] = 523.25; //c
freq[16] = 554.37; //c#,db
freq[17] = 587.33; //d
freq[18] = 622.25; //d#.eb
freq[19] = 659.26; //e
freq[20] = 698.46; //f
freq[21] = 739.99; //f#,gb
freq[22] = 783.99; //g
freq[23] = 830.61; //g#,ab
freq[24] = 880.00; //a
freq[25] = 932.33; //a#,bb
for (int i = 0; i < 250; i++) {
float aaa = i / 10;
int fl = floor(aaa);
color_to_freq[i] = freq[fl];
}
for (int i = 250; i < 255; i++) {
color_to_freq[i] = 987.77; //b
}
// for (int i = 0; i < 255; i++) {
// println(color_to_freq[i]);
// }
}
1