Why doesn't work MUTE with boolean in my code?

edited January 2017 in Library Questions

Hey! I'm having trouble to using .mute(); with boolean. As you can see in the code, I want to make processing to mute all my sounds when i hit 'k', and unmute them when I hit 'k' again. It's very important for me to make this program. When i do it with .pause() and .play(), it works, but it's not good for me, because i want to run these sounds in loop. I hope that u guys can help me. Here's my code: (on the start of the draw, wehere my problem is) (Btw.: sorry for my bad english) Thx

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

Minim m;
AudioInput in;
AudioPlayer hera;
AudioPlayer time;
AudioPlayer hotlinebling;
AudioPlayer kemosabe;


boolean onOff = false;
boolean pOn = false;

void setup() {
  size(640, 360);
  background(255);

  m = new Minim(this);
  hera = m.loadFile("1.mp3");
  hera.loop();

  time = m.loadFile("2.mp3");
  time.loop();

  hotlinebling = m.loadFile("hbt.mp3");
  hotlinebling.loop();


  kemosabe = m.loadFile("4.mp3");
  kemosabe.loop();

  in = m.getLineIn(Minim.MONO, 2048);
}      

void draw() {

  if (onOff == false) {
    kemosabe.mute();
    time.mute();
    hera.nmute();
    hotlinebling.mute();

    //Mikrofon 
    if (keyPressed) {
      if (key == 'p' && !pOn) {
        if (random(0, 6) < 3) {
          in.enableMonitoring();
        } else {
          in.disableMonitoring();
        }
        println(in.isMonitoring());
        pOn = true;
      }
    } else {
      pOn = false;
      in.disableMonitoring();
    }
  } else {
    kemosabe.unmute();
    hera.unmute();
    hotlinebling.unmute();
    time.unmute();
  }

  //Hera koka hallatszik 
  if (keyPressed ) {
    if (key=='0') 
      time.mute();
  } else {
    time.unmute();
  }

  if (keyPressed ) {
    if (key=='0') 
      kemosabe.mute();
  } else {
    kemosabe.unmute();
  }

  if (keyPressed ) {
    if (key=='0') 
      hotlinebling.mute();
  } else {
    hotlinebling.unmute();
  }

  //I've had hallatszik 
  if (keyPressed ) {
    if (key=='2') 
      hera.mute();
  } else {
    hera.unmute();
  }

  if (keyPressed ) {
    if (key=='2') 
      kemosabe.mute();
  } else {
    kemosabe.unmute();
  }

  if (keyPressed ) {
    if (key=='2') 
      hotlinebling.mute();
  } else {
    hotlinebling.unmute();
  }

  //Hotline bling
  if (keyPressed ) {
    if (key=='5') 
      hera.mute();
  } else {
    hera.unmute();
  }

  if (keyPressed ) {
    if (key=='5') 
      kemosabe.mute();
  } else {
    kemosabe.unmute();
  }

  if (keyPressed ) {
    if (key=='5') 
      time.mute();
  } else {
    time.unmute();
  }

  //Kemosabe hallatszik 
  if (keyPressed ) {
    if (key=='8') 
      hera.mute();
  } else {
    hera.unmute();
  }

  if (keyPressed ) {
    if (key=='8') 
      time.mute();
  } else {
    time.unmute();
  }

  if (keyPressed ) {
    if (key=='8') 
      hotlinebling.mute();
  } else {
    hotlinebling.unmute();
  }
}

void keyPressed() {
  if (key == 'm') {
   onOff = !onOff;
  }
}

Answers

  •   //Hera koka hallatszik 
      if (keyPressed) {
          if (key=='0') {
              time.mute();
          }
      } else {
          time.unmute();
      }
    

    is a key is pressed, then if that key is 0 then mute.

    if a key isn't pressed, unmute

    so everything is unmuting every loop when there's no key pressed. is this what you want?

    keyPressed is checked every 1/60th of a second, will remain true as long as a key is down. keyPressed() or keyReleased() is often easier to use because it doesn't repeat.

  • I want to make processing to mute all my sounds when i hit 'k', and unmute them when I hit 'k' again.

    there's no 'k' in the code.

  • and please don't post duplicates.

  • Okay, sorry for that, i'm new here, and i'm a bit desperate now.

    I misstyped it, its not 'k' , it's 'm' it this section:

    if (onOff == false) {
        kemosabe.mute();
        time.mute();
        hera.nmute();
        hotlinebling.mute();
    
        //Mikrofon 
        if (keyPressed) {
          if (key == 'p' && !pOn) {
            if (random(0, 6) < 3) {
              in.enableMonitoring();
            } else {
              in.disableMonitoring();
            }
            println(in.isMonitoring());
            pOn = true;
          }
        } else {
          pOn = false;
          in.disableMonitoring();
        }
      } else {
        kemosabe.unmute();
        hera.unmute();
        hotlinebling.unmute();
        time.unmute();
      }
    

    and this:

    void keyPressed() {
      if (key == 'm') {
       onOff = !onOff;
      }
    }
    

    what i need here:

    when i hit 'm' then every sound would be muted. When i hit 'm' again, they would be unmuted.

    But, i tell you my story, so you can maybe better understand my code. I builded a console , and there will be 5 buttons on it. (every button will hit a keyboard button). The sounds are heard continously, and when i hit 'm' these sounds sould be muted. Now i can speak into the microphone, and it will decide randomly, that my sound will heard from the output(speakers) or not.

    When i did this with .pause() and .play() it was good, but then i can't set the sounds in loop. So i thought mute() will be my friend, and do it for me, but no. The sounds won't mute, only for a milisec.

    These parts below are OK, it mutes every sound exept one. But only while i'm pressing a button. It's ok.

    //Hera koka hallatszik 
      if (keyPressed ) {
        if (key=='0') 
          time.mute();
      } else {
        time.unmute();
      }
    
      if (keyPressed ) {
        if (key=='0') 
          kemosabe.mute();
      } else {
        kemosabe.unmute();
      }
    
      if (keyPressed ) {
        if (key=='0') 
          hotlinebling.mute();
      } else {
        hotlinebling.unmute();
      }
    

    I hope my discription is not so confusing. Many thanks, for trying to help me

  • The sounds won't mute, only for a milisec.

    that's because the next time draw() is called, 60 times a second, and there is no key pressed everything gets unmuted.

  • Have u got an idea how can i solve this problem on another way? With pause() would be great as well, but then i cant make sounds loop :/

  •   if (keyPressed ) {
        if (key=='0') 
          time.mute();
      } else {
        println("time unmute"); // <<<< ADD THIS LINE
        time.unmute();
      }
    

    add the line above and you'll see what it's doing

  • Ok, i don't really got that. This part is ok. it works, and it works as it need to work. I would like to add a key, for example 'm', that would mute all the sounds as long as i don't press it again. But im kinda new to processing, and i can't fint another way. So i press 'm' it mutes the sounds, as long as i dont release 'm'.

  • There is a an example and some comments in one of the examples provided by the minim library. Open the examples dialog box from the Processing IDE and go to minim>>Advanced>>Controller>>muting>>muting.pde.

    I hope this helps,

    Kf

  • @Sly97 --

    Here is a switch that only turns on, never off again. It is not what you want:

    boolean switchA;
    void draw(){
      background(0);
      if (keyPressed){
        switchA = true;
        println(switchA);
      }
      if(switchA){
        rect(0,0,width-1,height-1);
      }
    }
    

    Here is a switch like an intercom button -- it only works while it is being pressed. Notice how the console output shows that it is constantly reseting to false when not being pressed. This is what you have done above. It is not what you want.

    boolean switchB;
    void draw() {
      background(0);
      if (keyPressed) {
        switchB = true;
      } else {
        switchB = false;
      }
      println(switchB);
      if(switchB){
        rect(0,0,width-1,height-1);
      }
    }
    

    Here is a switch like a light switch -- each time you flip it goes off, on, off, on. However, notice that if you hold the button down even a tiny bit the value flashes back and forth quickly between off-on -- again, that is not what you want.

    boolean switchC;
    void draw() {
      background(0);
      if (keyPressed) {
        switchC = !switchC;
        println(switchC);
      }
      if(switchC){
        rect(0,0,width-1,height-1);
      }
    }
    

    Here is the same switch, but using the keyReleased() function so that it only flips on/off once each time you press and release a key. This may be one way of doing what you want:

    boolean switchD;
    void draw() {
      background(0);
      if(switchD){
        rect(0,0,width-1,height-1);
      }
    }
    void keyReleased() {
      switchD = !switchD;
      println(switchD);
    }
    

    Finally, here is a switch with an "on" key (1) and an "off" key (2):

    boolean switchE;
    void draw() {
      background(0);
      if(switchE){
        rect(0,0,width-1,height-1);
      }
    }
    void keyReleased() {
      if(key=='1'){
        switchE = true;
      }
      if(key=='2'){
        switchE = false;
      }
      println(switchE);
    }
    

    It may be easier for you to organize and understand your key-related code if you embed it in the keyPressed() or keyReleased() functions instead of checking during the draw loop with keyPressed -- see the reference examples:

    1. https://processing.org/reference/keyPressed.html
    2. https://processing.org/reference/keyPressed_.html
    3. https://processing.org/reference/keyReleased_.html
  • i think his code suggests he wants a global mute (m) as well as being able to turn the 4 individual samples off and on (0, 2, 5, 8). but the individual code is interfering with the global code.

Sign In or Register to comment.