String samplename[] = 

{
    "ds_kick_big_amb.mp3",
  "ds_kick_big_low.mp3",
  "ds_kick_dry_click.mp3",
  "ds_kick_dry_wood_beater.mp3",
  "ds_snare_tight.mp3",
  "ds_snare_tight_comp.mp3",
  "ds_brush_snare_dry.mp3",
  "ds_china.mp3",
  "ds_cross_stick_rim.mp3",
  "ds_doumbek_slap.mp3",
  "ds_kick_thunder.mp3",
  "ds_loose_skin.mp3",
  "ds_loose_skin_mute.mp3",
  "ds_brush_snare.mp3"

};


import ddf.minim.*;

Minim minim;

AudioSample sample[]; 



float squareSize = 20; 

int currentNote = 0; 
int currentX, currentY;

int grid[][] = new int[16][16];
HashSet bugs = new HashSet();

boolean virgin = true;

void setup()
{
  size(320,320);
  minim = new Minim(this);
  sample = new AudioSample[samplename.length];
  for(int i = 0; i < samplename.length;i++){
    sample[i] = minim.loadSample(samplename[i], 2048);
    if(sample[i] == null) { 
      println("couldn't load "+samplename[i]);
    }
  }
  frameRate(30);
  textFont(loadFont("Aharoni-Bold-20.vlw"));
  for(int i = 0; i < sample.length; i++){
    bugs.add(new Bug(sample[i])); 
  }

}

void mousePressed(){
  if(currentX >= 0 && currentX < 16){
    if(currentY >= 0 && currentY < 16){
      if(grid[currentX][currentY] == 0){
        grid[currentX][currentY] = currentNote + 1;
      } 
      else {
        grid[currentX][currentY] = 0;

      }
    }
  }
}  
void mouseMoved(){
  currentX = floor(mouseX / squareSize);
  currentY = floor(mouseY / squareSize);
}

void draw()
{

  background(204);
 
 if(virgin){
   fill(255);
    text("DRUMDOTS 0.1",100,100);
  text("this is a noisy toy",100,140);
  text("so press space to start",100,180);
  return; 
 }
 
 
 
  strokeWeight(1);
  stroke(128);
  for(int x = 0; x < 320; x += squareSize * 4){
    line(x,0,x,height);
  }
  for(int y = 0; y < 320; y += squareSize * 4){

    line(0,y,width,y);
  }

  fill(0);
  for(int x = 0; x < 16; x++){
    for(int y = 0; y < 16; y++){
      if(grid[x][y]!= 0){
        char off = (char)grid[x][y];
        off--;
        char c = 'A';//+off;
        c += off;
        text(c,3+x * squareSize,  -3+(y+1) *squareSize); 
      }
    }


  }  

  strokeWeight(2);
  stroke(255,0,0);
  noFill();
  rect(currentX*squareSize,currentY*squareSize,squareSize,squareSize);

  Iterator i = bugs.iterator();
  while(i.hasNext()){
    Bug b = (Bug)i.next(); 
    b.move();
    b.draw();

  }

}

void keyPressed()
{
  if(key == ' '){
     virgin = false; 
  }

  int keynum = key - 'a';
  if(keynum < 0) keynum += 32;
  if(keynum >= 0 && keynum < sample.length){
    sample[keynum].trigger();    
    currentNote = keynum;
  }


}

void stop()
{
  // always close Minim audio classes when you are done with them
  for(int i = 0; i < sample.length;i++){
    sample[i].close();
  }  

  minim.stop();

  super.stop();
}

int counterMax = 10;
class Bug{
  float x,y;
  float  xspeed, yspeed;
AudioSample audio;
  int counter;
  Bug(AudioSample audio){
    this.audio = audio;
 x = random(width);
 y = random(height);
 xspeed = random(-5,5);
  yspeed = random(-5,5);
  }
  
  int drawred = 0;
void play(){
   audio.trigger(); 
   drawred = 10;
}


boolean gotit = false;

  void move(){
      x += xspeed ;
      y += yspeed;

    if(x <= 0){
        play();
        xspeed = abs(xspeed);
    }
    if(y <= 0){
        play();
        yspeed = abs(yspeed);
    }
    if(x >= width){
       play();
       xspeed = -abs(xspeed);
    }
    if(y >= height){
       play();
       yspeed = -abs(yspeed);
    }

    if(  abs(mouseX - x) < 10 && abs(mouseY - y) < 10 ){
      if(! gotit){ 
      play();
       xspeed = (mouseX - pmouseX)/  10;
       yspeed = (mouseY - pmouseY) / 10;
        gotit = true;
      }
    } else {
        gotit = false;
    }


  }


  void draw(){
    strokeWeight(2);
    stroke(0);
    fill(255);
if(drawred > 0){
   fill(255,0,0);
  drawred--; 
}
    //print(x);
    ellipse(x,y,squareSize,squareSize);
    counter++;
  } 
}