# ADSR and playNote with Minim

**URL:** https://discourse.processing.org/t/adsr-and-playnote-with-minim/16057
**Category:** Libraries
**Created:** [December 2, 2019, 7:40pm UTC](https://discourse.processing.org/t/adsr-and-playnote-with-minim/16057 "2019-12-02T19:40:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![flariut](https://avatars.discourse-cdn.com/v4/letter/f/b5a626/32.png) [@flariut](https://discourse.processing.org/u/flariut)
#### Post date: [December 2, 2019, 7:40pm UTC](https://discourse.processing.org/t/adsr-and-playnote-with-minim/16057/1 "2019-12-02T19:40:10Z")

</div>

Hi, I’m having troubles figuring out how to do something with minim.  
In the ADSR example, we have this:

```auto
    // new ADSR(amp, atk, sus, dec, rel)
    adsr = new ADSR( 0.5, 0.01, 0.05, 0.5, 0.5 );

```

However, to call the Instrument we have this:

```auto
    // playNote(init time, duration in beats, instrument)
    out.playNote( 1.25 + i*2.0, 0.3, new ToneInstrument( 75, 0.49 ) );

```

I find out that the “duration” in the playNote is affecting the ADSR! I don’t wan’t this, I just wan’t it to play the wave and keep the values of the ADSR, how can I achieve this? I’ve tried calculating the duration on the note based on atk, dec, sus, rel but still seems that it’s not the way it should be. How can I play the note without a prefixed duration?

Also, I’ve found out that if I don’t create the Instrument right inside of playNote (for example, creating the instrument inside and array and then calling it), bizarre behaviour occurs, it is mandatory to only make the new instance of instruments inside the playNote?

Thanks.

---

<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: [December 3, 2019, 3:51am UTC](https://discourse.processing.org/t/adsr-and-playnote-with-minim/16057/2 "2019-12-03T03:51:02Z")

</div>

hi, a very tricky question,  
and if you put some more effort in it  
to give us something ready to play with, to understand,  
would have been appreciated

so i start from minim ADSR example ( possibly like you )  
but work it over and check documentation  
and replaced the bunch of numbers with readable variables.

```auto
/* MINIM ADSRExample */
// http://code.compartmental.net/minim/adsr_class_adsr.html
// http://code.compartmental.net/minim-beta/documentation/audiooutput_method_playnote.htm
// https://discourse.processing.org/t/adsr-and-playnote-with-minim/16057

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

Minim minim;
AudioOutput out;

class ToneInstrument implements Instrument {
  Oscil sineOsc;
  ADSR adsr;
  float maxAmp= 0.5, attTime=0.01, decTime=0.05, susLvl = 0.01/*0.5*/, relTime = 0.5;  
  ToneInstrument( float frequency, float amplitude ) {    
    sineOsc = new Oscil( frequency, amplitude, Waves.SINE );
    adsr = new ADSR( maxAmp, attTime, decTime, susLvl, relTime );
    sineOsc.patch( adsr );
  }

  void noteOn( float dur ) {
    adsr.noteOn();
    adsr.patch( out );
   }
  void noteOff() {
    adsr.unpatchAfterRelease( out );
    adsr.noteOff();
  }
}

void setup() {
  size( 512, 200, P2D );
  minim = new Minim( this );
  out = minim.getLineOut( Minim.MONO, 2048 );
  out.pauseNotes();
  println("use key []");
}

void draw() {
  background( 0 );
  plot();
}

void keyPressed() {
  if ( key == ' ' ) play4( 440 );  
}

void play4(float hz) {
  println("play 4 notes "+hz+" hz");
  float amp = 0.5;
  float startTime=1, beat=2, duration=1; //sec
  for( int i = 0; i < 4; i++ )
    out.playNote( startTime + i*beat, duration, new ToneInstrument( hz, amp ) );
  out.resumeNotes();
}

void plot() {
  stroke( 255 );
  for( int i = 0; i < out.bufferSize() - 1; i++ ) {
    float x1 = map( i, 0, out.bufferSize(), 0, width );
    float x2 = map( i+1, 0, out.bufferSize(), 0, width );
    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);
  }  
}

```

yes, you are right, it is not possible to use the

```auto
playNote(startTime, duration, instrument);

```

without duration, so it is not meant to play ADSR as is.

so you find a other way to play the instrument and make your own timing.

* * *

still i was able to betray that whole construct,  
when, like in my above code, use

```auto
susLvl = 0.01/*0.5*/

```

make the Sustain tone low its  
overwritten duration does not matter, works like a pause.

just a hack…

---

<div class="post-metadata">

### Author: ![Alpaxon](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@Alpaxon](https://discourse.processing.org/u/Alpaxon)
#### Post date: [June 10, 2021, 12:13am UTC](https://discourse.processing.org/t/adsr-and-playnote-with-minim/16057/4 "2021-06-10T00:13:55Z")

</div>

Really - we only want to use the oscillators for starting a note within a given _keyPressed_ procedure/event, then listening it at its sustain level, and finally stop it from a corresponding _keyReleased_ event procedure… as it is technically known for a ADSR envelope.

---

<div class="post-metadata">

### Author: ![Alpaxon](https://avatars.discourse-cdn.com/v4/letter/a/3e96dc/32.png) [@Alpaxon](https://discourse.processing.org/u/Alpaxon)
#### Post date: [October 7, 2021, 3:46pm UTC](https://discourse.processing.org/t/adsr-and-playnote-with-minim/16057/5 "2021-10-07T15:46:29Z")

</div>

In this regard, the fixed time “susLvl” is a problem, because the note duration isn’t known at a time when you start pressing a piano key… That’s why we cannot use this library for creating a MIDI instrument.
