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.

323 lines
11KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # A piano roll viewer/editor
  4. # Copyright (C) 2012-2015 Filipe Coelho <falktx@falktx.com>
  5. # Copyright (C) 2014-2015 Perry Nguyen
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License as
  9. # published by the Free Software Foundation; either version 2 of
  10. # the License, or any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # For a full copy of the GNU General Public License see the doc/GPL.txt file.
  18. # ------------------------------------------------------------------------------------------------------------
  19. # Imports (Config)
  20. from carla_config import *
  21. # ------------------------------------------------------------------------------------------------------------
  22. # Imports (Global)
  23. if config_UseQt5:
  24. from PyQt5.QtCore import pyqtSlot, Qt, QEvent
  25. from PyQt5.QtGui import QKeyEvent
  26. from PyQt5.QtWidgets import QMainWindow
  27. else:
  28. from PyQt4.QtCore import pyqtSlot, Qt, QEvent
  29. from PyQt4.QtGui import QKeyEvent, QMainWindow
  30. # ------------------------------------------------------------------------------------------------------------
  31. # Imports (Custom)
  32. from carla_shared import *
  33. from carla_utils import *
  34. from widgets.pianoroll import *
  35. import ui_midipattern
  36. # ------------------------------------------------------------------------------------------------------------
  37. # Imports (ExternalUI)
  38. from carla_app import CarlaApplication
  39. from externalui import ExternalUI
  40. # ------------------------------------------------------------------------------------------------------------
  41. class MidiPatternW(ExternalUI, QMainWindow):
  42. PPQ = 48.0
  43. def __init__(self):
  44. ExternalUI.__init__(self)
  45. QMainWindow.__init__(self)
  46. self.ui = ui_midipattern.Ui_MidiPatternW()
  47. self.ui.setupUi(self)
  48. self.ui.piano = self.ui.graphicsView.piano
  49. # to be filled with note-on events, while waiting for their matching note-off
  50. self.fPendingNoteOns = [] # (channel, note, velocity, time)
  51. self.fTransportInfo = {
  52. "playing": False,
  53. "frame": 0,
  54. "bar": 0,
  55. "beat": 0,
  56. "tick": 0,
  57. "bpm": 120.0,
  58. "sigNum": 4.0,
  59. "sigDenom": 4.0
  60. }
  61. self.ui.act_edit_insert.triggered.connect(self.slot_editInsertMode)
  62. self.ui.act_edit_velocity.triggered.connect(self.slot_editVelocityMode)
  63. self.ui.act_edit_select_all.triggered.connect(self.slot_editSelectAll)
  64. self.ui.piano.midievent.connect(self.sendMsg)
  65. self.ui.piano.measureupdate.connect(self.updateMeasureBox)
  66. self.ui.piano.modeupdate.connect(self.ui.modeIndicator.changeMode)
  67. self.ui.piano.modeupdate.connect(self.slot_modeChanged)
  68. self.ui.timeSigBox.currentIndexChanged[int].connect(self.slot_paramChanged)
  69. self.ui.measureBox.currentIndexChanged[int].connect(self.slot_paramChanged)
  70. self.ui.defaultLengthBox.currentIndexChanged[int].connect(self.slot_paramChanged)
  71. self.ui.quantizeBox.currentIndexChanged[int].connect(self.slot_paramChanged)
  72. self.ui.timeSigBox.currentIndexChanged[str].connect(self.ui.piano.setTimeSig)
  73. self.ui.measureBox.currentIndexChanged[str].connect(self.ui.piano.setMeasures)
  74. self.ui.defaultLengthBox.currentIndexChanged[str].connect(self.ui.piano.setDefaultLength)
  75. self.ui.quantizeBox.currentIndexChanged[str].connect(self.ui.piano.setGridDiv)
  76. self.ui.hSlider.valueChanged.connect(self.ui.graphicsView.setZoomX)
  77. self.ui.vSlider.valueChanged.connect(self.ui.graphicsView.setZoomY)
  78. self.ui.graphicsView.setFocus()
  79. self.fIdleTimer = self.startTimer(30)
  80. self.setWindowTitle(self.fUiName)
  81. self.ready()
  82. def slot_editInsertMode(self):
  83. ev = QKeyEvent(QEvent.User, Qt.Key_F, Qt.NoModifier)
  84. self.ui.piano.keyPressEvent(ev)
  85. def slot_editVelocityMode(self):
  86. ev = QKeyEvent(QEvent.User, Qt.Key_D, Qt.NoModifier)
  87. self.ui.piano.keyPressEvent(ev)
  88. def slot_editSelectAll(self):
  89. ev = QKeyEvent(QEvent.User, Qt.Key_A, Qt.NoModifier)
  90. self.ui.piano.keyPressEvent(ev)
  91. def slot_modeChanged(self, mode):
  92. if mode == "insert_mode":
  93. self.ui.act_edit_insert.setChecked(True)
  94. self.ui.act_edit_velocity.setChecked(False)
  95. elif mode == "velocity_mode":
  96. self.ui.act_edit_insert.setChecked(False)
  97. self.ui.act_edit_velocity.setChecked(True)
  98. else:
  99. self.ui.act_edit_insert.setChecked(False)
  100. self.ui.act_edit_velocity.setChecked(False)
  101. def slot_paramChanged(self, index):
  102. sender = self.sender()
  103. if sender == self.ui.timeSigBox:
  104. param = 0
  105. elif sender == self.ui.measureBox:
  106. param = 1
  107. index += 1
  108. elif sender == self.ui.defaultLengthBox:
  109. param = 2
  110. elif sender == self.ui.quantizeBox:
  111. param = 3
  112. else:
  113. return
  114. self.sendControl(param, index)
  115. # -------------------------------------------------------------------
  116. # DSP Callbacks
  117. def dspParameterChanged(self, index, value):
  118. value = int(value)
  119. if index == 0: # TimeSig
  120. self.ui.timeSigBox.setCurrentIndex(value)
  121. elif index == 1: # Measures
  122. self.ui.measureBox.setCurrentIndex(value-1)
  123. elif index == 2: # DefLength
  124. self.ui.defaultLengthBox.setCurrentIndex(value)
  125. elif index == 3: # Quantize
  126. self.ui.quantizeBox.setCurrentIndex(value)
  127. def dspStateChanged(self, key, value):
  128. pass
  129. # -------------------------------------------------------------------
  130. # ExternalUI Callbacks
  131. def uiShow(self):
  132. self.show()
  133. def uiFocus(self):
  134. self.setWindowState((self.windowState() & ~Qt.WindowMinimized) | Qt.WindowActive)
  135. self.show()
  136. self.raise_()
  137. self.activateWindow()
  138. def uiHide(self):
  139. self.hide()
  140. def uiQuit(self):
  141. self.closeExternalUI()
  142. self.close()
  143. app.quit()
  144. def uiTitleChanged(self, uiTitle):
  145. self.setWindowTitle(uiTitle)
  146. # -------------------------------------------------------------------
  147. # Qt events
  148. def timerEvent(self, event):
  149. if event.timerId() == self.fIdleTimer:
  150. self.idleExternalUI()
  151. QMainWindow.timerEvent(self, event)
  152. def closeEvent(self, event):
  153. self.closeExternalUI()
  154. QMainWindow.closeEvent(self, event)
  155. # there might be other qt windows open which will block the UI from quitting
  156. app.quit()
  157. # -------------------------------------------------------------------
  158. # Custom callback
  159. def updateMeasureBox(self, index):
  160. self.measureBox.setCurrentIndex(index-1)
  161. def sendMsg(self, data):
  162. msg = data[0]
  163. if msg == "midievent-remove":
  164. note, start, length, vel = data[1:5]
  165. note_start = start * 60. / self.fTransportInfo["bpm"] * 4. / self.fTransportInfo["sigDenom"] * self.PPQ
  166. note_stop = note_start + length * 60. / self.fTransportInfo["bpm"] * 4. * self.fTransportInfo["sigNum"] / self.fTransportInfo["sigDenom"] * self.PPQ
  167. self.send([msg, note_start, 3, MIDI_STATUS_NOTE_ON, note, vel])
  168. self.send([msg, note_stop, 3, MIDI_STATUS_NOTE_OFF, note, vel])
  169. elif msg == "midievent-add":
  170. note, start, length, vel = data[1:5]
  171. note_start = start * 60. / self.fTransportInfo["bpm"] * self.PPQ
  172. note_stop = note_start + length * 60. / self.fTransportInfo["bpm"] * 4. * self.fTransportInfo["sigNum"] / self.fTransportInfo["sigDenom"] * self.PPQ
  173. self.send([msg, note_start, 3, MIDI_STATUS_NOTE_ON, note, vel])
  174. self.send([msg, note_stop, 3, MIDI_STATUS_NOTE_OFF, note, vel])
  175. def msgCallback(self, msg):
  176. msg = charPtrToString(msg)
  177. if msg == "midi-clear-all":
  178. # clear all notes
  179. self.ui.piano.clearNotes()
  180. elif msg == "midievent-add":
  181. # adds single midi event
  182. time = int(self.readlineblock())
  183. size = int(self.readlineblock())
  184. data = []
  185. for x in range(size):
  186. data.append(int(self.readlineblock()))
  187. self.handleMidiEvent(time, size, data)
  188. elif msg == "transport":
  189. playing = bool(self.readlineblock() == "true")
  190. frame, bar, beat, tick = [int(i) for i in self.readlineblock().split(":")]
  191. bpm, sigNum, sigDenom = [float(i) for i in self.readlineblock().split(":")]
  192. if beat != self.fTransportInfo["beat"]:
  193. print(beat)
  194. old_frame = self.fTransportInfo['frame']
  195. self.fTransportInfo = {
  196. "playing": playing,
  197. "frame": frame,
  198. "bar": bar,
  199. "beat": beat,
  200. "tick": tick,
  201. "bpm": bpm,
  202. "sigNum": sigNum,
  203. "sigDenom": sigDenom
  204. }
  205. if old_frame != frame:
  206. self.ui.piano.movePlayHead(self.fTransportInfo)
  207. else:
  208. ExternalUI.msgCallback(self, msg)
  209. # -------------------------------------------------------------------
  210. # Internal stuff
  211. def handleMidiEvent(self, time, size, data):
  212. #print("Got MIDI Event on UI", time, size, data)
  213. # NOTE: for now time comes in frames, which might not be desirable
  214. # we'll convert it to a smaller value for now (seconds)
  215. # later on we can have time as PPQ or similar
  216. time /= self.PPQ
  217. status = MIDI_GET_STATUS_FROM_DATA(data)
  218. channel = MIDI_GET_CHANNEL_FROM_DATA(data)
  219. if status == MIDI_STATUS_NOTE_ON:
  220. note = data[1]
  221. velo = data[2]
  222. # append (channel, note, velo, time) for later
  223. self.fPendingNoteOns.append((channel, note, velo, time))
  224. elif status == MIDI_STATUS_NOTE_OFF:
  225. note = data[1]
  226. velo = data[2]
  227. # find previous note-on that matches this note and channel
  228. for noteOnMsg in self.fPendingNoteOns:
  229. channel_, note_, velo_, time_ = noteOnMsg
  230. if channel_ != channel:
  231. continue
  232. if note_ != note:
  233. continue
  234. # found it
  235. #print("{} {} {} {}\n".format(note, time_, time-time_, velo_))
  236. start = time_ / 60. * self.fTransportInfo["bpm"] / 4. * self.fTransportInfo["sigDenom"]
  237. length = (time - time_) / 60. * self.fTransportInfo["bpm"] / 4. / self.fTransportInfo["sigNum"] * self.fTransportInfo["sigDenom"]
  238. self.ui.piano.drawNote(note, start, length, velo_)
  239. # remove from list
  240. self.fPendingNoteOns.remove(noteOnMsg)
  241. break
  242. #--------------- main ------------------
  243. if __name__ == '__main__':
  244. import resources_rc
  245. pathBinaries, pathResources = getPaths()
  246. gCarla.utils = CarlaUtils(os.path.join(pathBinaries, "libcarla_utils." + DLL_EXTENSION))
  247. gCarla.utils.set_process_name("MidiPattern")
  248. app = CarlaApplication("MidiPattern")
  249. gui = MidiPatternW()
  250. app.exit_exec()