You can start with implementing the root mean square and applying some sort of simple smoothing filter. Augmentations thereafter might be flipping the filter cutoff value depending on whether the envelope is rising or falling. All values could then be mapped through a transfer function to create any attack/release shape you want.
<Cabbage>
form caption("Untitled") size(400, 300), guiMode("queue"), pluginId("def1")
hslider bounds(10,10,300,10), range(0,1,0), channel("RMS")
</Cabbage>
<CsoundSynthesizer>
<CsOptions>
-n -d
</CsOptions>
<CsInstruments>
0dbfs = 1
instr 1
aIn inch 1
aRMS = tone:a(sqrt(aIn^2),2)
cabbageSetValue "RMS", k(aRMS)
outs aIn, aIn
endin
</CsInstruments>
<CsScore>
i 1 0 z
</CsScore>
</CsoundSynthesizer>