Minim core
ugens
analysis
Name Gain
Description Gain is another way of expressing an increase or decrease in the volume of something. It is represented in decibels (dB), which is a logorithmic scale. A gain of 0 dB means that you are not changing the volume of the incoming signal at all, positive gain boosts the signal and negative gain decreases it. You can effectively silence the incoming signal by setting the gain to something like -60.
Examples
/**
  This is an example of how to use a Gain UGen to control the gain of an audio file. 
  Gain is expressed in dB, which is short for decibels, and is way that people 
  often think about the level of digital audio. dB is a change in amplitude 
  relative to whatever the incoming amplitude is, so a gain value of 0 will not change 
  the incoming amplitude at all. Positive gain values increase the amplitude of the 
  incoming signal and negative values decrease it. It's not really possible to completely 
  silence incoming audio with a Gain UGen, but you can set the gain value so low that you 
  effectively silence the audio. 
  <p>
  dB is a logarithmic scale, so while 0 dB will not change the amplitude of incoming 
  audio at all, 6 dB will approximately double the amplitude and -6 dB will approximately halve it.
  <p>
  Move the mouse left and right to change the gain of the looping audio file. 
  All the way left is -6 dB, all the way right is +6 dB.
  <p>
  For more information about Minim and additional features, 
  visit http://code.compartmental.net/minim/
  <p>
  author: Damien Di Fede
*/

import ddf.minim.*;
import ddf.minim.ugens.*;

// declare everything we need to play our file
Minim minim;
FilePlayer filePlayer;
Gain       gain;
AudioOutput out;

// you can use your own file by putting it in the data directory of this sketch
// and changing the value assigned to fileName here.
String fileName = "groove.mp3";

void setup()
{
  // setup the size of the app
  size(640, 240);
  
  // create our Minim object for loading audio
  minim = new Minim(this);
  // this opens the file and puts it in the "play" state.                           
  filePlayer = new FilePlayer( minim.loadFileStream(fileName) );
  // and then we'll tell the recording to loop indefinitely
  filePlayer.loop();
  
  // start the Gain at 0 dB, which means no change in amplitude
  gain = new Gain(0.f);
  
  // get a line out from Minim. It's important that the file is the same audio format 
  // as our output (i.e. same sample rate, number of channels, etc).
  out = minim.getLineOut();
  
  // patch the file player to the output
  filePlayer.patch(gain).patch(out);
                        
}

// draw is run many times
void draw()
{
  // update the gain value. middle of the width will be the original amplitude 
  // of the audio file, far right is twice as loud and far left is half as loud.
  float dB = map(mouseX, 0, width, -6, 6);
  gain.setValue(dB);
  
  // erase the window to black
  background( 0 );
  // draw using a white stroke
  stroke( 255 );
  // draw the waveforms
  for( int i = 0; i < out.bufferSize() - 1; i++ )
  {
    // find the x position of each buffer value
    float x1  =  map( i, 0, out.bufferSize(), 0, width );
    float x2  =  map( i+1, 0, out.bufferSize(), 0, width );
    // draw a line from one buffer position to the next for both channels
    line( x1, 50 + out.left.get(i)*50, x2, 50 + out.left.get(i+1)*50);
    line( x1, 150 + out.right.get(i)*50, x2, 150 + out.right.get(i+1)*50);
  }  
  
  text("Current Gain is " + dB + " dB.", 10, 20);
}
Constructors
Gain();
Gain(dBvalue);
Parameters
dBvalue   float: the amount of gain to apply to the incoming signal
Fields
audio   The audio input is where incoming signals should be patched, however you do not need to patch directly to this input because patching to the Gain itself will accomplish the same thing.

gain   The gain input controls the value of this Gain. It will be interpreted as being in dB. 0 dB means that the incoming signal will not be changed, positive dB increases the amplitude of the signal, and negative dB decreases it. You can effectively silence the incoming signal by setting the gain to something like -60.

Methods
channelCount ( )   Returns the number of channels this UGen has been configured to generate.

getLastValues ( )   Return the last values generated by this UGen. This will most often be used by sub-classes when pulling data from their inputs.

patch ( )   Send the output of this UGen to another UGen, UGenInput, or AudioOutput.

printInputs ( )   Prints all inputs connected to this UGen (for debugging)

sampleRate ( )   Returns the sample rate of this UGen.

setChannelCount ( )   Let this UGen know how many channels of audio you will be asking it for.

setSampleRate ( )   Set the sample rate for this UGen.

setValue ( )   Set the value of this Gain to a given dB value.

tick ( )   Generates one sample frame for this UGen.

unpatch ( )   Unpatch this UGen from an AudioOutput or other UGen.

Usage Web & Application
Related