Trigger sample causes horrible loud distortion using MINIM

edited April 2016 in Library Questions

Hello, Im trying to get a sample to trigger when the mouse passes over a movie (which then loops and enlarges) and stop when the mouse is no longer on that movie.

In the below code everything is working correctly but the sound that is heard is horribly distorted. I think this is because it is playing a new sample every frame, but I'm not sure how to prevent that. I can get the sample to play back fine in mousePressed() (outside of draw) but i don't want to use that- i want the sound to play only when the mouse passes over the movie box. If the solution is to move the "sample.trigger();" out of draw where would it go?

Im on OSX 10.10.5 and using Processing 3.02

Code is below - but be warned - turn your speakers down if u play that exact code!

Thanks.

import processing.video.*;
import ddf.minim.*;


Movie movie1; 
Minim minim;
AudioSample sample;

float bx;
float by;
int boxSize;
boolean overBox = false;
float boxMax = 100;
float boxMin = 25;

void setup() {
fullScreen(2);
minim = new Minim(this);
bx = width/2;
by = height/2;
imageMode(CENTER);
boxSize = 25;

movie1 = new Movie(this, "TORCH_2_1.mov");
movie1.loop();
movie1.pause();
sample = minim.loadSample("Paper Rustle_1.mp3");
}

void movieEvent(Movie movie1) {
movie1.read();
}

 void draw() {

background(155);

if (mouseX > bx-(boxSize/2) && mouseX < bx+(boxSize/2) &&  
mouseY > by-(boxSize/2) && mouseY < by+(boxSize/2)) {
overBox = true; 

} else {

overBox = false;
}

if ((overBox) && (boxSize < boxMax)) {

image(movie1, bx, by,boxSize++,boxSize++);
movie1.loop();


}
if ((!overBox)&&(boxSize > boxMin)){

image(movie1, bx, by,boxSize-=3,boxSize-=3);
movie1.pause();


}
image(movie1,bx,by,boxSize,boxSize);

if (overBox) {
sample.trigger();

}

if (!overBox) {
sample.stop();

}

}

Answers

  • Your assumption is correct. Just add some logic to prevent it from triggering, when it has already triggered and the mouse is still over the box:

    import processing.video.*;
    import ddf.minim.*;
    
    
    Movie movie1; 
    Minim minim;
    AudioSample sample;
    
    float bx;
    float by;
    int boxSize;
    boolean overBox = false;
    boolean canTrigger = true;
    float boxMax = 100;
    float boxMin = 25;
    
    void setup() {
      fullScreen(2);
      minim = new Minim(this);
      bx = width/2;
      by = height/2;
      imageMode(CENTER);
      boxSize = 25;
    
      movie1 = new Movie(this, "TORCH_2_1.mov");
      movie1.loop();
      movie1.pause();
      sample = minim.loadSample("Paper Rustle_1.mp3");
    }
    
    void movieEvent(Movie movie1) {
      movie1.read();
    }
    
    void draw() {
    
      background(155);
    
      if (mouseX > bx-(boxSize/2) && mouseX < bx+(boxSize/2) &&  
        mouseY > by-(boxSize/2) && mouseY < by+(boxSize/2)) {
        overBox = true;
      } else {
    
        overBox = false;
      }
    
      if ((overBox) && (boxSize < boxMax)) {
    
        image(movie1, bx, by, boxSize++, boxSize++);
        movie1.loop();
      }
      if ((!overBox)&&(boxSize > boxMin)) {
    
        image(movie1, bx, by, boxSize-=3, boxSize-=3);
        movie1.pause();
      }
      image(movie1, bx, by, boxSize, boxSize);
    
      if (canTrigger && overBox) {
        sample.trigger();
        canTrigger = false;
      }
    
      if (!overBox) {
        sample.stop();
        canTrigger = true;
      }
    }
    
  • This is great, thanks so much for the help!

Sign In or Register to comment.