Intro to SoundCipher

SoundCipher

Many programs use sounds or music, whether it be a click when you press a button or a soundtrack to a video game music and sound is everywhere. SoundCipher is a library which allows programmers to implement sounds and music into their Processing scripts. It provides users with a multitude of instruments and ways to use them. You can see the demonstration that we will be recreating down below or at: https://www.ktbyte.com/projects/project/79661
Starting out with SoundCipher is simple. To begin you must declare a SoundCipher object as demonstrated below:
SoundCipher sc = new SoundCipher(this); //Declares & instantiates "sc" as SoundCipher object.

void setup() {
     size(480, 360);
}
Next, to play a sound the “playNote” function will be added into the “mousePressed” function so a sound is played every click.
SoundCipher sc = new SoundCipher(this); //Declares & instantiates "sc" as SoundCipher object.

void setup() {
     size(480, 360);
}

void mousePressed() {
     sc.playNote(60, 50, 0.5); //plays given note with specified dynamic (loudness) and duration: playNote(NOTE, DYNAMIC, DURATION)
}
After, to make the sound based off of the position of our mouse as in the example we must add a note and dynamic variable and map the note variable to our mouse’s y-position and the dynamic to our mouse’s x-position. Lastly we must substitute the values in our “playNote” function to the variables.
SoundCipher sc = new SoundCipher(this); //Declares & instantiates "sc" as SoundCipher object.

void setup() {
     size(480, 360);
}

void mousePressed() {
     int note = (int)map(mouseX, 0, width, 22, 100); //maps note value between 22 & 100 based off mouseX
     int dynamic = (int)map(mouseY, height, 0, 25, 75); //maps dynamic value between 25 & 75 based off mouseY

     sc.playNote(note, dynamic, 0.5); //plays given note with specified dynamic (loudness) and duration: playNote(NOTE, DYNAMIC, DURATION)
}
Once you’ve completed the following it’s time to add the visual effects as seen in the demo. First you’ll need to change your setup function slightly to recreate the exact look. You’ll then a “lastMouseX” and “lastMouseY” variable and set it to the mouseX and mouseY at the end of the mousePressed function to store values for creating the lines. Once this is complete you can add the lines from the last mouse coordinates to the current mouse coordinates and the ellipse at the current mouse position. To top it off add the fill line seen down below to make the color dependent on the mouse position.
SoundCipher sc = new SoundCipher(this); //Declares & instantiates "sc" as SoundCipher object.
float lastMouseX, lastMouseY;

void setup() {
     size(480, 360);
}

void mousePressed() {
    if (lastMouseX == 0) {
        lastMouseX = mouseX; //Insures last mouse position isn't undefined/0 when first ran.
        lastMouseY = mouseY; //^
    }

     stroke(map(mouseY, 0, height, 0, 200), map(mouseX, 0, height, 0, 200), map(mouseY, 0, height, 0, 200));
     line(lastMouseX, lastMouseY, mouseX, mouseY);
    
     noStroke();
     fill(map(mouseY, 0, height, 0, 200), map(mouseX, 0, height, 0, 200), map(mouseY, 0, height, 0, 200));
     ellipse(mouseX, mouseY, 10, 10);

     int note = (int)map(mouseX, 0, width, 22, 100); //maps note value between 22 & 100 based off mouseX
     int dynamic = (int)map(mouseY, height, 0, 25, 75); //maps dynamic value between 25 & 75 based off mouseY

     sc.playNote(note, dynamic, 0.5); //plays given note with specified dynamic (loudness) and duration: playNote(NOTE, DYNAMIC, DURATION)
     lastMouseX = mouseX; //Updates last mouse position
     lastMouseY = mouseY; //^
}
Finally, to customize the sound a little more we’ll change the instrument to one from the following list.
int PIANO = 0,
    XYLOPHONE = 13,
    ELECTRIC_GUITAR = 27,
    ACOUSTIC_BASS = 32,
    STRINGS = 48,
    ORCHESTRA_HIT = 55,
    TRUMPET = 56,
    TUBA = 58,
    BRASS = 61,
    ALTO_SAX = 65,
    CLARINET = 71,
    FLUTE = 73,
    TAIKO = 116,
    SYNTH_DRUM = 118;
In this case we’ll go with the xylophone. You can see how to add/cahange instruments down below.
SoundCipher sc = new SoundCipher(this); //Declares & instantiates "sc" as SoundCipher object.
float lastMouseX, lastMouseY;
int XYLOPHONE = 13;

void setup() {
     size(480, 360);
}

void mousePressed() {
    if (lastMouseX == 0) {
        lastMouseX = mouseX; //Insures last mouse position isn't undefined/0 when first ran.
        lastMouseY = mouseY; //^
    }

     stroke(map(mouseY, 0, height, 0, 200), map(mouseX, 0, height, 0, 200), map(mouseY, 0, height, 0, 200));
     line(lastMouseX, lastMouseY, mouseX, mouseY);
    
     noStroke();
     fill(map(mouseY, 0, height, 0, 200), map(mouseX, 0, height, 0, 200), map(mouseY, 0, height, 0, 200));
     ellipse(mouseX, mouseY, 10, 10);

     int note = (int)map(mouseX, 0, width, 22, 100); //maps note value between 22 & 100 based off mouseX
     int dynamic = (int)map(mouseY, height, 0, 25, 75); //maps dynamic value between 25 & 75 based off mouseY

     sc.instrument = XYLOPHONE; //changes "sc" instrument to xylophone
     sc.playNote(note, dynamic, 0.5); //plays given note with specified dynamic (loudness) and duration: playNote(NOTE, DYNAMIC, DURATION)
     lastMouseX = mouseX; //Updates last mouse position
     lastMouseY = mouseY; //^
}
Now that you’re able to use SoundCipher try remaking this program but with your own twist. It could be anything from making the instrument a different one every time or having it play a series of notes every time you click.

Leave a Reply

Your email address will not be published. Required fields are marked *