How to 'store' previous keyPressed data in the same sketch

2»

Answers

  • @Chrisir

    Try in line 14 boolean[] keys = new boolean[7]; instead of 7 better 8

    I tried changing it to 8, but it's out of bounds because there are only 7 colors specified

    I think when you say song....play(); better say

    song...rewind();song....play();

    I added the rewind to keyReleased(), but it's giving me this weird delay.. It plays the file and replays it again after it's released

  • edited April 2018

    boolean[] keys = new boolean[7]; instead of 7 better 8

    7 is the upper bound. That’s why g doesn’t work. When you set it to 8, you need to adjust colors and sounds. But then you got g.

    rewind before play

    Post your entire code

  • edited April 2018

    @Chrisir

    Changing it to 8 worked!

    Putting rewind before play still gives me a weird delay, though.

            import ddf.minim.*;
            Minim minim;
            AudioPlayer player;
    
            PFont f;
    
            String combination="";
            String prevcombination="";
    
            // define the ArrayList
            ArrayList<Block> blocks = new ArrayList();
            int cx, cy;
    
            boolean[] keys = new boolean[8];
    
    
            boolean flagShowMessages=true; 
    
            //define colors
            color[] colors = { 
              color(143, 205, 156), 
              color(150, 198, 234), 
              color(239, 57, 66), 
              color(253, 240, 77), 
              color(198, 233, 246), 
              color(239, 90, 122), 
              color(248, 151, 29),
              color(248, 151, 29)
            };
    
            AudioPlayer[] songs; 
    
            // setup and draw --------------------------------------------
    
            void setup() {
    
              size(960, 240);
              colorMode(RGB, 255);
    
              minim = new Minim(this);
    
             songs = new AudioPlayer[7];
              songs[0]=minim.loadFile("A.mp3"); 
              songs[1]=minim.loadFile("B.mp3");
              songs[2]=minim.loadFile("C.mp3"); 
              songs[3]=minim.loadFile("D.mp3"); 
              songs[4]=minim.loadFile("E.mp3"); 
              songs[5]=minim.loadFile("F.mp3"); 
              songs[6]=minim.loadFile("G.mp3"); 
    
              for (boolean currentKey : keys) {
                currentKey=false;
              }
    
              println(keys);
            }
    
            // start draw
            void draw() {
              // delete scene   
              background(#ffffff);
    
              // show blocks
              for (int i = 0; i < blocks.size(); i++) {
                blocks.get(i).draw();
              }
    
              //for
    
              // show messages 
              showMessages();
    
              // use keys
              evaluateKeysArray();
              //
            }//func
    
            // -----------------------------------------------------
    
            void showMessages() {
    
              if (!flagShowMessages) {
                return; // leave here
              }
              textSize(13);
              fill(0);
              text ("\nTo have an effect two keys are required.\n"
                +"You can release both keys or one and add another key.\nUse x to toggle this text.", 
                width-241, 19, 
                100, 900);
    
              textSize(24);
              for ( int i = 0; i < keys.length; i++) {
                fill(colors[i]);
                text (char(i+97)+" "+keys[i], 
                  width-101, 19+25*i, 
                  100, 900);
              }//for
            }
    
            // use keys
            void evaluateKeysArray() {
              int count=0; 
              for ( int i = 0; i < keys.length-1; i++) {
                if (keys[i]) {
                  count++;
                }
              }
    
              if (count==0) {
                // do nothing
              } else if (count==1) {
                // assign keys to colors
                for ( int i = 0; i < keys.length-1; i++) {
                  // Is 1 key pressed? 
    
                  if (keys[i]) {
                    combination = str(i);
                    if (!combination.equals(prevcombination)) {
                      // start new block 
    
                      blocks.add( new Block( cx, cy, 
                        colors[i], // The first color
                        colors[i] ) ); // The second color
                    } else {
    
                      // widen 
                      if ( blocks.size() > 0 ) {
                        blocks.get(blocks.size() - 1).widen();
                        cx++;
                      }
                    }
                    prevcombination=combination;
                    return;
                  }
                }
              } else if (count==2) {
    
                // assign keys to colors
                for ( int i = 0; i < keys.length-1; i++) {
                  for ( int i2 = i+1; i2 < keys.length; i2++) {
                    // Are 2 keys pressed simultaneously? 
    
                    if (keys[i] && keys[i2]) {
                      combination = str(i) + str(i2);
    
                      if (!combination.equals(prevcombination)) {
                        // start new block 
    
                        blocks.add( new Block( cx, cy, 
                          colors[i], // The first color
                          colors[i2] ) ); // The second color
                      } else {
                        // widen 
                        if ( blocks.size() > 0 ) {
                          blocks.get(blocks.size() - 1).widen();
                          cx++;
                        }
                      }
                      prevcombination=combination;
                    }
                  }
                }
              }//else if
            }//func
    
            // -----------------------------------------------------
    
            // define keys
            void keyPressed() {
    
              if (key=='x') { 
                flagShowMessages= !flagShowMessages;
                return;
              }
    
              int indexOfKey = int(key) - 97; 
              println (key
                + " : "
                + int(key) 
                + " : "
                + indexOfKey);
    
              if (indexOfKey>=0 && indexOfKey<keys.length) // limit keys to a-g
                keys[indexOfKey] = true;
                songs[indexOfKey].rewind();
                songs[indexOfKey].play();
    
    
            }
    
            void keyReleased() {
    
              println("released: " + key);
              int indexOfKey = int(key) - 97;
    
              if (indexOfKey>=0 && indexOfKey<keys.length) { // limit keys to a-g
                keys[indexOfKey] = false;
    
    
    
              }
            }
    
            // =============================================
    
            // define the block
            class Block {
    
              float x, y, 
                w;
              color c, k;
    
              Block(float ix, float iy, 
                color ic, color ics) {
                x = ix;
                y = iy;
                w = 0;
                // c = ic; //////////?????
                c = ic; //////////?????
                k = ics;
              }
    
              void draw() {
                fill(c);
                noStroke();
                rect(x, y, w, 240);
                colorMode(RGB, 255);
                for ( int i = 0; i < w; i++) {
                  stroke(lerpColor(c, k, i/w));
                  line(x+i, y, 
                    x+i, y+239); //240 made it bigger than the previous one
                }
              }
              void widen() {
                w++;
              }
            }
            //
    
  • Looks pretty good

    Maybe try to move (not copy) the rewind line 186 with the if (copy the if)

  • @Chrisir

    Sorry, I don't quite understand what you mean?

  • if (indexOfKey>=0 && indexOfKey<keys.length) {

    songs[indexOfKey].rewind();

    }

    In keyReleased instead of keyPressed

  • @Chrisir

    Now it plays it once when pressed and again when released.

  • edited April 2018

    If you are trying to repeatedly play short clips with high performance using Minim, you might try using Sampler.

    http://code.compartmental.net/minim/sampler_class_sampler.html

    For an example, see:

    https://forum.processing.org/two/discussion/comment/94846/#Comment_94846

  • one error was here:

     if (indexOfKey>=0 && indexOfKey<keys.length) // limit keys to a-g
        keys[indexOfKey] = true;
        songs[indexOfKey].rewind();
        songs[indexOfKey].play();
    

    if you had use ctrl-t for auto-format, your error would have been visible immediatly

     if (indexOfKey>=0 && indexOfKey<keys.length) // limit keys to a-g
        keys[indexOfKey] = true;
     songs[indexOfKey].rewind();
     songs[indexOfKey].play();
    

    Only the first line is dependent from if, not the others !

    We need { } !

       if (indexOfKey>=0 && indexOfKey<keys.length) {
            // limit keys to a-g
            keys[indexOfKey] = true;
            songs[indexOfKey].rewind();
            songs[indexOfKey].play();
        }
    
  • in this new version: improved sound handling (without the idea from jeremy)

    and you can press space bar now to reset

    import ddf.minim.*;
    Minim minim;
    AudioPlayer player;
    
    PFont f;
    
    String combination="";
    String prevcombination="";
    
    // define the ArrayList
    ArrayList<Block> blocks = new ArrayList();
    int cx, cy;
    
    boolean[] keys = new boolean[8];
    
    boolean flagShowMessages=true; 
    
    //define colors
    color[] colors = { 
      color(143, 205, 156), 
      color(150, 198, 234), 
      color(239, 57, 66), 
      color(253, 240, 77), 
      color(198, 233, 246), 
      color(239, 90, 122), 
      color(248, 151, 29), 
      color(248, 151, 29)
    };
    
    AudioPlayer[] songs; 
    
    // setup and draw --------------------------------------------
    
    void setup() {
    
      size(960, 240);
      colorMode(RGB, 255);
    
      minim = new Minim(this);
    
      songs = new AudioPlayer[7];
      songs[0]=minim.loadFile("A.mp3"); 
      songs[1]=minim.loadFile("B.mp3");
      songs[2]=minim.loadFile("C.mp3"); 
      songs[3]=minim.loadFile("D.mp3"); 
      songs[4]=minim.loadFile("E.mp3"); 
      songs[5]=minim.loadFile("F.mp3"); 
      songs[6]=minim.loadFile("G.mp3"); 
    
      for (int i = 0; i < keys.length; i++) {
        keys[i]=false;
      }//for
    }//func
    
    void draw() {
      // delete scene   
      background(#ffffff);
    
      // show blocks
      for (int i = 0; i < blocks.size(); i++) {
        blocks.get(i).draw();
      }
    
      //for
    
      // show messages 
      showMessages();
    
      // use keys
      evaluateKeysArray();
      //
    }//func
    
    // -----------------------------------------------------
    
    void showMessages() {
    
      if (!flagShowMessages) {
        return; // leave here
      }
      textSize(13);
      fill(0);
      text ("\nUse keys a to g (press one or two keys).\n"
        +"You can release both keys or one and add another key.\n"
        +"Use x to switch this text on and off.", 
        width-241, 3, 
        100, 900);
    
      textSize(24);
      for ( int i = 0; i < keys.length; i++) {
        fill(colors[i]);
        text (char(i+97)+" "+keys[i], 
          width-101, 19+25*i, 
          100, 900);
      }//for
    }
    
    void evaluateKeysArray() {
    
      // use keys
    
      // count keys 
      int count=0; 
      for ( int i = 0; i < keys.length; i++) {
        if (keys[i]) {
          count++;
        }
      }
    
      if (count==1) {
        // assign keys to colors
        for ( int i = 0; i < keys.length-1; i++) {
    
          // Is 1 key pressed? 
          if (keys[i]) {
            combination = str(i);
            if (!combination.equals(prevcombination)) {
              // start new block 
    
              blocks.add( new Block( cx, cy, 
                colors[i], // The first color
                colors[i] ) ); // The second color
            } else {
    
              // widen 
              if ( blocks.size() > 0 ) {
                blocks.get(blocks.size() - 1).widen();
                cx++;
              }
            }
            prevcombination=combination;
            return;
          }
        }
      } else if (count==2) {
        // assign keys to colors
        for ( int i = 0; i < keys.length-1; i++) {
          for ( int i2 = i+1; i2 < keys.length; i2++) {
    
            // Are 2 keys pressed simultaneously? 
            if (keys[i] && keys[i2]) {
    
              combination = str(i) + str(i2);
    
              if (!combination.equals(prevcombination)) {
                // start new block 
    
                blocks.add( new Block( cx, cy, 
                  colors[i], // The first color
                  colors[i2] ) ); // The second color
              } else {
                // widen 
                if ( blocks.size() > 0 ) {
                  blocks.get(blocks.size() - 1).widen();
                  cx++;
                }
              }
              prevcombination=combination;
              return;
            }
          }
        }
      }//else if
    }//func
    
    // -----------------------------------------------------
    
    // define keys
    void keyPressed() {
    
      if (key=='x') { 
        flagShowMessages= !flagShowMessages;//toggle
        return;
      } else if (key==' ') { 
        blocks.clear();
        cx=0;
        return;
      }
    
      int indexOfKey = int(key) - 97; 
      //println (key
      //  + " : "
      //  + int(key) 
      //  + " : "
      //  + indexOfKey);
    
      if (indexOfKey>=0 && indexOfKey<keys.length) { 
        // limit keys to a-g
        keys[indexOfKey] = true;
        songs[indexOfKey].play();
      }
    }
    
    void keyReleased() {
    
      // println("released: " + key);
      int indexOfKey = int(key) - 97;
    
      if (indexOfKey>=0 && indexOfKey<keys.length) {
        // limit keys to a-g
        keys[indexOfKey] = false;
        songs[indexOfKey].pause();
        songs[indexOfKey].rewind();
      }
    }
    
    // =============================================
    
    // define the block
    class Block {
    
      float x, y, 
        w=0;
      color c, k;
    
      Block(float ix, float iy, 
        color ic, color ics) {
        x = ix;
        y = iy;
        c = ic;
        k = ics;
      }//constr
    
      void draw() {
        fill(c);
        noStroke();
        rect(x, y, w, 240);
        colorMode(RGB, 255);
        for (int i = 0; i < w; i++) {
          stroke(lerpColor(c, k, i/w));
          line(x+i, y, 
            x+i, y+239); //240 made it bigger than the previous one
        }
      }
    
      void widen() {
        w++;
      }
      //
    }//class
    //
    
  • Changing it to 8 worked!

    using letters a to g may mean that you don't have enough colors and songs to handle letter g. Fix that maybe.

    Regards, Chrisir ;-)

Sign In or Register to comment.