# How to increase freq with a keyPressed func over than mouseMoved

**URL:** <https://discourse.processing.org/t/how-to-increase-freq-with-a-keypressed-func-over-than-mousemoved/5987>\
**Category:** Libraries\
**Created:** [November 27, 2018, 9:36am UTC](https://discourse.processing.org/t/how-to-increase-freq-with-a-keypressed-func-over-than-mousemoved/5987 "2018-11-27T09:36:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![admbdr](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@admbdr](https://discourse.processing.org/u/admbdr)\
**Post date:** [November 27, 2018, 9:36am UTC](https://discourse.processing.org/t/how-to-increase-freq-with-a-keypressed-func-over-than-mousemoved/5987/1 "2018-11-27T09:36:15Z")

</div>

```auto
import ddf.minim.*;
import ddf.minim.ugens.*;

Minim minim;
AudioOutput out;
Oscil wave;

void setup()
{
  size(512, 200, P3D);
  
  minim = new Minim(this);
  
  // use the getLineOut method of the Minim object to get an AudioOutput object
  out = minim.getLineOut();
  
  // create a sine wave Oscil, set to 440 Hz, at 0.5 amplitude
  wave = new Oscil( 440, 0.5f, Waves.SINE );
  // patch the Oscil to the output
  wave.patch( out );
}

void draw()
{
  background(0);
  stroke(255);
  strokeWeight(1);
  
  // draw the waveform of the output
  for(int i = 0; i < out.bufferSize() - 1; i++)
  {
    line( i, 50 - out.left.get(i)*50, i+1, 50 - out.left.get(i+1)*50 );
    line( i, 150 - out.right.get(i)*50, i+1, 150 - out.right.get(i+1)*50 );
  }

  // draw the waveform we are using in the oscillator
  stroke( 128, 0, 0 );
  strokeWeight(4);
  for( int i = 0; i < width-1; ++i )
  {
    point( i, height/2 - (height*0.49) * wave.getWaveform().value( (float)i / width ) );
  }
}

void mouseMoved()
{
  // usually when setting the amplitude and frequency of an Oscil
  // you will want to patch something to the amplitude and frequency inputs
  // but this is a quick and easy way to turn the screen into
  // an x-y control for them.
  
  float amp = map( mouseY, 0, height, 1, 0 );
  wave.setAmplitude( amp );
  
  float freq = map( mouseX, 0, width, 110, 880 );
  wave.setFrequency( freq );
}
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 27, 2018, 10:15am UTC](https://discourse.processing.org/t/how-to-increase-freq-with-a-keypressed-func-over-than-mousemoved/5987/2 "2018-11-27T10:15:41Z")

</div>

sorry, your posted code is not functional:

- -a- there seems to be “}” too much at the end
- -b- because you posted it as pasted text some good things seems to be deleted

```auto
import ddf.minim.*;
import ddf.minim.ugens.*;

```

show in your posting:

> import ddf.minim.;  
> import ddf.minim.ugens.;

so i only see errors.

- -c- why you not just say that you used the example of  
[File][Example][Contributed Libraries][Minim][Basics][SynthsizedSound]  
where you deleted the header explanation and the  
very existing

```auto
void keyPressed()
{ 
//...
}

```

now we know that you talk about the well known  
mouse [Theremin](https://en.wikipedia.org/wiki/Theremin)

// ok, lets start:  
if you want to keep the existing mouse operation mouseX  
but use the keyboard additionally you need some changes:

- need a global freq variable
- need to use that for the mouse
- and use a keyboard action also to add / sub something from it.

```auto
float myfreq = 440;

// ...

// float freq = map( mouseX, 0, width, 110, 880 );
// wave.setFrequency( freq );
  myfreq = map( mouseX, 0, width, 110, 880 );
  wave.setFrequency( myfreq );

// ...

void keyPressed()
{ 
  // using the UP DOWN ARROW KEY to change the frequency
  if ( keyCode == UP )
  { 
    println("UP");
    wave.setFrequency( myfreq += 10 );
  }
  if ( keyCode == DOWN )
  { 
    println("DOWN");
    wave.setFrequency( myfreq -= 10 );
  }
}

```
