User Tools

Site Tools


prensf2010:volume
// Nels Oscar
// User defined volume function operating through the
// Effects interface. 
// Key commands
// SPACE : Play
// UP    : vol+
// DOWN  : vol-
import ddf.minim.*;
import ddf.minim.effects.*;
 
Minim minim;
AudioSample sample;
Vol v;
float volume=1.0;
void setup(){
  minim=new Minim(this);
  sample=minim.loadSample("c4.wav");
  v=new Vol(volume);
  sample.addEffect(v);
  sample.trigger();
}
 
void draw(){}
 
void keyPressed(){
  if(key==' '){
    sample.trigger();
    println("::Playing Sample::");
  }else if(keyCode==UP&&volume<1.0){
    volume+=0.02;  
  }else if(keyCode==DOWN&&volume>0.001){
    volume-=0.02;
  }
  println("Volume: "+(100*volume)+"%");
  v.setVolume(volume);
}
 
void stop(){
  minim.stop();
  super.stop();
}
 
class Vol implements AudioEffect{
  public float vol=1.0;
 
  public Vol(float v){
    vol=v;
  }
 
  public void process(float[] sig){
    for(int i=0; i<sig.length; i++)
      sig[i]*=vol;
  }
 
  public void process(float[] sigLeft, float[] sigRight){
    process(sigLeft);
    process(sigRight);
  }
  public void setVolume(float v){
    vol=v;
  }
}
prensf2010/volume.txt · Last modified: 2010/07/05 09:26 by nels_oscar