# Attack, release shapes compressor

**URL:** <https://forum.cabbageaudio.com/t/attack-release-shapes-compressor/3321>\
**Category:** Csound Noobs\
**Created:** [16 July 2022 10:24 UTC](https://forum.cabbageaudio.com/t/attack-release-shapes-compressor/3321 "2022-07-16T10:24:07Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Oeyvind](https://forum.cabbageaudio.com/user_avatar/forum.cabbageaudio.com/oeyvind/32/4681_2.png) [@Oeyvind](https://forum.cabbageaudio.com/u/Oeyvind)\
**Post date:** [20 July 2022 16:03 UTC](https://forum.cabbageaudio.com/t/attack-release-shapes-compressor/3321/6 "2022-07-20T16:03:56Z")

</div>

Here’s a compressor that uses follow2 as the envelope follower. I have not used shaping on the envelope, but IIRC follow2 is roughly exponential. I think you will get most things done with just adjusting the attack and release times.

```
<CsoundSynthesizer>
<CsOptions>
;-odac0 -b1024 -B2048 ; realtime audio out
-ocompressor3.wav ; output to audio file
</CsOptions>
<CsInstruments>

  sr = 48000
  ksmps = 64
  nchnls = 2
  0dbfs = 1

; ***************************************************
;ftables
; ***************************************************

  giSample2 ftgen 0, 0, 0, 1, "MoodySlide2_110.wav", 0, 0, 0

; ***************************************************
; Simple Compressor, ratio expressed in dB,
; lookahead time in milliseconds.
; Using envelope follower instead of simple amplitude tracking, so we can adjust attack and release time separately
; ***************************************************
instr 2

 iamp = ampdbfs(p4) ; Amp in -dB
 kthresh = p5 ; compression threshold
 kratio = p6 ; compression ratio
 kattack = p7 ; attack time
 krelease = p8 ; release time

; audio generator
 aenv linseg 0,0.0001, 1, 1, 1, 0.1, 0.2, 1, 0.2, 1, 0
 a1 loscil iamp, 1, giSample2, 1 ; test sample

; compressor
 arms follow2 a1, kattack/1000, krelease/1000 ; envelope follower
 karms downsamp arms ; workaround for dbfsamp (should take a-rate input)
 arms_dB = upsamp(dbfsamp(karms)) ; convert to dB scale and upsample
 aovershoot = arms_dB - kthresh ; how much over the threshold are we?
 atarget = kthresh + (aovershoot*(1/kratio)) ; target output level (with current input level, threshold and ratio)
 ampMod_dB = atarget - arms_dB ; difference from target = adjust amount
 ampMod_dB limit ampMod_dB, -150, 0 ; do not adjust unless negative
 ampMod = ampdbfs(ampMod_dB) ; convert back to normalized scale
 acomp = a1*ampMod ; apply amplitude modification

; audio out
 outs acomp, acomp
endin
; ***************************************************

</CsInstruments>
<CsScore>

;compression
; start dur amp thresh ratio attack release
i2 0 3 0 -20 8 60 100
i2 ^+4 3 0 -20 8 15 100
i2 ^+4 3 0 -20 8 2 20
i2 ^+4 3 0 -20 8 30 20
i2 ^+4 3 0 -20 8 60 20
</CsScore>
</CsoundSynthesizer>
```

---

_[View the full topic](https://forum.cabbageaudio.com/t/attack-release-shapes-compressor/3321)._
