def euclidean_rhythm(steps=16, pulses=4, shift=0, length=1, onset=False):
    """euclidean rhythm

    Returns a list with a given number of pulses
    spaced as equally as possible over a given number
    of steps. Based on the paper 'The Euclidean Algorithm
    Generates Traditional Musical Rhythms' by Godfried
    Toussaint (2005). Optionally, it gives the note onsets
    using a bar with duration "length". Rewritten from bjorklund
    function.

    """

    # Create the pattern with pulses (ones) and zeros
    pattern = [[1] for i in range(pulses)]
    suffix = [[0] for i in range(steps - pulses)]

    # Iterate
    while len(suffix) > 1:
        for i, sf in enumerate(suffix):
            if pattern:
                pattern[i % len(pattern)] += sf
            else:
                pattern.append(sf)

        # Reshift
        lsuf = len(suffix)
        lpat = len(pattern)
        suffix = pattern[(lsuf % lpat):]
        pattern = pattern[:(lsuf % lpat)]

    # output flattened list, and shift
    pattern = pattern + suffix
    pattern = [bool(val) for L in pattern for val in L]
    pattern = pattern[-shift:] + pattern[:-shift]

    if onset:
        onsets = [i*(1.0/steps) for i in range(steps) if pattern[i]]
        onsets = [on*length for on in onsets]
        output = (pattern, onsets)
    else:
        output = pattern

    return output