View Single Post
Old 04-29-2019, 01:06 PM   #4
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 8,056
Default

Here you are.
Additionally the program selects one of four commands (e.g. batch files) to be started depending on the active USB Midi interfaces.

(Please pardon my programming style, as this is my first Python program ever.)

Code:
'''
@author: mschnell
'''

# import winreg
#import pygame
import pygame.midi
#import re
import os
import configparser
from pip._vendor.appdirs import unicode
            
def noprefix(n) :
    p = True
    prefix = n[0:3]
    if not (prefix[1:] == "- ") :
#        print("'"+prefix[1:3]+"'")
        p = False
    else :
        pp = ord(prefix[0])
        if (pp<ord("1")) or (pp >ord("9")) :
            p = False
    if p :
        nn = n[3:]
    else :
        nn = n
        prefix = ""
    return (nn, prefix)
        
        
def searchdevice(a, n, prefix, io):    
    i1 = ""
    o1 = ""
    if not prefix :
        [n, ap] = noprefix(n)        
    for x in a :
        if (x.find("in") >= 0) or (x.find("on") >= 0) :
            if not prefix :
                [an, ap] = noprefix(a[x])
            else:
                an = a[x]
            if  an ==  n :
                z = x[0:1]
                if z ==  "i" :
                    if not io :
                        i1 = x #int(y)
                        break
                else :
                    if io :
                        o1 = x #int(y)
                        break       
    return (i1, o1)

def swap1 (c, a1, ai, c2):
    a1 = a1[0]+c2+a1[2:]
    ai = ai[0]+c2+ai[2:]
#    print("swap ", a1, ai)
    x = c[a1]
    c[a1] = c[ai]
    c[ai] = x;
    return    

def swapdevices (c, x1, xi):
    if x1 != xi :
        s1 = x1[0]+"."+x1[2:];
        si = xi[0]+"."+xi[2:];
        print("   Device", '"'+c[xi]+'": swap Reaper ID', si, "to", s1)  
        swap1(c, x1, xi, "n")
        swap1(c, x1, xi, "a")
        swap1(c, x1, xi, "t")
#    else :
#        print("identical ", x1, xi)        
    return

if __name__ == '__main__':

    command_00 = "batch00"
    command_01 = "batch01"
    command_10 = "batch10"
    command_11 = "batch11"
    device_1  = "device1"  
    device_2  = "device2"  

    current = os.path.dirname(os.path.abspath(__file__))
#    print("current :", current)
    os.chdir(current)

    config = configparser.ConfigParser()
    config.read("start.ini")
    configmidi  = config["MIDI"]
    compare1 = configmidi[device_1]
    compare2 = configmidi[device_2]
    command = [configmidi[command_00],
               configmidi[command_01],
               configmidi[command_10],
               configmidi[command_11]]
#    print("compare =", compare)
#    confid = configmidi["ID"]
#    print("ID =", confid)

    midi = pygame.midi
    midi.init()
    devicecount = midi.get_count()
#    print("devicecount =", devicecount)
    i = 0
    found = 0
    devicelist = []
    while i < devicecount:
        info = midi.get_device_info(i)
        devicename = info[1]
        i_o = info[3]
        devicename = unicode(devicename, "utf-8")
        devicelist.append([devicename, i_o])
        if i_o != 0 :
            i_o_ = "out"
        else :
            i_o_ = " in"            
        print ("active", i_o_, "Device:", '"'+devicename+'"')
        n = devicename.find(compare1)
        if n>= 0:        
            print("   =", device_1)
            found |= 1        
#            found_id = i;    
        n = devicename.find(compare2)
        if n>= 0:        
            print("   =", device_2)
            found |= 2        
#            found_id = i;    
        i += 1
        
    midihw    = config["REAPER"]["midihwtemplate"]
    midihwneu = config["REAPER"]["midihw"]
    inisection = "mididevcache"

    config = configparser.ConfigParser()    
    config.read(midihw)
    cis = config[inisection]
    [i1, o1] = searchdevice(cis, compare1, False, False)
#    print("devicelist:", devicelist)
    for dev in devicelist :
        dev_n = dev[0]
        dev_o = dev[1] != 0;
        if dev_o :
            dev_o_ = "out"
        else:
            dev_o_ = " in"
        print(dev_o_, 'Device "'+dev_n+'" :')
        [i1, o1] = searchdevice(cis, dev_n, False, dev_o)
#        if i1 != "" :
#            print(" in: 1st Reaper ID =", i1[0]+"."+i1[2:], ":",  '"'+cis[i1]+'"')
#        if o1 != "" :
#            print("out: 1st Reaper ID =", o1[0]+"."+o1[2:], ":", '"'+cis[o1]+'"')
        if (i1 != "") or (o1 != "") :
            print("   found in", midihw)
        
        if (i1 != "") & (not dev_o) :
            [ii, oi] = searchdevice(cis, dev_n, True, dev_o)
            if ii != "" :
                swapdevices(cis, i1, ii)
        if (o1 != "") & dev_o :
            [ii, oi] = searchdevice(cis, dev_n, True, dev_o)
            if oi != "" :
                swapdevices(cis, o1, oi)

    inifileobj = open(midihwneu, 'w')
    config.write(inifileobj)
    inifileobj.close()
        
    c = command[found]
    print("execute command:",c)

    if c != 0: 
        os.system(c)
    midi.quit()
and an appropriate ini file:
Code:
[MIDI]
device1 = MIDI Mapper
device2 = SYNTH
batch00=start00.bat
batch01=start01.bat
batch10=start10.bat
batch11=start11.bat
[REAPER]
midihw = c:/Users/mschnell/AppData/Roaming/REAPER/reaper-midihw.ini
midihwtemplate = reaper-midihw.template
mschnell is offline   Reply With Quote