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.1KB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 (Custom Stuff)
  19. from carla_shared import *
  20. # ------------------------------------------------------------------------------------------------------------
  21. # External UI
  22. class ExternalUI(object):
  23. def __init__(self):
  24. object.__init__(self)
  25. self.fQuitReceived = False
  26. if len(sys.argv) > 1:
  27. self.fSampleRate = float(sys.argv[1])
  28. self.fUiName = sys.argv[2]
  29. self.fPipeClient = gCarla.utils.pipe_client_new(lambda s,msg: self.msgCallback(msg))
  30. else:
  31. self.fSampleRate = 44100.0
  32. self.fUiName = "TestUI"
  33. self.fPipeClient = None
  34. # -------------------------------------------------------------------
  35. # Public methods
  36. def ready(self):
  37. if self.fPipeClient is None:
  38. # testing, show UI only
  39. self.uiShow()
  40. def isRunning(self):
  41. if self.fPipeClient is not None:
  42. return gCarla.utils.pipe_client_is_running(self.fPipeClient)
  43. return False
  44. def idleExternalUI(self):
  45. if self.fPipeClient is not None:
  46. gCarla.utils.pipe_client_idle(self.fPipeClient)
  47. def closeExternalUI(self):
  48. if self.fPipeClient is None:
  49. return
  50. if not self.fQuitReceived:
  51. self.send(["exiting"])
  52. gCarla.utils.pipe_client_destroy(self.fPipeClient)
  53. self.fPipeClient = None
  54. # -------------------------------------------------------------------
  55. # Host DSP State
  56. def getSampleRate(self):
  57. return self.fSampleRate
  58. def sendControl(self, index, value):
  59. self.send(["control", index, value])
  60. def sendProgram(self, channel, bank, program):
  61. self.send(["program", channel, bank, program])
  62. def sendConfigure(self, key, value):
  63. self.send(["configure", key, value])
  64. def sendNote(self, onOff, channel, note, velocity):
  65. self.send(["note", onOff, channel, note, velocity])
  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 = int(self.readlineblock())
  96. value = float(self.readlineblock())
  97. self.dspParameterChanged(index, value)
  98. elif msg == "program":
  99. channel = int(self.readlineblock())
  100. bank = int(self.readlineblock())
  101. program = int(self.readlineblock())
  102. self.dspProgramChanged(channel, bank, program)
  103. elif msg == "configure":
  104. key = self.readlineblock() #.replace("\r", "\n")
  105. value = self.readlineblock() #.replace("\r", "\n")
  106. self.dspStateChanged(key, value)
  107. elif msg == "note":
  108. onOff = bool(self.readlineblock() == "true")
  109. channel = int(self.readlineblock())
  110. note = int(self.readlineblock())
  111. velocity = int(self.readlineblock())
  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() #.replace("\r", "\n")
  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 send(self, lines):
  134. if self.fPipeClient is None or len(lines) == 0:
  135. return
  136. gCarla.utils.pipe_client_lock(self.fPipeClient)
  137. try:
  138. for line in lines:
  139. if line is None:
  140. line2 = "(null)"
  141. elif isinstance(line, str):
  142. line2 = line.replace("\n", "\r")
  143. elif isinstance(line, bool):
  144. line2 = "true" if line else "false"
  145. elif isinstance(line, int):
  146. line2 = "%i" % line
  147. elif isinstance(line, float):
  148. line2 = "%.10f" % line
  149. else:
  150. print("unknown data type to send:", type(line))
  151. return
  152. gCarla.utils.pipe_client_write_msg(self.fPipeClient, line2 + "\n")
  153. finally:
  154. gCarla.utils.pipe_client_flush_and_unlock(self.fPipeClient)