Beads and Arrays
in
Contributed Library Questions
•
1 year ago
I'm writing a program that triggers a different sound every time a box on screen is clicked. I've managed to load the sounds into an array and draw a box to the screen for each sound in the array. Does anyone one know how I might assign an individual sound to each box so that when the box is clicked a sound is played? The boxes can be dragged around the screen, so the sound must play no matter where on screen the box is located.
- import java.io.File;
- import beads.*;
- // Beads variables
- AudioContext ac;
- int numSamples = 0;
- int sampleWith = 0;
- String [] sourceFile;
- Gain [] gain;
- Glide [] gainAmt;
- SamplePlayer [] samplePlayer;
- // Interface variables
- SonicB [] sonicB = new SonicB[0];
- int grab = -1;
- float time = 0;
- int blobRad = 60;
- Blob blob = new Blob();
- void setup (){
- size(800, 600);
- smooth();
- float x = 30;
- while(x < width){
- float y = 30;
- while (y < height){
- makeSonicB(x, y, 20);
- y = y + 60;
- }
- x = x + 60;
- }
- ac = new AudioContext();
- File folder = new File(sketchPath("") + "samples");
- File [] fileList = folder.listFiles();
- for (int i = 0; i < fileList.length; i ++) {
- if (fileList[i].isFile()) {
- if (fileList[i].getName().endsWith(".wav")) {
- numSamples ++;
- }
- }
- }
- if (numSamples <= 0) {
- println("No samples found in " + sketchPath("") + "samples/");
- println("Exiting...");
- exit();
- }
- sampleWith = 20;
- sourceFile = new String[numSamples];
- int count = 0;
- for (int i = 0; i < fileList.length; i ++) {
- if (fileList[i].isFile()) {
- if (fileList[i].getName().endsWith(".wav")) {
- sourceFile[count] = fileList[i].getName();
- count ++;
- }
- }
- }
- gain = new Gain[numSamples];
- gainAmt = new Glide[numSamples];
- samplePlayer = new SamplePlayer[numSamples];
- try {
- for (count = 0; count < numSamples; count ++) {
- println("loading" + sketchPath("") + "samples/" + sourceFile[count]);
- samplePlayer[count] = new SamplePlayer(ac, new Sample(sketchPath("") + "samples/" + sourceFile[count]));
- samplePlayer[count].setKillOnEnd(false);
- gainAmt[count] = new Glide(ac, 0.0);
- gain[count] = new Gain(ac, 1, gainAmt[count]);
- gain[count].addInput(samplePlayer[count]);
- ac.out.addInput(gain[count]);
- }
- }
- catch (Exception e){
- println("Error loading samples");
- e.printStackTrace();
- exit();
- }
- ac.start();
- }
- void draw(){
- background(255);
- if (mousePressed == true){
- selectBox();
- if (grab >= 0){
- sonicB[grab].mouseMove();
- }
- }
- for (int i = 0; i < numSamples; i ++){
- sonicB[i].display();
- sonicB[i].drag(0.75);
- }
- time += 0.02;
- blob.display();
- }
- void makeSonicB(float x, float y, float rad){
- SonicB newSonicB = new SonicB(sonicB.length, color(150, 20, 220), x, y, rad);
- sonicB = (SonicB []) append(sonicB, newSonicB);
- }
- void mousePressed(){
- }
- void mouseReleased(){
- grab = -1;
- }
- void selectBox(){
- for (int i = 0; i < sonicB.length; i ++){
- if (dist(sonicB[i].xPos, sonicB[i].yPos, mouseX, mouseY) < sonicB[i].rad / 2){
- grab = sonicB[i].id;
- }
- }
- }
- void keyPressed() {
- if (key == CODED) {
- if (keyCode == UP) {
- blobRad += 10;
- }
- else if (keyCode == DOWN) {
- blobRad -=10;
- }
- if (blobRad >= 400) {
- blobRad = 60;
- }
- }
- }
- void triggerSample(){
- }
- ///////////////////////////////////////////////////////////////////////
- class SonicB{
- color c;
- float xPos;
- float yPos;
- float speedX;
- float speedY;
- float rad;
- int id;
- SonicB(int _id, color _c, float _xPos, float _yPos, float _size){
- id = _id;
- c = _c;
- xPos = _xPos;
- yPos = _yPos;
- rad = _size;
- }
- void display(){
- rectMode(CENTER);
- noStroke();
- fill(c);
- rect(xPos, yPos, rad, rad);
- fill(c, 20);
- rect(xPos, yPos, rad*2, rad*2);
- }
- void mouseMove(){
- xPos = mouseX;
- yPos = mouseY;
- speedX = mouseX - pmouseX;
- speedY = mouseY - pmouseY;
- }
- void drag(float drag){
- if (abs(speedX) > 0.01 || abs(speedY) > 0.01) {
- speedX *= drag;
- speedY *= drag;
- xPos = xPos + speedX;
- yPos = yPos + speedY;
- if (yPos < 0) yPos = height;
- if (yPos > height) yPos = 0;
- if (xPos < 0) xPos = width;
- if (xPos > width) xPos = 0;
- }
- }
- }
- ///////////////////////////////////////////////////////////
- class Blob{
- Blob(){
- }
- void display(){
- fill(180, 160, 240, 60);
- ellipse(mouseX, mouseY, blobRad, blobRad);
- }
- }
1