Exemplo n.º 1
0
def rec(configfile='default_setup.ini'):
    """
    Run a basic miditest to see how the CodeKlavier is receiving your midi.

    :param string configfile: Path to the configuration file (default: default_setup.ini)
    """
    #Read config and settings
    config = configparser.ConfigParser()
    config.read(configfile, encoding='utf8')

    try:
        myPort = config['midi'].getint('port')
        device_id = config['midi'].getint('device_id')
    except KeyError:
        raise LookupError('Missing key information in the config file.')

    if (myPort == None or device_id == None):
        raise LookupError('Missing key information in the config file.')

    codeK = Setup()
    codeK.open_port(myPort)
    print('your device id is: ', device_id, '\n')
    print("CodeKlavier is RECORDING. Press Control-C to exit.")
    timestamp = time.strftime("%y-%m-%d")
    ck_deltatime = 0
    per_note = 0
    recfile = open('ml_data/_', 'w')
    headers = 'source_id,midi_note,velocity,ck_deltatime,dif_deltatime'
    recfile.write(headers + '\n')
    try:
        while True:
            msg = codeK.get_message()

            if msg:
                message, deltatime = msg
                ck_deltatime += deltatime
                per_note += deltatime
                if message[0] != 254:
                    if message[0] == device_id:  #note-off hardcoded
                        per_note = 0
                    dif = delta_difference(per_note)
                    midimsg = list(map(str, message))
                    data_line = ','.join(midimsg) + ',' + str(
                        ck_deltatime) + ',' + str(dif) + '\n'
                    clean_line = re.sub(r"\[?\]?", '', data_line)
                    recfile.write(clean_line)
                    print(clean_line)
            #time.sleep(0.01)

    except KeyboardInterrupt:
        print('saving recording...')
    finally:
        recfile.close()
        title = input(
            'Dear CK user, please type a comprehensive title for the recording and press ENTER:'
        )
        usertitle = title + '_' + timestamp + '.csv'
        os.rename("ml_data/_", "ml_data/" + usertitle)
        print("recording saved with title: ", usertitle)
        codeK.end()
Exemplo n.º 2
0
def main(text_id=1, configfile='default_setup.ini'):
    """
    Start the Text Printer/ Typer

    :param string text_id: the text to load.
    :param string configfile: use this configuration file. Defaults to 'default_setup.ini'.
    """

    #Read config and settings
    config = configparser.ConfigParser()
    config.read(configfile, encoding='utf8')

    try:
        myPort = config['midi'].getint('port')
        device_id = config['midi'].getint('device_id')
        pedal_id = config['midi'].getint('pedal_id')
    except KeyError:
        raise LookupError('Missing key information in the config file.')

    if (myPort == None or device_id == None):
        raise LookupError('Missing key information in the config file.')

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

    print("\nCodeKlavier is ready and ON.")
    print("You are performing: Fixed Text Typer")
    print("\nPress Control-C to exit.")

    #codeK.set_callback(TextPrint(myPort, device_id, pedal_id))
    text = TextPrint(myPort, device_id, pedal_id, text_id)

    # Loop to program to keep listening for midi input
    try:
        while True:
            msg = codeK.get_message()

            if msg:
                text.printText(msg)
            time.sleep(0.01)

    except KeyboardInterrupt:
        print('')
    finally:
        print("Bye-Bye :(")
        codeK.end()
Exemplo n.º 3
0
def miditest(configfile='default_setup.ini'):
    """
    Run a basic miditest to see how the CodeKlavier is receiving your midi.

    :param string configfile: Path to the configuration file (default: default_setup.ini)
    """
    #Read config and settings
    config = configparser.ConfigParser()
    config.read(configfile, encoding='utf8')

    try:
        myPort = config['midi'].getint('port')
        device_id = config['midi'].getint('noteon_id')
    except KeyError:
        raise LookupError('Missing key information in the config file.')

    if (myPort == None or device_id == None):
        raise LookupError('Missing key information in the config file.')

    codeK = Setup()
    codeK.open_port(myPort)
    print('your device id is: ', device_id, '\n')
    print("CodeKlavier is ON. Press Control-C to exit.")
    try:
        while True:
            msg = codeK.get_message()

            if msg:
                message, deltatime = msg
                print('deltatime: ', deltatime, 'msg: ', message)

            time.sleep(0.01)

    except KeyboardInterrupt:
        print('')
    finally:
        print("Bye-Bye :(")
        codeK.end()
Exemplo n.º 4
0
def ck_loop(prototype='hello world'):
    """
    the main loop thread for the Codeklavier to listen to incoming midi messages

    :param string prototype: The name of the prototype to process
    """
    global mapping, parameters, conditionalsRange, conditionals, \
           param_interval, threads_are_perpetual, range_trigger, \
           notecounter, hello_world_on, noteCounter, motippets_is_listening, \
           ck_deltatime, ck_deltatime_mem

    codeK_thread = Setup()
    codeK_thread.open_port(myPort)

    print('port', myPort)

    #go to the end of the codespace screen
    mapping.goDown()

    if prototype == 'hello world':
        try:
            while hello_world_on:
                msg = codeK_thread.get_message()

                if msg:
                    message, deltatime = msg

                    if message[0] != 254 and message[0] != 208:
                        if message[2] > 0:  #only noteOn
                            if (message[0] == device_id):
                                notecounter += 1

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

                                    codeK_thread.close_port()

                                    #mapping = Mapping_Motippets()

                                    hello_world_on = False
                                    motippets_is_listening = True
                                    #notecounter = 0

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

                                mapping.mapping(message[1])

                time.sleep(0.01)

        except KeyboardInterrupt:
            print('')
        finally:
            #print('hybrid thread stopped')
            codeK_thread.end()

    elif prototype == 'motippets':
        # reset deltatimes
        ck_deltatime = 0
        ck_deltatime_mem = []

        try:
            while motippets_is_listening:
                msg = codeK_thread.get_message()

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

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

                            ck_deltatime_mem.append(ck_deltatime)

                            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_thread.close_port()

                                #mapping = Mapping_HelloWorld()

                                motippets_is_listening = False
                                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:
            #print('hybrid thread stopped')
            codeK_thread.end()
Exemplo n.º 5
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.º 6
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()
Exemplo n.º 7
0
def main(configfile='default_setup.ini'):
    """Start motippets!

    :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
    #Read config and settings
    config = configparser.ConfigParser(delimiters=(':'), comment_prefixes=('#'))
    config.read(configfile, encoding='utf8')

    try:
        myPort = config['midi'].getint('port')
        device_id = config['midi'].getint('device_id')
    except KeyError:
        raise LookupError('Missing key information in the config file.')

    if (myPort == None or device_id == None):
        raise LookupError('Missing key information in the config file.')

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

    # Use your favourite mapping of the keys
    mapping = Mapping_Motippets()

    print("\nCodeKlavier is ready and ON.")
    print("You are performing: Motippets")
    print("\nPress Control-C to exit.")

    # activesense compensation
    ck_deltatime_mem = []
    ck_deltatime = 0
    ck_deltadif = 0

    # 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)

    #multiprocessing vars
    threads = {}
    notecounter = 0
    range_trigger = 0
    param_interval = 0
    threads_are_perpetual = True

    # MAIN Loop to keep listening for midi input
    #TODO: split this up in smaller functions, so it becomes easier to understand what is going on
    try:
        while threads_are_perpetual:
            msg = codeK.get_message()

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

                if message[0] != 254:

                    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

                            ##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

                            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)

                                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.º 8
0
            print(BColors.WARNING + "  ____   ____   ____  __  __ _ ")
            print(" |  _ \ / __ \ / __ \|  \/  | |")
            print(" | |_) | |  | | |  | | \  / | |")
            print(" |  _ <| |  | | |  | | |\/| | |")
            print(" | |_) | |__| | |__| | |  | |_|")
            print(" |____/ \____/ \____/|_|  |_(_)      THE END ¯\('…')/¯" +
                  BColors.ENDC)
            print("")

        time.sleep(1)


# MAIN Loop to keep listening for midi input
try:
    while threads_are_perpetual:
        msg = codeK.get_message()

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

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

                motif1_played = memMid._motif1_counter
                motif2_played = mainMem._motif2_counter
Exemplo n.º 9
0
    def record(self, framesize=10):
        """
        Run a basic miditest to see how the CodeKlavier is receiving your midi.
    
        :param string configfile: Path to the configuration file (default: default_setup.ini)
        """

        timestamp = time.strftime("%y-%m-%d")
        ck_deltatime = 0
        per_note = 0
        recfile = open('ml_data/_', 'w')
        headers = ''

        #Read config and settings
        config = configparser.ConfigParser()
        config.read(self._configfile, encoding='utf8')

        try:
            myPort = config['midi'].getint('port')
            note_on = config['midi'].getint('noteon_id')
            note_off = config['midi'].getint('noteoff_id')
        except KeyError:
            raise LookupError('Missing key information in the config file.')

        if (myPort == None or note_on == None):
            raise LookupError('Missing key information in the config file.')

        codeK = Setup()
        codeK.open_port(myPort)
        print('your note on id is: ', note_on, '\n')
        print("CodeKlavier is RECORDING. Press Control-C to save and exit.")
        for i in range(0, framesize):
            if framesize == 1:
                index = ''
            else:
                index = str(i)
            headers += 'midi_note' + index + ',velocity' + index + ',duration' + index + ',label' + index

        recfile.write(headers + '\n')
        data_line = ''
        deltatime_mem = {}
        ostinato_length = 9
        note_counter = 1

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

                if msg:
                    message, deltatime = msg
                    ck_deltatime += deltatime
                    per_note += deltatime
                    if message[0] != 254:
                        if message[0] == note_on:
                            deltatime_mem[message[1]] = ck_deltatime
                            velocity = message[2]
                        if message[0] == note_off:

                            if note_counter > 9:
                                note_counter = 1

                            per_note = 0
                            dif = self.delta_difference(per_note)
                            #midimsg = list(map(str, message)) #full msg not needed
                            midinote = message[1]
                            label = note_counter / ostinato_length
                            #label = 0
                            note_duration = ck_deltatime - deltatime_mem.pop(
                                midinote)
                            data_line += str(midinote) + ',' + str(
                                velocity) + ',' + str(
                                    note_duration) + ',' + str(label)
                            data_line += '\n'
                            clean_line = re.sub(r"\[?\]?", '', data_line)
                            print(clean_line)
                            recfile.write(clean_line)
                            data_line = ''
                            note_counter += 1

                time.sleep(0.01)

        except KeyboardInterrupt:
            print('saving recording...')
        finally:
            recfile.close()
            title = input(
                'Dear CK user, please type a comprehensive title for the recording and press ENTER:'
            )
            usertitle = title + '_' + timestamp + '.csv'
            os.rename("ml_data/_", "ml_data/" + usertitle)
            print("recording saved with title: ", usertitle)
            codeK.end()