We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I'm trying to create an array of audios in beads so that they play independently when each SMT zone is touched. The problem I am having is how to initialize and link a different audio file to each zone.
The hope is to be able to use a kinect to play audio files independently within zones. Please let me know if you have any ideas on improving code, or how to use arrays to reduce the footwork of the code.
Thanks
//Set for 6' height from ground 15'6" distance
import vialab.SMT.*;
import beads.*;
//Array of Zones
int arrayNum = 56;
TouchtouchZone[] touchZone = new TouchtouchZone[arrayNum];
boolean isEven;
boolean isOdd;
//Array of Sounds
int soundsNum = 23;
Sound[] sounds = new Sound[soundsNum];
AudioContext ac;
//Playback Variables
int playDelay = 0;
boolean canPlay = true;
//Grid Line Variables
int x, y;
float areaW, areaH;
int num, num2;
float spacing, spacing2;
float xVal, yVal;
//Aspect Ratio Variables
Float aspectX, aspectY;
int buttonSize;
float edgeSpace;
int gridStartX, gridStartY;
//Setup Display INFO
boolean window_fullscreen = false;
int window_width = 1200;
int window_height = 800;
int window_halfWidth;
int window_halfHeight;
int fps_limit = 60;
void setup() {
//Display setup
if ( window_fullscreen) {
window_width = displayWidth;
window_height = displayHeight;
}
window_halfWidth = window_width / 2;
window_halfHeight = window_height / 2;
//processing window setup
frameRate( fps_limit);
size( window_width, window_height, SMT.RENDERER);
SMT.init( this, TouchSource.AUTOMATIC);
//Audio Setup
ac = new AudioContext();
//Aspect Ratio Variables
edgeSpace = 20.0;
// aspectX = 640.0;
// aspectY = 480.0;
aspectX = (float)width;
aspectY = (float)height - edgeSpace*2;
// THIS IS NOT PERFECT YET
gridStartX = (int)(aspectX-aspectY)/2;
gridStartY = (int)edgeSpace;
//Grid Line Variables
// X
num = 8;
areaW=aspectY;
// Y
num2 = 7;
areaH=aspectY;
buttonSize = (int)(aspectY/num2);
// ARRAY MAKES BUTTONS IN CHECKERBOARD STYLE
for (int i=0; i<arrayNum; i++) {
if ((i<=7)&&(i % 2 == 0)) {
x = gridStartX+(i*buttonSize);
y = gridStartY;
}
if (((i>7)&&(i<=15))&&(i % 2 != 0)) {
x = gridStartX+((i-8)*buttonSize);
y = gridStartY+buttonSize;
}
if (((i>15)&&(i<=23))&&(i % 2 == 0)) {
x = gridStartX+((i-16)*buttonSize);
y = gridStartY+(2*buttonSize);
}
if (((i>23)&&(i<=31))&&(i % 2 != 0)) {
x = gridStartX+((i-24)*buttonSize);
y = gridStartY+(3*buttonSize);
}
if (((i>31)&&(i<=39))&&(i % 2 == 0)) {
x = gridStartX+((i-32)*buttonSize);
y = gridStartY+(4*buttonSize);
}
if (((i>39)&&(i<=47))&&(i % 2 != 0)) {
x = gridStartX+((i-40)*buttonSize);
y = gridStartY+(5*buttonSize);
}
if (((i>47)&&(i<=56))&&(i % 2 == 0)) {
x = gridStartX+((i-48)*buttonSize);
y = gridStartY+(6*buttonSize);
}
touchZone[i] = new TouchtouchZone(x, y, buttonSize, buttonSize, 100, 100, 150, 200);
SMT.add(touchZone[i]);
}
// ARRAY INITIALIZES AUDIO SETUP
for (int i=0; i<soundsNum; i++) {
sounds[i] = new Sound(0, 0);
sounds[i].audioSetup();
}
ac.start();
}
void draw() {
background(0);
fill(30);
spacing = buttonSize;
// fill(255);
playDelay++;
if (playDelay >= 15) {
canPlay = true;
} else {
canPlay = false;
}
text("Play Delay: "+playDelay, width-100, height-20);
//FOR GRID DEBUGGING
// rect(0, gridStartY, aspectX, aspectY);
for (int m = 0; m < num; m++) {
for (int n = 0; n < num2; n++) {
stroke(125);
strokeWeight(3);
x = gridStartX+(m*buttonSize);
y = gridStartY+(n*buttonSize);
rect(x, y, buttonSize, buttonSize);
}
}
}
public void drawFrameRate() {
float fps = this.frameRate;
String fps_text = String.format( "fps: %.0f", fps);
pushStyle();
fill( 240, 240, 240, 180);
textAlign( RIGHT, TOP);
textMode( MODEL);
textSize( 32);
text( fps_text, window_width - 10, 10);
popStyle();
}
private class touchZone extends Zone {
protected int colour_red;
protected int colour_green;
protected int colour_blue;
protected int colour_alpha;
public touchZone( int x, int y, int width, int height,
int colour_red, int colour_green, int colour_blue, int colour_alpha) {
super( x, y, width, height);
this.colour_red = colour_red;
this.colour_green = colour_green;
this.colour_blue = colour_blue;
this.colour_alpha = colour_alpha;
this.setCaptureTouches( false);
}
//draw method
public void draw() {
pushStyle();
noStroke();
fill( colour_red, colour_green, colour_blue, colour_alpha);
rect( 0, 0, this.getWidth(), this.getHeight(), 5);
popStyle();
}
public void touch() {
}
//we define the press method so that touches will be unassigned when they 'exit' the zone.
public void press( Touch touch) {
}
}
private class TouchtouchZone extends touchZone {
public TouchtouchZone( int x, int y, int width, int height,
int colour_red, int colour_green, int colour_blue, int colour_alpha) {
super( x, y, width, height,
colour_red, colour_green, colour_blue, colour_alpha);
}
//touch method
public void touch() {
Touch touch = getActiveTouch( 0);
touch.setTint(
colour_red, colour_green, colour_blue, colour_alpha);
for (int i=0; i<soundsNum; i++) {
if (canPlay) {
sounds[i].audioPlay();
}
playDelay=0;
}
}
}
static final boolean isEven(int n) {
return (n & 1) == 0;
}
static final boolean isOdd(int n) {
return !isEven(n);
}
class Sound {
//object variables
float xPos, yPos;
String sourceFile1, sourceFile2, sourceFile3, sourceFile4, sourceFile5, sourceFile6,
sourceFile7, sourceFile8, sourceFile9, sourceFile10, sourceFile11, sourceFile12, sourceFile13,
sourceFile14, sourceFile15, sourceFile16; // this will hold the path to our audio file
SamplePlayer sp1;
SamplePlayer sp2;
SamplePlayer sp3;
SamplePlayer sp4;
SamplePlayer sp5;
SamplePlayer sp6;
SamplePlayer sp7;
SamplePlayer sp8;
SamplePlayer sp9;
SamplePlayer sp10;
SamplePlayer sp11;
SamplePlayer sp12;
SamplePlayer sp13;
SamplePlayer sp14;
SamplePlayer sp15;
SamplePlayer sp16;
//Gain
Gain g;
Glide gainValue;
//Reverb
Reverb r; // our Reverberation unit generator
Sound (float _Xpos, float _Ypos) {
xPos = _Xpos;
yPos = _Ypos;
}
void audioSetup() {
// sourceFile = dataPath("0.mp3");
// sourceFile = dataPath("1.mp3");
sourceFile1 = dataPath("clap-1.mp3");
sourceFile2 = dataPath("snare-1.mp3");
sourceFile3 = dataPath("clap-3.mp3");
sourceFile4 = dataPath("clap-4.mp3");
sourceFile5 = dataPath("crash-1.mp3");
sourceFile6 = dataPath("crash-2.mp3");
sourceFile7 = dataPath("crash-3.mp3");
sourceFile8 = dataPath("crash-4.mp3");
sourceFile9 = dataPath("mid-1.mp3");
sourceFile10 = dataPath("mid-2.mp3");
sourceFile11 = dataPath("mid-3.mp3");
sourceFile12 = dataPath("mid-4.mp3");
sourceFile13 = dataPath("clap-2.mp3");
sourceFile14 = dataPath("snare-2.mp3");
sourceFile15 = dataPath("snare-3.mp3");
sourceFile16 = dataPath("snare-4.mp3");
try {
sp1 = new SamplePlayer(ac, new Sample(sourceFile1));
sp2 = new SamplePlayer(ac, new Sample(sourceFile2));
// sp3 = new SamplePlayer(ac, new Sample(sourceFile3));
// sp4 = new SamplePlayer(ac, new Sample(sourceFile4));
// sp5 = new SamplePlayer(ac, new Sample(sourceFile5));
// sp6 = new SamplePlayer(ac, new Sample(sourceFile6));
// sp7 = new SamplePlayer(ac, new Sample(sourceFile7));
// sp8 = new SamplePlayer(ac, new Sample(sourceFile8));
// sp9 = new SamplePlayer(ac, new Sample(sourceFile9));
// sp10 = new SamplePlayer(ac, new Sample(sourceFile10));
// sp12 = new SamplePlayer(ac, new Sample(sourceFile11));
// sp12 = new SamplePlayer(ac, new Sample(sourceFile12));
// sp13 = new SamplePlayer(ac, new Sample(sourceFile13));
// sp14 = new SamplePlayer(ac, new Sample(sourceFile14));
// sp15 = new SamplePlayer(ac, new Sample(sourceFile15));
// sp16 = new SamplePlayer(ac, new Sample(sourceFile16));
}
catch(Exception e)
{
println("Exception while at_ting to load sample!");
e.printStackTrace();
exit();
}
sp1.setKillOnEnd(false);
sp2.setKillOnEnd(false);
// sp3.setKillOnEnd(false);
// sp4.setKillOnEnd(false);
// sp5.setKillOnEnd(false);
// sp6.setKillOnEnd(false);
// sp7.setKillOnEnd(false);
// sp8.setKillOnEnd(false);
// sp9.setKillOnEnd(false);
// sp10.setKillOnEnd(false);
// sp11.setKillOnEnd(false);
// sp12.setKillOnEnd(false);
// sp13.setKillOnEnd(false);
// sp14.setKillOnEnd(false);
// sp15.setKillOnEnd(false);
// sp16.setKillOnEnd(false);
Gain g = new Gain(ac, 2, 0.2);
g.addInput(sp1);
g.addInput(sp2);
// g.addInput(sp3);
// g.addInput(sp4);
// g.addInput(sp5);
// g.addInput(sp6);
// g.addInput(sp7);
// g.addInput(sp8);
// g.addInput(sp9);
// g.addInput(sp10);
// g.addInput(sp11);
// g.addInput(sp12);
// g.addInput(sp13);
// g.addInput(sp14);
// g.addInput(sp15);
// g.addInput(sp16);
ac.out.addInput(g);
}
void audioPlay() {
for (int i=0; i<soundsNum; i++) {
if (i == 1) {
sp1.start(); // ply the audio file
sp1.setToLoopStart();
}
if (i == 2) {
sp2.start(); // ply the audio file
sp2.setToLoopStart();
}
}
}
void textDisplay() {
}
}
Answers
Ok, so here is the thing is its huge long glory without any arrays... (class of sounds to follow in next post).
and on through zones 16-28... } void pressTouchZone16( Touch touch) { }
Then the sounds class...
https://forum.Processing.org/two/discussion/8082/from-several-variables-to-arrays
i'd have a square class, holding the position, colour of the square, the smt zone and the sound associated with it. keep all the things associated with one thing on one object.
Your code looks, sorry, terrible. If you find yourself writing the same thing over and over again then you're usually doing something wrong.
Hey Koogs, I agree it is terrible, it just came down to not knowing how to do it efficiently and having to have in done in a certain time. I understand the concepts of arrays and classes, but I'm sure you understand with certain libraries they can get quite confusing without extensive experience or a history in CS. Any further help is appreciated and as I'm able to condense it down I will post to try and help others in the future with the same issues.
Thanks for the link to read GoToLoop. Will check it out, although the problem I had before wasn't with constructing arrays and classes, but more specifically the tactics of it for this application. To ensure each SMT touch source produced a separate sound.
Here it is mega cut down..
Still need help with forLoop for the draw/touchZone.
Here's what I needed for audio—loads everything in the "samples" folder. Assigns it separate gain and glide. I'll post the rest of the code for SMT soon.