Line break and backspace in string

edited November 2014 in Questions about Code

Hi everyone,

I am obviously new in Processing and I wanted to code a typewriter-like program that would load sounds every time a key is pressed, as well as printing it. The only thing is that the symbol printed needs to be different from the key (for example, when "2" is pressed, "ah" has to be printed). I managed to make it work for all the keys except for BACKSPACE and RETURN. It works for DELETE, even if I didn't specify anything for this key, but I can't make it work for the other ones. Does anyone have any idea of how I could do it? Here is the code so that you can have a look at it. Thanks for your help!

import ddf.minim.*;
Minim minim;
AudioPlayer song;
String text1 = " ";
PFont font;
int bfill = 150;
boolean randomizer = false;

void setup() {

  size(400, 400);
  smooth();
  font = loadFont("Arial-BoldMT-20.vlw");
  textFont(font, 20);
  minim = new Minim(this);
}

void draw() {
  background(bfill);
  fill(255);
  text(text1, 0, 40, width, height);
  if (randomizer) {
    for (int j = 0; j < 400; j++) {
      text1 += char(int(random(18, 126)));
    }
    text(text1, 0, 40, width, height);
    text1 = " ";
  }
}

void keyPressed() {
  println(key);
  if (key == BACKSPACE) {
    if (text1.length() > 0) {
      text1 = text1.substring(0, text1.length() - 1);
    }
  } 
  if (key == RETURN) {
    text1 += key;
  }
  if (key == '1') {
    text1 += "æ";
    song = minim.loadFile("æ.mp4");
    song.play();
  }
  if (key == '2') {
    text1 += "ah";
    song = minim.loadFile("ah.mp4");
    song.play();
  }
  if (key == '3') {
    text1 += "aw";
    song = minim.loadFile("aw.mp4");
    song.play();
  }
  if (key == '4') {
    text1 += "ɑ";
    song = minim.loadFile("ɑ.mp4");
    song.play();
  }
  if (key == '5') {
    text1 += "ɑr";
    song = minim.loadFile("ɑr.mp4");
    song.play();
  }
  if (key == '6') {
    text1 += "ɒ";
    song = minim.loadFile("ɒ.mp4");
    song.play();
  }
  if (key == '7') {
    text1 += "ə";
    song = minim.loadFile("ə.mp4");
    song.play();
  }
  if (key == '8') {
    text1 += "ər";
    song = minim.loadFile("ər.mp4");
    song.play();
  }
  if (key == '9') {
    text1 += "ɛ";
    song = minim.loadFile("ɛ.mp4");
    song.play();
  }      
  if (key == '0') {
    text1 += "ɛr";
    song = minim.loadFile("ɛr.mp4");
    song.play();
  }
  if (key == '-') {
    text1 += "ɪ";
    song = minim.loadFile("ɪ.mp4");
    song.play();
  }
  if (key == '=') {
    text1 += "ɪr";
    song = minim.loadFile("ɪr.mp4");
    song.play();
  }
  if (key == 'q') {
    text1 += "q";
    song = minim.loadFile("ð.mp4");
    song.play();
  }
  if (key == 'w') {
    text1 += "w";
    song = minim.loadFile("w.mp4");
    song.play();
  }
  if (key == 'e') {
    text1 += "i";
    song = minim.loadFile("i.mp4");
    song.play();
  }
  if (key == 'r') {
    text1 += "r";
    song = minim.loadFile("r.mp4");
    song.play();
  }
  if (key == 't') {
    text1 += "t";
    song = minim.loadFile("t.mp4");
    song.play();
  }
  if (key == 'y') {
    text1 += "y";
    song = minim.loadFile("y.mp4");
    song.play();
  }
  if (key == 'u') {
    text1 += "u";
    song = minim.loadFile("u.mp4");
    song.play();
  }
  if (key == 'i') {
    text1 += "i";
    song = minim.loadFile("aj.mp4");
    song.play();
  }
  if (key == 'o') {
    text1 += "o";
    song = minim.loadFile("o.mp4");
    song.play();
  }
  if (key == 'p') {
    text1 += "p";
    song = minim.loadFile("p.mp4");
    song.play();
  }
  if (key == '[') {
    text1 += "ɔj";
    song = minim.loadFile("ɔj.mp4");
    song.play();
  }
  if (key == ']') {
    text1 += "ɔr";
    song = minim.loadFile("ɔr.mp4");
    song.play();
  }
  if (key == 'a') {
    text1 += "a";
    song = minim.loadFile("e.mp4");
    song.play();
  }
  if (key == 's') {
    text1 += "s";
    song = minim.loadFile("s.mp4");
    song.play();
  }
  if (key == 'd') {
    text1 += "d";
    song = minim.loadFile("d.mp4");
    song.play();
  }
  if (key == 'f') {
    text1 += "f";
    song = minim.loadFile("f.mp4");
    song.play();
  }
  if (key == 'g') {
    text1 += "g";
    song = minim.loadFile("g.mp4");
    song.play();
  }
  if (key == 'h') {
    text1 += "h";
    song = minim.loadFile("h.mp4");
    song.play();
  }
  if (key == 'j') {
    text1 += "j";
    song = minim.loadFile("j.mp4");
    song.play();
  }
  if (key == 'k') {
    text1 += "k";
    song = minim.loadFile("c.mp4");
    song.play();
  }
  if (key == 'l') {
    text1 += "l";
    song = minim.loadFile("l.mp4");
    song.play();
  }
  if (key == ';') {
    text1 += "ŋ";
    song = minim.loadFile("ŋ.mp4");
    song.play();
  }
  if (key == 'z') {
    text1 += "z";
    song = minim.loadFile("z.mp4");
    song.play();
  }
  if (key == 'x') {
    text1 += "ʃ";
    song = minim.loadFile("ʃ.mp4");
    song.play();
  }
  if (key == 'c') {
    text1 += "tʃ";
    song = minim.loadFile("tʃ.mp4");
    song.play();
  }
  if (key == 'v') {
    text1 += "v";
    song = minim.loadFile("v.mp4");
    song.play();
  }
  if (key == 'b') {
    text1 += "b";
    song = minim.loadFile("b.mp4");
    song.play();
  }
  if (key == 'n') {
    text1 += "n";
    song = minim.loadFile("n.mp4");
    song.play();
  }
  if (key == 'm') {
    text1 += "m";
    song = minim.loadFile("m.mp4");
    song.play();
  }
  if (key == ',') {
    text1 += "ʊ";
    song = minim.loadFile("ʊ.mp4");
    song.play();
  }
  if (key == '.') {
    text1 += "ʒ";
    song = minim.loadFile("ʒ.mp4");
    song.play();
  } 
}

void stop()
{
  song.close();
  minim.stop();
  super.stop();
}

Answers

  • Please, format your code post: highlight it & hit CTRL+K.
    Anyways, a similar online example: http://studio.processingtogether.com/sp/pad/export/ro.9Zo$UbIWYZEDR/latest

  • edited November 2014

    Thanks for the answer GoToLoop, and sorry for the format of my code, will do better next time, I didn't realize it was not keeping the intends as in Processing.

    The similar example you showed me is a little bit confusing to be, it is using arrays right? How can I call every independent key and attribute it a sound and specific "printed element" with this system? So far I've been doing it like this:

    void keyPressed() { 
       if (key == '1') { 
       text1 += "æ"; 
       song = minim.loadFile("æ.mp4"); 
       song.play();
       }
    }
    

    Is there a way to integrate this to the example you showed me? Not so familiar with arrays sorry.

  • ... and sorry for the format of my code, will do better next time, ...

    No need for next time. We can edit our own posts at any time! ;;)

  • Ha ha perfect, thanks^^ looks way better now

  • Is there a way to integrate this to the example you showed me? Not so familiar with arrays sorry.

    W/o learning arrays (and other data structures), programing becomes an endless repetition of lines!

  • I'll check this up! Thanks again

  • edited November 2014

    I've decided to adapt my online version to match your sketch. Ended up successful but very complicated! 8-|

    Besides an array, it uses a class named Phoneme, which represents a key to type in, 1 or 2 phoneme letters plus an AudioPlayer: ch, hash, au respectively.

    It relies on Arrays.binarySearch() in order to find out the array's index which matches a keyTyped():
    http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#binarySearch-java.lang.Object:A-java.lang.Object-

    Well, better check it out for yourself. Any further doubts related to it, just ask here: ;)

    /**
     * Key Phoneme Player (v2.05)
     * by GoToLoop & Vjalil (2014/Nov)
     *
     * forum.processing.org/two/discussion/7940/
     * line-break-and-backspace-in-string
     */
    
    import ddf.minim.Minim;
    import ddf.minim.AudioPlayer;
    
    import java.util.Arrays;
    
    static final String FONT  = "DejaVu Sans Bold";
    static final String TITLE = "Key Phoneme Player", EXT = "mp3";
    
    static final color BG = 0100, FG = #FFFF00;
    static final int LIMIT = 01000, MARGIN = 010, DIM = 20;
    
    static final int QTY = 3, FPS = 10;
    final Phoneme[] phones = new Phoneme[QTY];
    
    String txt = "";
    
    void setup() {
      size(400, 400, JAVA2D);
    
      smooth(4);
      noLoop();
      frameRate(FPS);
    
      fill(FG);
      noStroke();
      rectMode(CORNER);
    
      printArray(PFont.list());
      println();
    
      textFont(createFont(FONT, DIM, true));
      textAlign(LEFT, TOP);
    
      Minim minim = new Minim(this);
    
      phones[0] = new Phoneme('8', "ər", EXT, minim);
      phones[1] = new Phoneme('-', "ɪ", EXT, minim);
      phones[2] = new Phoneme('c', "tʃ", EXT, minim);
    
      Arrays.sort(phones);
    
      int i = 0;
      for (Phoneme ph : phones)
        println("[" + nf(i++, 2) + "]", ph.ch, nf(ph.ch, 3), ph);
    }
    
    void draw() {
      background(BG);
      text(txt, 0, MARGIN, width, height);
      frame.setTitle(TITLE + "\t\tChars: " + txt.length());
      println(txt);
    }
    
    void keyTyped() {
      char k = key;
      if (k == CODED)  return;
    
      redraw();
    
      int len = txt.length();
      if (k == BACKSPACE)  txt = txt.substring(0, max(0, len-1));
      else if (len >= LIMIT)  return;
    
      if (k == ENTER | k == RETURN)  txt += ENTER;
      else if (k == DELETE)          txt  = "";
      else if (k == ' ')             txt += ' ';
      else if (k <  ' ')             return;
    
      int idx = Arrays.binarySearch(phones, k);
      if (idx < 0)  return;
    
      txt += phones[idx].toString();
      phones[idx].au.play();
    }
    
    static final class Phoneme implements Comparable {
      final char ch;  // key
      final int hash; // 2 phoneme letters
      final AudioPlayer au; // audio file
    
      Phoneme(char c, String s, String ext, Minim mi) {
        au = mi.loadFile(s + "." + ext);
        ch = c;
    
        int len = s.length();
        char c1 = len > 0? s.charAt(0) : 0;
        char c2 = len > 1? s.charAt(1) : 0;
    
        hash = c1<<020 | c2;
      }
    
      @ Override boolean equals(Object o) {
        return o.hashCode() == hash;
      }
    
      @ Override int hashCode() {
        return hash;
      }
    
      @ Override int compareTo(Object o) {
        return o instanceof Phoneme
          ? ch - ((Phoneme) o).ch : o instanceof Character
          ? ch - (Character) o : ch - (Integer) o;
      }
    
      @ Override String toString() {
        char c1 = (char) (hash>>020);
        char c2 = (char) hash;
        return "" + c1 + (c2>0? c2 : "");
      }
    }
    
  • edited November 2014

    After some thought, some stuffs in class Phoneme won't ever be necessary.
    More specifically, the whole hashCode() & equals() features.
    Cut them out in order to make the class easier to comprehend. Version 3.04: o->

    static final class Phoneme implements Comparable {
      final char ch;        // key
      final String ph;      // phoneme letters
      final AudioPlayer au; // audio file
    
      Phoneme(char c, String s, String ext, Minim mi) {
        au = mi.loadFile((ph = s) + "." + ext);
        ch = c;
      }
    
      @ Override int compareTo(Object o) {
        return o instanceof Phoneme
          ? ch - ((Phoneme) o).ch : o instanceof Character
          ? ch - (Character) o : ch - (Integer) o;
      }
    
      @ Override String toString() {
        return ph;
      }
    }
    
  • Thank you so much for the help GoToLoop! I tried to run the program but I keep getting this error message and I don't know what it means..!Screen Shot 2014-11-04 at 5.50.19 PM

  • edited November 2014 Answer ✓

    It says that its createInput()'s failed! Probably file name String, that is s + "." + ext, wasn't found!
    Pay attention that in phones[0] = new Phoneme('8', "ər", EXT, minim);, constant EXT = "mp3".
    You gotta modify those constants to match your own situation! ~:>
    Constants you should pay special attention to: QTY, FONT & EXT.

Sign In or Register to comment.