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.

345 lines
12KB

  1. #!/usr/bin/env python3
  2. # SPDX-FileCopyrightText: 2011-2024 Filipe Coelho <falktx@falktx.com>
  3. # SPDX-FileCopyrightText: 2014-2015 Perry Nguyen
  4. # SPDX-License-Identifier: GPL-2.0-or-later
  5. # ------------------------------------------------------------------------------------------------------------
  6. # Imports (Global)
  7. from qt_compat import qt_config
  8. if qt_config == 5:
  9. from PyQt5.QtCore import pyqtSlot, QT_VERSION, Qt, QEvent
  10. from PyQt5.QtGui import QKeyEvent
  11. from PyQt5.QtWidgets import QMainWindow
  12. elif qt_config == 6:
  13. from PyQt6.QtCore import pyqtSlot, QT_VERSION, Qt, QEvent
  14. from PyQt6.QtGui import QKeyEvent
  15. from PyQt6.QtWidgets import QMainWindow
  16. # ------------------------------------------------------------------------------------------------------------
  17. # Imports (Custom)
  18. from carla_shared import *
  19. from carla_utils import *
  20. from widgets.pianoroll import *
  21. import ui_midipattern
  22. # ------------------------------------------------------------------------------------------------------------
  23. # Imports (ExternalUI)
  24. from carla_app import CarlaApplication
  25. from externalui import ExternalUI
  26. # ------------------------------------------------------------------------------------------------------------
  27. class MidiPatternW(ExternalUI, QMainWindow):
  28. TICKS_PER_BEAT = 48
  29. def __init__(self):
  30. ExternalUI.__init__(self)
  31. QMainWindow.__init__(self)
  32. self.ui = ui_midipattern.Ui_MidiPatternW()
  33. self.ui.setupUi(self)
  34. self.ui.piano = self.ui.graphicsView.piano
  35. # to be filled with note-on events, while waiting for their matching note-off
  36. self.fPendingNoteOns = [] # (channel, note, velocity, time)
  37. self.fTimeSignature = (4,4)
  38. self.fTransportInfo = {
  39. "playing": False,
  40. "frame": 0,
  41. "bar": 0,
  42. "beat": 0,
  43. "tick": 0,
  44. "bpm": 120.0,
  45. }
  46. self.ui.act_edit_insert.triggered.connect(self.slot_editInsertMode)
  47. self.ui.act_edit_velocity.triggered.connect(self.slot_editVelocityMode)
  48. self.ui.act_edit_select_all.triggered.connect(self.slot_editSelectAll)
  49. self.ui.piano.midievent.connect(self.sendMsg)
  50. self.ui.piano.noteclicked.connect(self.sendTemporaryNote)
  51. self.ui.piano.measureupdate.connect(self.updateMeasureBox)
  52. self.ui.piano.modeupdate.connect(self.ui.modeIndicator.changeMode)
  53. self.ui.piano.modeupdate.connect(self.slot_modeChanged)
  54. if QT_VERSION >= 0x60000:
  55. self.ui.timeSigBox.currentIndexChanged.connect(self.slot_paramChanged)
  56. self.ui.measureBox.currentIndexChanged.connect(self.slot_paramChanged)
  57. self.ui.defaultLengthBox.currentIndexChanged.connect(self.slot_paramChanged)
  58. self.ui.quantizeBox.currentIndexChanged.connect(self.slot_paramChanged)
  59. self.ui.timeSigBox.currentTextChanged.connect(self.slot_setTimeSignature)
  60. self.ui.measureBox.currentTextChanged.connect(self.ui.piano.setMeasures)
  61. self.ui.defaultLengthBox.currentTextChanged.connect(self.ui.piano.setDefaultLength)
  62. self.ui.quantizeBox.currentTextChanged.connect(self.ui.piano.setGridDiv)
  63. else:
  64. self.ui.timeSigBox.currentIndexChanged[int].connect(self.slot_paramChanged)
  65. self.ui.measureBox.currentIndexChanged[int].connect(self.slot_paramChanged)
  66. self.ui.defaultLengthBox.currentIndexChanged[int].connect(self.slot_paramChanged)
  67. self.ui.quantizeBox.currentIndexChanged[int].connect(self.slot_paramChanged)
  68. self.ui.timeSigBox.currentIndexChanged[str].connect(self.slot_setTimeSignature)
  69. self.ui.measureBox.currentIndexChanged[str].connect(self.ui.piano.setMeasures)
  70. self.ui.defaultLengthBox.currentIndexChanged[str].connect(self.ui.piano.setDefaultLength)
  71. self.ui.quantizeBox.currentIndexChanged[str].connect(self.ui.piano.setGridDiv)
  72. self.ui.hSlider.valueChanged.connect(self.ui.graphicsView.setZoomX)
  73. self.ui.vSlider.valueChanged.connect(self.ui.graphicsView.setZoomY)
  74. self.ui.graphicsView.setFocus()
  75. self.fIdleTimer = self.startTimer(30)
  76. self.setWindowTitle(self.fUiName)
  77. self.ready()
  78. def slot_editInsertMode(self):
  79. ev = QKeyEvent(QEvent.User, Qt.Key_F, Qt.NoModifier)
  80. self.ui.piano.keyPressEvent(ev)
  81. def slot_editVelocityMode(self):
  82. ev = QKeyEvent(QEvent.User, Qt.Key_D, Qt.NoModifier)
  83. self.ui.piano.keyPressEvent(ev)
  84. def slot_editSelectAll(self):
  85. ev = QKeyEvent(QEvent.User, Qt.Key_A, Qt.NoModifier)
  86. self.ui.piano.keyPressEvent(ev)
  87. def slot_modeChanged(self, mode):
  88. if mode == "insert_mode":
  89. self.ui.act_edit_insert.setChecked(True)
  90. self.ui.act_edit_velocity.setChecked(False)
  91. elif mode == "velocity_mode":
  92. self.ui.act_edit_insert.setChecked(False)
  93. self.ui.act_edit_velocity.setChecked(True)
  94. else:
  95. self.ui.act_edit_insert.setChecked(False)
  96. self.ui.act_edit_velocity.setChecked(False)
  97. def slot_paramChanged(self, index):
  98. sender = self.sender()
  99. if sender == self.ui.timeSigBox:
  100. param = 0
  101. elif sender == self.ui.measureBox:
  102. param = 1
  103. index += 1
  104. elif sender == self.ui.defaultLengthBox:
  105. param = 2
  106. elif sender == self.ui.quantizeBox:
  107. param = 3
  108. else:
  109. return
  110. self.sendControl(param, index)
  111. def slot_setTimeSignature(self, sigtext):
  112. try:
  113. timesig = tuple(map(float, sigtext.split('/')))
  114. except ValueError:
  115. return
  116. if len(timesig) != 2:
  117. return
  118. self.fTimeSignature = timesig
  119. self.ui.piano.setTimeSig(timesig)
  120. # -------------------------------------------------------------------
  121. # DSP Callbacks
  122. def dspParameterChanged(self, index, value):
  123. value = int(value)
  124. if index == 0: # TimeSig
  125. self.ui.timeSigBox.blockSignals(True)
  126. self.ui.timeSigBox.setCurrentIndex(value)
  127. self.slot_setTimeSignature(self.ui.timeSigBox.currentText())
  128. self.ui.timeSigBox.blockSignals(False)
  129. elif index == 1: # Measures
  130. self.ui.measureBox.blockSignals(True)
  131. self.ui.measureBox.setCurrentIndex(value-1)
  132. self.ui.piano.setMeasures(self.ui.measureBox.currentText())
  133. self.ui.measureBox.blockSignals(False)
  134. elif index == 2: # DefLength
  135. self.ui.defaultLengthBox.blockSignals(True)
  136. self.ui.defaultLengthBox.setCurrentIndex(value)
  137. self.ui.piano.setDefaultLength(self.ui.defaultLengthBox.currentText())
  138. self.ui.defaultLengthBox.blockSignals(False)
  139. elif index == 3: # Quantize
  140. self.ui.quantizeBox.blockSignals(True)
  141. self.ui.quantizeBox.setCurrentIndex(value)
  142. self.ui.piano.setQuantize(self.ui.quantizeBox.currentText())
  143. self.ui.quantizeBox.blockSignals(False)
  144. def dspStateChanged(self, key, value):
  145. pass
  146. # -------------------------------------------------------------------
  147. # ExternalUI Callbacks
  148. def uiShow(self):
  149. self.show()
  150. def uiFocus(self):
  151. self.setWindowState((self.windowState() & ~Qt.WindowMinimized) | Qt.WindowActive)
  152. self.show()
  153. self.raise_()
  154. self.activateWindow()
  155. def uiHide(self):
  156. self.hide()
  157. def uiQuit(self):
  158. self.closeExternalUI()
  159. self.close()
  160. app.quit()
  161. def uiTitleChanged(self, uiTitle):
  162. self.setWindowTitle(uiTitle)
  163. # -------------------------------------------------------------------
  164. # Qt events
  165. def timerEvent(self, event):
  166. if event.timerId() == self.fIdleTimer:
  167. self.idleExternalUI()
  168. QMainWindow.timerEvent(self, event)
  169. def closeEvent(self, event):
  170. self.closeExternalUI()
  171. QMainWindow.closeEvent(self, event)
  172. # there might be other qt windows open which will block the UI from quitting
  173. app.quit()
  174. # -------------------------------------------------------------------
  175. # Custom callback
  176. def updateMeasureBox(self, index):
  177. self.ui.measureBox.blockSignals(True)
  178. self.ui.measureBox.setCurrentIndex(index-1)
  179. self.ui.measureBox.blockSignals(False)
  180. def sendMsg(self, data):
  181. msg = data[0]
  182. if msg == "midievent-add":
  183. note, start, length, vel = data[1:5]
  184. note_start = start * self.TICKS_PER_BEAT
  185. note_stop = note_start + length * 4. * self.fTimeSignature[0] / self.fTimeSignature[1] * self.TICKS_PER_BEAT
  186. self.send([msg, note_start, 3, MIDI_STATUS_NOTE_ON, note, vel])
  187. self.send([msg, note_stop, 3, MIDI_STATUS_NOTE_OFF, note, vel])
  188. elif msg == "midievent-remove":
  189. note, start, length, vel = data[1:5]
  190. note_start = start * self.TICKS_PER_BEAT
  191. note_stop = note_start + length * 4. * self.fTimeSignature[0] / self.fTimeSignature[1] * self.TICKS_PER_BEAT
  192. self.send([msg, note_start, 3, MIDI_STATUS_NOTE_ON, note, vel])
  193. self.send([msg, note_stop, 3, MIDI_STATUS_NOTE_OFF, note, vel])
  194. def sendTemporaryNote(self, note, on):
  195. self.send(["midi-note", note, on])
  196. def msgCallback(self, msg):
  197. msg = charPtrToString(msg)
  198. if msg == "midi-clear-all":
  199. # clear all notes
  200. self.ui.piano.clearNotes()
  201. elif msg == "midievent-add":
  202. # adds single midi event
  203. time = self.readlineblock_int()
  204. size = self.readlineblock_int()
  205. data = tuple(self.readlineblock_int() for x in range(size))
  206. self.handleMidiEvent(time, size, data)
  207. elif msg == "transport":
  208. playing, frame, bar, beat, tick = tuple(int(i) for i in self.readlineblock().split(":"))
  209. bpm = self.readlineblock_float()
  210. playing = bool(int(playing))
  211. old_frame = self.fTransportInfo['frame']
  212. self.fTransportInfo = {
  213. "playing": playing,
  214. "frame": frame,
  215. "bar": bar,
  216. "beat": beat,
  217. "tick": tick,
  218. "bpm": bpm,
  219. "ticksPerBeat": self.TICKS_PER_BEAT,
  220. }
  221. if old_frame != frame:
  222. self.ui.piano.movePlayHead(self.fTransportInfo)
  223. elif msg == "parameters":
  224. timesig, measures, deflength, quantize = tuple(int(i) for i in self.readlineblock().split(":"))
  225. self.dspParameterChanged(0, timesig)
  226. self.dspParameterChanged(1, measures)
  227. self.dspParameterChanged(2, deflength)
  228. self.dspParameterChanged(3, quantize)
  229. else:
  230. ExternalUI.msgCallback(self, msg)
  231. # -------------------------------------------------------------------
  232. # Internal stuff
  233. def handleMidiEvent(self, time, size, data):
  234. #print("handleMidiEvent", time, size, data)
  235. status = MIDI_GET_STATUS_FROM_DATA(data)
  236. channel = MIDI_GET_CHANNEL_FROM_DATA(data)
  237. if status == MIDI_STATUS_NOTE_ON:
  238. note = data[1]
  239. velo = data[2]
  240. # append (channel, note, velo, time) for later
  241. self.fPendingNoteOns.append((channel, note, velo, time))
  242. elif status == MIDI_STATUS_NOTE_OFF:
  243. note = data[1]
  244. # find previous note-on that matches this note and channel
  245. for noteOnMsg in self.fPendingNoteOns:
  246. on_channel, on_note, on_velo, on_time = noteOnMsg
  247. if on_channel != channel:
  248. continue
  249. if on_note != note:
  250. continue
  251. # found it
  252. self.fPendingNoteOns.remove(noteOnMsg)
  253. break
  254. else:
  255. return
  256. self.ui.piano.drawNote(note,
  257. on_time/self.TICKS_PER_BEAT,
  258. (time-on_time)/self.TICKS_PER_BEAT/self.fTimeSignature[0],
  259. on_velo)
  260. #--------------- main ------------------
  261. if __name__ == '__main__':
  262. import resources_rc
  263. pathBinaries, _ = getPaths()
  264. gCarla.utils = CarlaUtils(os.path.join(pathBinaries, "libcarla_utils." + DLL_EXTENSION))
  265. gCarla.utils.set_process_name("MidiPattern")
  266. app = CarlaApplication("MidiPattern")
  267. gui = MidiPatternW()
  268. app.exit_exec()