Example #1
0
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.WINDOW_SIZE / dsp.randint(1, dsp.WINDOW_SIZE)

    packets = dsp.split(snd, split_size)
    packets = [p*2 for i, p in enumerate(packets) if i % 2]

    out = ''.join(packets)

    if envelope:
        out = dsp.env(out, envelope)

    return out 
Example #2
0
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.WINDOW_SIZE / dsp.randint(1, dsp.WINDOW_SIZE)

    packets = dsp.split(snd, split_size)
    packets = [p * 2 for i, p in enumerate(packets) if i % 2]

    out = ''.join(packets)

    if envelope:
        out = dsp.env(out, envelope)

    return out
Example #3
0
    def play(self, generator, ns, voice_id, voice_index, loop=True):
        sys.path.insert(0, os.getcwd())
        gen = __import__(generator)

        midi_devices = {}

        if hasattr(gen, 'midi'):
            for device, device_id in gen.midi.iteritems():
                dsp.log('\ndevice: %s device_id: %s' % (device, device_id))
                try:
                    midi_devices[device] = MidiManager(device_id, ns)
                    dsp.log('setting midi manager %s' % device_id)
                except:
                    dsp.log('Could not load midi device %s with id %s' % (device, device_id))

        param_manager = ParamManager(ns)

        if audio_engine == 'alsa':
            out = self.open_alsa_pcm(ns.device)
        elif audio_engine == 'portaudio':
            out = self.open_pyaudio_pcm(ns.device)
        else:
            print 'Playback is disabled.'
            return False

        try:
            os.nice(-2)
        except OSError:
            os.nice(0)

        group = None
        if hasattr(gen, 'groups'):
            group = gen.groups[ voice_index % len(gen.groups) ]

        meta = {
            'midi': midi_devices,
            'param': param_manager,
            'id': voice_id,
            'group': group
        }

        if not hasattr(ns, 'reload'):
            setattr(ns, 'reload', False)

        while getattr(ns, '%s-%s-loop' % (generator, voice_id)) == True:
            if getattr(ns, 'reload') == True:
                reload(gen)

            snd = gen.play(meta)
            snd = dsp.split(snd, 500)
            for s in snd:
                try:
                    out.write(s)
                except AttributeError:
                    dsp.log('Could not write to audio device')
                    return False

        return True
Example #4
0
    def play(self, generator, ns, voice_id, voice_index, loop=True):
        sys.path.insert(0, os.getcwd())
        gen = __import__(generator)

        midi_devices = {}

        if hasattr(gen, 'midi'):
            for device, device_id in gen.midi.iteritems():
                dsp.log('\ndevice: %s device_id: %s' % (device, device_id))

                try:
                    pygame.midi.init()
                    device_info = pygame.midi.get_device_info(device_id)
                    device_type = 'input' if device_info[2] else 'output'
                    pygame.midi.quit()
                except:
                    dsp.log('could not read device %s' % device_id)
                    device_type = 'output'

                try:
                    mappings = None
                    if hasattr(gen, 'mappings'):
                        if device in gen.mappings:
                            mappings = gen.mappings[device]

                    midi_devices[device] = MidiManager(device_id, ns, device_type, mappings)
                    dsp.log('setting midi manager %s' % device_id)
                except:
                    dsp.log('Could not load midi device %s with id %s' % (device, device_id))

        param_manager = ParamManager(ns)

        if audio_engine == 'alsa':
            out = self.open_alsa_pcm(ns.device)
        elif audio_engine == 'portaudio':
            out = self.open_pyaudio_pcm(ns.device)
        else:
            print 'Playback is disabled.'
            return False

        try:
            os.nice(-2)
        except OSError:
            os.nice(0)

        group = None
        if hasattr(gen, 'groups'):
            group = gen.groups[ voice_index % len(gen.groups) ]

        if hasattr(gen, 'sbank'):
            sbank = {}
            for snd in gen.sbank:
                sbank[snd[0]] = dsp.read(snd[1]).data
        else:
            sbank = None

        meta = {
            'midi': midi_devices,
            'param': param_manager,
            'id': voice_id,
            'group': group, 
            'sbank': sbank
        }

        if not hasattr(ns, 'reload'):
            setattr(ns, 'reload', False)

        render_process = None

        def render_again(gen, meta, voice_id, ns):
            snd = gen.play(meta)
            setattr(ns, 'buffer-%s' % voice_id, snd)

        iterations = 0
        while getattr(ns, '%s-%s-loop' % (generator, voice_id)) == True:
            meta['iterations'] = iterations
            iterations += 1

            dsp.log('playing %s, id %s, iter %s' % (generator, voice_id, iterations))

            if getattr(ns, 'reload') == True and not hasattr(gen, 'automate'):
                reload(gen)

            # automate will always override play
            if hasattr(gen, 'play') and not hasattr(gen, 'automate'):
                if hasattr(ns, 'buffer-%s' % voice_id):
                    snd = getattr(ns, 'buffer-%s' % voice_id)
                else:
                    # First play render
                    snd = gen.play(meta)

                if render_process is None or not render_process.is_alive():
                    # async start render of next buffer
                    render_process = mp.Process(name='render-%s' % voice_id, target=render_again, args=(gen, meta, voice_id, ns))
                    render_process.start()

                snd = dsp.split(snd, 500)

                # if grid is on, wait for a tick to start playback
                if ns.grid:
                    self.tick.wait()

                for s in snd:
                    try:
                        out.write(s)
                    except AttributeError:
                        dsp.log('Could not write to audio device')
                        return False

            if hasattr(gen, 'automate'):
                gen.automate(meta)

                if hasattr(gen, 'loop_time'):
                    time.sleep(gen.loop_time)

        delattr(ns, 'buffer-%s' % voice_id)

        return True
Example #5
0
    def play(self, generator, ns, voice_id, voice_index, loop=True):
        sys.path.insert(0, os.getcwd())
        gen = __import__(generator)

        midi_devices = {}

        if hasattr(gen, 'midi'):
            for device, device_id in gen.midi.iteritems():
                dsp.log('\ndevice: %s device_id: %s' % (device, device_id))

                try:
                    pygame.midi.init()
                    device_info = pygame.midi.get_device_info(device_id)
                    device_type = 'input' if device_info[2] else 'output'
                    pygame.midi.quit()
                except:
                    dsp.log('could not read device %s' % device_id)
                    device_type = 'output'

                try:
                    mappings = None
                    if hasattr(gen, 'mappings'):
                        if device in gen.mappings:
                            mappings = gen.mappings[device]

                    midi_devices[device] = MidiManager(device_id, ns,
                                                       device_type, mappings)
                    dsp.log('setting midi manager %s' % device_id)
                except:
                    dsp.log('Could not load midi device %s with id %s' %
                            (device, device_id))

        param_manager = ParamManager(ns)

        if audio_engine == 'alsa':
            out = self.open_alsa_pcm(ns.device)
        elif audio_engine == 'portaudio':
            out = self.open_pyaudio_pcm(ns.device)
        else:
            print 'Playback is disabled.'
            return False

        try:
            os.nice(-2)
        except OSError:
            os.nice(0)

        group = None
        if hasattr(gen, 'groups'):
            group = gen.groups[voice_index % len(gen.groups)]

        if hasattr(gen, 'sbank'):
            sbank = {}
            for snd in gen.sbank:
                sbank[snd[0]] = dsp.read(snd[1]).data
        else:
            sbank = None

        meta = {
            'midi': midi_devices,
            'param': param_manager,
            'id': voice_id,
            'group': group,
            'sbank': sbank
        }

        if not hasattr(ns, 'reload'):
            setattr(ns, 'reload', False)

        render_process = None

        def render_again(gen, meta, voice_id, ns):
            snd = gen.play(meta)
            setattr(ns, 'buffer-%s' % voice_id, snd)

        iterations = 0
        while getattr(ns, '%s-%s-loop' % (generator, voice_id)) == True:
            meta['iterations'] = iterations
            iterations += 1

            dsp.log('playing %s, id %s, iter %s' %
                    (generator, voice_id, iterations))

            if getattr(ns, 'reload') == True and not hasattr(gen, 'automate'):
                reload(gen)

            # automate will always override play
            if hasattr(gen, 'play') and not hasattr(gen, 'automate'):
                if hasattr(ns, 'buffer-%s' % voice_id):
                    snd = getattr(ns, 'buffer-%s' % voice_id)
                else:
                    # First play render
                    snd = gen.play(meta)

                if render_process is None or not render_process.is_alive():
                    # async start render of next buffer
                    render_process = mp.Process(name='render-%s' % voice_id,
                                                target=render_again,
                                                args=(gen, meta, voice_id, ns))
                    render_process.start()

                snd = dsp.split(snd, 500)

                # if grid is on, wait for a tick to start playback
                if ns.grid:
                    self.tick.wait()

                for s in snd:
                    try:
                        out.write(s)
                    except AttributeError:
                        dsp.log('Could not write to audio device')
                        return False

            if hasattr(gen, 'automate'):
                gen.automate(meta)

                if hasattr(gen, 'loop_time'):
                    time.sleep(gen.loop_time)

        delattr(ns, 'buffer-%s' % voice_id)

        return True
Example #6
0
def play(args):
    length = dsp.stf(dsp.rand(5, 10))
    volume = 0.2 
    octave = 2 
    note = 'd'
    quality = tune.major
    reps = dsp.randint(3, 11)
    glitch = False
    superglitch = False
    pulse = False
    ratios = tune.terry

    rhodes = dsp.read('sounds/220rhodes.wav').data

    #scale = [1,3,5,4]
    scale = [1,5,8,6]

    for arg in args:
        a = arg.split(':')

        if a[0] == 't':
            length = dsp.stf(float(a[1]))

        if a[0] == 'v':
            volume = float(a[1]) / 100.0

        if a[0] == 'o':
            octave = int(a[1])

        if a[0] == 'n':
            note = a[1]

        if a[0] == 'q':
            if a[1] == 'M':
                quality = tune.major
            elif a[1] == 'm':
                quality = tune.minor
            else:
                quality = tune.major

        if a[0] == 'r':
            reps = int(a[1])

        if a[0] == 'p':
            pulse = True

        if a[0] == 'g':
            glitch = True

        if a[0] == 'gg':
            glitch = True
            superglitch = True

        if a[0] == 'tr':
            if a[1] == 'young':
                ratios = tune.young
            elif a[1] == 'terry':
                ratios = tune.terry

        if a[0] == 's':
            scale = [int(s) for s in a[1].split('.')]

    out = ''
    for i in range(reps):
        freq = tune.step(i, note, octave, scale, quality, ratios)
        diff = freq / 440.0

        n = dsp.transpose(rhodes, diff)
        n = dsp.fill(n, length)

        o = [dsp.tone(length, freq * i * 0.5) for i in range(4)]
        o = [dsp.env(oo) for oo in o]
        o = [dsp.pan(oo, dsp.rand()) for oo in o]
        o = dsp.mix([dsp.amp(oo, dsp.rand(0.05, 0.1)) for oo in o])
        out += dsp.mix([n, o])

    if glitch == True:
        if superglitch == True:
            mlen = dsp.mstf(100)
        else:
            mlen = dsp.flen(out) / 8

        out = dsp.vsplit(out, dsp.mstf(1), mlen)
        out = [dsp.pan(o, dsp.rand()) for o in out]
        out = ''.join(dsp.randshuffle(out))

    if pulse == True:
        plen = dsp.mstf(dsp.rand(80, 500))
        out = dsp.split(out, plen)
        mpul = len(out) / dsp.randint(4, 8)

        out = [dsp.env(o) * mpul for i, o in enumerate(out) if i % mpul == 0]
        opads = dsp.wavetable('sine', len(out), dsp.rand(plen * 0.25, plen))
        out = [dsp.pad(o, 0, int(opads[i])) for i, o in enumerate(out)]
        out = dsp.env(''.join(out))

    return dsp.cache(dsp.amp(out, volume))