Removing first entry when next enters

I’ve this code that lets you type words when keypressed but it only shows one word at the time. I want it to show multiply words (lets say 20) at the time and when word 21 is entered, the first word gets deleted and word 21 becomes 20, and so on and on.

Could somebody help me please? I don’t want to change the way I’ve coded the keys since I need it this way. Thanks in advance! Below is my code!

PFont font;

String myText = "Testing part"; // Starting Text, is only vissable once pressed

String aKPInput = "Medium"; // Input Key : A
String bKPInput = "Is"; // Input Key : B
String cKPInput = "The"; // Input Key : C
String dKPInput = "Message"; // Input Key : D
String eKPInput = "Conversation"; // Input Key : E
String fKPInput = "On"; // Input Key : F
String gKPInput = "We"; // Input Key : G
String hKPInput = "Contact"; // Input Key : H
String iKPInput = "Greenlight"; // Input Key : I
String jKPInput = "Processing"; // Input Key : J
String kKPInput = "Fake News"; // Input Key : K
String lKPInput = "Networking"; // Input Key : L
String mKPInput = "Enlightment"; // Input Key : M
String nKPInput = "Are"; // Input Key : N
String oKPInput = "Concept"; // Input Key : O
String pKPInput = "MAP"; // Input Key : P
String qKPInput = "Walking"; // Input Key : Q
String rKPInput = "Sunshine"; // Input Key : R
String sKPInput = "Greyscale"; // Input Key : S
String tKPInput = "Who"; // Input Key : T
String uKPInput = "Questionmark"; // Input Key : U
String vKPInput = "Probably"; // Input Key : V
String wKPInput = "And"; // Input Key : W
String xKPInput = "I"; // Input Key : X
String yKPInput = "Like"; // Input Key : Y
String zKPInput = "Big Butts"; // Input Key : Z

void setup() {
  size(1280, 720);
  font = createFont("Sans Serif", 80);
  textFont(font, 120);
  textAlign(CENTER, CENTER);
}

void draw() {
  background(0);
  // for (int i = 0; i < wordsDisplay; i++) {
  text(myText, 0, 0, width, height);
  // }
}

void keyPressed() {

  switch(key) {
  case 'a': // KEYBOARD A 
    myText = aKPInput;
    break;
  case 'b': // KEYBOARD B
    myText = bKPInput;
    break;
  case 'c': // KEYBOARD C 
    myText = cKPInput;
    break;
  case 'd': // KEYBOARD D 
    myText = dKPInput;
    break;
  case 'e': // KEYBOARD E 
    myText = eKPInput;
    break;
  case 'f': // KEYBOARD F
    myText = fKPInput;
    break;
  case 'g': // KEYBOARD G
    myText = gKPInput;
    break; 
  case 'h': // KEYBOARD H 
    myText = hKPInput;
    break;
  case 'i': // KEYBOARD I 
    myText = iKPInput;
    break; 
  case 'j': // KEYBOARD J 
    myText = jKPInput;
    break; 
  case 'k': // KEYBOARD K 
    myText = kKPInput;
    break;
  case 'l': // KEYBOARD L 
    myText = lKPInput;
    break;
  case 'm': // KEYBOARD M 
    myText = mKPInput;
    break;
  case 'n': // KEYBOARD N 
    myText = nKPInput;
    break;
  case 'o': // KEYBOARD O
    myText = oKPInput;
    break;
  case 'p': // KEYBOARD P
    myText = pKPInput;
    break;
  case 'q': // KEYBOARD Q 
    myText = qKPInput;
    break;
  case 'r': // KEYBOARD R 
    myText = rKPInput;
    break;
  case 's': // KEYBOARD S
    myText = sKPInput;
    break;
  case 't': // KEYBOARD T 
    myText = tKPInput;
    break;
  case 'u': // KEYBOARD U 
    myText = uKPInput;
    break;
  case 'v': // KEYBOARD V
    myText = vKPInput;
    break;
  case 'w': // KEYBOARD W 
    myText = wKPInput;
    break;
  case 'x': // KEYBOARD X 
    myText = xKPInput;
    break;
  case 'y': // KEYBOARD Y 
    myText = yKPInput;
    break;
  case 'z': // KEYBOARD Z 
    myText = zKPInput;
    break;
  }
}

1 Like

You can add myText to an arraylist.

When it reaches 20 (size()) for loop over it with i < size()-2 (!!!)

a1.set(i) = a1.get(i+1);

I never worked with an arraylist before. Could you suggest code please?

See reference for Arraylist

You can also use array

yeah sorry man. Realy sorry but I don’t understand anything of what you mean. I’m reading and watching videos about arraylists and how they work but can’t figure it out what you mean by (size)) and a1.set(i) …

It’s stupid to place my wrong codes that I’ve tried but honestly, I don’t understand it and I can’t figure it out.

here it is (with array)


PFont font;

String[] wordsDisplay=new String[20];
int addIndex=0;
boolean firsttime=true; 

// -----------------------------------------------------------------------------

void setup() {
  size(1280, 720);
  font = createFont("Sans Serif", 17);
  textFont(font, 17);
  //textAlign(LEFT);
}

void draw() {
  background(0);
  for (int i = 0; i < wordsDisplay.length; i++) {
    if (wordsDisplay[i]!=null)
      text(wordsDisplay[i], 
        44, 44+i*19);
  }
}

//---------------------------------------------------------------------------

void keyPressed() {

  String myText = "Testing part"; // Starting Text, is only vissable once pressed

  String aKPInput = "Medium"; // Input Key : A
  String bKPInput = "Is"; // Input Key : B
  String cKPInput = "The"; // Input Key : C
  String dKPInput = "Message"; // Input Key : D
  String eKPInput = "Conversation"; // Input Key : E
  String fKPInput = "On"; // Input Key : F
  String gKPInput = "We"; // Input Key : G
  String hKPInput = "Contact"; // Input Key : H
  String iKPInput = "Greenlight"; // Input Key : I
  String jKPInput = "Processing"; // Input Key : J
  String kKPInput = "Fake News"; // Input Key : K
  String lKPInput = "Networking"; // Input Key : L
  String mKPInput = "Enlightment"; // Input Key : M
  String nKPInput = "Are"; // Input Key : N
  String oKPInput = "Concept"; // Input Key : O
  String pKPInput = "MAP"; // Input Key : P
  String qKPInput = "Walking"; // Input Key : Q
  String rKPInput = "Sunshine"; // Input Key : R
  String sKPInput = "Greyscale"; // Input Key : S
  String tKPInput = "Who"; // Input Key : T
  String uKPInput = "Questionmark"; // Input Key : U
  String vKPInput = "Probably"; // Input Key : V
  String wKPInput = "And"; // Input Key : W
  String xKPInput = "I"; // Input Key : X
  String yKPInput = "Like"; // Input Key : Y
  String zKPInput = "Big Butts"; // Input Key : Z

  switch(key) {
    //
  case 'a': // KEYBOARD A 
    myText = aKPInput;
    break;
  case 'b': // KEYBOARD B
    myText = bKPInput;
    break;
  case 'c': // KEYBOARD C 
    myText = cKPInput;
    break;
  case 'd': // KEYBOARD D 
    myText = dKPInput;
    break;
  case 'e': // KEYBOARD E 
    myText = eKPInput;
    break;
  case 'f': // KEYBOARD F
    myText = fKPInput;
    break;
  case 'g': // KEYBOARD G
    myText = gKPInput;
    break; 
  case 'h': // KEYBOARD H 
    myText = hKPInput;
    break;
  case 'i': // KEYBOARD I 
    myText = iKPInput;
    break; 
  case 'j': // KEYBOARD J 
    myText = jKPInput;
    break; 
  case 'k': // KEYBOARD K 
    myText = kKPInput;
    break;
  case 'l': // KEYBOARD L 
    myText = lKPInput;
    break;
  case 'm': // KEYBOARD M 
    myText = mKPInput;
    break;
  case 'n': // KEYBOARD N 
    myText = nKPInput;
    break;
  case 'o': // KEYBOARD O
    myText = oKPInput;
    break;
  case 'p': // KEYBOARD P
    myText = pKPInput;
    break;
  case 'q': // KEYBOARD Q 
    myText = qKPInput;
    break;
  case 'r': // KEYBOARD R 
    myText = rKPInput;
    break;
  case 's': // KEYBOARD S
    myText = sKPInput;
    break;
  case 't': // KEYBOARD T 
    myText = tKPInput;
    break;
  case 'u': // KEYBOARD U 
    myText = uKPInput;
    break;
  case 'v': // KEYBOARD V
    myText = vKPInput;
    break;
  case 'w': // KEYBOARD W 
    myText = wKPInput;
    break;
  case 'x': // KEYBOARD X 
    myText = xKPInput;
    break;
  case 'y': // KEYBOARD Y 
    myText = yKPInput;
    break;
  case 'z': // KEYBOARD Z 
    myText = zKPInput;
    break;
  }//switch 
  //

  if (addIndex > wordsDisplay.length-1) {
    // list is full - we move each element up before adding a new one at the end

    for (int i=0; i < wordsDisplay.length-1; i++) {  
      wordsDisplay[i] = wordsDisplay[i+1];
    }

    wordsDisplay[wordsDisplay.length-1] = myText;
    //
  } else {
    // list is not yet full
    wordsDisplay[addIndex] = myText; 
    addIndex++;
  }
  //
}//func 
//
2 Likes

If you feel like Arraylist is a bit overwhelming, it might be easier to start with arrays instead. Once you get the hang of that you could take another look at Arraylist, because it might be better suited for your situation.

You could see an array as a variable that holds multiple variable values. If we look at the first example on the page I linked just now, it’s similar to:

int number1 = 90;
int number2 = 150;
int number3 = 30;
int a = number1 + number2;
int b = number2 + number3;

In your case you have value for each letter in the alphabet, meaning you need an array size of 26 slots. You could appoint the value of letter a to the first slot, the value of b to the second slot, etcetera, until all the slots in your array are filled.

Using arrays can be handy for many situations. Aside from keeping your data (in your case the words Medium, Is, The, etcetera) compact together, it simplifies certain activities for you. For instance, you can check the length of your array:

int[] numbers = new int[3];
numbers[0] = 90;
numbers[1] = 150;
numbers[2] = 30;
println(numbers.length);

If you would have three separate int variables instead of an array, this functionality wouldn’t be available to you.

Hope that helps you on your way!

edit
Woops, studying @Chrisir his code and I made a mistake about the array size you need. Still hope this post is helpful in some way :slight_smile:

2 Likes

Thanks man. Wouldn’t have don’t it without you. Thanks man!

1 Like

I tried to implement the changes you send my in pm:
Atm it only speaks one word at the time, but not the complete array. It also doesn’t have a delay yet.

This is the code so far:

PFont font;
import guru.ttslib.*;
TTS tts;

String[] wordsDisplay=new String[20];
int addIndex=0;
boolean firsttime=true; 

String myText = "Testing part"; // Starting Text, is only vissable once pressed

// -----------------------------------------------------------------------------

void setup() {
  size(1280, 720);
  font = createFont("Sans Serif", 17);
  textFont(font, 17);
  //textAlign(LEFT);
    tts = new TTS();
}

void draw() {
  background(0);
  for (int i = 0; i < wordsDisplay.length; i++) {
    if (wordsDisplay[i]!=null)
      text(wordsDisplay[i], 
        44, 44+i*19);
  }
  for (int i = 0; i < 2; i = i+1) {
 tts.speak(myText);
  println("Word " + myText + " is being spoken");}
}

//---------------------------------------------------------------------------

void keyPressed() {

  String aKPInput = "Medium"; // Input Key : A
  String bKPInput = "Is"; // Input Key : B
  String cKPInput = "The"; // Input Key : C
  String dKPInput = "Message"; // Input Key : D
  String eKPInput = "Conversation"; // Input Key : E
  String fKPInput = "On"; // Input Key : F
  String gKPInput = "We"; // Input Key : G
  String hKPInput = "Contact"; // Input Key : H
  String iKPInput = "Greenlight"; // Input Key : I
  String jKPInput = "Processing"; // Input Key : J
  String kKPInput = "Fake News"; // Input Key : K
  String lKPInput = "Networking"; // Input Key : L
  String mKPInput = "Enlightment"; // Input Key : M
  String nKPInput = "Are"; // Input Key : N
  String oKPInput = "Concept"; // Input Key : O
  String pKPInput = "MAP"; // Input Key : P
  String qKPInput = "Walking"; // Input Key : Q
  String rKPInput = "Sunshine"; // Input Key : R
  String sKPInput = "Greyscale"; // Input Key : S
  String tKPInput = "Who"; // Input Key : T
  String uKPInput = "Questionmark"; // Input Key : U
  String vKPInput = "Probably"; // Input Key : V
  String wKPInput = "And"; // Input Key : W
  String xKPInput = "I"; // Input Key : X
  String yKPInput = "Like"; // Input Key : Y
  String zKPInput = "Big Butts"; // Input Key : Z

  switch(key) {
    //
  case 'a': // KEYBOARD A 
    myText = aKPInput;
    break;
  case 'b': // KEYBOARD B
    myText = bKPInput;
    break;
  case 'c': // KEYBOARD C 
    myText = cKPInput;
    break;
  case 'd': // KEYBOARD D 
    myText = dKPInput;
    break;
  case 'e': // KEYBOARD E 
    myText = eKPInput;
    break;
  case 'f': // KEYBOARD F
    myText = fKPInput;
    break;
  case 'g': // KEYBOARD G
    myText = gKPInput;
    break; 
  case 'h': // KEYBOARD H 
    myText = hKPInput;
    break;
  case 'i': // KEYBOARD I 
    myText = iKPInput;
    break; 
  case 'j': // KEYBOARD J 
    myText = jKPInput;
    break; 
  case 'k': // KEYBOARD K 
    myText = kKPInput;
    break;
  case 'l': // KEYBOARD L 
    myText = lKPInput;
    break;
  case 'm': // KEYBOARD M 
    myText = mKPInput;
    break;
  case 'n': // KEYBOARD N 
    myText = nKPInput;
    break;
  case 'o': // KEYBOARD O
    myText = oKPInput;
    break;
  case 'p': // KEYBOARD P
    myText = pKPInput;
    break;
  case 'q': // KEYBOARD Q 
    myText = qKPInput;
    break;
  case 'r': // KEYBOARD R 
    myText = rKPInput;
    break;
  case 's': // KEYBOARD S
    myText = sKPInput;
    break;
  case 't': // KEYBOARD T 
    myText = tKPInput;
    break;
  case 'u': // KEYBOARD U 
    myText = uKPInput;
    break;
  case 'v': // KEYBOARD V
    myText = vKPInput;
    break;
  case 'w': // KEYBOARD W 
    myText = wKPInput;
    break;
  case 'x': // KEYBOARD X 
    myText = xKPInput;
    break;
  case 'y': // KEYBOARD Y 
    myText = yKPInput;
    break;
  case 'z': // KEYBOARD Z 
    myText = zKPInput;
    break;
  }//switch 
  //

  if (addIndex > wordsDisplay.length-1) {
    // list is full - we move each element up before adding a new one at the end

    for (int i=0; i < wordsDisplay.length-1; i++) {  
      wordsDisplay[i] = wordsDisplay[i+1];
    }

    wordsDisplay[wordsDisplay.length-1] = myText;
    //
  } else {
    // list is not yet full
    wordsDisplay[addIndex] = myText; 
    addIndex++;
  }
  //
}//func 
//

Question:

  • Did you do a research whether we can ask tts if it’s still speaking?

my version:

speaks when a new word is added (see keyPressed())

Chrisir


PFont font;
import guru.ttslib.*;
TTS tts;

String[] wordsDisplay=new String[20];
int addIndex=0;
boolean firsttime=true; 

String myText = "Testing part"; // Starting Text, is only vissable once pressed

// -----------------------------------------------------------------------------

void setup() {
  size(1280, 720);
  font = createFont("Sans Serif", 17);
  textFont(font, 17);
  //textAlign(LEFT);
  tts = new TTS();
}

void draw() {
  background(0);
  for (int i = 0; i < wordsDisplay.length; i++) {
    if (wordsDisplay[i]!=null)
      text(wordsDisplay[i], 
        44, 44+i*19);
  }//for
}

//---------------------------------------------------------------------------

void keyPressed() {

  String aKPInput = "Medium"; // Input Key : A
  String bKPInput = "Is"; // Input Key : B
  String cKPInput = "The"; // Input Key : C
  String dKPInput = "Message"; // Input Key : D
  String eKPInput = "Conversation"; // Input Key : E
  String fKPInput = "On"; // Input Key : F
  String gKPInput = "We"; // Input Key : G
  String hKPInput = "Contact"; // Input Key : H
  String iKPInput = "Greenlight"; // Input Key : I
  String jKPInput = "Processing"; // Input Key : J
  String kKPInput = "Fake News"; // Input Key : K
  String lKPInput = "Networking"; // Input Key : L
  String mKPInput = "Enlightment"; // Input Key : M
  String nKPInput = "Are"; // Input Key : N
  String oKPInput = "Concept"; // Input Key : O
  String pKPInput = "MAP"; // Input Key : P
  String qKPInput = "Walking"; // Input Key : Q
  String rKPInput = "Sunshine"; // Input Key : R
  String sKPInput = "Greyscale"; // Input Key : S
  String tKPInput = "Who"; // Input Key : T
  String uKPInput = "Questionmark"; // Input Key : U
  String vKPInput = "Probably"; // Input Key : V
  String wKPInput = "And"; // Input Key : W
  String xKPInput = "I"; // Input Key : X
  String yKPInput = "Like"; // Input Key : Y
  String zKPInput = "Big Butts"; // Input Key : Z

  switch(key) {
    //
  case 'a': // KEYBOARD A 
    myText = aKPInput;
    break;
  case 'b': // KEYBOARD B
    myText = bKPInput;
    break;
  case 'c': // KEYBOARD C 
    myText = cKPInput;
    break;
  case 'd': // KEYBOARD D 
    myText = dKPInput;
    break;
  case 'e': // KEYBOARD E 
    myText = eKPInput;
    break;
  case 'f': // KEYBOARD F
    myText = fKPInput;
    break;
  case 'g': // KEYBOARD G
    myText = gKPInput;
    break; 
  case 'h': // KEYBOARD H 
    myText = hKPInput;
    break;
  case 'i': // KEYBOARD I 
    myText = iKPInput;
    break; 
  case 'j': // KEYBOARD J 
    myText = jKPInput;
    break; 
  case 'k': // KEYBOARD K 
    myText = kKPInput;
    break;
  case 'l': // KEYBOARD L 
    myText = lKPInput;
    break;
  case 'm': // KEYBOARD M 
    myText = mKPInput;
    break;
  case 'n': // KEYBOARD N 
    myText = nKPInput;
    break;
  case 'o': // KEYBOARD O
    myText = oKPInput;
    break;
  case 'p': // KEYBOARD P
    myText = pKPInput;
    break;
  case 'q': // KEYBOARD Q 
    myText = qKPInput;
    break;
  case 'r': // KEYBOARD R 
    myText = rKPInput;
    break;
  case 's': // KEYBOARD S
    myText = sKPInput;
    break;
  case 't': // KEYBOARD T 
    myText = tKPInput;
    break;
  case 'u': // KEYBOARD U 
    myText = uKPInput;
    break;
  case 'v': // KEYBOARD V
    myText = vKPInput;
    break;
  case 'w': // KEYBOARD W 
    myText = wKPInput;
    break;
  case 'x': // KEYBOARD X 
    myText = xKPInput;
    break;
  case 'y': // KEYBOARD Y 
    myText = yKPInput;
    break;
  case 'z': // KEYBOARD Z 
    myText = zKPInput;
    break;
  }//switch 
  //

  // speak new word 
  tts.speak(myText);
  println("Word " + myText + " is being spoken");

  // add word to list 
  if (addIndex > wordsDisplay.length-1) {
    // list is full - we move each element up before adding a new one at the end

    for (int i=0; i < wordsDisplay.length-1; i++) {  
      wordsDisplay[i] = wordsDisplay[i+1];
    }

    wordsDisplay[wordsDisplay.length-1] = myText;
    //
  } else {
    // list is not yet full
    wordsDisplay[addIndex] = myText; 
    addIndex++;
  }
  //
}//func 
//

this above version speaks the word as soon as it is entered

do you want this?

Or do you want the entire list to be spoken on and on?

The entire list to be spoken on and on. And when entering a new word, pause for a second to start looping again so this way the system couldn’t be overrun.

So when no key has been pressed for 3 seconds, then start entire list to be spoken on and on again. Is that possible?

Possible

I don’t have time

but for speaking the list try

in draw

 // speak new word 
  tts.speak( wordsDisplay[i_speak] );
  println("Word " 
  +  wordsDisplay[i_speak]
  + " is being spoken");

if(!tts.isSpeaking())
    i_speak++; 

and before setup()

int i_speak=0;

1 Like

isSpeaking doesn’t exist so this don’t work :s

do research please, if there is another possibility

otherwise, just do a timer and if it’s applies:

    i_speak++; 

see here

https://www.local-guru.net/blog/pages/ttslib

I haven’t found something like isStillSpeaking()

I think though, it’s queuing the text automatically internally.

So texts spoken won’t overlap

1 Like

I haven’t figured it out how to speak the how list at once over and over again. I don’t get the I_speak++; like you suggested implemented in the code :s Any suggestions still left?

how about a restart timer with some estimated settings

import guru.ttslib.*;
StereoAudioPlayer sound; // unused interface

String[] words = {"Hi!","I","am","a","speaking","Processing","sketch"};
TTS tts = new TTS();

long ts,tm; //______________________ repeat timer

void setup() {
  tts.setPitch( 350 );
  tts.setPitchRange( 50 );
  tts.setPitchShift( 0.5 );
  tm = words.length * 500+2000; //__ 2 words per sec + delay ??
  ts = millis(); //_________________ timer init
}

void draw() {
  timer(); //_______________________ timer check / call
}

void speak() { //___________________ make text to speak and talk
  String tspeak = "";
  for (int i = 0; i< words.length; i++) tspeak += " "+words[i];
  println(tspeak);
  tts.speak(tspeak);  
}

void timer() { //___________________ timer function
  if ( millis() > ts + tm ) {
    ts += tm; // restart
    println("_tevent");
    speak(); 
  }
}

1 Like