def fade_in_out(function, rise_stop, fall_start, fall_length, out):
    """Helper function for generating a signal, that first fades in and then fades out."""
    length = len(out)
    if rise_stop == fall_start:
        if rise_stop == fall_length:
            out[:] = function(length)
        else:
            out[0:rise_stop] = function(2 * rise_stop)[0:rise_stop]
            out[fall_start:] = function(2 * fall_length)[fall_length:]
    else:
        if rise_stop == fall_length:
            fade = function(rise_stop + fall_length)
            fade_in = fade[0:rise_stop]
            fade_out = fade[rise_stop:]
        else:
            fade_in = function(2 * rise_stop)[0:rise_stop]
            fade_out = function(2 * fall_length)[fall_length:]
        if rise_stop <= fall_start:
            out[0:rise_stop] = fade_in
            out[rise_stop:fall_start] = 1.0
            out[fall_start:] = fade_out
        else:
            fall_stop = fall_start + fall_length
            out[0:fall_start] = fade_in[0:fall_start]
            if fall_stop >= len(out):
                overlap_length = rise_stop - fall_start
                out[fall_start:rise_stop] = fade_in[fall_start:rise_stop
                    ] * fade_out[0:overlap_length]
                out[rise_stop:] = fade_out[overlap_length:]
            else:
                out[fall_start:fall_stop] = fade_in[fall_start:fall_stop
                    ] * fade_out
                out[fall_stop:] = 0.0
