Example #1
0
def adsr(snd, a=10, d=50, s=1.0, r=100):
    sndlen = flen(snd)
    attack = mstf(a)
    decay = mstf(d)
    sustain_to = s
    release = mstf(r)

    if attack + decay + release > sndlen:
        sustain_length = sndlen - (attack + decay + release)
    else:
        sustain_length = 0
        decay = sndlen - (attack + release)

    if attack + release > sndlen:
        attack = sndlen / 2
        release = sndlen / 2
        decay = 0

    out = env(cut(snd, 0, attack), 'line')
   
    if decay > 0:
        decay = cut(snd, flen(out), decay)
        if sustain_to < 1 and sustain_length > 0:
            decaytable = wavetable('phasor', 1024, 1, sustain_to)
            out += benv(decay, decaytable)
            out += amp(cut(snd, flen(out), sustain_length), sustain_to)

    out += env(cut(snd, flen(out), release), 'phasor')

    return out
Example #2
0
def stretch(snd, length=None, speed=None, grain_size=20):
    """ Crude granular time stretching and pitch shifting """

    original_length = flen(snd)

    if speed is not None:
        snd = transpose(snd, speed)

    current_length = flen(snd)

    if original_length != current_length or length is not None:
        grain_size = mstf(grain_size)
        numgrains = length / (grain_size / 2)
        block_size = current_length / numgrains

        grains = []
        original_position = 0
        count = 0

        while count <= numgrains:
            grain = cut(snd, original_position, grain_size)

            grains += [ grain ]

            original_position += block_size
            count += 1

        snd = cross(grains, ftms(grain_size / 2))

    return snd
Example #3
0
def adsr(snd, a=10, d=50, s=1.0, r=100):
    sndlen = flen(snd)
    attack = mstf(a)
    decay = mstf(d)
    sustain_to = s
    release = mstf(r)

    if attack + decay + release > sndlen:
        sustain_length = sndlen - (attack + decay + release)
    else:
        sustain_length = 0
        decay = sndlen - (attack + release)

    if attack + release > sndlen:
        attack = sndlen / 2
        release = sndlen / 2
        decay = 0

    out = env(cut(snd, 0, attack), 'line')

    if decay > 0:
        decay = cut(snd, flen(out), decay)
        if sustain_to < 1 and sustain_length > 0:
            decaytable = wavetable('phasor', 1024, 1, sustain_to)
            out += benv(decay, decaytable)
            out += amp(cut(snd, flen(out), sustain_length), sustain_to)

    out += env(cut(snd, flen(out), release), 'phasor')

    return out
Example #4
0
def stretch(snd, length=None, speed=None, grain_size=20):
    """ Crude granular time stretching and pitch shifting """

    original_length = flen(snd)

    if speed is not None:
        snd = transpose(snd, speed)

    current_length = flen(snd)

    if original_length != current_length or length is not None:
        grain_size = mstf(grain_size)

        numgrains = length / (grain_size / 2)
        numgrains = numgrains if numgrains > 0 else 1

        block_size = current_length / numgrains

        grains = []
        original_position = 0
        count = 0

        while count <= numgrains:
            grain = cut(snd, original_position, grain_size)

            grains += [ grain ]

            original_position += block_size
            count += 1

        snd = cross(grains, ftms(grain_size / 2))

    return snd
Example #5
0
def fnoise(sound, coverage):
    target_frames = int(flen(sound) * coverage)

    for i in range(target_frames):
        p = randint(0, flen(sound) - 1)
        f = cut(sound, p, 1)
        sound = replace_into(sound, f, randint(0, flen(sound) - 1))

    return sound
Example #6
0
def fnoise(sound, coverage):
    target_frames = int(flen(sound) * coverage)

    for i in range(target_frames):
        p = randint(0, flen(sound) - 1)
        f = cut(sound, p, 1)
        sound = replace_into(sound, f, randint(0, flen(sound) - 1))

    return sound
Example #7
0
def taper(snd, amount=441):
    first = cut(snd, 0, amount)
    first = env(first, "line")

    middle = cut(snd, amount, flen(snd) - (amount * 2))

    last = cut(snd, flen(first) + flen(middle), amount)
    last = env(last, "phasor")

    return "%s%s%s" % (first, middle, last)
Example #8
0
def taper(snd, amount=441):
    first = cut(snd, 0, amount)
    first = env(first, 'line')

    middle = cut(snd, amount, flen(snd) - (amount * 2))

    last = cut(snd, flen(first) + flen(middle), amount)
    last = env(last, 'phasor')

    return '%s%s%s' % (first, middle, last)
Example #9
0
def taper(snd, amount=441):
    first = cut(snd, 0, amount)
    first = env(first, 'line')

    middle = cut(snd, amount, flen(snd) - (amount * 2))

    last = cut(snd, flen(first) + flen(middle), amount)
    last = env(last, 'phasor')

    return '%s%s%s' % (first, middle, last)
Example #10
0
def vsplit(input, minsize, maxsize):
    # min/max size is in frames...
    output = []
    pos = 0

    for chunk in range(flen(input) / minsize):
        chunksize = randint(minsize, maxsize)
        if pos + chunksize < flen(input) - chunksize:
            output.append(cut(input, pos, chunksize))
            pos += chunksize

    return output
Example #11
0
def vsplit(input, minsize, maxsize):
    # min/max size is in frames...
    output = []
    pos = 0

    for chunk in range(flen(input) / minsize):
        chunksize = randint(minsize, maxsize)
        if pos + chunksize < flen(input) - chunksize:
            output.append(cut(input, pos, chunksize))
            pos += chunksize

    return output
Example #12
0
def fill(string, length, chans=2, silence=False):
    if flen(string) < length:
        if silence == False:
            try:
                repeats = length / flen(string) + 1
            except ZeroDivisionError:
                return string
        else:
            return pad(string, 0, length - flen(string))

        string = string * repeats

    return cut(string, 0, length)
Example #13
0
def fill(string, length, chans=2, silence=False):
    if flen(string) < length:
        if silence == False:
            try:
                repeats = length / flen(string) + 1
            except ZeroDivisionError:
                return string
        else:
            return pad(string, 0, length - flen(string))

        string = string * repeats

    return cut(string, 0, length)
Example #14
0
def crosstwo(a, b, length):
    length = mstf(length)

    out = ''

    out += cut(a, 0, flen(a) - length)

    fadeout = cut(a, flen(a) - length, length)
    fadeout = env(fadeout, 'phasor')

    fadein = cut(b, 0, length)
    fadein = env(fadein, 'line')

    out += mix([ fadein, fadeout ])

    out += cut(b, length, flen(b) - length)

    return out
Example #15
0
def crosstwo(a, b, length):
    length = mstf(length)

    out = ""

    out += cut(a, 0, flen(a) - length)

    fadeout = cut(a, flen(a) - length, length)
    fadeout = env(fadeout, "phasor")

    fadein = cut(b, 0, length)
    fadein = env(fadein, "line")

    out += mix([fadein, fadeout])

    out += cut(b, length, flen(b) - length)

    return out
Example #16
0
def crosstwo(a, b, length):
    length = mstf(length)

    out = ''

    out += cut(a, 0, flen(a) - length)

    fadeout = cut(a, flen(a) - length, length)
    fadeout = env(fadeout, 'phasor')

    fadein = cut(b, 0, length)
    fadein = env(fadein, 'line')

    out += mix([fadein, fadeout])

    out += cut(b, length, flen(b) - length)

    return out
Example #17
0
 def __len__(self):
     return flen(self.data)
Example #18
0
def insert_into(haystack, needle, position):
    # split string at position index
    hay = cut(haystack, 0, position)
    stack = cut(haystack, position, flen(haystack) - position)
    return "%s%s%s" % (hay, needle, stack)
Example #19
0
def replace_into(haystack, needle, position):
    hayend = position * audio_params[1] * audio_params[0]
    stackstart = hayend - (flen(needle) * audio_params[1] * audio_params[0])
    return "%s%s%s" % (haystack[:hayend], needle, haystack[stackstart:])
Example #20
0
def rcut(snd, length):
    """ Cut a segment of a given length from a random position in the given sound. """
    return cut(snd, randint(0, flen(snd) - length), length)
Example #21
0
 def __len__(self):
     return flen(self.data)
Example #22
0
def rcut(snd, length):
    """ Cut a segment of a given length from a random position in the given sound. """
    return cut(snd, randint(0, flen(snd) - length), length)
Example #23
0
def replace_into(haystack, needle, position):
    hayend = position * audio_params[1] * audio_params[0]
    stackstart = hayend - (flen(needle) * audio_params[1] * audio_params[0])
    return "%s%s%s" % (haystack[:hayend], needle, haystack[stackstart:])
Example #24
0
def insert_into(haystack, needle, position):
    # split string at position index
    hay = cut(haystack, 0, position)
    stack = cut(haystack, position, flen(haystack) - position)
    return "%s%s%s" % (hay, needle, stack)