|
@@ -56,80 +56,76 @@ class simplemidi(object):
|
|
|
self.__midi = os.open( self.__name, option)
|
|
|
|
|
|
self.__mode = "a"
|
|
|
-
|
|
|
+ self.is_open = 1
|
|
|
except OSError as e:
|
|
|
- print("File", self.__name, "ERR: {0} ".format(e.args) )
|
|
|
-
|
|
|
- try:
|
|
|
- self.__mode = "rw"
|
|
|
- option = os.O_RDONLY | os.O_NONBLOCK
|
|
|
- self.__midi = os.open( self.__name, option)
|
|
|
- print("DEVICE MODE:",self.__mode)
|
|
|
- except OSError as e:
|
|
|
- print("File", self.__name, "ERR: {0} ".format(e.args) )
|
|
|
- input()
|
|
|
- sys.exit()
|
|
|
+ print("__open File", self.__name, "ERR: {0} ".format(e.args) )
|
|
|
+ print(" sleep 1sec...")
|
|
|
+ time.sleep(1)
|
|
|
+ self.is_open = 0
|
|
|
|
|
|
print("DEVICE MODE:",self.__mode)
|
|
|
+
|
|
|
def init(self):
|
|
|
#placeholder pygame
|
|
|
pass
|
|
|
+
|
|
|
def get_device_info(self,nr):
|
|
|
if nr == 1:
|
|
|
return "simplemidi", self.__device
|
|
|
- else:
|
|
|
+ return None
|
|
|
|
|
|
- return None
|
|
|
def write_delayed(self,data):
|
|
|
#import thread
|
|
|
thread.start_new_thread(self._write_delayed,([data,0.01],)) #midi writeloop
|
|
|
thread.start_new_thread(self._write_delayed,([data,0.1],)) #midi writeloop
|
|
|
thread.start_new_thread(self._write_delayed,([data,1],)) #midi writeloop
|
|
|
+
|
|
|
def _write_delayed(self,data):
|
|
|
time.sleep(data[1])
|
|
|
self.write(data[0])
|
|
|
|
|
|
def write(self,data):
|
|
|
- self.__lock.acquire()
|
|
|
- # change midi file to write mode
|
|
|
- if self.__mode == "rw":
|
|
|
- os.close(self.__midi)
|
|
|
- option = os.O_WRONLY | os.O_NONBLOCK
|
|
|
- self.__midi = os.open( self.__name, option)
|
|
|
+ try:
|
|
|
+ self.__lock.acquire()
|
|
|
+ # change midi file to write mode
|
|
|
+ if self.__mode == "rw":
|
|
|
+ os.close(self.__midi)
|
|
|
+ option = os.O_WRONLY | os.O_NONBLOCK
|
|
|
+ self.__midi = os.open( self.__name, option)
|
|
|
|
|
|
- if len(data) == 3:
|
|
|
- msg = ""
|
|
|
-
|
|
|
- try:
|
|
|
- msg = chr(int(data[0])) + chr(int(data[1])) + chr(int(data[2]) )
|
|
|
- #if data[0] != 191:
|
|
|
- # print(data#[msg])
|
|
|
- os.write(self.__midi, bytes(msg,"utf-8") )
|
|
|
- except Exception as e:# SyntaxError:print("midi err",[msg,data ])
|
|
|
- print("midi-single-write:", e, data)
|
|
|
- #self.__close() #STOPPING MIDI ...
|
|
|
- #self.__open()
|
|
|
- elif len(data) > 3:
|
|
|
- #print("multi sending---------------------------")
|
|
|
- for i in data:
|
|
|
- if len(i) == 3:
|
|
|
- msg = ""
|
|
|
-
|
|
|
- try:
|
|
|
- msg = chr(int(i[0])) + chr(int(i[1])) + chr(int(i[2]))
|
|
|
- print([msg])
|
|
|
- os.write(self.__midi, msg )
|
|
|
- except Exception as e:
|
|
|
- pass
|
|
|
- print("midi-multi-write:", e, data)
|
|
|
+ if len(data) == 3:
|
|
|
+ msg = ""
|
|
|
|
|
|
-
|
|
|
- # change midi file to read mode
|
|
|
- if self.__mode == "rw":
|
|
|
- os.close(self.__midi)
|
|
|
- option = os.O_RDONLY | os.O_NONBLOCK
|
|
|
- self.__midi = os.open( self.__name, option)
|
|
|
- self.__lock.release()
|
|
|
+ try:
|
|
|
+ msg = chr(int(data[0])) + chr(int(data[1])) + chr(int(data[2]) )
|
|
|
+ os.write(self.__midi, bytes(msg,"utf-8") )
|
|
|
+ except Exception as e:# SyntaxError:print("midi err",[msg,data ])
|
|
|
+ print("midi-single-write:", e, data)
|
|
|
+ time.sleep(1)
|
|
|
+ self.__open()
|
|
|
+
|
|
|
+ elif len(data) > 3:
|
|
|
+ #print("multi sending---------------------------")
|
|
|
+ for i in data:
|
|
|
+ if len(i) == 3:
|
|
|
+ msg = ""
|
|
|
+
|
|
|
+ try:
|
|
|
+ msg = chr(int(i[0])) + chr(int(i[1])) + chr(int(i[2]))
|
|
|
+ print([msg])
|
|
|
+ os.write(self.__midi, msg )
|
|
|
+ except Exception as e:
|
|
|
+ pass
|
|
|
+ print("midi-multi-write:", e, data)
|
|
|
+
|
|
|
+
|
|
|
+ # change midi file to read mode
|
|
|
+ if self.__mode == "rw":
|
|
|
+ os.close(self.__midi)
|
|
|
+ option = os.O_RDONLY | os.O_NONBLOCK
|
|
|
+ self.__midi = os.open( self.__name, option)
|
|
|
+ finally:
|
|
|
+ self.__lock.release()
|
|
|
|
|
|
def read(self,count=3):
|
|
|
self.__lock.acquire()
|
|
@@ -140,6 +136,7 @@ class simplemidi(object):
|
|
|
|
|
|
def poll(self,sysex=0):
|
|
|
self.__lock.acquire()
|
|
|
+ ok = 0
|
|
|
try:
|
|
|
if sysex:
|
|
|
self.__data = os.read(self.__midi, 1) #read abort if no data to read
|
|
@@ -153,10 +150,16 @@ class simplemidi(object):
|
|
|
except IndexError as e:
|
|
|
print("File", self.__name, "ERR: {0} ".format(e.args) ,[inp])
|
|
|
|
|
|
- self.__lock.release()
|
|
|
- return 1
|
|
|
- except OSError:
|
|
|
+ ok = 1
|
|
|
+ except KeyboardInterrupt as e:
|
|
|
+ raise e
|
|
|
+ except: # OSError:
|
|
|
time.sleep(0.01) # CPU STRESSLESS
|
|
|
+ finally:
|
|
|
self.__lock.release()
|
|
|
- return 0
|
|
|
+
|
|
|
+ return ok
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|