function eval_adsr(Ta, Td, Vs, Tr)

% eval_adsr(0.1, 0.2, 0.2, 0.2)

fs = 10000;

aa = 1/(Ta*fs)
ba = (1-aa)
a_d = 5/(Td*fs)
a_r = 5/(Tr*fs)
state = 0;
s = 0;
tol = 1E-4;
ke = 1-exp(-1)
n = 0;
p = 0;
k = 1/ke;
kk = 0;
s
while (1),
    
    if state == 0
        s = aa + ba*s;
        if s >= (ke-tol)
            state = 1;
            k = (1-Vs)/ke;
            kk = Vs;
        end;
    end;
    if state == 1
        s = (1-a_d)*s;
        if s <= (tol)
            state = 2;
            s = 1;
            k = Vs;
            kk = 0;
            p = 0;
        end;
    end;
    if state == 2
        if p >= 0.1*fs
            state = 3;
        end;
        p = p + 1;
    end;
    if state == 3
        s = (1-a_r)*s;
        if s <= (tol)
            break;
        end;
    end;
    n = n + 1;
    y(n) = k*s + kk;
end;

plot((0:n-1)/fs, 0.8-y); grid;