get value of keypressed
in
Contributed Library Questions
•
9 months ago
Hi everyone.
I just started with processing and im stuck now.
I want to play two frequencies at the same time. Right now i can only play one frequency at a time.
i need to make a new function that takes the frequency when to keys are pressed at the same time and then dived them by two. Thats is the new freqency that should be played.
i hope you guys can help me or point me at the right direction.
Here is my code
import ddf.minim.*; //sound library
import ddf.minim.signals.* ;
import ddf.minim.effects.* ;
Minim minim;
AudioOutput au_out ;
SquareWave sqw ;
LowPassSP lpass ;
Boolean[] firstkey, secondkey;
float[] notes = {
0, 261.63, 293.67, 329.63, 349.23, 391.99, 440, 493.88, 523.25
};
color w = #FFFFFF;
color b = #000000;
color[] buttonsColor = {
w, w, w, w, w, w, w, w
};
void setup() {
size(800, 600);
minim = new Minim(this) ;
au_out = minim.getLineOut() ; //channel
sqw = new SquareWave(440, 1, 44100); // frequentie wave
lpass = new LowPassSP(200, 44100); // filter
au_out.addSignal(sqw);
au_out.addEffect(lpass);
sqw.setFreq(notes[0]) ;
frameRate(60);
}
void draw ()
{
textSize(14);
text("Use the following keys to play the piano.'A', 'S', 'D', 'F', 'H', 'J', 'K', 'L', ", 10, 400);
fill(buttonsColor[0]);
rect(50, 50, 70, 300);
fill(buttonsColor[1]);
rect(120, 50, 70, 300);
fill(buttonsColor[2]);
rect(190, 50, 70, 300);
fill(buttonsColor[3]);
rect(260, 50, 70, 300);
fill(buttonsColor[4]);
rect(330, 50, 70, 300);
fill(buttonsColor[5]);
rect(400, 50, 70, 300);
fill(buttonsColor[6]);
rect(470, 50, 70, 300);
fill(buttonsColor[7]);
rect(540, 50, 70, 300);
fill(b);
rect(85, 50, 40, 225);
rect(170, 50, 40, 225);
rect(295, 50, 40, 225);
rect(380, 50, 40, 225);
rect(380, 50, 40, 225);
rect(450, 50, 40, 225);
}
void keyPressed() {
if (key == 'a') {
sqw.setFreq(notes[1]) ;
buttonsColor[0] = b;
}
if (key == 's') {
sqw.setFreq(notes[2]);
buttonsColor[1] = b;
}
if(key == 'd') {
sqw.setFreq(notes[3]) ;
buttonsColor[2] = b;
}
if(key == 'f') {
sqw.setFreq(notes[4]) ;
buttonsColor[3] = b;
}
if(key == 'h') {
sqw.setFreq(notes[5]) ;
buttonsColor[4] = b;
}
if(key == 'j') {
sqw.setFreq(notes[6]) ;
buttonsColor[5] = b;
}
if(key == 'k') {
sqw.setFreq(notes[7]) ;
buttonsColor[6] = b;
}
if(key == 'l') {
sqw.setFreq(notes[8]) ;
buttonsColor[7] = b;
}
}
void keyReleased()
{
if(key == 'a'){
sqw.setFreq(notes[0]);
buttonsColor[0] = w;
}
if(key == 's'){
sqw.setFreq(notes[0]) ;
buttonsColor[1] = w;
}
if(key == 'd'){
sqw.setFreq(notes[0]) ;
buttonsColor[2] = w;
}
if(key == 'f'){
sqw.setFreq(notes[0]) ;
buttonsColor[3] = w;
}
if(key == 'h'){
sqw.setFreq(notes[0]) ;
buttonsColor[4] = w;
}
if(key == 'j'){
sqw.setFreq(notes[0]) ;
buttonsColor[5] = w;
}
if(key == 'k'){
sqw.setFreq(notes[0]) ;
buttonsColor[6] = w;
}
if(key == 'l') {
sqw.setFreq(notes[0]) ;
buttonsColor[7] = w;
}
}
import ddf.minim.signals.* ;
import ddf.minim.effects.* ;
Minim minim;
AudioOutput au_out ;
SquareWave sqw ;
LowPassSP lpass ;
Boolean[] firstkey, secondkey;
float[] notes = {
0, 261.63, 293.67, 329.63, 349.23, 391.99, 440, 493.88, 523.25
};
color w = #FFFFFF;
color b = #000000;
color[] buttonsColor = {
w, w, w, w, w, w, w, w
};
void setup() {
size(800, 600);
minim = new Minim(this) ;
au_out = minim.getLineOut() ; //channel
sqw = new SquareWave(440, 1, 44100); // frequentie wave
lpass = new LowPassSP(200, 44100); // filter
au_out.addSignal(sqw);
au_out.addEffect(lpass);
sqw.setFreq(notes[0]) ;
frameRate(60);
}
void draw ()
{
textSize(14);
text("Use the following keys to play the piano.'A', 'S', 'D', 'F', 'H', 'J', 'K', 'L', ", 10, 400);
fill(buttonsColor[0]);
rect(50, 50, 70, 300);
fill(buttonsColor[1]);
rect(120, 50, 70, 300);
fill(buttonsColor[2]);
rect(190, 50, 70, 300);
fill(buttonsColor[3]);
rect(260, 50, 70, 300);
fill(buttonsColor[4]);
rect(330, 50, 70, 300);
fill(buttonsColor[5]);
rect(400, 50, 70, 300);
fill(buttonsColor[6]);
rect(470, 50, 70, 300);
fill(buttonsColor[7]);
rect(540, 50, 70, 300);
fill(b);
rect(85, 50, 40, 225);
rect(170, 50, 40, 225);
rect(295, 50, 40, 225);
rect(380, 50, 40, 225);
rect(380, 50, 40, 225);
rect(450, 50, 40, 225);
}
void keyPressed() {
if (key == 'a') {
sqw.setFreq(notes[1]) ;
buttonsColor[0] = b;
}
if (key == 's') {
sqw.setFreq(notes[2]);
buttonsColor[1] = b;
}
if(key == 'd') {
sqw.setFreq(notes[3]) ;
buttonsColor[2] = b;
}
if(key == 'f') {
sqw.setFreq(notes[4]) ;
buttonsColor[3] = b;
}
if(key == 'h') {
sqw.setFreq(notes[5]) ;
buttonsColor[4] = b;
}
if(key == 'j') {
sqw.setFreq(notes[6]) ;
buttonsColor[5] = b;
}
if(key == 'k') {
sqw.setFreq(notes[7]) ;
buttonsColor[6] = b;
}
if(key == 'l') {
sqw.setFreq(notes[8]) ;
buttonsColor[7] = b;
}
}
void keyReleased()
{
if(key == 'a'){
sqw.setFreq(notes[0]);
buttonsColor[0] = w;
}
if(key == 's'){
sqw.setFreq(notes[0]) ;
buttonsColor[1] = w;
}
if(key == 'd'){
sqw.setFreq(notes[0]) ;
buttonsColor[2] = w;
}
if(key == 'f'){
sqw.setFreq(notes[0]) ;
buttonsColor[3] = w;
}
if(key == 'h'){
sqw.setFreq(notes[0]) ;
buttonsColor[4] = w;
}
if(key == 'j'){
sqw.setFreq(notes[0]) ;
buttonsColor[5] = w;
}
if(key == 'k'){
sqw.setFreq(notes[0]) ;
buttonsColor[6] = w;
}
if(key == 'l') {
sqw.setFreq(notes[0]) ;
buttonsColor[7] = w;
}
}
1