We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
As part of a bigger sketch, where some values are affected by incoming MIDI, I want to be able to make an ADSR (Attack, Decay, Sustain, Release) -envelope.
How would you go about this?
Best, hjesper
Answers
Do you just mean using the map() function?
How are you using the ADSR? What does it control?
Hi, thanks for your answer!
It isn't map I am looking for. I know that one well.
I was thinking about how to translate the midi information that goes from 0 to 127 in a millisecond to something that mimics what an ADSR on a synthesizer does.
For example when midi comes in you would let the value gradually increase from 0 to 127 in 5 miliseconds, and then when midi is released the value would decrease to 0 over a period of 50 miliseconds.
It could basically control everything, but I imagine it controlling the size of a shape.
That sounds interesting. I think you're saying to convert incoming MIDI data to a variable which you can then use for other things. You could use either MIDI cc, or note value, and extract this. How far have you got with reading incoming MIDI data? You'll need eg 6N137 optocoupler, and then read from a GPIO pin - once you have the MIDI data it's easy.
Sorry, read this again & worked out what you mean this time. How about this code? On pressing the mouse, value increases linearly to reach sustainLevel after attackTime ms. On releasing, it works out current value in case attack phase not reached maximum, then decays back to zero over decayTime.
that looks perfect!! psyched to try it out! thanks a lot
I tested, and the behaviour of it has got some problems.
If you let "value" decay all the way to its finishing value it doesn't go all the way to 0 again.
If you restart the attack cycle, while "value" is still decaying, it results in jumpy values.
I added a circle here to illustrate the point.
I'll have a play this evening - was going to have another go anyway to try & get it to draw on the screen, as that would show it better. Good point about the attack restarting - would need to copy the valueStartDecay = value; into the attack portion, so it gets the current value when the attack restarts, that's straightforward to do. I might try do a whole ADSR envelope if I get a chance - would you use that?
Thanks! I am also trying to figure it out, but I am mostly just making it stop working. Haha (new to coding)
I am mostly using the attack/sustain/release, so I don't have an urgent need for a full ADSR. But I am definitely curios of how you would do it!
This looks much better. Much more hectic to implement a full ADSR compared to an ASR, so I gave up on that - code gets too complex & much harder to understand. Have tried to comment it properly.
Wow thats perfect!! Very responsive too. Thanks a lot for your help!
My next task would be to put all this in a class (at least I think that is what it is called). So I have the ASR in a different tab, and I can make new instances of it without making the sketch too messy.
Stoked to use this!!
I worked out that night how to add the decay segment in - actually much easier that I thought, as just needs an extra if statement during the attack phase. Classes are trickier, I struggle with the syntax of OOP, but useful if you're making multiple copies. Putting the code into a function is easiest - you just move the relevant code into a function like
and call it from the draw() loop:
updateADSR();
Then the function can stay at the bottom of the sketch & you don't need to look at it again. To have it in a separate tab would mean making a library, & you can then use it in any future sketches. I tend to take the lazy way & copy the code into a function.
Like this. I've put the code into updateADSR() & drawADSR() so it doesn't clutter the main draw() loop. I've added a decay part as well, set decayTime = 0 to not use this. It looks cooler if the colours change each time, and we don't redraw the screen. I'll put any changes in future on github as this post is getting long. https://github.com/fuzzySi/processing/blob/master/ADSR
I'd be really interested to see what you come up with.