# Trigger a sound file when an object \> width/2

**URL:** <https://discourse.processing.org/t/trigger-a-sound-file-when-an-object-width-2/7438>\
**Category:** Libraries\
**Created:** [January 12, 2019, 8:56am UTC](https://discourse.processing.org/t/trigger-a-sound-file-when-an-object-width-2/7438 "2019-01-12T08:56:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BrokenCode](https://avatars.discourse-cdn.com/v4/letter/b/9de0a6/32.png) [@BrokenCode](https://discourse.processing.org/u/BrokenCode)\
**Post date:** [January 12, 2019, 8:56am UTC](https://discourse.processing.org/t/trigger-a-sound-file-when-an-object-width-2/7438/1 "2019-01-12T08:56:58Z")

</div>

Hello everyone,  
I’m a bit stack with a rather simple file trigger, the thing is:

I have some text() that I can drag and drop, and I would like that when one of them pass the width/2 value, it play a specific sound file. When I drag the text() back and xpos \< width/2 it should stop only that sound file. I’m currently using sound library, minim seems overcomplicated for what I’m looking for, but I’m open to anything.

I currently have some problem with the trigger, because it does it at frameRate, rather than an impulse. I tried using some boolean for control, but it’s not working… maybe you can give me some hints 🙂

Thank you

The drang and drop function is adapted from [https://forum.processing.org/two/discussion/19190/click-and-drag-with-arrays-of-objects](https://forum.processing.org/two/discussion/19190/click-and-drag-with-arrays-of-objects)

```auto
import processing.sound.*;
SoundFile file;

Text[] textboxes = new Text[20];

boolean isPlaying = false;
boolean play = false;

void setup() {
  size(500, 500);
  textSize(32);
  for (int i = 0; i < 20; i++) {
    textboxes[i] = (new Text("CIAO", 20, 20 + (i*32)));
  }

  // Load a soundfile from the /data folder of the sketch and play it back
  file = new SoundFile(this, "Broken Code - Supreme Instruction - 152bpm - 24 BIT MASTER.wav");
}

void draw() {
  background(0, 200, 200);
  for (int i = 0; i < textboxes.length; i++) {
    textboxes[i].show();

    if (textboxes[0].xpos > width/2) {
      play = true;
      isPlaying = true;
    } else {
      play = false;
    }
  }
  
  isItPlaying();

  pushMatrix();
  stroke(0);
  strokeWeight(10);
  line(width/2, 0, width/2, height);
  popMatrix();
}

void isItPlaying() {
  if (isPlaying == true && play == true) {
    println("now play");
    file.play();
    isPlaying = false;
  } else if (isPlaying == false && play == false) {
    file.stop();
  }
}

void mousePressed() {
  for (int i = 0; i < textboxes.length; i++) {
    //checking to see if the mouse is over the box and turning it white if it is
    if (textboxes[i].textover == true) {
      textboxes[i].locked = true;
      // print("mouse is pressed");
    } else {

      textboxes[i].locked = false;
      // print("mouse isn't pressed");
    }
    textboxes[i].xoffset = mouseX - textboxes[i].xpos;
    textboxes[i].yoffset = mouseY - textboxes[i].ypos;
    // print(textboxes[i].locked);
  }
}

void mouseDragged() {
  for (int i = 0; i < textboxes.length; i++) {
    if (textboxes[i].locked) {
      textboxes[i].xpos = mouseX - textboxes[i].xoffset;
      textboxes[i].ypos = mouseY - textboxes[i].yoffset;
    }
  }
}

void mouseReleased() {
  for (int i = 0; i < textboxes.length; i++) {
    textboxes[i].locked = false;
  }
}

class Text {

  float xpos = random(width);
  float ypos = random(height);
  float textsize;
  float textheight;
  boolean textover = false;
  boolean locked = false;
  float xoffset = 0;
  float yoffset = 0;

  String text;

  Text(String text, float xpos, float ypos) {
    this.xpos = xpos;
    this.ypos = ypos;
    this.text = text;
    textsize = textWidth(text);
    textheight = textAscent() + textDescent();
  }

  void show() {
    text(text, this.xpos, this.ypos);

    if (mouseX > this.xpos && mouseX < this.xpos + this.textsize &&
      mouseY > this.ypos - this.textheight/2 && mouseY < this.ypos + this.textheight/2) {
      this.textover = true;
      fill(255);

      if (mousePressed && this.textover) {
        stroke(200, 79, 100);
        strokeWeight(5);
      } else {
        noStroke();
      }
    } else {
      this.textover = false;
      noStroke();
    }
    // rect(this.xpos, this.ypos, this.textsize, this.textsize, 7);
  };
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 12, 2019, 11:52am UTC](https://discourse.processing.org/t/trigger-a-sound-file-when-an-object-width-2/7438/2 "2019-01-12T11:52:13Z")

</div>

if i understand correctly you want select the sound title by drag and drop to start it.

i think for this need

- -a- a array of songs
- -b- the array of text titles should remember if it is playing

try:

```auto
import processing.sound.*;
int numsounds = 5;
SoundFile[] sfile = new SoundFile[numsounds];
Text[] textboxes = new Text[numsounds];

void setup() {
  size(500, 500);
  textSize(25);
  for (int i = 0; i < numsounds; i++) {
    textboxes[i] = new Text("song "+i, 20, 20 + (i*32), i);
    sfile[i] = new SoundFile(this, "vibraphon.aiff"); // Load a soundfile from the /data folder of the sketch and play it back
  }
}

void draw() {
  background(0, 200, 200);
  stroke(0);
  strokeWeight(10);
  line(width/2, 0, width/2, height);
  for (int i = 0; i < textboxes.length; i++) textboxes[i].show();
  isItPlaying();
}

void isItPlaying() {
  for (int i = 0; i < textboxes.length; i++) {
    boolean thisshouldplay = false;
    if (textboxes[i].xpos > width/2) thisshouldplay = true;

    if ( !textboxes[i].isPlaying && thisshouldplay ) {
      println("i "+i+" start");
      sfile[i].play();
      textboxes[i].isPlaying = true;
    } 

    if (textboxes[i].isPlaying && !thisshouldplay && sfile[i].isPlaying() ) {
      println("i "+i+" stop");
      sfile[i].stop(); //pause(); //stop(); // all not work on RPI
      textboxes[i].isPlaying = false;
    }
  }
}

void mousePressed() {
  for (int i = 0; i < textboxes.length; i++) {
    //checking to see if the mouse is over the box and turning it white if it is
    if (textboxes[i].textover == true) textboxes[i].locked = true;
    else textboxes[i].locked = false;

    textboxes[i].xoffset = mouseX - textboxes[i].xpos;
    textboxes[i].yoffset = mouseY - textboxes[i].ypos;
    // print(textboxes[i].locked);
  }
}

void mouseDragged() {
  for (int i = 0; i < textboxes.length; i++) {
    if (textboxes[i].locked) {
      textboxes[i].xpos = mouseX - textboxes[i].xoffset;
      textboxes[i].ypos = mouseY - textboxes[i].yoffset;
      //println("i "+i+" x "+textboxes[i].xpos+" y "+textboxes[i].ypos);
    }
  }
}

void mouseReleased() {
  for (int i = 0; i < textboxes.length; i++) {
    textboxes[i].locked = false;
  }
}

class Text {

  float xpos;
  float ypos;
  float textsize;
  float textheight;
  boolean textover = false;
  boolean locked = false;
  float xoffset = 0;
  float yoffset = 0;

  String text;
  boolean isPlaying = false;
  int id;

  Text(String text, float xpos, float ypos, int id) {
    this.xpos = xpos;
    this.ypos = ypos;
    this.id = id;
    this.text = text;
    textsize = textWidth(text);
    textheight = textAscent() + textDescent();
  }

  void show() {
    text(text, this.xpos, this.ypos);

    if (mouseX > this.xpos && mouseX < this.xpos + this.textsize &&
      mouseY > this.ypos - this.textheight/2 && mouseY < this.ypos + this.textheight/2) {
      this.textover = true;
      fill(255);

      if (mousePressed && this.textover) {
        stroke(200, 79, 100);
        strokeWeight(5);
      } else {
        noStroke();
      }
    } else {
      this.textover = false;
      noStroke();
    }
  }
}

```

but my system has some problem about the stop or pause  
so try with minim  
and make like a playlist

```auto
// https://discourse.processing.org/t/trigger-a-sound-file-when-an-object-width-2/7438
// v0.2 soundfile array and new play logic
// v0.3 change from sound to minim lib, use playlist array for title and file

import ddf.minim.*;
Minim minim;

int numsounds = 3;
String[][] playlist = { { "song 1 ", "data/groove.mp3"}, { "song 2 ", "data/groove.mp3"}, { "song 3 ", "data/groove.mp3"} };
AudioPlayer[] sfile = new AudioPlayer[numsounds];
Text[] textboxes = new Text[numsounds];

boolean dbug = true;

void setup() {
  size(500, 500);
  minim = new Minim(this);
  textSize(25);
  for (int i = 0; i < numsounds; i++) {
    if (dbug) println("i "+i+" "+playlist[i][0]+" file "+playlist[i][1]);
    textboxes[i] = new Text(playlist[i][0], 20, 20 + (i*32), i);
    sfile[i] = minim.loadFile(playlist[i][1]); // Load a soundfile from the /data folder of the sketch and play it back
  }
}

void draw() {
  background(0, 200, 200);
  stroke(0);
  strokeWeight(10);
  line(width/2, 0, width/2, height);
  for (int i = 0; i < textboxes.length; i++) textboxes[i].show();
  isItPlaying();
}

void isItPlaying() {
  for (int i = 0; i < textboxes.length; i++) {
    boolean thisshouldplay = false;
    if (textboxes[i].xpos > width/2) thisshouldplay = true;

    if ( !textboxes[i].isPlaying && thisshouldplay ) {
      if (dbug) println("i "+i+" "+playlist[i][0]+" "+playlist[i][1]+" start");
      if ( sfile[i].position() == sfile[i].length() ) // if the player is at the end of the file,we have to rewind it before telling it to play again
      {
        sfile[i].rewind();
        sfile[i].play();
      } else sfile[i].play();

      textboxes[i].isPlaying = true;
    } 

    if (textboxes[i].isPlaying && !thisshouldplay && sfile[i].isPlaying() ) {
      if (dbug) println("i "+i+" "+playlist[i][0]+" "+playlist[i][1]+" stop");
      // sfile[i].stop(); //pause(); //stop(); // all not work on RPI lib sound
      sfile[i].pause(); //stop(); not work on RPI lib minim
      textboxes[i].isPlaying = false;
    }
  }
}

void mousePressed() {
  for (int i = 0; i < textboxes.length; i++) {
    //checking to see if the mouse is over the box and turning it white if it is
    if (textboxes[i].textover == true) textboxes[i].locked = true;
    else textboxes[i].locked = false;

    textboxes[i].xoffset = mouseX - textboxes[i].xpos;
    textboxes[i].yoffset = mouseY - textboxes[i].ypos;
    // print(textboxes[i].locked);
  }
}

void mouseDragged() {
  for (int i = 0; i < textboxes.length; i++) {
    if (textboxes[i].locked) {
      textboxes[i].xpos = mouseX - textboxes[i].xoffset;
      textboxes[i].ypos = mouseY - textboxes[i].yoffset;
      //println("i "+i+" x "+textboxes[i].xpos+" y "+textboxes[i].ypos);
    }
  }
}

void mouseReleased() {
  for (int i = 0; i < textboxes.length; i++) {
    textboxes[i].locked = false;
  }
}

class Text {

  float xpos;
  float ypos;
  float textsize;
  float textheight;
  boolean textover = false;
  boolean locked = false;
  float xoffset = 0;
  float yoffset = 0;

  String text;
  boolean isPlaying = false;
  int id;

  Text(String text, float xpos, float ypos, int id) {
    this.xpos = xpos;
    this.ypos = ypos;
    this.id = id;
    this.text = text;
    textsize = textWidth(text);
    textheight = textAscent() + textDescent();
  }

  void show() {
    text(text, this.xpos, this.ypos);

    if (mouseX > this.xpos && mouseX < this.xpos + this.textsize &&
      mouseY > this.ypos - this.textheight/2 && mouseY < this.ypos + this.textheight/2) {
      this.textover = true;
      fill(255);

      if (mousePressed && this.textover) {
        stroke(200, 79, 100);
        strokeWeight(5);
      } else {
        noStroke();
      }
    } else {
      this.textover = false;
      noStroke();
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![BrokenCode](https://avatars.discourse-cdn.com/v4/letter/b/9de0a6/32.png) [@BrokenCode](https://discourse.processing.org/u/BrokenCode)\
**Post date:** [January 12, 2019, 1:30pm UTC](https://discourse.processing.org/t/trigger-a-sound-file-when-an-object-width-2/7438/3 "2019-01-12T13:30:56Z")

</div>

Thank you kll!!  
That’s a very nice work 🙂  
I think I’ll never be able to make that happens, still now, I’m finding a little bit tricky to understand isItPlaying() method 🤔

Why are you saying that the skecth with sound library has problems? I tried it for a while and nothing occurred, maybe some specific situations causes some troubles?

Thank you also for the minim version, I really really appreciate that! 😃

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 12, 2019, 1:40pm UTC](https://discourse.processing.org/t/trigger-a-sound-file-when-an-object-width-2/7438/4 "2019-01-12T13:40:36Z")

</div>

> [@BrokenCode](#):
>
> Why are you saying that the skecth with sound library has problems? I tried it for a while and nothing occurred, maybe some specific situations causes some troubles?

i just play it on Raspberry Pi, RASPIAN linux OS, Processing 3.4 IDE  
and the sound lib STOP or PAUSE just not worked, so i try minim and that worked.

did you notice the wrong data structure?  
now you have the song title stored 2 times,  
in the playlist and in the text array  
( you can take it out there and just use there

```auto
//text(text, this.xpos, this.ypos);
text(playlist[id][0], this.xpos, this.ypos);

```

)

if you ever get a playlist file import working let me see it pls.

---

<div class="post-metadata">

**Author:** ![BrokenCode](https://avatars.discourse-cdn.com/v4/letter/b/9de0a6/32.png) [@BrokenCode](https://discourse.processing.org/u/BrokenCode)\
**Post date:** [January 12, 2019, 1:52pm UTC](https://discourse.processing.org/t/trigger-a-sound-file-when-an-object-width-2/7438/5 "2019-01-12T13:52:02Z")

</div>

mm I’m running your code without modification on Processing 3.4 on a Windows 10 machine and seems to work smoothly…

I also tried to automate the process of assign a single sound file to esch text() and it works:

```auto
import processing.sound.*;
int numsounds = 2;

SoundFile[] sfile = new SoundFile[numsounds];
Text[] textboxes = new Text[numsounds];

void setup() {
  size(500, 500);
  textSize(25);

  // we'll have a look in the data folder
  java.io.File folder = new java.io.File(dataPath(""));

  // list the files in the data folder
  String[] filenames = folder.list();

  // get and display the number of jpg files
  println(filenames.length + " files in specified directory");

  // display the filenames
  for (int i = 0; i < filenames.length; i++) {
    println(filenames[i]);
  }

  for (int i = 0; i < numsounds; i++) {
    textboxes[i] = new Text("song "+i, 20, 20 + (i*32), i);
    sfile[i] = new SoundFile(this, filenames[i]); // Load a soundfile from the /data folder of the sketch and play it back
  }
}

void draw() {
  background(0, 200, 200);
  stroke(0);
  strokeWeight(10);
  line(width/2, 0, width/2, height);
  for (int i = 0; i < textboxes.length; i++) {  
    textboxes[i].show();
  };
  isItPlaying();
}

void isItPlaying() {
  for (int i = 0; i < textboxes.length; i++) {

    boolean thisshouldplay = false;

    if (textboxes[i].xpos > width/2) {
      thisshouldplay = true;
    }

    if ( !textboxes[i].isPlaying && thisshouldplay ) {
      println("i "+i+" start");
      sfile[i].play();
      textboxes[i].isPlaying = true;
    } 

    if (textboxes[i].isPlaying && !thisshouldplay && sfile[i].isPlaying() ) {
      println("i "+i+" stop");
      sfile[i].stop(); //pause(); //stop(); // all not work on RPI
      textboxes[i].isPlaying = false;
    }
  }
}

void mousePressed() {
  for (int i = 0; i < textboxes.length; i++) {
    if (textboxes[i].textover == true) {
      textboxes[i].locked = true;
    } else {
      textboxes[i].locked = false;
    }

    textboxes[i].xoffset = mouseX - textboxes[i].xpos;
    textboxes[i].yoffset = mouseY - textboxes[i].ypos;
  }
}

void mouseDragged() {
  for (int i = 0; i < textboxes.length; i++) {
    if (textboxes[i].locked) {
      textboxes[i].xpos = mouseX - textboxes[i].xoffset;
      textboxes[i].ypos = mouseY - textboxes[i].yoffset;
    }
  }
}

void mouseReleased() {
  for (int i = 0; i < textboxes.length; i++) {
    textboxes[i].locked = false;
  }
}

class Text {

  float xpos;
  float ypos;
  float textsize;
  float textheight;
  boolean textover = false;
  boolean locked = false;
  float xoffset = 0;
  float yoffset = 0;

  String text;
  boolean isPlaying = false;
  int id;

  Text(String text, float xpos, float ypos, int id) {
    this.xpos = xpos;
    this.ypos = ypos;
    this.id = id;
    this.text = text;
    textsize = textWidth(text);
    textheight = textAscent() + textDescent();
  }

  void show() {
    text(text, this.xpos, this.ypos);

    if (mouseX > this.xpos && mouseX < this.xpos + this.textsize &&
      mouseY > this.ypos - this.textheight/2 && mouseY < this.ypos + this.textheight/2) {
      this.textover = true;
      fill(255);

      if (mousePressed && this.textover) {
        stroke(200, 79, 100);
        strokeWeight(5);
      } else {
        noStroke();
      }
    } else {
      this.textover = false;
      noStroke();
    }
  }
}

```

What’s the issue you’re facing? When you drag and drop the text back it doesn’t stop the audio file?
