# Help with minim

**URL:** https://discourse.processing.org/t/help-with-minim/6784
**Category:** Libraries
**Created:** [December 20, 2018, 3:34pm UTC](https://discourse.processing.org/t/help-with-minim/6784 "2018-12-20T15:34:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Dia](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Dia](https://discourse.processing.org/u/Dia)
#### Post date: [December 20, 2018, 3:34pm UTC](https://discourse.processing.org/t/help-with-minim/6784/1 "2018-12-20T15:34:48Z")

</div>

Hi , so i’m trying to build a synth using minim, i took aside, the “CreateAnInstrument” exemple to build a keyboard, the problem is i would like to make so that when the key to play the note is released, the note stops playing, and that it plays forever as long as the key is pressed (on the code here it plays for 2.9sec).  
I also would like to be able to change the Waveform from sine to something else (i tried with .setWaveform(Waves.SAW )😉

I tried everything, i’m still a beginner though, help/advice would be welcomed !

What i have so far :

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

boolean release;

Minim minim;
AudioOutput out;

// to make an Instrument we must define a class
// that implements the Instrument interface.
class SineInstrument implements Instrument {
  Oscil wave;
  Line ampEnv;
  
  SineInstrument( float frequency ) {
    // make a sine wave oscillator
    // the amplitude is zero because 
    // we are going to patch a Line to it anyway
    wave = new Oscil( frequency, 0, Waves.SINE );
    ampEnv = new Line();
    ampEnv.patch( wave.amplitude );
  }
  
  // this is called by the sequencer when this instrument
  // should start making sound. the duration is expressed in seconds.
  void noteOn( float duration ) {
    // start the amplitude envelope
    ampEnv.activate( duration, 0.2f, 0 );
    // attach the oscil to the output so it makes sound
    wave.patch( out );
  }
  
  // this is called by the sequencer when the instrument should
  // stop making sound
  void noteOff() {
    wave.unpatch( out );
  }
}

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();
  
  // when providing an Instrument, we always specify start time and duration
  out.playNote( 0.0, 0.0, new SineInstrument( 97.99 ) );
  // we can use the Frequency class to create frequencies from pitch names
}

void draw()
{
  background(0);
  stroke(255);
  
  // draw the waveforms
  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 );
  }
}

void keyPressed(){
  
  switch ( key ){
    case 'q': // La
  out.playNote( 0.0, 2.9, new SineInstrument( Frequency.ofPitch( "A3" ).asHz() ) );
    break;
    case 'z': // La#
  out.playNote( 0.0, 2.9, new SineInstrument( Frequency.ofPitch( "A#3" ).asHz() ) );
    break;
    case 's': // Si
  out.playNote( 0.0, 2.9, new SineInstrument( Frequency.ofPitch( "B3" ).asHz() ) );
    break;
    case 'd': // Do
  out.playNote( 0.0, 2.9, new SineInstrument( Frequency.ofPitch( "C4" ).asHz() ) );
    break;
    case 'r': // Do#
  out.playNote( 0.0, 2.9, new SineInstrument( Frequency.ofPitch( "C#4" ).asHz() ) );
    break;
    case 'f': // Ré
  out.playNote( 0.0, 2.9, new SineInstrument( Frequency.ofPitch( "D4" ).asHz() ) );
    break;
    case 't': // Ré#
  out.playNote( 0.0, 2.9, new SineInstrument( Frequency.ofPitch( "D#4" ).asHz() ) );
    break;
    case 'g': // Mi
  out.playNote( 0.0, 2.9, new SineInstrument( Frequency.ofPitch( "E4" ).asHz() ) );
    break;
    case 'h': // Fa
  out.playNote( 0.0, 2.9, new SineInstrument( Frequency.ofPitch( "F4" ).asHz() ) );
    break;
    case 'u': // Fa#
  out.playNote( 0.0, 2.9, new SineInstrument( Frequency.ofPitch( "F#4" ).asHz() ) );
    break;
    case 'j': // Sol
  out.playNote( 0.0, 2.9, new SineInstrument( Frequency.ofPitch( "G4" ).asHz() ) );
    break;
    case 'i': // Sol#
  out.playNote( 0.0, 2.9, new SineInstrument( Frequency.ofPitch( "G#4" ).asHz() ) );
    break;
    case 'k': // La +1
  out.playNote( 0.0, 2.9, new SineInstrument( Frequency.ofPitch( "A4" ).asHz() ) );
    break; 
  }
} // void keyPressed

void keyReleased(){
    if (key == 'q' ||key == 'z' ||key == 's' ||key == 'd' ||key == 'r' ||key == 'f' ||key == 't' ||key == 'g' ||key == 'h' ||key == 'u' ||key == 'j' ||key == 'i' ||key == 'k'){
      release = true;
  }
} // keyReleased

```

---

<div class="post-metadata">

### Author: ![umiham\_ntorosim](https://avatars.discourse-cdn.com/v4/letter/u/9de0a6/32.png) [@umiham\_ntorosim](https://discourse.processing.org/u/umiham_ntorosim)
#### Post date: [December 21, 2020, 5:48pm UTC](https://discourse.processing.org/t/help-with-minim/6784/2 "2020-12-21T17:48:08Z")

</div>

Store every instrument object in an array when you activate them in “keyPressed”, and use noteOff() method of the object you stored to stop the sound they make. Luckily and surely, every sound a key make is retrievable because one key can only make one sound.

---

<div class="post-metadata">

### Author: ![jay\_m](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jay_m/32/11020_2.png) [@jay\_m](https://discourse.processing.org/u/jay_m)
#### Post date: [December 22, 2020, 11:45am UTC](https://discourse.processing.org/t/help-with-minim/6784/3 "2020-12-22T11:45:24Z")

</div>

that seems to be a 2 year old post… hopefully OP has resolved its issue long ago ! 🙂

As positive contribution, here is a small thing I wrote in the past that maps some keyboard keys to sine waves

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

Minim minim;
AudioOutput soundOut;

final String piano = "qwertyuiop[]"; // mapping those keyboard keys to the frequencies
boolean[] activeKeys; // to prevent auto-repeat to mess with audio output

final float[] frequencies = {
  523.25, // C 
  554.37, // C sharp
  587.33, // D
  622.25, // D sharp
  659.26, // E
  698.46, // F
  739.99, // F sharp
  783.99, // G
  830.61, // G sharp
  880.00, // A
  932.33, // A sharp
  987.77, // B
};
Oscil[] waves;

final int numberOfFrequencies = frequencies.length;

void setup()
{
  size(100, 100);
  minim = new Minim(this);
  soundOut = minim.getLineOut();
  waves = new Oscil[numberOfFrequencies];
  activeKeys = new boolean[numberOfFrequencies];
  for (int i=0; i<numberOfFrequencies; i++) {
    waves[i] = new Oscil( frequencies[i], 0.25f, Waves.SINE ); // create a sine wave, at 0.25 amplitude
    activeKeys[i] = false;
  }
  background(0);
}

void draw() {}

void keyPressed() {
  int selector = piano.indexOf(key);
  if ((selector != -1) && (activeKeys[selector] == false)) {
    waves[selector].patch(soundOut);
    activeKeys[selector] = true;
  }
}

void keyReleased() {
  int selector = piano.indexOf(key);
  if (selector != -1) {
    activeKeys[selector] = false;
    waves[selector].unpatch(soundOut);
  }
}

```

Each character in the `"qwertyuiop[]"` string is mapped to a corresponding frequency. You can press multiple keys at the same time (to get an ugly sound 😇)
