How to set volume in minim?

edited September 2014 in How To...

Hello, I am trying to loop a song where when the audio input is at certain level, the volume of the song would be lowered; and when it is retrieved back to normal volume of audio input, the song will continue to play. What I am trying to do now is to mute and unmute the song, but it doesn't seem working...

import ddf.minim.*;

Minim       minim;
AudioInput  in;
AudioPlayer song;

void setup()
{
  size(512, 200);

  minim = new Minim(this);
  in = minim.getLineIn();
  song = minim.loadFile("song.mp3", 1024);
}

void draw()
{
  background(0);
  stroke(255);

  //draw sound line
  for(int i = 0; i < in.bufferSize() - 1; i++){
    line( i, height/7 + in.left.get(i)*height/7, 
          i+1, height/7 + in.left.get(i+1)*height/7 );
    line( i, height*6/7 + in.right.get(i)*height/7, 
          i+1, height*6/7 + in.right.get(i+1)*height/7 );
  }

  if (in.left.level()>0.5){
    song.mute();
  } else {   
    song.play();
  }

  println(in.left.level());
}

Within the if function, I have tried using boolean and song.pause(); and the same issue happened that the whole song would stop playing and wouldn't replay again. I also tried setVolume(); where it said volume is not supported. Hence, I came up with an idea to set the volume to mute and play. But it's still not working...

Answers

  • Perhaps once an AudioPlayer use mute(), corresponding AudioInput won't get any more level()? :-??
    Try out some keyPressed() or mousePressed() user intervention in order to play() it again! L-)

  • I have actually figured it out, the code works as followed:

    import ddf.minim.*;
    import ddf.minim.spi.*;
    import ddf.minim.signals.*;
    import ddf.minim.analysis.*;
    import ddf.minim.ugens.*;
    import ddf.minim.effects.*;
    
    Minim       minim;
    AudioInput  in;
    AudioPlayer song;
    
    void setup()
    {
      size(512, 200);
    
      minim = new Minim(this);
      in = minim.getLineIn();
      song = minim.loadFile("song.mp3", 1024);
      song.loop();
    }
    
    void draw()
    {
      background(0);
      stroke(255);
    
      //draw sound line
      for(int i = 0; i < in.bufferSize() - 1; i++){
        line( i, height/7 + in.left.get(i)*height/7, 
              i+1, height/7 + in.left.get(i+1)*height/7 );
        line( i, height*6/7 + in.right.get(i)*height/7, 
              i+1, height*6/7 + in.right.get(i+1)*height/7 );
      }
    
    
      if (in.left.level()>0.15){
        song.mute();
      } else if (in.left.level()<0.15){   
        song.unmute();
      }
    
      println(in.left.level()); 
    }
    
  • edited September 2014

    Oh, there's unmute() from Controller class too: :))

    import javax.sound.sampled.BooleanControl;
    public static BooleanControl.Type MUTE = BooleanControl.Type.MUTE;
    
    public void mute() {
      setValue(MUTE, true);
    }
    
    public void unmute() {
      setValue(MUTE, false);
    }
    
    private void setValue(BooleanControl.Type type, boolean v) {
      if (hasControl(type)) ((BooleanControl) getControl(type)).setValue(v);
      else                  Minim.error(type.toString() + " is not supported.");
    }
    

    Anyways, here's a tweaked version of yours: B-)

    // forum.processing.org/two/discussion/7237/how-to-set-volume-in-minim
    
    import ddf.minim.Minim;
    import ddf.minim.AudioInput;
    import ddf.minim.AudioPlayer;
    
    Minim       minim;
    AudioInput  in;
    AudioPlayer song;
    
    float h$7, h6$7;
    
    static final String FILE_NAME = "song.mp3";
    static final short  AUDIO_BUFFER = 1024;
    static final float  MUTE_THRESHOLD = .15, WEIGHT = 1.5;
    
    void setup() {
      size(512, 200, JAVA2D);
      smooth(4);
      frameRate(60);
    
      stroke(-1);
      strokeWeight(WEIGHT);
    
      in = (minim = new Minim(this)).getLineIn();
      song = minim.loadFile(FILE_NAME, AUDIO_BUFFER);
    
      h$7 = height/7.0;
      h6$7 = 6.0/7.0 * height;
    }
    
    void draw() {
      background(0);
    
      int i = 0, len = in.bufferSize();
      float left = in.left.get(0), right = in.right.get(0);
    
      while (++i < len) {
        line(i-1, left*h$7 + h$7, i, (left = in.left.get(i))*h$7 + h$7);
        line(i-1, right*h$7 + h6$7, i, (right = in.right.get(i))*h$7 + h6$7);
      }
    
      float level = in.left.level();
      frame.setTitle(FILE_NAME + "'s intensity: " + level);
    
      if (level > MUTE_THRESHOLD)  song.mute();
      else                         song.unmute();
    }
    
Sign In or Register to comment.