예제 #1
0
def noise(params, amplitude=1, offset=0):
    '''
    Generate a noise signal
    :param params: buffer parameters, controls length of signal created
    :param amplitude: wave amplitude (array or value)
    :param offset: offset of wave mean from zero (array or value)
    :return: array of resulting signal
    '''
    amplitude = create_buffer(params, amplitude)
    offset = create_buffer(params, offset)
    output = offset + amplitude * (np.random.random(params.length) * 2 - 1)
    return output
예제 #2
0
def square_wave(params, frequency=400, amplitude=1, offset=0, ratio=0.5):
    '''
    Generate a square wave
    :param params: buffer parameters, controls length of signal created
    :param frequency: wave frequency (array or value)
    :param amplitude: wave amplitude (array or value)
    :param offset: offset of wave mean from zero (array or value)
    :param ratio: ratio of high to low time (array or value)
    :return: array of resulting signal
    '''
    frequency = create_buffer(params, frequency)
    amplitude = create_buffer(params, amplitude)
    offset = create_buffer(params, offset)
    ratio = create_buffer(params, ratio)
    phase = np.add.accumulate(frequency / params.sample_rate) % 1
    output = offset + amplitude * np.where(phase < ratio, 1, -1)
    return output
예제 #3
0
def echo(params, source, delay, strength):
    '''
    Create an echo
    :param params:
    :param source:
    :param delay:
    :param strength:
    :return:
    '''
    source = create_buffer(params, source)
    delay = create_buffer(params, delay)
    strength = create_buffer(params, strength)
    output = source[:]
    for i in range(params.length):
        d = int(i - delay[i])
        if 0 <= d < params.length:
            output[i] += source[d]*strength[i]
    return output
예제 #4
0
def saw_wave(params, frequency=400, amplitude=1, offset=0, ratio=0.5):
    '''
    Generate a saw wave
    :param params: buffer parameters, controls length of signal created
    :param frequency: wave frequency (array or value)
    :param amplitude: wave amplitude (array or value)
    :param offset: offset of wave mean from zero (array or value)
    :param ratio: ratio of rise to fall time (array or value)
    :return: array of resulting signal
    '''
    frequency = create_buffer(params, frequency)
    amplitude = create_buffer(params, amplitude)
    offset = create_buffer(params, offset)
    ratio = np.clip(create_buffer(params, ratio), 0.000001, 0.999999)
    phase = np.add.accumulate(frequency / params.sample_rate) % 1
    output = offset + amplitude * np.where(
        phase < ratio, -1 + 2 * phase / ratio, 1 - 2 * (phase - ratio) /
        (1 - ratio))
    return output
예제 #5
0
def table_wave(params, frequency=400, amplitude=1, offset=0, table=None):
    '''
    Generate a wave from a wavetable
    :param params: buffer parameters, controls length of signal created
    :param frequency: wave frequency (array or value)
    :param amplitude: wave amplitude (array or value)
    :param offset: offset of wave mean from zero (array or value)
    :param table: table of values representing one full cycle of the waveform. Nominally the values
     should be between +/-1 (array). The array can be any size.
    :return: array of resulting signal
    '''
    if not table:
        table = create_sine_table(65536)
    size = table.size
    frequency = create_buffer(params, frequency)
    amplitude = create_buffer(params, amplitude)
    offset = create_buffer(params, offset)
    index = np.floor((np.add.accumulate(frequency / params.sample_rate) % 1) *
                     size).astype(int)
    output = offset + amplitude * table[index]
    return output
예제 #6
0
 def __init__(self, params, instrument, step):
     self.params = params
     self.instrument = instrument
     self.step = step
     self.buffer = buffer.create_buffer(params, 0)
     self.pos = 0