Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
6.2KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # External UI
  4. # Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License as
  8. # published by the Free Software Foundation; either version 2 of
  9. # the License, or any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # For a full copy of the GNU General Public License see the doc/GPL.txt file.
  17. # -----------------------------------------------------------------------
  18. # Imports
  19. from os import fdopen, O_NONBLOCK
  20. from fcntl import fcntl, F_GETFL, F_SETFL
  21. from sys import argv
  22. # -----------------------------------------------------------------------
  23. # External UI
  24. class ExternalUI(object):
  25. def __init__(self):
  26. object.__init__(self)
  27. self.fPipeRecv = None
  28. self.fPipeSend = None
  29. self.fQuitReceived = False
  30. if len(argv) > 1:
  31. self.fSampleRate = float(argv[1])
  32. self.fUiName = argv[2]
  33. self.fPipeRecvFd = int(argv[3])
  34. self.fPipeSendFd = int(argv[4])
  35. fcntl(self.fPipeRecvFd, F_SETFL, fcntl(self.fPipeRecvFd, F_GETFL) | O_NONBLOCK)
  36. self.fPipeRecv = fdopen(self.fPipeRecvFd, 'r')
  37. self.fPipeSend = fdopen(self.fPipeSendFd, 'w')
  38. else:
  39. self.fSampleRate = 44100.0
  40. self.fUiName = "TestUI"
  41. self.fPipeRecv = None
  42. self.fPipeSend = None
  43. # send empty line (just newline char)
  44. self.send([""])
  45. def showUiIfTesting(self):
  46. if self.fPipeRecv is None:
  47. self.d_uiShow()
  48. # -------------------------------------------------------------------
  49. # Host DSP State
  50. def d_getSampleRate(self):
  51. return self.fSampleRate
  52. def d_editParameter(self, index, started):
  53. self.send(["editParam", index, started])
  54. def d_setParameterValue(self, index, value):
  55. self.send(["control", index, value])
  56. def d_setProgram(self, channel, bank, program):
  57. self.send(["program", channel, bank, program])
  58. def d_setState(self, key, value):
  59. self.send(["configure", key, value])
  60. def d_sendNote(self, onOff, channel, note, velocity):
  61. self.send(["note", onOff, channel, note, velocity])
  62. # -------------------------------------------------------------------
  63. # DSP Callbacks
  64. def d_parameterChanged(self, index, value):
  65. return
  66. def d_programChanged(self, channel, bank, program):
  67. return
  68. def d_stateChanged(self, key, value):
  69. return
  70. def d_noteReceived(self, onOff, channel, note, velocity):
  71. return
  72. # -------------------------------------------------------------------
  73. # ExternalUI Callbacks
  74. def d_uiShow(self):
  75. return
  76. def d_uiHide(self):
  77. return
  78. def d_uiQuit(self):
  79. return
  80. def d_uiTitleChanged(self, uiTitle):
  81. return
  82. # -------------------------------------------------------------------
  83. # Public methods
  84. def closeExternalUI(self):
  85. if not self.fQuitReceived:
  86. self.send(["exiting"])
  87. if self.fPipeRecv is not None:
  88. self.fPipeRecv.close()
  89. self.fPipeRecv = None
  90. if self.fPipeSend is not None:
  91. self.fPipeSend.close()
  92. self.fPipeSend = None
  93. def idleExternalUI(self):
  94. while True:
  95. if self.fPipeRecv is None:
  96. return True
  97. try:
  98. msg = self.fPipeRecv.readline().strip()
  99. except IOError:
  100. return False
  101. if not msg:
  102. return True
  103. elif msg == "control":
  104. index = int(self.fPipeRecv.readline())
  105. value = float(self.fPipeRecv.readline())
  106. self.d_parameterChanged(index, value)
  107. elif msg == "program":
  108. channel = int(self.fPipeRecv.readline())
  109. bank = int(self.fPipeRecv.readline())
  110. program = int(self.fPipeRecv.readline())
  111. self.d_programChanged(channel, bank, program)
  112. elif msg == "configure":
  113. key = self.fPipeRecv.readline().strip().replace("\r", "\n")
  114. value = self.fPipeRecv.readline().strip().replace("\r", "\n")
  115. self.d_stateChanged(key, value)
  116. elif msg == "note":
  117. onOff = bool(self.fPipeRecv.readline().strip() == "true")
  118. channel = int(self.fPipeRecv.readline())
  119. note = int(self.fPipeRecv.readline())
  120. velocity = int(self.fPipeRecv.readline())
  121. self.d_noteReceived(onOff, channel, note, velocity)
  122. elif msg == "show":
  123. self.d_uiShow()
  124. elif msg == "hide":
  125. self.d_uiHide()
  126. elif msg == "quit":
  127. self.fQuitReceived = True
  128. self.d_uiQuit()
  129. elif msg == "uiTitle":
  130. uiTitle = self.fPipeRecv.readline().strip().replace("\r", "\n")
  131. self.d_uiTitleChanged(uiTitle)
  132. else:
  133. print("unknown message: \"" + msg + "\"")
  134. return True
  135. # -------------------------------------------------------------------
  136. # Internal stuff
  137. def send(self, lines):
  138. if self.fPipeSend is None:
  139. return
  140. for line in lines:
  141. if line is None:
  142. line2 = "(null)"
  143. elif isinstance(line, str):
  144. line2 = line.replace("\n", "\r")
  145. else:
  146. if isinstance(line, bool):
  147. line2 = "true" if line else "false"
  148. elif isinstance(line, int):
  149. line2 = "%i" % line
  150. elif isinstance(line, float):
  151. line2 = "%.10f" % line
  152. else:
  153. print("unknown data type to send:", type(line))
  154. return
  155. self.fPipeSend.write(line2 + "\n")
  156. self.fPipeSend.flush()