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
def alias(snd, passthru=False, envelope='flat', split_size=0): """ A simple time domain bitcrush-like effect. The sound is cut into blocks between 1 and 64 frames in size if split_size is zero, otherwise split_size is the number of frames in each block. Every other block is discarded, and each remaining block is repeated in place. Set passthru to True to return the sound without processing. (Can be useful when processing grains in list comprehensions.) By default, a random amplitude envelope is also applied to the final sound. """ if passthru: return snd if envelope == 'flat': envelope = False elif envelope is None: envelope = 'random' if split_size == 0: split_size = dsp_grain / randint(1, dsp_grain) packets = split(snd, split_size) packets = [p*2 for i, p in enumerate(packets) if i % 2] out = ''.join(packets) if envelope: out = env(out, envelope) return out
def interleave(list_one, list_two): """ Interleave the elements of two lists. :: >>> dsp.interleave([1,1], [0,0]) [1, 0, 1, 0] """ # find the length of the longest list if len(list_one) > len(list_two): big_list = len(list_one) elif len(list_two) > len(list_one): big_list = len(list_two) else: if rand.randint(0, 1) == 0: big_list = len(list_one) else: big_list = len(list_two) combined_lists = [] # loop over it and insert alternating items for index in range(big_list): if index <= len(list_one) - 1: combined_lists.append(list_one[index]) if index <= len(list_two) - 1: combined_lists.append(list_two[index]) return combined_lists
def env( audio_string, wavetype="sine", fullres=False, highval=1.0, lowval=0.0, wtype=0, amp=1.0, phase=0.0, offset=0.0, mult=1.0, ): """ Temp wrapper for new env function \n Purpose and Function of each parameter: * wavetype: Specifies the wavetype that the original audio string is multiplied with * Options: * sine/sine2pi * cos/cos2pi * hann * tri * saw/line * isaw/phasor * vary * impulse * square * random * fullres: Does not currently change the sound * highval: Does not currently change the sound * lowval: Does not currently change the sound * wtype: Sets the wave type (0-8), but it is easier just to use the wavetype parameter * amp: Changes the amplitude of the wave that the audio string is multiplied with * phase: Changes the phase of the wave that the audio string is multiplied with * offset: Offsets the wave that the audio string is multiplied with * mult: A multiplier for the frequency of the wave that the original audio string is multiplied with. """ # Quick and dirty mapping to transition to the new api if wavetype == "sine2pi" or wavetype == "sine": wtype = 0 elif wavetype == "cos2pi" or wavetype == "cos": wtype = 1 elif wavetype == "hann": wtype = 2 elif wavetype == "tri": wtype = 3 elif wavetype == "saw" or wavetype == "line": wtype = 4 elif wavetype == "isaw" or wavetype == "phasor": wtype = 5 elif wavetype == "vary": wtype = 6 elif wavetype == "impulse": wtype = 7 elif wavetype == "square": wtype = 8 elif wavetype == "random": wtype = randint(0, 8) return cenv(audio_string, int(wtype), float(amp), float(phase), float(offset), float(mult))
def env(audio_string, wavetype="sine", fullres=False, highval=1.0, lowval=0.0, wtype=0, amp=1.0, phase=0.0, offset=0.0, mult=1.0): """ Temp wrapper for new env function \n Purpose and Function of each parameter: * wavetype: Specifies the wavetype that the original audio string is multiplied with * Options: * sine/sine2pi * cos/cos2pi * hann * tri * saw/line * isaw/phasor * vary * impulse * square * random * fullres: Does not currently change the sound * highval: Does not currently change the sound * lowval: Does not currently change the sound * wtype: Sets the wave type (0-8), but it is easier just to use the wavetype parameter * amp: Changes the amplitude of the wave that the audio string is multiplied with * phase: Changes the phase of the wave that the audio string is multiplied with * offset: Offsets the wave that the audio string is multiplied with * mult: A multiplier for the frequency of the wave that the original audio string is multiplied with. """ # Quick and dirty mapping to transition to the new api if wavetype == 'sine2pi' or wavetype == 'sine': wtype = 0 elif wavetype == 'cos2pi' or wavetype == 'cos': wtype = 1 elif wavetype == 'hann': wtype = 2 elif wavetype == 'tri': wtype = 3 elif wavetype == 'saw' or wavetype == 'line': wtype = 4 elif wavetype == 'isaw' or wavetype == 'phasor': wtype = 5 elif wavetype == 'vary': wtype = 6 elif wavetype == 'impulse': wtype = 7 elif wavetype == 'square': wtype = 8 elif wavetype == 'random': wtype = randint(0, 8) return cenv(audio_string, int(wtype), float(amp), float(phase), float(offset), float(mult))
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
def rotate(items, start=0, vary=False): """ Rotate a list by a given (optionally variable) offset :: >>> dsp.rotate(range(10), 3) [3, 4, 5, 6, 7, 8, 9, 0, 1, 2] >>> dsp.rotate(range(10), vary=True) [6, 7, 8, 9, 0, 1, 2, 3, 4, 5] >>> dsp.rotate(range(10), vary=True) [8, 9, 0, 1, 2, 3, 4, 5, 6, 7] """ if vary == True: start = rand.randint(0, len(items)) return items[-start % len(items):] + items[:-start % len(items)]
def wavetable(wtype="sine", size=512, highval=1.0, lowval=0.0): """ The end is near. That'll do, wavetable() """ wtable = [] wave_types = [ "sine", "gauss", "cos", "line", "saw", "impulse", "phasor", "sine2pi", "cos2pi", "vary", "flat" ] if wtype == "random": wtype = wave_types[int(rand(0, len(wave_types) - 1))] if wtype == "sine": wtable = [ math.sin(i * math.pi) * (highval - lowval) + lowval for i in frange(size, 1.0, 0.0) ] elif wtype == "hann" or wtype == "hanning": wtable = [ 0.5 * (1 - math.cos((2 * math.pi * i) / (size - 1))) for i in range(size) ] elif wtype == "gauss": def gauss(x): # From: http://johndcook.com/python_phi.html # Prolly doing it wrong! a1 = 0.254829592 a2 = -0.284496736 a3 = 1.421413741 a4 = -1.453152027 a5 = 1.061405429 p = 0.3275911 sign = 1 if x < 0: sign = -1 x = abs(x) / math.sqrt(2.0) t = 1.0 / (1.0 + p * x) y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * math.exp(-x * x) return abs(abs(sign * y) - 1.0) wtable = [ gauss(i) * (highval - lowval) + lowval for i in frange(size, 2.0, -2.0) ] elif wtype == "sine2pi": wtable = [ math.sin(i * math.pi * 2) * (highval - lowval) + lowval for i in frange(size, 1.0, 0.0) ] elif wtype == "cos2pi": wtable = [ math.cos(i * math.pi * 2) * (highval - lowval) + lowval for i in frange(size, 1.0, 0.0) ] elif wtype == "cos": wtable = [ math.cos(i * math.pi) * (highval - lowval) + lowval for i in frange(size, 1.0, 0.0) ] elif wtype == "itri": # Inverted triangle wtable = [ math.fabs(i) for i in frange(size, highval, lowval - highval) ] # Only really a triangle wave when centered on zero elif wtype == "tri": wtable = [(2.0 / (size + 1)) * ((size + 1) / 2.0 - math.fabs(i - ((size - 1) / 2.0))) for i in range(size)] elif wtype == "saw" or wtype == "line": wtable = [i for i in frange(size, highval, lowval)] elif wtype == "phasor": wtable = wavetable("line", size, highval, lowval) list.reverse(wtable) elif wtype == "impulse": wtable = [float(randint(-1, 1)) for i in range(size / randint(2, 12))] wtable.extend([0.0 for i in range(size - len(wtable))]) elif wtype == "vary": if size < 32: bsize = size else: bsize = size / int(rand(2, 16)) btable = [[ wave_types[int(rand(0, len(wave_types) - 1))], rand(lowval, highval) ] for i in range(bsize)] if len(btable) > 0: btable[0] = lowval else: btable = [lowval] wtable = breakpoint(btable, size) elif wtype == "flat": wtable = [highval for i in range(size)] return wtable
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)
def noise(length): return ''.join([ byte_string(randint(-32768, 32767)) for i in range(length * audio_params[0]) ])
def wavetable(wtype="sine", size=512, highval=1.0, lowval=0.0): """ The end is near. That'll do, wavetable() """ wtable = [] wave_types = ["sine", "gauss", "cos", "line", "saw", "impulse", "phasor", "sine2pi", "cos2pi", "vary", "flat"] if wtype == "random": wtype = wave_types[int(rand(0, len(wave_types) - 1))] if wtype == "sine": wtable = [math.sin(i * math.pi) * (highval - lowval) + lowval for i in frange(size, 1.0, 0.0)] elif wtype == "hann" or wtype == "hanning": wtable = [ 0.5 * ( 1 - math.cos((2 * math.pi * i) / (size - 1))) for i in range(size) ] elif wtype == "gauss": def gauss(x): # From: http://johndcook.com/python_phi.html # Prolly doing it wrong! a1 = 0.254829592 a2 = -0.284496736 a3 = 1.421413741 a4 = -1.453152027 a5 = 1.061405429 p = 0.3275911 sign = 1 if x < 0: sign = -1 x = abs(x)/math.sqrt(2.0) t = 1.0/(1.0 + p * x) y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * math.exp(-x * x) return abs(abs(sign * y) - 1.0) wtable = [gauss(i) * (highval - lowval) + lowval for i in frange(size, 2.0, -2.0)] elif wtype == "sine2pi": wtable = [math.sin(i * math.pi * 2) * (highval - lowval) + lowval for i in frange(size, 1.0, 0.0)] elif wtype == "cos2pi": wtable = [math.cos(i * math.pi * 2) * (highval - lowval) + lowval for i in frange(size, 1.0, 0.0)] elif wtype == "cos": wtable = [math.cos(i * math.pi) * (highval - lowval) + lowval for i in frange(size, 1.0, 0.0)] elif wtype == "itri": # Inverted triangle wtable = [math.fabs(i) for i in frange(size, highval, lowval - highval)] # Only really a triangle wave when centered on zero elif wtype == "tri": wtable = [ (2.0 / (size + 1)) * ((size + 1) / 2.0 - math.fabs(i - ((size - 1) / 2.0))) for i in range(size) ] elif wtype == "saw" or wtype == "line": wtable = [i for i in frange(size, highval, lowval)] elif wtype == "phasor": wtable = wavetable("line", size, highval, lowval) list.reverse(wtable) elif wtype == "impulse": wtable = [float(randint(-1, 1)) for i in range(size / randint(2, 12))] wtable.extend([0.0 for i in range(size - len(wtable))]) elif wtype == "vary": if size < 32: bsize = size else: bsize = size / int(rand(2, 16)) btable = [ [wave_types[int(rand(0, len(wave_types)-1))], rand(lowval, highval)] for i in range(bsize) ] if len(btable) > 0: btable[0] = lowval else: btable = [lowval] wtable = breakpoint(btable, size) elif wtype == "flat": wtable = [highval for i in range(size)] return wtable
def noise(length): return ''.join([byte_string(randint(-32768, 32767)) for i in range(length * audio_params[0])])