Sounds with a mouse hover
in
Contributed Library Questions
•
11 months ago
Hi I'm trying to create essentially a keyboard which you can play the keys using a mouse hover (You can't see keys but different areas of the screen are different notes)
I found this online I really can't remember where:
import arb.soundcipher.*;
SoundCipher sc = new SoundCipher(this);
SoundCipher ex = new SoundCipher(this);
SoundCipher bg = new SoundCipher(this);
int t = 0;
int fps = 100;
int adding = -1;
String newPosition = "";
int playingExample = -1;
void setup() {
size(500, 500);
smooth();
frameRate(fps);
// generate piano
for( int i = 3; i < height / 14 - 3; i++ ) {
Key k = new Key(i);
}
bg.instrument(49);
bg.playNote(50, 20, 100000000);
}
void draw() {
frameRate(fps);
background(0);
t++;
// reset timer at full circle
if( t == 361 ) t = 0;
// draw timeline circle
noFill();
stroke(255);
ellipse( width/2 + 25, height/2, 400, 400 );
// draw current position circle
fill( 0 );
ellipse( width/2 + 200*sin(radians(t)) + 25, height/2 + 200*cos(radians(t)), 20, 20 );
// draw info text
fill(255);
text( "Press UP and DOWN keys to adjust speed ("+Integer.toString(fps)+"%).", 10, 20 );
if( adding > 0 ) {
fill(255);
text("Please enter a number between 0 - 360 for the position: " + newPosition, 10, height-5 );
}
}
public class Key {
int position;
Key( int p ) {
position = p;
registerDraw(this);
}
void draw() {
if( mouseX < 50 && mouseY > position*15 && mouseY < position*15 + 15 ) {
if( playingExample != position ) {
if( playingExample >= 0 ) {
ex.stop();
}
ex.playNote(position*4, 60, 1);
playingExample = position;
}
if( mousePressed ) {
fill(150);
adding = position;
} else {
fill(200);
}
} else {
fill(255);
}
stroke(0);
rect(0, position * 15, 50, 15);
}
}
public class Sound {
public int position;
public int pitch;
float x, y;
boolean enabled = true;
Sound( int position_, int pitch_ ) {
position = position_;
pitch = pitch_;
registerDraw(this);
x = width/2 + 200*sin(radians(position)) + 25;
y = height/2 + 200*cos(radians(position));
}
void draw() {
if( enabled ) {
boolean hovering = false;
// check if note should be played
if( t == position ) {
sc.playNote(pitch, 100, 1);
}
fill( pitch*2, pitch*2, pitch*2 );
// check if mouse is hovering above the circle
if( mouseX > (x-10) && mouseX < (x+10) && mouseY > (y-10) && mouseY < (y+10) )
{
hovering = true;
fill(200);
}
// check if mouse is pressed
if( mousePressed && hovering )
{
// remove this item
this.destroy();
}
ellipse( x, y, 20, 20 );
}
}
void destroy() {
enabled = false;
}
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
if( fps < 150 ) {
fps = fps + 10;
}
} else if (keyCode == DOWN) {
if( fps > 50 ) {
fps = fps - 10;
}
}
}
}
void keyTyped() {
if( int(key) >= 48 && int(key) <= 57 ) {
if( adding > 0 && newPosition.length() < 3 ) {
fill(255);
newPosition = (String)newPosition + (String)Integer.toString(int(int(key) - 48));
}
} else if( int(key) == 10 ) {
newPosition = Integer.toString(Integer.parseInt(newPosition) % 360);
new Sound( int(Integer.parseInt(newPosition)), int(adding*4) );
newPosition = "";
adding = -1;
}
}
Like the keys on the left I don't know how to code the size of the area the sound is, if that makes sense, any help or examples would be appreciated.
1