Gain in Minim

edited October 2014 in Library Questions

Hi!

I'm using a gain object in my sketch and I want to get his current dB value. I'm trying with

myGain.gain.getLastValue()

but it doesn't work. Any idea?

Thanks!

Tagged:

Answers

  • edited October 2014

    What do you mean by "it doesn't work".

    float[] values = myGain.getLastValues();
    println(values);
    float firstValue = values[0];
    float lastValue = values[values.length-1];
    
  • edited October 2014

    I change the gain with the mouse. It works. But, if I want to print the gain dB value, it always prints 0.0 dB

    import ddf.minim.*;
    import ddf.minim.ugens.*;
    
    Minim minim;
    Oscil myWave;
    Gain myGain;
    AudioOutput out;
    
    void setup() {
      size(512, 200);
    
      minim = new Minim(this);
    
      myWave = new Oscil( 440, 0.5f, Waves.SINE );
    
      myGain = new Gain();
    
      out = minim.getLineOut();
    
      myWave.patch(myGain);
      myGain.patch(out);
    }
    
    void draw() {
      println(myGain.gain.getLastValue());
    }
    
    void mouseMoved() {
      float dB = map(mouseY, 0, height, 0, -30);
      myGain.setValue(dB);
    }
    
  • See the source code: https://github.com/ddf/Minim/blob/master/src/ddf/minim/ugens/Gain.java

    The original dBvalue is not saved, so if you want to keep that, you should save it yourself in another variable.

  • edited October 2014

    yes, I see

    Thanks ammon!

  • edited October 2014 Answer ✓

    Or you could put this customized class in your sketch...

    import ddf.minim.*; 
    import ddf.minim.ugens.*;
    
    Minim minim; 
    Oscil myWave; 
    Gain myGain; 
    AudioOutput out;
    
    void setup() { 
      size(512, 200);
      minim = new Minim(this);
      myWave = new Oscil( 440, 0.5f, Waves.SINE );
      myGain = new Gain();
      out = minim.getLineOut();
      myWave.patch(myGain); 
      myGain.patch(out);
    }
    
    void draw() { 
      println(myGain.getLastdBValue());
    }
    
    void mouseMoved() { 
      float dB = map(mouseY, 0, height, 0, -30); 
      myGain.setValue(dB);
    }
    
    class Gain extends UGen {
      UGenInput audio;
      UGenInput gain;
      float mValue;
      float dBvalue;
    
      public Gain() {
        this(0);
      }
    
      Gain(float dBvalue) {
        this.dBvalue = dBvalue;
        mValue = (float)Math.pow(10.0, (0.05 * dBvalue));
        audio = new UGenInput(InputType.AUDIO);
        gain = new UGenInput(InputType.CONTROL);
      }
    
      void setValue( float dBvalue ) {
        this.dBvalue = dBvalue;
        mValue = (float)Math.pow(10.0, (0.05 * dBvalue));
      }
    
      float getLastdBValue() {
        return dBvalue;
      }
    
      protected void uGenerate(float[] channels) {
        if ( gain.isPatched() ) {
          mValue = (float)Math.pow(10.0, (0.05 * gain.getLastValue()));
        }
        for (int i = 0; i < channels.length; ++i) {
          channels[i] = mValue * audio.getLastValues()[i];
        }
      }
    }
    
  • yes, but I'm working on a tutorial for beginners. I think it would be clearer for them to save the gain value in a variable.

    Thank you!

Sign In or Register to comment.