Attack, release shapes compressor

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>
2 Likes