function eval_adsr(Ta, Td, Vs, Tr)

% eval_adsr(0.1, 0.2, 0.2, 0.2)

fs = 1000;

a_a = 1/(Ta*fs);
a_d = -1/(Td*fs/log(Vs))
a_d = 5/(Td*fs)
a_r = 5/(Tr*fs)
state = 0;
s = 0;
tol = 1E-4;
target = 1;
n = 0;
p = 0;
k = 1/0.63;
while (1),
    
    if state == 0       % Attack
        s = a_a + (1-a_a)*s;
        if s >= (0.63-tol)
            state = 1
            target = Vs;
        end;
    end;
    if state == 1       % Decay
        s = (1-a_d)*s;
        if s <= (0.63*Vs+tol)
            state = 2
            target = 0;
            p = 0;
        end;
    end;
    if state == 2       % sustain
        s = Vs/k;
        if p >= 1*fs
            state = 3
        end;
        p = p + 1;
    end;
    if state == 3       % Release
        s = (1-a_r)*s;
        if s <= (target+tol)
            break;
        end;
    end;
    n = n + 1;
    y(n) = k*s;
end;

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