Sound only plays once, why?

edited March 2014 in Library Questions

Hi community!
I've writen some code but i don't know how to get it working properly. I want to make a sound play and scaling the shape to the sound when the mouse moves over a certain area (rectangle or other shape). The sound works, but only once. After the sound has played i'm not able to replay it. And i've on more question. How can i scale a triangle, is there a CENTER mode like for rects? Cause my code only works with Java i've attached a short video as well as the original code. Thank's! :)

youtu.be/tfW7C1k-qXU

import ddf.minim.*;

Minim minim;
AudioPlayer Snare, Bass, Cymbal_Crash;


void setup()
{
  size(500, 250);

  minim = new Minim(this);
  Snare = minim.loadFile("Snare_Hit.wav");
  Bass = minim.loadFile("Bass_Drum.wav");
  Cymbal_Crash = minim.loadFile("Cymbal_Crash.wav");

}

void draw()
{
  background(255);

  ellipse(120.5, 125, 47, 47);
  rect(250, 125, 47, 47);


  //Snare_Hit = Circle
  noFill();
  float mix1 = Snare.mix.level();
  println(mix1);
  float grau1 = map(mix1, 0, 0.5, 0, 255);

  stroke(grau1);
  ellipse(120.5, 125, mix1*200, mix1*200);


  //Bass_Drum = Rect
  noFill();
  float mix2 = Bass.mix.level();
  println(mix2);
  float grau2 = map(mix2, 0, 0.5, 0, 255);

  stroke(grau2);
  rectMode(CENTER);
  rect(250, 125, mix2*200, mix2*200);


  //Cymbal_Crash = Triangle
  noFill();
  float mix3 = Cymbal_Crash.mix.level();
  println(mix3);
  float grau3 = map(mix3, 0, 0.5, 0, 255);

  stroke(grau3);
  triangle(356.866, 148, 383.135, 102.5, 409.405, 148);



    if (mouseX > 97+8 && mouseX < 144+8 && mouseY > 101.5+8 && mouseY < 148.5+8)
    {
      Snare.play();
    }
    if (mouseX > 225 && mouseX < 275 && mouseY > 100 && mouseY < 150)
    {
      Bass.play();
    }
    if (mouseX > 356 && mouseX < 411 && mouseY > 101.5 && mouseY < 149)
    {
      Cymbal_Crash.play();
    }

}

Answers

  • call rewind() before play(). the playhead needs to be pushed back to the start after you played it. rewind does this.

  • edited March 2014

    and you can use sound.trigger() or you could when I last checked

  • @mschi: The problem with rewind() ist that the sound keeps playing and overlays endless times so you only get to hear a mess. I would like to get the sound playing always one time when you move above the shape. Any ideas? :)
    And trigger() does not seem to work anymore?

  • try pause(); rewind(); play();

    maybe just calling play(0); also does the job.

  • Unfortounately this doesn't change anything. ^^
    I really don't know what's wrong here, that's the actual code:

    import ddf.minim.*;
    
    Minim minim;
    AudioPlayer Snare, Bass, Cymbal_Crash;
    
    
    void setup()
    {
      size(500, 250);
    
      minim = new Minim(this);
      Snare = minim.loadFile("Snare_Hit.wav");
      Bass = minim.loadFile("Bass_Drum.wav");
      Cymbal_Crash = minim.loadFile("Cymbal_Crash.wav");
    
    }
    
    void draw()
    {
      background(255);
    
      ellipse(120.5, 125, 47, 47);
      rect(250, 125, 47, 47);
    
    
      //Snare_Hit = Circle
      noFill();
      float mix1 = Snare.mix.level();
      println(mix1);
      float grau1 = map(mix1, 0, 0.5, 0, 255);
    
      stroke(grau1);
      ellipse(120.5, 125, mix1*200, mix1*200);
    
    
      //Bass_Drum = Rect
      noFill();
      float mix2 = Bass.mix.level();
      println(mix2);
      float grau2 = map(mix2, 0, 0.5, 0, 255);
    
      stroke(grau2);
      rectMode(CENTER);
      rect(250, 125, mix2*200, mix2*200);
    
    
      //Cymbal_Crash = Triangle
      noFill();
      float mix3 = Cymbal_Crash.mix.level();
      println(mix3);
      float grau3 = map(mix3, 0, 0.5, 0, 255);
    
      stroke(grau3);
      triangle(356.866, 148, 383.135, 102.5, 409.405, 148);
    
    
    
        if (mouseX > 97+8 && mouseX < 144+8 && mouseY > 101.5+8 && mouseY < 148.5+8)
        {
          Snare.pause();
          Snare.rewind();
          Snare.play();
        }
    
        if (mouseX > 225 && mouseX < 275 && mouseY > 100 && mouseY < 150)
        {
          Bass.pause();
          Bass.rewind();
          Bass.play();
        }
    
        if (mouseX > 356 && mouseX < 411 && mouseY > 101.5 && mouseY < 149)
        {
          Cymbal_Crash.pause();
          Cymbal_Crash.rewind();
          Cymbal_Crash.play();
         }
    
    }
    

    And that's a short demonstration of the repetitive sound:
    youtu.be/QHWtoFx17jI

  • In setup just add player.loop();

  • Answer ✓

    take your play trigger code out of draw() and insert it into a mousePressed function:

    void mousePressed() {
        if (mouseX > 97+8 && mouseX < 144+8 && mouseY > 101.5+8 && mouseY < 148.5+8)
        {
          Snare.pause();
          Snare.rewind();
          Snare.play();
        }
    
        if (mouseX > 225 && mouseX < 275 && mouseY > 100 && mouseY < 150)
        {
          Bass.pause();
          Bass.rewind();
          Bass.play();
        }
    
        if (mouseX > 356 && mouseX < 411 && mouseY > 101.5 && mouseY < 149)
        {
          Cymbal_Crash.pause();
          Cymbal_Crash.rewind();
          Cymbal_Crash.play();
         }
    }
    
  • @mschi: Wow, that's really cool and does the job for me, thank you very much! :)
    Do you also know how to scale a triangle with it's ankerpoint centered? ^^

  • edited March 2014

    you basically have to do something like that:

    draw(){
    pushMatrix();
      // define position where circle is supposed to sit
      translate(50, 50);
    
      // big is a boolean that is true as long as the circle is supposed to be big
      // you have to set it to true/false at some place
      if(big)scale(2.0);
      else scale(1.0);
    
      ellipseMode(CENTER);
      ellipse(0, 0, 40,40);
    popMatrix();
    
    pushMatrix();
    ... do the same with rect
    popMatrix();
    
    pushMatrix();
    ... do the same with triangle
    popMatrix();
    }
    

    background infos here http://processing.org/tutorials/transform2d/

  • edited March 2014

    yes .trigger works and it would be way easier. This is a processing example that I changed a little:

    import ddf.minim.*;
    
    Minim minim;
    AudioSample kick;
    AudioSample snare;
    
    void setup() {
      size(512, 200, P3D);
      minim = new Minim(this);
      kick = minim.loadSample( "kick.mp3", 512);
      snare = minim.loadSample("snare.mp3", 512);
    }
    
    void draw() {
      background(0);
      stroke(255);
      for (int i = 0; i < kick.bufferSize() - 1; i++) {
        float x1 = map(i, 0, kick.bufferSize(), 0, width);
        float x2 = map(i+1, 0, kick.bufferSize(), 0, width);
        line(x1, 50 - kick.mix.get(i)*50, x2, 50 - kick.mix.get(i+1)*50);
        line(x1, 150 - snare.mix.get(i)*50, x2, 150 - snare.mix.get(i+1)*50);
      }
    }
    
    void mousePressed() {
      if (mouseX < width/2) snare.trigger();
      if (mouseY > width/2) kick.trigger();
    }
    
  • edited April 2014

    Hello, I think I know what the mousePressed is suppose to do but it is being used with width instead of height which does not seem to have the proper effect. I changed it to this and it seems to work better. Cool little mini drum pad BTW. DJ xSUBn ({-_-})

    void mousePressed() {
      if (mouseY < height/2) { snare.trigger(); } else { kick.trigger(); }
    }
    
Sign In or Register to comment.