Play files related to words in the order of a text

Hello,

My goal is to translate text into sounds and videos according to sound files and video material. I splitted the text into words but I just could not figure out how to correctly search for my words in the text and say If (word==’ '), play this file. I know the code is incomplete, but I hope it will show what I want to do. I could only find out how to play the file if it contains a specific word. So in a nutshell: what I need is to play files one after another in the order of my text. Could somebody help me out?
Many thanks in advance!

import processing.sound.*;
SoundFile file;

  String delimiters = " ,.?!;:[]";
  String [] s = loadStrings("DieLeidendesjungenWerther.txt");
  String everything = join(s, "" );
  String[] words = splitTokens(everything,delimiters);
 
void setup() {
  size(600,300);
  background(0);
 
  frameRate(5);
}
 
void draw() {

 if (words == 'Herz') {
    file = new SoundFile(this, "heartbeat.wav");
    file.play();
 }
}

1 Like

This won’t work.

Remember that words is an ARRAY. This means, a list.

You have to say words[i] (make i an global variable int i=0; and increment it during draw())

Second point: == doesn’t work with Strings. instead use

if (words[i].equals ("Herz")) ..............

I hope this helps.

Remark

In general it’s better to load all files in setup() and then just let them play in draw().

You could use a HashMap to store a String (Herz) together with its sound. See reference.

Warm regards,

Chrisir

Full Sketch



import processing.sound.*;
SoundFile file;

String delimiters = " ,.?!;:[]";
String [] s = {
  "Die ", 
  "Leiden ", 
  "des ", 
  "jungen ", 
  "Werther ", 
  "die ", 
  "Herz "
};

int i=0;

String everything = join(s, "" );
String[] words = splitTokens(everything, delimiters);

void setup() {
  size(600, 300);
  background(0);

  frameRate(5);
}

void draw() {

  if (words[i].equals ("Herz")) {
    //   file = new SoundFile(this, "heartbeat.wav");
    //   file.play();
    println("0");
  } else if (words[i].equals ("die")) {
    //   file = new SoundFile(this, "heartbeat.wav");
    //   file.play();
    println("1");
  } else {
    println("Unknown");
  }

  // inc
  if (i<words.length-1) {
    i++;
  } else {
    noLoop();
  }
}

1 Like

if you have a String text split it with the String words[] = split(text,' ') so it turns the text into an array of strings, separated by spaces. So a string hello! today is a nice day! Will be split into {"hello!","today","is","a","nice","day!"} Now just create a function that removes all of the extra things like !?.,-_;:=)(/&%$#"! ... and return just the word. After that just loop and check for documented words (words with audio files)

The purify function I wrote (to get rid of extra symbols)

Code
char acceptable[] = ("aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789").toCharArray();
void setup() {
  println(purify("He!!oT&!$\\/\\/ () |\\| `|` $|-|()\\/\\/! i used come characters to create a text out of symbols. After they are purified they won't show"));
} 
void draw() {
} 
String purify(String input) {
  String newString = "";
  for(int i = 0; i < input.length(); i++) {
    for(int j = 0; j < acceptable.length; j++) {
      if(input.charAt(i) == acceptable[j]) {
        newString += acceptable[j];
        break;
      }
    }
  }
  return newString;
}

shortened code:

Short code
char acceptable[] = ("aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ0123456789").toCharArray();
String purify(String input) {
  String newString = "";
  for (int i = 0; i < input.length(); i++) for (int j = 0; j < acceptable.length; j++) if (input.charAt(i) == acceptable[j]) {
    newString += acceptable[j];
    break;
  }
  return newString;
}

1 Like