GMU:Processing im Park/Katja Bliß/source7: Difference between revisions

From Medien Wiki
(Created page with "import ddf.minim.*; Minim minim; int n = 9; AudioPlayer[] players = new AudioPlayer[n]; int[][] locations = { {95, 100, 20}, {115, 112, 20}, {126, 92, 20}, {193, 103, 2...")
 
No edit summary
 
Line 1: Line 1:
<source lang="java">
import ddf.minim.*;
import ddf.minim.*;


Line 100: Line 102:
       }
       }
     }
     }
</source>

Latest revision as of 17:17, 27 January 2016

import ddf.minim.*;

Minim minim;

int n = 9;
AudioPlayer[] players = new AudioPlayer[n];
int[][] locations = {
  {95, 100, 20},
  {115, 112, 20},
  {126, 92, 20},
  {193, 103, 20},
  {230, 109, 20},
  {271, 118, 20},
  {330, 470, 20},
  {297, 272, 20},
  {182, 165, 20},
  {238, 142, 20},
  };

int activeLocation = -1;
PImage img;


void setup() {
  size(382,360);
  // init minim
  minim = new Minim(this);
  img = loadImage("Parkhoehle.png");
  for (int i = 0; i < n; i++) {
    players[i] = minim.loadFile("Parkhoehle_" + (i+1) + ".wav");
    }    
  }
 
void draw() {

      image(img, 0, 0);
      fill(255, 150);
      noStroke();
    
      // set dummy value for the active location 
      activeLocation = -1; 
      
      for(int i = 0; i < locations.length; i ++) {
        // get location triple
        int[] loc = locations[i];
    
        // assign variables
        int x = loc[0];
        int y = loc[1];
        int r = loc[2];
    
        // check if we are at the i'th location
        if(atLocation(x, y, r)) {    
          // set active loaction to the current location
          activeLocation = i;          
          // hilight the location    
          ellipse(x, y, 2*r, 2*r);  
          }
      }
    }
    // check if the mouse is within radius r of location(x, y)
    boolean atLocation(int x, int y, int r) {
      return mouseX > (x - r) && mouseX < (x + r) && mouseY > (y - r) && mouseY < (y + r);
    }
   
    void mousePressed() {

      // is the mouse hovering over one of the locations?
      if(activeLocation != -1) {

        // pause all players
        for (int i = 0; i < n; i++) {
          players[i].pause();
        }

        // play the sound associated with the current location
        println("Sound number " + (activeLocation + 1));
        players[activeLocation].rewind();
        players[activeLocation].play();
      }
    }

    
    
    void keyPressed() {
      for (int i = 0; i < n; i++) { 
        players[i].pause();
      }

      // get position of digit pressed ( '1' = 0, '2' = 1 ... '9' = 8 )
      int i = (key - '1');
   
      // check if we are in the valid range
      if (i >= 0 && i < n) {

        // play the respective sample
        println("Sound number " + (i + 1));
        players[i].rewind();
        players[i].play();
      }
    }