Quantcast
Channel: Kinect - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 530

Creating an array of audio in beads.

$
0
0

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() {
  }
}

Screen Shot 2016-07-14 at 11.20.12 AM


Viewing all articles
Browse latest Browse all 530

Trending Articles