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.

externalui.py 6.6KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # External UI
  4. # Copyright (C) 2013-2020 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 (Custom Stuff)
  19. from carla_backend import charPtrToString
  20. from carla_shared import *
  21. # ------------------------------------------------------------------------------------------------------------
  22. # External UI
  23. class ExternalUI(object):
  24. def __init__(self):
  25. object.__init__(self)
  26. self.fQuitReceived = False
  27. if len(sys.argv) > 1:
  28. self.fSampleRate = float(sys.argv[1])
  29. self.fUiName = sys.argv[2]
  30. self.fPipeClient = gCarla.utils.pipe_client_new(lambda s,msg: self.msgCallback(msg))
  31. else:
  32. self.fSampleRate = 44100.0
  33. self.fUiName = "TestUI"
  34. self.fPipeClient = None
  35. # -------------------------------------------------------------------
  36. # Public methods
  37. def ready(self):
  38. if self.fPipeClient is None:
  39. # testing, show UI only
  40. self.uiShow()
  41. def isRunning(self):
  42. if self.fPipeClient is not None:
  43. return gCarla.utils.pipe_client_is_running(self.fPipeClient)
  44. return False
  45. def idleExternalUI(self):
  46. if self.fPipeClient is not None:
  47. gCarla.utils.pipe_client_idle(self.fPipeClient)
  48. def closeExternalUI(self):
  49. if self.fPipeClient is None:
  50. return
  51. # FIXME
  52. if not self.fQuitReceived:
  53. self.send(["exiting"])
  54. gCarla.utils.pipe_client_destroy(self.fPipeClient)
  55. self.fPipeClient = None
  56. # -------------------------------------------------------------------
  57. # Host DSP State
  58. def getSampleRate(self):
  59. return self.fSampleRate
  60. def sendControl(self, index, value):
  61. self.send(["control", index, value])
  62. def sendProgram(self, channel, bank, program):
  63. self.send(["program", channel, bank, program])
  64. def sendConfigure(self, key, value):
  65. self.send(["configure", key, value])
  66. # -------------------------------------------------------------------
  67. # DSP Callbacks
  68. def dspParameterChanged(self, index, value):
  69. return
  70. def dspProgramChanged(self, channel, bank, program):
  71. return
  72. def dspStateChanged(self, key, value):
  73. return
  74. def dspNoteReceived(self, onOff, channel, note, velocity):
  75. return
  76. # -------------------------------------------------------------------
  77. # ExternalUI Callbacks
  78. def uiShow(self):
  79. return
  80. def uiFocus(self):
  81. return
  82. def uiHide(self):
  83. return
  84. def uiQuit(self):
  85. self.closeExternalUI()
  86. def uiTitleChanged(self, uiTitle):
  87. return
  88. # -------------------------------------------------------------------
  89. # Callback
  90. def msgCallback(self, msg):
  91. msg = charPtrToString(msg)
  92. #if not msg:
  93. #return
  94. if msg == "control":
  95. index = self.readlineblock_int()
  96. value = self.readlineblock_float()
  97. self.dspParameterChanged(index, value)
  98. elif msg == "program":
  99. channel = self.readlineblock_int()
  100. bank = self.readlineblock_int()
  101. program = self.readlineblock_int()
  102. self.dspProgramChanged(channel, bank, program)
  103. elif msg == "configure":
  104. key = self.readlineblock()
  105. value = self.readlineblock()
  106. self.dspStateChanged(key, value)
  107. elif msg == "note":
  108. onOff = self.readlineblock_bool()
  109. channel = self.readlineblock_int()
  110. note = self.readlineblock_int()
  111. velocity = self.readlineblock_int()
  112. self.dspNoteReceived(onOff, channel, note, velocity)
  113. elif msg == "show":
  114. self.uiShow()
  115. elif msg == "focus":
  116. self.uiFocus()
  117. elif msg == "hide":
  118. self.uiHide()
  119. elif msg == "quit":
  120. self.fQuitReceived = True
  121. self.uiQuit()
  122. elif msg == "uiTitle":
  123. uiTitle = self.readlineblock()
  124. self.uiTitleChanged(uiTitle)
  125. else:
  126. print("unknown message: \"" + msg + "\"")
  127. # -------------------------------------------------------------------
  128. # Internal stuff
  129. def readlineblock(self):
  130. if self.fPipeClient is None:
  131. return ""
  132. return gCarla.utils.pipe_client_readlineblock(self.fPipeClient, 5000)
  133. def readlineblock_bool(self):
  134. if self.fPipeClient is None:
  135. return False
  136. return gCarla.utils.pipe_client_readlineblock_bool(self.fPipeClient, 5000)
  137. def readlineblock_int(self):
  138. if self.fPipeClient is None:
  139. return 0
  140. return gCarla.utils.pipe_client_readlineblock_int(self.fPipeClient, 5000)
  141. def readlineblock_float(self):
  142. if self.fPipeClient is None:
  143. return 0.0
  144. return gCarla.utils.pipe_client_readlineblock_float(self.fPipeClient, 5000)
  145. def send(self, lines):
  146. if self.fPipeClient is None or len(lines) == 0:
  147. return False
  148. hasError = False
  149. gCarla.utils.pipe_client_lock(self.fPipeClient)
  150. try:
  151. for line in lines:
  152. if line is None:
  153. line2 = "(null)"
  154. elif isinstance(line, str):
  155. line2 = line.replace("\n", "\r")
  156. elif isinstance(line, bool):
  157. line2 = "true" if line else "false"
  158. elif isinstance(line, int):
  159. line2 = "%i" % line
  160. elif isinstance(line, float):
  161. line2 = "%.10f" % line
  162. else:
  163. print("unknown data type to send:", type(line))
  164. hasError = True
  165. break
  166. if not gCarla.utils.pipe_client_write_msg(self.fPipeClient, line2 + "\n"):
  167. hasError = True
  168. break
  169. finally:
  170. return gCarla.utils.pipe_client_flush_and_unlock(self.fPipeClient) and not hasError