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.

186 lines
5.5KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # DISTRHO Plugin Toolkit (DPT)
  4. # Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
  5. #
  6. # Permission to use, copy, modify, and/or distribute this software for any purpose with
  7. # or without fee is hereby granted, provided that the above copyright notice and this
  8. # permission notice appear in all copies.
  9. #
  10. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  11. # TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  12. # NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. # DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  14. # IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  15. # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. # -----------------------------------------------------------------------
  17. # Imports
  18. from os import fdopen, O_NONBLOCK
  19. from fcntl import fcntl, F_GETFL, F_SETFL
  20. from sys import argv
  21. # -----------------------------------------------------------------------
  22. # External UI
  23. class ExternalUI(object):
  24. def __init__(self):
  25. object.__init__(self)
  26. self.fPipeRecv = None
  27. self.fPipeSend = None
  28. self.fQuitReceived = False
  29. self.fSampleRate = float(argv[1])
  30. self.fUiName = argv[2]
  31. self.fPipeRecvFd = int(argv[3])
  32. self.fPipeSendFd = int(argv[4])
  33. fcntl(self.fPipeRecvFd, F_SETFL, fcntl(self.fPipeRecvFd, F_GETFL) | O_NONBLOCK)
  34. self.fPipeRecv = fdopen(self.fPipeRecvFd, 'r')
  35. self.fPipeSend = fdopen(self.fPipeSendFd, 'w')
  36. # send empty line (just newline char)
  37. self.send([""])
  38. # -------------------------------------------------------------------
  39. # Host DSP State
  40. def d_getSampleRate(self):
  41. return self.fSampleRate
  42. def d_editParameter(self, index, started):
  43. self.send(["editParam", index, started])
  44. def d_setParameterValue(self, index, value):
  45. self.send(["control", index, value])
  46. def d_setState(self, key, value):
  47. self.send(["configure", key, value])
  48. def d_sendNote(self, onOff, channel, note, velocity):
  49. self.send(["note", onOff, channel, note, velocity])
  50. # -------------------------------------------------------------------
  51. # DSP Callbacks
  52. def d_parameterChanged(self, index, value):
  53. return
  54. def d_programChanged(self, index):
  55. return
  56. def d_stateChanged(self, key, value):
  57. return
  58. def d_noteReceived(self, onOff, channel, note, velocity):
  59. return
  60. # -------------------------------------------------------------------
  61. # ExternalUI Callbacks
  62. def d_uiShow(self):
  63. return
  64. def d_uiHide(self):
  65. return
  66. def d_uiQuit(self):
  67. return
  68. def d_uiTitleChanged(self, uiTitle):
  69. return
  70. # -------------------------------------------------------------------
  71. # Public methods
  72. def closeExternalUI(self):
  73. if not self.fQuitReceived:
  74. self.send(["exiting"])
  75. if self.fPipeRecv is not None:
  76. self.fPipeRecv.close()
  77. self.fPipeRecv = None
  78. if self.fPipeSend is not None:
  79. self.fPipeSend.close()
  80. self.fPipeSend = None
  81. def idleExternalUI(self):
  82. if self.fPipeRecv is None:
  83. return False
  84. try:
  85. msg = self.fPipeRecv.readline().strip()
  86. except IOError:
  87. return False
  88. if msg == "":
  89. return True
  90. if msg == "control":
  91. index = int(self.fPipeRecv.readline())
  92. value = float(self.fPipeRecv.readline())
  93. self.d_parameterChanged(index, value)
  94. elif msg == "program":
  95. index = int(self.fPipeRecv.readline())
  96. self.d_programChanged(index)
  97. elif msg == "configure":
  98. key = self.fPipeRecv.readline().strip().replace("\r", "\n")
  99. value = self.fPipeRecv.readline().strip().replace("\r", "\n")
  100. self.d_stateChanged(key, value)
  101. elif msg == "note":
  102. onOff = bool(self.fPipeRecv.readline().strip() == "true")
  103. channel = int(self.fPipeRecv.readline())
  104. note = int(self.fPipeRecv.readline())
  105. velocity = int(self.fPipeRecv.readline())
  106. self.d_noteReceived(onOff, channel, note, velocity)
  107. elif msg == "show":
  108. self.d_uiShow()
  109. elif msg == "hide":
  110. self.d_uiHide()
  111. elif msg == "quit":
  112. self.fQuitReceived = True
  113. self.d_uiQuit()
  114. elif msg == "uiTitle":
  115. uiTitle = self.fPipeRecv.readline().strip().replace("\r", "\n")
  116. self.d_uiTitleChanged(uiTitle)
  117. else:
  118. print("unknown message: \"" + msg + "\"")
  119. return True
  120. # -------------------------------------------------------------------
  121. # Internal stuff
  122. def send(self, lines):
  123. if self.fPipeSend is None:
  124. return
  125. for line in lines:
  126. if isinstance(line, str):
  127. line2 = line.replace("\n", "\r")
  128. else:
  129. if isinstance(line, bool):
  130. line2 = "true" if line else "false"
  131. elif isinstance(line, int):
  132. line2 = "%i" % line
  133. elif isinstance(line, float):
  134. line2 = "%.10f" % line
  135. else:
  136. return
  137. self.fPipeSend.write(line2 + "\n")
  138. self.fPipeSend.flush()