Exemplo n.º 1
0
def main():
    """Start the Hybrid prototype

    :param string configfile: The configurationfile to use. Defaults to default_setup.ini
    """
    global mapping, parameters, conditionalsRange, conditionals, \
           param_interval, threads_are_perpetual, range_trigger, \
           notecounter, hello_world_on, noteCounter, ck_deltatime, \
           ck_deltatime_mem, codeK, mainMem, memLow, memMid, memHi, \
           tremoloHi, tremoloLow, tremoloMid, motippets_is_listening

    codeK = Setup()
    codeK.print_welcome(27)
    #codeK.open_port(myPort)

    codeK.print_lines(20, 1)
    print("Prototype loaded: Presenter 0.4")
    print("CodeKlavier is ready and LISTENING.")
    codeK.print_lines(20, 1)
    print("\nPress Control-C to exit.\n")

    # default mapping
    mapping = Mapping_Motippets(False, 'snippets Presenter')

    # main memory (i.e. listen to the whole register)
    mainMem = Motippets(mapping, device_id)

    # midi listening per register
    memLow = Motippets(mapping, device_id)
    memMid = Motippets(mapping, device_id)
    memHi = Motippets(mapping, device_id)

    #midi listening for tremolos
    tremoloHi = Motippets(mapping, device_id)
    tremoloMid = Motippets(mapping, device_id)
    tremoloLow = Motippets(mapping, device_id)

    #midi listening for conditionals
    conditionals = {
        1: Motippets(mapping, device_id),
        2: Motippets(mapping, device_id),
        3: Motippets(mapping, device_id)
    }

    conditionalsRange = Motippets(mapping, device_id)
    parameters = Motippets(mapping, device_id)

    try:
        notecounter = 0

        if hello_world_on is True:
            threads['toggle_h'] = Thread(target=ck_loop,
                                         name='ck loop thread',
                                         args=('hello world', ))
            threads['toggle_h'].start()

        if motippets_is_listening is True:
            threads['toggle_m'] = Thread(target=ck_loop,
                                         name='ck loop thread',
                                         args=('motippets', ))
            threads['toggle_m'].start()

        while hybrid_running:
            time.sleep(0.1)  #main loop

    except KeyboardInterrupt:
        print('')
    finally:
        codeK.end()
Exemplo n.º 2
0
def main():
    """Start the Hybrid prototype

    :param string configfile: The configurationfile to use. Defaults to default_setup.ini
    """
    global mapping, parameters, conditionalsRange, conditionals, \
           param_interval, threads_are_perpetual, range_trigger, \
           notecounter, hello_world_on, noteCounter, ck_deltatime, \
           ck_deltatime_mem, codeK, mainMem, memLow, memMid, memHi, \
           tremoloHi, tremoloLow, tremoloMid

    codeK = Setup()
    codeK.print_welcome(27)
    codeK.open_port(myPort)

    codeK.print_lines(20, 1)
    print("Prototype loaded: Hybrid 0.3")
    print("CodeKlavier is ready and LISTENING.")
    codeK.print_lines(20, 1)
    print("\nPress Control-C to exit.\n")

    # default mapping
    mapping = Mapping_Motippets(False)

    # main memory (i.e. listen to the whole register)
    mainMem = Motippets(mapping, device_id)

    # midi listening per register
    memLow = Motippets(mapping, device_id)
    memMid = Motippets(mapping, device_id)
    memHi = Motippets(mapping, device_id)

    #midi listening for tremolos
    tremoloHi = Motippets(mapping, device_id)
    tremoloMid = Motippets(mapping, device_id)
    tremoloLow = Motippets(mapping, device_id)

    #midi listening for conditionals
    conditionals = {
        1: Motippets(mapping, device_id),
        2: Motippets(mapping, device_id),
        3: Motippets(mapping, device_id)
    }

    conditionalsRange = Motippets(mapping, device_id)
    parameters = Motippets(mapping, device_id)

    try:
        while keep_main_alive:
            while motippets_is_listening:
                msg = codeK.get_message()

                if msg:
                    message, deltatime = msg
                    ck_deltatime += deltatime

                    if message[0] != 254:

                        #note offs:
                        #if (message[0] == noteoff_id or message[2] == 0):
                        #ck_deltatime = 0

                        if message[0] == device_id:
                            if message[2] > 0 and message[0] == device_id:
                                notecounter += 1

                                ck_deltatime_mem.append(ck_deltatime)
                                #print('deltatimes before: ', ck_deltatime_mem)

                                if len(ck_deltatime_mem) > 2:
                                    ck_deltatime = 0
                                    ck_deltatime_mem = ck_deltatime_mem[-2:]
                                    ck_deltatime_mem[0] = 0

                                if len(ck_deltatime_mem) == 2:
                                    ck_deltadif = ck_deltatime_mem[
                                        1] - ck_deltatime_mem[0]
                                else:
                                    ck_deltadif = 0

                                if message[1] == 107:
                                    print('toggle prototype -> Hello World')

                                    codeK.close_port()

                                    #mapping = Mapping_HelloWorld()

                                    hello_world_on = True
                                    #notecounter = 0

                                    threads['toggle_h'] = Thread(
                                        target=ck_loop,
                                        name='ck loop thread',
                                        args=('hello world', ))
                                    threads['toggle_h'].start()

                                ##motifs:
                                mainMem.parse_midi(msg, 'full', ck_deltadif)
                                memLow.parse_midi(msg, 'low', ck_deltadif)
                                memMid.parse_midi(msg, 'mid', ck_deltadif)
                                memHi.parse_midi(msg, 'hi', ck_deltadif)

                                motif1_played = memMid._motif1_counter
                                motif2_played = mainMem._motif2_counter

                                minimotif1_low_mapped = memLow._unmapCounter1
                                minimotif2_low_mapped = memLow._unmapCounter2
                                minimotif3_low_mapped = memLow._unmapCounter3

                                minimotif1_mid_mapped = memMid._unmapCounter1
                                minimotif2_mid_mapped = memMid._unmapCounter2
                                minimotif3_mid_mapped = memMid._unmapCounter3_m

                                minimotif1_hi_mapped = memHi._unmapCounter1
                                minimotif2_hi_mapped = memHi._unmapCounter2

                                ##tremolos:
                                if motif1_played > 0 or motif2_played > 0:
                                    if minimotif1_low_mapped > 0:
                                        tremoloLow.parse_midi(
                                            msg, 'tremoloLow', ck_deltadif, 1)
                                    elif minimotif2_low_mapped > 0:
                                        tremoloLow.parse_midi(
                                            msg, 'tremoloLow', ck_deltadif, 2)
                                    elif minimotif3_low_mapped > 0:
                                        tremoloLow.parse_midi(
                                            msg, 'tremoloLow', ck_deltadif, 3)

                                    if minimotif1_mid_mapped > 0:
                                        tremoloMid.parse_midi(
                                            msg, 'tremoloMid', ck_deltadif, 1)
                                    elif minimotif2_mid_mapped > 0:
                                        tremoloMid.parse_midi(
                                            msg, 'tremoloMid', ck_deltadif, 2)
                                    elif minimotif3_mid_mapped > 0:
                                        tremoloMid.parse_midi(
                                            msg, 'tremoloMid', ck_deltadif, 3)

                                    if minimotif1_hi_mapped > 0:
                                        tremoloHi.parse_midi(
                                            msg, 'tremoloHi', ck_deltadif, 1)
                                    elif minimotif2_hi_mapped > 0:
                                        tremoloHi.parse_midi(
                                            msg, 'tremoloHi', ck_deltadif, 2)

                                ##conditionals
                                conditional_value = conditionals[1].parse_midi(
                                    msg, 'conditional 1', ck_deltadif)
                                conditional2_value = conditionals[
                                    2].parse_midi(msg, 'conditional 2',
                                                  ck_deltadif)
                                conditional3_value = conditionals[
                                    3].parse_midi(msg, 'conditional 3',
                                                  ck_deltadif)

                                if isinstance(conditional_value,
                                              int) and conditional_value > 0:
                                    conditional_params = parameters.parse_midi(
                                        msg, 'params', ck_deltadif)

                                    #set the parameter for the timer:
                                    if isinstance(
                                            conditional_params,
                                            int) and conditional_params > 0:
                                        if conditional_value != 4:
                                            threads['set_param'] = Thread(
                                                target=set_parameters,
                                                name='set timer value',
                                                args=(conditional_params,
                                                      'amount'))
                                            threads['set_param'].start()
                                        elif conditional_value == 4:  #gong bomb
                                            threads['set_param'] = Thread(
                                                target=set_parameters,
                                                name='set countdown value',
                                                args=(conditional_params,
                                                      'gomb'))
                                            threads['set_param'].start()

                                    if param_interval > 0:
                                        #if conditional_value != 4:
                                        notecounter = 0  # reset the counter
                                        threads[conditional_value] = Thread(
                                            target=noteCounter,
                                            name=
                                            'conditional note counter thread',
                                            args=(param_interval, 100,
                                                  conditional_value, True))
                                        threads[conditional_value].start()
                                        #elif conditional_value == 4:
                                        #start the countdown
                                        #gomb = Thread(target=gong_bomb, name='gomb', args=(param_interval, True))
                                        #gomb.start()

                                if isinstance(conditional2_value,
                                              int) and conditional2_value > 0:
                                    conditionalsRange._conditionalStatus = conditional2_value
                                    conditional_params = parameters.parse_midi(
                                        msg, 'params', ck_deltadif)

                                    # set range parameter:
                                    if isinstance(
                                            conditional_params,
                                            int) and conditional_params > 0:
                                        if conditional_value != 4:
                                            threads['set_param'] = Thread(
                                                target=set_parameters,
                                                name='set timer value',
                                                args=(conditional_params,
                                                      'range'))
                                            threads['set_param'].start()
                                        elif conditional_value == 4:  #gong bomb
                                            threads['set_param'] = Thread(
                                                target=set_parameters,
                                                name='set countdown value',
                                                args=(conditional_params,
                                                      'gomb'))
                                            threads['set_param'].start()

                                    if param_interval > 0:
                                        threads[conditional2_value] = Thread(
                                            target=rangeCounter,
                                            name='conditional range thread',
                                            args=('random', 'more than', 2,
                                                  conditional2_value,
                                                  param_interval))
                                        threads[conditional2_value].start()

                                if isinstance(conditional3_value,
                                              int) and conditional3_value > 0:
                                    conditionalsRange._conditionalStatus = conditional3_value
                                    conditional_params = parameters.parse_midi(
                                        msg, 'params', ck_deltadif)

                                    # set range parameter:
                                    if isinstance(
                                            conditional_params,
                                            int) and conditional_params > 0:
                                        if conditional_value != 4:
                                            threads['set_param'] = Thread(
                                                target=set_parameters,
                                                name='set timer value',
                                                args=(conditional_params,
                                                      'range'))
                                            threads['set_param'].start()
                                        elif conditional_value == 4:  #gong bomb
                                            threads['set_param'] = Thread(
                                                target=set_parameters,
                                                name='set countdown value',
                                                args=(conditional_params,
                                                      'gomb'))
                                            threads['set_param'].start()

                                    if param_interval > 0:
                                        threads[conditional3_value] = Thread(
                                            target=rangeCounter,
                                            name='conditional range thread',
                                            args=('random', 'less than', 3,
                                                  conditional3_value,
                                                  param_interval))
                                        threads[conditional3_value].start()

                                #range parser
                                if range_trigger == 1:
                                    conditionalsRange.parse_midi(
                                        msg, 'conditional_range')

                time.sleep(0.01)  #check

    except KeyboardInterrupt:
        print('')
    finally:
        codeK.end()
Exemplo n.º 3
0
def main(configfile='default_setup.ini'):
    """
    start the CKalculator
    """
    global ckalculator_listens, ck_deltatime_mem

    config = configparser.ConfigParser(delimiters=(':'),
                                       comment_prefixes=('#'))
    config.read(configfile, encoding='utf8')

    # TODO: optimize...
    try:
        myPort = config['midi'].getint('port')
        noteon_id = config['midi'].getint('noteon_id')
        noteoff_id = config['midi'].getint('noteoff_id')
        pedal_id = config['midi'].getint('pedal_id')
        pedal_sostenuto = config['midi'].getint('pedal_midi_sostenuto')
        staccato = config['articulation'].getfloat('staccato')
        sostenuto = config['articulation'].getfloat('sostenuto')
        chord = config['articulation'].getfloat('chord')
    except KeyError:
        raise LookupError(
            'Missing midi and articulation information in the config file.')

    if (myPort == None or noteon_id == None):
        raise LookupError(
            'Missing port and device id information in the config file.')

    codeK = Setup()
    codeK.print_welcome(27)
    codeK.open_port(myPort)

    codeK.print_lines(20, 1)
    print("Prototype loaded: Ckalculator 0.1")
    print("CodeKlavier is ready and LISTENING.")
    codeK.print_lines(20, 1)
    print("\nPress Control-C to exit.\n")

    cKalc = Ckalculator(noteon_id, noteoff_id, pedal_id)
    per_note = 0
    ck_deltatime = 0
    articulation = {
        'chord': chord,
        'staccato': staccato,
        'sostenuto': sostenuto
    }

    try:
        while ckalculator_listens:
            msg = codeK.get_message()

            if msg:
                message, deltatime = msg
                per_note += deltatime
                ck_deltatime += deltatime
                #print(message)

                if message[0] != 254:

                    #note offs:
                    if (message[0] == noteoff_id
                            or (message[0] == noteon_id and message[2] == 0)):
                        midinote = message[1]
                        print(ck_note_dur)
                        note_duration = ck_deltatime - ck_note_dur.pop(
                            midinote)
                        cKalc.parse_midi(msg,
                                         'full',
                                         ck_deltatime_per_note=note_duration,
                                         ck_deltatime=ck_deltatime,
                                         articulaton=articulation)

                    if message[0] == pedal_id and message[1] == pedal_sostenuto:
                        per_note = 0
                        cKalc.parse_midi(msg,
                                         'full',
                                         ck_deltatime_per_note=0,
                                         ck_deltatime=0,
                                         articulaton=articulation)

                    if message[0] == noteon_id:
                        #per_note = 0
                        ck_note_dur[message[1]] = ck_deltatime
                        if message[2] > 0:
                            dif = delta_difference(ck_deltatime)
                            cKalc.parse_midi(msg,
                                             'full',
                                             ck_deltatime_per_note=per_note,
                                             ck_deltatime=dif,
                                             articulaton=articulation)

            time.sleep(0.01)

    except KeyboardInterrupt:
        print('')
    finally:
        codeK.end()