In [1]:
%pylab inline
pylab.rcParams['figure.figsize'] = (18, 6)

import numpy as np


Populating the interactive namespace from numpy and matplotlib

In [2]:
sample_rate = 48000
tau = 2.0 * np.pi

In [3]:
def exp_ramp(current, target, attack, linearity):
    flip = False
    if current > target:
        flip = True
        current, target = target, current
    out = current
    out_arr = [out]
    sample_count = 0
    for i in range(0, 500):
        out += attack * ((target/linearity) - out)
        if flip:
            out_arr.append(target - out + current)
        else:
            out_arr.append(out)
        if out > target:
            out = target
        else:
            sample_count += 1
    print("reach %s in %s samples" % (target, sample_count))
    return np.array(out_arr)

In [4]:
plot(exp_ramp(0.0, 1.0, 0.006, 0.63))


reach 1.0 in 165 samples
Out[4]:
[<matplotlib.lines.Line2D at 0x106ef0950>]

In [5]:
plot(exp_ramp(0.0, 1.0, 0.003, 0.63))


reach 1.0 in 330 samples
Out[5]:
[<matplotlib.lines.Line2D at 0x107372cd0>]

In [6]:
plot(exp_ramp(0.0, 1.0, 0.003, 0.7))


reach 1.0 in 400 samples
Out[6]:
[<matplotlib.lines.Line2D at 0x1075a8e90>]

In [7]:
plot(exp_ramp(0.0, 1.0, 0.004, 0.54))


reach 1.0 in 193 samples
Out[7]:
[<matplotlib.lines.Line2D at 0x1077d5510>]

In [8]:
plot(exp_ramp(0.0, 1.0, 0.008, 0.85))


reach 1.0 in 236 samples
Out[8]:
[<matplotlib.lines.Line2D at 0x1079f7ad0>]

In [9]:
plot(exp_ramp(0.0, 1.0, 0.008, 0.85))


reach 1.0 in 236 samples
Out[9]:
[<matplotlib.lines.Line2D at 0x107c17d90>]

In [10]:
plot(exp_ramp(0.5, 1.0, 0.008, 0.85))


reach 1.0 in 167 samples
Out[10]:
[<matplotlib.lines.Line2D at 0x107e48b50>]

In [11]:
plot(exp_ramp(0.0, 0.5, 0.005, 0.5))


reach 0.5 in 138 samples
Out[11]:
[<matplotlib.lines.Line2D at 0x108079110>]

In [12]:
plot(exp_ramp(0.0, 1.0, 0.01, 0.875))


reach 1.0 in 206 samples
Out[12]:
[<matplotlib.lines.Line2D at 0x10829a690>]

In [ ]:


In [13]:
def exp_ramp2(start, end, attack, linearity):
    # always start at 0, so the curve of the ramp stays consistent
    offset = start
    out_mult = end - start
    
    out = 0.0
    out_arr = [out]
    sample_count = 0
    for i in range(0, 500):
        out += attack * ((1.0/linearity) - out)
        out_arr.append(offset + (out_mult * out))
        if out > 1.0:
            out = 1.0
        else:
            sample_count += 1
    print("reach %s in %s samples" % (1.0, sample_count))
    return np.array(out_arr)

In [14]:
plot(exp_ramp2(0.0, 1.0, 0.005, 0.5))


reach 1.0 in 138 samples
Out[14]:
[<matplotlib.lines.Line2D at 0x1084bad10>]

In [15]:
plot(exp_ramp2(0.5, 1.0, 0.005, 0.5))


reach 1.0 in 138 samples
Out[15]:
[<matplotlib.lines.Line2D at 0x1086eb2d0>]

In [16]:
plot(exp_ramp2(1.0, 0.1, 0.005, 0.5))


reach 1.0 in 138 samples
Out[16]:
[<matplotlib.lines.Line2D at 0x10890d810>]

In [17]:
plot(exp_ramp2(1.0, 0.1, 0.0088, 0.88))


reach 1.0 in 239 samples
Out[17]:
[<matplotlib.lines.Line2D at 0x108b30610>]

In [18]:
sample_rate = 44100
attack_ms = 100
attack_samples = sample_rate / 1000.0 * attack_ms
print("attack samples: %s" % attack_samples)


attack samples: 4410.0

In [19]:
def attack_coef_from_ms(linearity_coef, attack_ms, sample_rate):
    attack_samples = sample_rate / 1000.0 * attack_ms
    coef = np.e * linearity_coef * linearity_coef / attack_samples
    return coef
print(attack_coef_from_ms(0.88, 100, 44100))


0.000477332754639

In [20]:
def exp_ramp_ms(start, end, attack_ms, linearity, sample_rate):
    attack_samples = floor(sample_rate / 1000.0 * attack_ms)
    attack_coef = np.e * linearity * linearity / attack_samples
    print("attack coef: %s" % attack_coef)
    # always start at 0, so the curve of the ramp stays consistent
    offset = start
    out_mult = end - start
    
    out = 0.0
    out_arr = [out]
    sample_count = 0
    while 1:
        out += attack_coef * ((1.0/linearity) - out)
        out_arr.append(offset + (out_mult * out))
        if out > 1.0:
            out = 1.0
            break
        else:
            sample_count += 1
    print("reached target in %s samples" % sample_count)
    return np.array(out_arr)

In [21]:
plot(exp_ramp_ms(0.0, 1.0, 100, 0.88, 44100))


attack coef: 0.000477332754639
reached target in 4440 samples
Out[21]:
[<matplotlib.lines.Line2D at 0x108d61350>]

In [22]:
plot(exp_ramp_ms(0.0, 1.0, 30, 0.88, 44100))


attack coef: 0.00159110918213
reached target in 1331 samples
Out[22]:
[<matplotlib.lines.Line2D at 0x108f85390>]

In [23]:
plot(exp_ramp_ms(0.0, 1.0, 30, 0.8, 44100))


attack coef: 0.00131496626622
reached target in 1223 samples
Out[23]:
[<matplotlib.lines.Line2D at 0x1091a4fd0>]

In [24]:
plot(exp_ramp_ms(0.0, 1.0, 30, 0.7, 44100))


attack coef: 0.00100677104758
reached target in 1195 samples
Out[24]:
[<matplotlib.lines.Line2D at 0x1093d45d0>]

In [25]:
def exp_ramp_ms_hard(start, end, attack_ms, linearity, sample_rate):
    attack_samples = int(floor(sample_rate / 1000.0 * attack_ms))
    attack_coef = np.e * linearity * linearity / attack_samples
    print("attack coef: %s" % attack_coef)
    # always start at 0, so the curve of the ramp stays consistent
    offset = start
    out_mult = end - start
    
    out = 0.0
    out_arr = [out]
    sample_count = 0
    for i in range(0, attack_samples):
        out += attack_coef * ((1.0/linearity) - out)
        out_arr.append(offset + (out_mult * out))
        if out > 1.0:
            out = 1.0
            break
        else:
            sample_count += 1
    print("reached %s in %s samples" % (out, sample_count))
    return np.array(out_arr)

In [26]:
plot(exp_ramp_ms_hard(0.0, 1.0, 30, 0.88, 44100))


attack coef: 0.00159110918213
reached 0.998139747745 in 1323 samples
Out[26]:
[<matplotlib.lines.Line2D at 0x109600910>]

In [27]:
plot(exp_ramp_ms_hard(0.0, 1.0, 500, 0.44, 44100))


attack coef: 2.3866637732e-05
reached 0.929983771412 in 22050 samples
Out[27]:
[<matplotlib.lines.Line2D at 0x10985f550>]

In [28]:
plot(exp_ramp_ms_hard(0.0, 1.0, 500, 0.63, 44100))


attack coef: 4.89290729123e-05
reached 1.0 in 20319 samples
Out[28]:
[<matplotlib.lines.Line2D at 0x109b79d50>]

In [31]:
def plot_lin(start, end, time_ms, sample_rate):
    samples = sample_rate / 1000 * time_ms
    increment = float(end - start) / samples
    out = start
    out_arr = [out]
    for i in range(0, samples):
        out += increment
        out_arr.append(out)
    return np.array(out_arr)

In [32]:
plot(exp_ramp_ms_hard(0.0, 1.0, 30, 0.88, 44100))
plot(plot_lin(0.0, 1.0, 30, 44100))


attack coef: 0.00159110918213
reached 0.998139747745 in 1323 samples
Out[32]:
[<matplotlib.lines.Line2D at 0x10960dad0>]

In [33]:
plot(exp_ramp_ms(0.0, 1.0, 30, 0.8, 44100))
plot(plot_lin(0.0, 1.0, 30, 44100))


attack coef: 0.00131496626622
reached target in 1223 samples
Out[33]:
[<matplotlib.lines.Line2D at 0x10a0f7350>]

In [34]:
plot(exp_ramp_ms(0.0, 1.0, 30, 0.63, 44100))
plot(plot_lin(0.0, 1.0, 30, 44100))


attack coef: 0.000815484548538
reached target in 1218 samples
Out[34]:
[<matplotlib.lines.Line2D at 0x10a326710>]

In [35]:
plot(exp_ramp_ms(0.0, 1.0, 100, 0.63, 44100))
plot(plot_lin(0.0, 1.0, 100, 44100))


attack coef: 0.000244645364561
reached target in 4063 samples
Out[35]:
[<matplotlib.lines.Line2D at 0x10a552bd0>]

In [36]:
plot(exp_ramp_ms(0.0, 1.0, 500, 0.63, 44100))
plot(plot_lin(0.0, 1.0, 500, 44100))


attack coef: 4.89290729123e-05
reached target in 20319 samples
Out[36]:
[<matplotlib.lines.Line2D at 0x10a780a90>]

In [37]:
plot(exp_ramp_ms(0.0, 1.0, 500, 0.75, 44100))
plot(plot_lin(0.0, 1.0, 500, 44100))


attack coef: 6.93439241954e-05
reached target in 19990 samples
Out[37]:
[<matplotlib.lines.Line2D at 0x10aad3f10>]

In [38]:
plot(exp_ramp_ms(0.0, 1.0, 500, 0.44, 44100))
plot(plot_lin(0.0, 1.0, 500, 44100))


attack coef: 2.3866637732e-05
reached target in 24293 samples
Out[38]:
[<matplotlib.lines.Line2D at 0x10a5b6ed0>]

In [41]:
plot(exp_ramp_ms(0.0, 1.0, 500, 0.525, 44100))
plot(plot_lin(0.0, 1.0, 500, 44100))


attack coef: 3.39785228557e-05
reached target in 21908 samples
Out[41]:
[<matplotlib.lines.Line2D at 0x10b6fa610>]

In [61]:
plot(exp_ramp_ms(0.0, 1.0, 500, 0.7, 48000))
plot(plot_lin(0.0, 1.0, 500, 48000))


attack coef: 5.54982539977e-05
reached target in 21693 samples
Out[61]:
[<matplotlib.lines.Line2D at 0x10fe8ccd0>]

In [65]:
plot(exp_ramp_ms(0.0, 1.0, 10, 0.55, 48000))
plot(plot_lin(0.0, 1.0, 10, 48000))


attack coef: 0.00171308386064
reached target in 465 samples
Out[65]:
[<matplotlib.lines.Line2D at 0x110cc2cd0>]

In [66]:
plot(exp_ramp_ms(67, 73, 10, 0.55, 48000))
plot(plot_lin(67, 73, 10, 48000))


attack coef: 0.00171308386064
reached target in 465 samples
Out[66]:
[<matplotlib.lines.Line2D at 0x110f09450>]

In [ ]: