# Play files related to words in the order of a text

**URL:** <https://discourse.processing.org/t/play-files-related-to-words-in-the-order-of-a-text/26966>\
**Category:** Libraries\
**Created:** [January 11, 2021, 7:59am UTC](https://discourse.processing.org/t/play-files-related-to-words-in-the-order-of-a-text/26966 "2021-01-11T07:59:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sophie](https://avatars.discourse-cdn.com/v4/letter/s/35a633/32.png) [@Sophie](https://discourse.processing.org/u/Sophie)\
**Post date:** [January 11, 2021, 7:59am UTC](https://discourse.processing.org/t/play-files-related-to-words-in-the-order-of-a-text/26966/1 "2021-01-11T07:59:04Z")

</div>

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!

```auto
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();
 }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [January 11, 2021, 2:37pm UTC](https://discourse.processing.org/t/play-files-related-to-words-in-the-order-of-a-text/26966/2 "2021-01-11T14:37:33Z")

</div>

> [@Sophie](#):
>
> ` if (words == 'Herz') {`

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**

```auto

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();
  }
}

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [January 12, 2021, 8:03am UTC](https://discourse.processing.org/t/play-files-related-to-words-in-the-order-of-a-text/26966/3 "2021-01-12T08:03:26Z")

</div>

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**
>
> ```auto
> 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**
>
> ```auto
> 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;
> }
> 
> ```
