How can I make the text to sound?

Hi guys, I asked this once, but still can't make it works..

I want to make the sentence I make to convert little melody when enterkey pressed.

those are codes

import ddf.minim.*;

Minim minim; 
AudioPlayer c, d, e, f, g, a, b;

PFont font; 
int num = 500; 
char t[] = new char[num]; 
int key_num = 0; 
int text_w_gap = 30; 
int text_h_gap = 50; 
int ypos = 50; 
int xpos = 10; 
int val;

boolean playSong=false;

void setup() { 
  size(500, 400); 
  font = createFont("whiteday10-48.vlw", 14); 
  textFont(font, 32); 
  fill(7,161,8);
  minim = new Minim(this);

  c = minim.loadFile("c.mp3"); 
  d = minim.loadFile("d.mp3"); 
  e = minim.loadFile("e.mp3"); 
  f = minim.loadFile("f.mp3"); 
  g = minim.loadFile("g.mp3"); 
  a = minim.loadFile("a.mp3");
}

void draw() { 
  background(0);

  if (playSong) { 
    for (int i=0; i<key_num; i++) { 
      playNote(t[i]);
    }
  } else
  { 
    for (int i=0; i<key_num; i++) { 
      text(t[i], 
        (xpos+(i*text_w_gap))%width, 
        ypos+( (int(xpos+(i*text_w_gap))) / width * text_h_gap));
    } 
    if (key_num >= num) { 
      key_num = 0;
    }
  }
}

void keyPressed() { 
  if (key!=8) { 
    t[key_num] = key; 
    key_num++;
  } else if (key==RETURN||key==ENTER) { 
    playSong=true;
  } else if (key==8) { 
    key_num--;
  } 
  println("pressed " + int(key) + " " + keyCode);
}

void playNote(int KeyCode) { 
  if (KeyCode==65) { 
    c.play();
  }
  if (KeyCode==66) { 
    d.play();
  }
  if (KeyCode==67) { 
    e.play();
  }
  if (KeyCode==68) { 
    f.play();
  }
  if (KeyCode==69) { 
    g.play();
  }
  if (KeyCode==70) { 
    a.play();
  }
  if (KeyCode==71) {
    b.play();
  }
}

keycodes are just examples..

I hope someone shows me the functional code that I can understand. thanks for reading..

Answers

  • ???

    Didn't you like my way??

    You are making it worse

  • No chrisir, I really appreciate your help really I do

    I just can't make them work I'm crying right now...

    I just started processing, I can't get it work..

    Thing I did was try to make it sound but I have no clue right now..

  • In println section I can see the keycord for the sound but

    how can I apply those keycord to minim?

  • I posted my version in the other thread

    Did it work??

    Where it says =='a'

    Did you type abababb and hit return in my code?

  • after I typed abaaaaba, I hit enterkey

  • yes... returnkey

  • After I run this code, I typed aaccdd, when I hit return It doesn't sound

    import ddf.minim.*;
    
    Minim minim; 
    AudioPlayer c, d, e, f, g, a, b, cc;
    
    PFont font; 
    int num = 500; 
    char t[] = new char[num]; 
    int key_num = 0; 
    int text_w_gap = 20; 
    int text_h_gap = 30; 
    int ypos = 50; 
    int xpos = 10; 
    int val;
    
    boolean playSong=false;
    
    void setup() { 
      size(500, 400); 
      font = createFont("whiteday10-48.vlw", 14); 
      textFont(font, 32); 
      minim = new Minim(this);
    
      c = minim.loadFile("c.mp3"); 
      d = minim.loadFile("d.mp3"); 
      e = minim.loadFile("e.mp3"); 
      f = minim.loadFile("f.mp3"); 
      g = minim.loadFile("g.mp3"); 
      a = minim.loadFile("a.mp3");
    } 
    
    void draw() { 
      background(0);
    
      // ------------------ 
    
      if (playSong) { 
        for (int i=0; i<key_num; i++) { 
          playNote(t[i]);
        }//for
      }
      //if 
      else // ------------------ 
      { 
        for (int i=0; i<key_num; i++) { 
          text(t[i], 
            (xpos+(i*text_w_gap))%width, 
            ypos+( (int(xpos+(i*text_w_gap))) / width * text_h_gap));
        } 
        if (key_num >= num) { 
          key_num = 0;
        }
      }
    }
    
    void keyPressed() { 
      if (key!=8) { 
        t[key_num] = key; 
        key_num++;
      } else if (key==RETURN||key==ENTER) { 
        playSong=true;
      } else if (key==8) { 
        key_num--;
      } 
      println("pressed " + int(key) + " " + keyCode);
    }
    
    void playNote(char val) { 
      if (val=='a') { 
        c.play();
        if (c.position() > 600 )
        {
          c.rewind();
        }
      } 
      if (val=='b') { 
        d.play();
        if (d.position() > 600 )
        {
          d.rewind();
        }
      }
      if (val=='c') { 
        e.play();
        if (e.position() > 600 )
        {
          e.rewind();
        }
      }
      if (val=='d') { 
        f.play();
        if (f.position() > 600 )
        {
          f.rewind();
        }
      }
      if (val=='e') { 
        g.play();
        if (g.position() > 600 )
        {
          g.rewind();
        }
      }
    
      if (val=='f') { 
        a.play();
        if (a.position() > 600 )
        {
          a.rewind();
        }
      } 
      if (val=='g') {
      }
    }
    //
    

    I've got the mp3 files matched name on data folder in same folder.

  • Now we are talking

    when you write

    c.play(); in draw does it work?

    Maybe you need to turn the play for loop to just play and i++; after a few frames

  • edited May 2016

    I want the sound once, when I hit return,

    like if I typed aabbcc and return, It sound aabbcc once sequentially.

    import ddf.minim.*;
    
    Minim minim; 
    AudioPlayer c, d, e, f, g, a, b, cc;
    
    PFont font; 
    int num = 500; 
    char t[] = new char[num]; 
    int key_num = 0; 
    int text_w_gap = 20; 
    int text_h_gap = 30; 
    int ypos = 50; 
    int xpos = 10; 
    int val;
    
    boolean playSong=false;
    
    void setup() { 
      size(500, 400); 
      font = createFont("whiteday10-48.vlw", 14); 
      textFont(font, 32); 
      minim = new Minim(this);
    
      c = minim.loadFile("c.mp3"); 
      d = minim.loadFile("d.mp3"); 
      e = minim.loadFile("e.mp3"); 
      f = minim.loadFile("f.mp3"); 
      g = minim.loadFile("g.mp3"); 
      a = minim.loadFile("a.mp3");
    } 
    
    void draw() { 
      background(0);
    
      // ------------------ 
    
      if (playSong) { 
        for (int i=0; i<key_num; i++) { 
          playNote(t[i]);
        }//for
      }
      //if 
      else // ------------------ 
      { 
        for (int i=0; i<key_num; i++) { 
          text(t[i], 
            (xpos+(i*text_w_gap))%width, 
            ypos+( (int(xpos+(i*text_w_gap))) / width * text_h_gap));
        } 
        if (key_num >= num) { 
          key_num = 0;
        }
      }
    }
    
    void keyPressed() { 
      if (key!=8) { 
        t[key_num] = key; 
        key_num++;
      } else if (key==RETURN||key==ENTER) { 
        playSong=true;
      } else if (key==8) { 
        key_num--;
      } 
      println("pressed " + int(key) + " " + keyCode);
    }
    
    void playNote(char val) { 
      if (val=='a') { 
        c.play();
        } 
      if (val=='b') { 
        d.play();
        }
      if (val=='c') { 
        e.play();
        }
      if (val=='d') { 
        f.play();
        }
      if (val=='e') { 
        g.play();
        }
      if (val=='f') { 
        a.play();
        }
      if (val=='g') {
        b.play();
      }
    }
    //
    
  • KakaoTalk_Photo_2016-05-29-02-25-59_63

    like this way

  • Does it work?

    You haven't answered.

  • I can't run your code, I don't have your mp3

  • nooooo It doesn't work...

  • Now we are talking

    when you write

    c.play(); in draw does it work?

    Maybe you need to turn the play for loop to just play and i++; after a few frames

  • I can't understand the last sentence.

    you mean c.play(); to c.loop(); ?

    It still doesn't work.. I dont get it either I'm suck at english or suck at processing

    maybe both..

  • You need to isolate the problem. You need find out exactly where the problem lies. So if you try to say c.play in setup() just to see if the audio file plays like it should. I think that's what Chrisir is trying to say.

  • thanks....

    ;-)

  • import ddf.minim.*;
    
    Minim minim; 
    AudioPlayer c, d, e, f, g, a, b, cc;
    
    PFont font; 
    int num = 500; 
    char t[] = new char[num]; 
    int key_num = 0; 
    int text_w_gap = 20; 
    int text_h_gap = 30; 
    int ypos = 50; 
    int xpos = 10; 
    int val;
    
    // for play 
    int startTime;
    int iForPlaySong=0;
    
    boolean playSong=false;
    
    void setup() { 
      size(500, 400); 
      font = createFont("whiteday10-48.vlw", 14); 
      textFont(font, 32); 
      minim = new Minim(this);
    
      c = minim.loadFile("c.mp3"); 
      d = minim.loadFile("d.mp3"); 
      e = minim.loadFile("e.mp3"); 
      f = minim.loadFile("f.mp3"); 
      g = minim.loadFile("g.mp3"); 
      a = minim.loadFile("a.mp3");
    } 
    
    void draw() { 
      background(0);
    
      // ------------------ 
    
      if (playSong) { 
        //for (int i=0; i<key_num; i++) {  
        if ( millis()>startTime+600) {
          if (iForPlaySong < key_num) {
            println("play " + t[iForPlaySong]);
            playNote(t[iForPlaySong]);
            iForPlaySong++;
            startTime=millis();
          }
        }
        //  }//for
      }
      //if 
      else // ------------------ 
      { 
        for (int i=0; i<key_num; i++) { 
          text(t[i], 
            (xpos+(i*text_w_gap))%width, 
            ypos+( (int(xpos+(i*text_w_gap))) / width * text_h_gap));
        } 
        if (key_num >= num) { 
          key_num = 0;
        }
      }
    }
    
    void keyPressed() { 
      if  (key==8) {
        key_num--;
      } else if (key==RETURN||key==ENTER) {
        println("Here 11111111111111111111111");
        playSong=true;      
        startTime=0;
        //playNote(t[iForPlaySong]);
        //iForPlaySong++;
      } else { 
        t[key_num] = key; 
        key_num++;
      } 
      println("pressed " + int(key) + " " + keyCode);
    }
    
    void playNote(char val) { 
      if (val=='a') { 
        c.play();
      } 
      if (val=='b') { 
        d.play();
      }
      if (val=='c') { 
        e.play();
      }
      if (val=='d') { 
        f.play();
      }
      if (val=='e') { 
        g.play();
      }
      if (val=='f') { 
        a.play();
      }
      if (val=='g') {
        b.play();
      }
    }
    //
    
  • edited May 2016

    THANK YOU SO MUCH! Chrisir :)

    I can't even explain how thankful I am right now! I just check this question!

    It does work perfectly.

    I change a bit. thank you so much!!

    I try to add some bgm for the melody. I truly thank for you help! thank you so much!

    this is proceeding code right now! ( I think I use thx so much too much haha)

     import ddf.minim.*;
    
    Minim minim; 
    AudioPlayer s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14;
    
    PFont font; 
    int num = 500; 
    char t[] = new char[num]; 
    int key_num = 0; 
    int text_w_gap = 20; 
    int text_h_gap = 30; 
    int ypos = 50; 
    int xpos = 10; 
    int val;
    int notetime = 350;
    
    // for play 
    int startTime;
    int iForPlaySong=0;
    
    boolean playSong=false;
    
    void setup() { 
      size(500, 400); 
      font = createFont("whiteday10-48.vlw", 14); 
      textFont(font, 32); 
      minim = new Minim(this);
    
      s1 = minim.loadFile("1.mp3"); 
      s2 = minim.loadFile("2.mp3"); 
      s3 = minim.loadFile("3.mp3"); 
      s4 = minim.loadFile("4.mp3"); 
      s5 = minim.loadFile("5.mp3"); 
      s6 = minim.loadFile("6.mp3");
      s7 = minim.loadFile("7.mp3");
      s8 = minim.loadFile("8.mp3");
      s9 = minim.loadFile("9.mp3");
      s10 = minim.loadFile("10.mp3");
      s11 = minim.loadFile("11.mp3");
      s12 = minim.loadFile("12.mp3");
      s13 = minim.loadFile("13.mp3");
      s14 = minim.loadFile("14.mp3");
    } 
    
    void draw() { 
      background(0);
    
      // ------------------ 
    
      if (playSong) { 
        //for (int i=0; i<key_num; i++) {  
        if ( millis()>startTime+notetime) {
          if (iForPlaySong < key_num) {
            println("play " + t[iForPlaySong]);
            playNote(t[iForPlaySong]);
            iForPlaySong++;
            startTime=millis();
          }
        }
        //  }//for
      }
      //if 
      else // ------------------ 
      { 
        for (int i=0; i<key_num; i++) { 
          text(t[i], 
            (xpos+(i*text_w_gap))%width, 
            ypos+( (int(xpos+(i*text_w_gap))) / width * text_h_gap));
        } 
        if (key_num >= num) { 
          key_num = 0;
        }
      }
    }
    
    void keyPressed() { 
      if  (key==8) {
        key_num--;
      } else if (key==RETURN||key==ENTER) {
        println("Here 11111111111111111111111");
        playSong=true;      
        startTime=0;
        //playNote(t[iForPlaySong]);
        //iForPlaySong++;
      } else { 
        t[key_num] = key; 
        key_num++;
      } 
      println("pressed " + int(key) + " " + keyCode);
    }
    
    void playNote(char val) { 
      if (val=='q') { 
        s1.play();
        if (s1.position() > notetime )
        {
          s1.rewind();
        }
      } 
      if (val=='w') { 
        s2.play();
        if (s2.position() > notetime )
        {
          s2.rewind();
        }
      }
      if (val=='e') { 
        s3.play();
        if (s3.position() > notetime )
        {
          s3.rewind();
        }
      }
      if (val=='r') { 
        s4.play();
        if (s4.position() > notetime )
        {
          s4.rewind();
        }
      }
      if (val=='t') { 
        s5.play();
        if (s5.position() > notetime )
        {
          s5.rewind();
        }
      }
      if (val=='a') { 
        s6.play();
        if (s6.position() > notetime )
        {
          s6.rewind();
        }
      }
      if (val=='s') {
        s7.play();
        if (s7.position() > notetime )
        {
          s7.rewind();
        }
      }
      if (val=='d') {
        s8.play();
        if (s8.position() > notetime )
        {
          s8.rewind();
        }
      }
      if (val=='f') {
        s9.play();
        if (s9.position() > notetime )
        {
          s9.rewind();
        }
      }
      if (val=='g') {
        s10.play();
        if (s10.position() > notetime )
        {
          s10.rewind();
        }
      }
      if (val=='z') {
        s11.play();
        if (s11.position() > notetime )
        {
          s11.rewind();
        }
      }
      if (val=='x') {
        s12.play();
        if (s12.position() > notetime )
        {
          s12.rewind();
        }
      }
      if (val=='c') {
        s13.play();
        if (s13.position() > notetime )
        {
          s13.rewind();
        }
      }
      if (val=='v') {
        s14.play();
        if (s14.position() > notetime )
        {
          s14.rewind();
        }
      }
    }
    //
    
  • the main issue was this btw

    if (key!=8) {  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        t[key_num] = key; 
        key_num++;
      } else if (key==RETURN||key==ENTER) { 
        playSong=true;
      } 
    

    since we start with if (key!=8) { it failed, because return is also != 8, it never came to playsong = true............

    Bad.

    I found out by placing println("1"); what never got executed, so I got suspicious....

        if (key!=8) { 
            t[key_num] = key; 
            key_num++;
          } else if (key==RETURN||key==ENTER) { 
            println("1"); // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
            playSong=true;
          } 
    

    Then he played all notes at once, so I had to tear down the for-loop in draw() and place a timer there.... puuh!

    Best, Chrisir ;-)

  • wow.. I got to study that part even though I can't understand right now

    I'm gonna btw !!

    thank you!! :) :)

  • Answer ✓

    ;-)

Sign In or Register to comment.