simplemidi_wraper.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. This file is part of LibreLight.
  5. LibreLight is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, version 2 of the License.
  8. LibreLight is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with LibreLight. If not, see <http://www.gnu.org/licenses/>.
  14. (c) 2012 micha@librelight.de
  15. """
  16. # file descriptor on linux /dev/midi1
  17. import sys
  18. import os
  19. #import fcntl
  20. import _thread as thread
  21. import time
  22. import copy
  23. class simplemidi(object):
  24. def __init__(self,device="/dev/midi1"):
  25. self.__lock = thread.allocate_lock()
  26. self.__mode = ""
  27. #self.__lock.acquire()
  28. #self.__lock.release()
  29. self.__data = ""
  30. self.__name = device
  31. self.__open()
  32. def __close(self):
  33. os.close( self.__midi )
  34. def __open(self):
  35. try:
  36. option = os.O_RDWR | os.O_NONBLOCK # os.O_RDONLY | os.O_NONBLOCK
  37. self.__midi = os.open( self.__name, option)
  38. self.__mode = "a"
  39. except OSError as e:
  40. print("File", self.__name, "ERR: {0} ".format(e.args) )
  41. try:
  42. self.__mode = "rw"
  43. option = os.O_RDONLY | os.O_NONBLOCK
  44. self.__midi = os.open( self.__name, option)
  45. print("DEVICE MODE:",self.__mode)
  46. except OSError as e:
  47. print("File", self.__name, "ERR: {0} ".format(e.args) )
  48. input()
  49. sys.exit()
  50. print("DEVICE MODE:",self.__mode)
  51. def init(self):
  52. #placeholder pygame
  53. pass
  54. def get_device_info(self,nr):
  55. if nr == 1:
  56. return "simplemidi", self.__device
  57. else:
  58. return None
  59. def write_delayed(self,data):
  60. #import thread
  61. thread.start_new_thread(self._write_delayed,([data,0.01],)) #midi writeloop
  62. thread.start_new_thread(self._write_delayed,([data,0.1],)) #midi writeloop
  63. thread.start_new_thread(self._write_delayed,([data,1],)) #midi writeloop
  64. def _write_delayed(self,data):
  65. time.sleep(data[1])
  66. self.write(data[0])
  67. def write(self,data):
  68. self.__lock.acquire()
  69. # change midi file to write mode
  70. if self.__mode == "rw":
  71. os.close(self.__midi)
  72. option = os.O_WRONLY | os.O_NONBLOCK
  73. self.__midi = os.open( self.__name, option)
  74. if len(data) == 3:
  75. msg = ""
  76. try:
  77. msg = chr(int(data[0])) + chr(int(data[1])) + chr(int(data[2]) )
  78. #if data[0] != 191:
  79. # print(data#[msg])
  80. os.write(self.__midi, bytes(msg,"utf-8") )
  81. except Exception as e:# SyntaxError:print("midi err",[msg,data ])
  82. print("midi-single-write:", e, data)
  83. #self.__close() #STOPPING MIDI ...
  84. #self.__open()
  85. elif len(data) > 3:
  86. #print("multi sending---------------------------")
  87. for i in data:
  88. if len(i) == 3:
  89. msg = ""
  90. try:
  91. msg = chr(int(i[0])) + chr(int(i[1])) + chr(int(i[2]))
  92. print([msg])
  93. os.write(self.__midi, msg )
  94. except Exception as e:
  95. pass
  96. print("midi-multi-write:", e, data)
  97. # change midi file to read mode
  98. if self.__mode == "rw":
  99. os.close(self.__midi)
  100. option = os.O_RDONLY | os.O_NONBLOCK
  101. self.__midi = os.open( self.__name, option)
  102. self.__lock.release()
  103. def read(self,count=3):
  104. self.__lock.acquire()
  105. data = copy.deepcopy(self.__data)
  106. self.__lock.release()
  107. return data
  108. def poll(self,sysex=0):
  109. self.__lock.acquire()
  110. try:
  111. if sysex:
  112. self.__data = os.read(self.__midi, 1) #read abort if no data to read
  113. else:
  114. inp = os.read(self.__midi, 3) #read abort if no data to read
  115. try:
  116. #self.__data = [ ord( inp[0] ) ,ord( inp[1] ), ord( inp[2] ) ]
  117. self.__data = [ inp[0] , inp[1] , inp[2] ]
  118. except KeyError as e:
  119. print("File", self.__name, "ERR: {0} ".format(e.args) ,[inp])
  120. except IndexError as e:
  121. print("File", self.__name, "ERR: {0} ".format(e.args) ,[inp])
  122. self.__lock.release()
  123. return 1
  124. except OSError:
  125. time.sleep(0.01) # CPU STRESSLESS
  126. self.__lock.release()
  127. return 0