Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Slider changing Frequencies?

$
0
0

Hey guys, so I have this slider, which should change the frequencies when you move it, but I don't know how to realise this. Does anyone have an idea on how to do this?

// import everything necessary to make sound.
import controlP5.*;
import ddf.minim.*;
import ddf.minim.ugens.*;

// create all of the variables that will need to be accessed in
// more than one methods (setup(), draw(), stop()).
ControlP5 cp5;
ControlP5 cp6;
Minim minim;
AudioOutput out;

Oscil      wave;
// keep track of the current Frequency so we can display it
Frequency  currentFreq;

int Hertz = 100;
int slider2 = 50;

// setup is run once at the beginning
void setup()
{
  // initialize the drawing window
  size(500, 130);

  cp5 = new ControlP5(this);
  cp5.addSlider("Hertz").setPosition(205,105).setRange(100,200);

  cp6 = new ControlP5(this);
  cp6.addSlider("Volume").setPosition(350,105).setRange(0,100);

  // initialize the minim and out objects
  minim = new Minim(this);
  out   = minim.getLineOut();

  currentFreq = Frequency.ofHertz( 100 );
  wave = new Oscil( currentFreq, 0.6f, Waves.TRIANGLE );

  wave.patch( out );
}

// draw is run many times
void draw()
{
  // erase the window to brown
  background( 0, 0, 0 );
  // draw using a beige stroke
  stroke( 255, 255, 255 );

  text( "Frequenz in Hertz: " + currentFreq.asHz(), 15, 113 );

  // 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 );
    // draw a line from one buffer position to the next for both channels
    line( x1, 50 + out.left.get(i)*50, x1, 50 + out.left.get(i+1)*50);
  }
}

// change the Hz when pressing keys on the keyboard
void keyPressed()
{
  if ( key == 'a' ) currentFreq = Frequency.ofHertz( 100 );
  if ( key == 's' ) currentFreq = Frequency.ofHertz( 200 );

  // note that there are two other static methods for constructing Frequency objects
  // currentFreq = Frequency.ofHertz( 440 );

  wave.setFrequency( currentFreq );
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles