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.

498 lines
16KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Pixmap Keyboard, a custom Qt4 widget
  4. # Copyright (C) 2011-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 (Config)
  19. from carla_config import *
  20. # ------------------------------------------------------------------------------------------------------------
  21. # Imports (Global)
  22. if config_UseQt5:
  23. from PyQt5.QtCore import pyqtSignal, pyqtSlot, qCritical, Qt, QPointF, QRectF, QTimer, QSize
  24. from PyQt5.QtGui import QColor, QFont, QPainter, QPixmap
  25. from PyQt5.QtWidgets import QScrollArea, QWidget
  26. else:
  27. from PyQt4.QtCore import pyqtSignal, pyqtSlot, qCritical, Qt, QPointF, QRectF, QTimer, QSize
  28. from PyQt4.QtGui import QColor, QFont, QPainter, QPixmap, QScrollArea, QWidget
  29. # ------------------------------------------------------------------------------------------------------------
  30. kMidiKey2RectMapHorizontal = {
  31. '0': QRectF(0, 0, 24, 57), # C
  32. '1': QRectF(14, 0, 15, 33), # C#
  33. '2': QRectF(24, 0, 24, 57), # D
  34. '3': QRectF(42, 0, 15, 33), # D#
  35. '4': QRectF(48, 0, 24, 57), # E
  36. '5': QRectF(72, 0, 24, 57), # F
  37. '6': QRectF(84, 0, 15, 33), # F#
  38. '7': QRectF(96, 0, 24, 57), # G
  39. '8': QRectF(112, 0, 15, 33), # G#
  40. '9': QRectF(120, 0, 24, 57), # A
  41. '10': QRectF(140, 0, 15, 33), # A#
  42. '11': QRectF(144, 0, 24, 57) # B
  43. }
  44. kMidiKey2RectMapVertical = {
  45. '11': QRectF(0, 0, 57, 24), # B
  46. '10': QRectF(0, 13, 33, 15), # A#
  47. '9': QRectF(0, 24, 57, 24), # A
  48. '8': QRectF(0, 41, 33, 15), # G#
  49. '7': QRectF(0, 48, 57, 24), # G
  50. '6': QRectF(0, 69, 33, 15), # F#
  51. '5': QRectF(0, 72, 57, 24), # F
  52. '4': QRectF(0, 96, 57, 24), # E
  53. '3': QRectF(0, 111, 33, 15), # D#
  54. '2': QRectF(0, 120, 57, 24), # D
  55. '1': QRectF(0, 139, 33, 15), # C#
  56. '0': QRectF(0, 144, 57, 24) # C
  57. }
  58. kMidiKeyboard2KeyMap = {
  59. # 3th octave
  60. '%i' % Qt.Key_Z: 48,
  61. '%i' % Qt.Key_S: 49,
  62. '%i' % Qt.Key_X: 50,
  63. '%i' % Qt.Key_D: 51,
  64. '%i' % Qt.Key_C: 52,
  65. '%i' % Qt.Key_V: 53,
  66. '%i' % Qt.Key_G: 54,
  67. '%i' % Qt.Key_B: 55,
  68. '%i' % Qt.Key_H: 56,
  69. '%i' % Qt.Key_N: 57,
  70. '%i' % Qt.Key_J: 58,
  71. '%i' % Qt.Key_M: 59,
  72. # 4th octave
  73. '%i' % Qt.Key_Q: 60,
  74. '%i' % Qt.Key_2: 61,
  75. '%i' % Qt.Key_W: 62,
  76. '%i' % Qt.Key_3: 63,
  77. '%i' % Qt.Key_E: 64,
  78. '%i' % Qt.Key_R: 65,
  79. '%i' % Qt.Key_5: 66,
  80. '%i' % Qt.Key_T: 67,
  81. '%i' % Qt.Key_6: 68,
  82. '%i' % Qt.Key_Y: 69,
  83. '%i' % Qt.Key_7: 70,
  84. '%i' % Qt.Key_U: 71,
  85. }
  86. kBlackNotes = (1, 3, 6, 8, 10)
  87. # ------------------------------------------------------------------------------------------------------------
  88. # MIDI Keyboard, using a pixmap for painting
  89. class PixmapKeyboard(QWidget):
  90. # enum Orientation
  91. HORIZONTAL = 0
  92. VERTICAL = 1
  93. # signals
  94. noteOn = pyqtSignal(int)
  95. noteOff = pyqtSignal(int)
  96. notesOn = pyqtSignal()
  97. notesOff = pyqtSignal()
  98. def __init__(self, parent):
  99. QWidget.__init__(self, parent)
  100. self.fOctaves = 6
  101. self.fLastMouseNote = -1
  102. self.fEnabledKeys = []
  103. self.fFont = self.font()
  104. self.fFont.setFamily("Monospace")
  105. self.fFont.setPixelSize(12)
  106. self.fFont.setBold(True)
  107. self.fPixmap = QPixmap("")
  108. self.setCursor(Qt.PointingHandCursor)
  109. self.setMode(self.HORIZONTAL)
  110. def allNotesOff(self, sendSignal=True):
  111. self.fEnabledKeys = []
  112. if sendSignal:
  113. self.notesOff.emit()
  114. self.update()
  115. def sendNoteOn(self, note, sendSignal=True):
  116. if 0 <= note <= 127 and note not in self.fEnabledKeys:
  117. self.fEnabledKeys.append(note)
  118. if sendSignal:
  119. self.noteOn.emit(note)
  120. self.update()
  121. if len(self.fEnabledKeys) == 1:
  122. self.notesOn.emit()
  123. def sendNoteOff(self, note, sendSignal=True):
  124. if 0 <= note <= 127 and note in self.fEnabledKeys:
  125. self.fEnabledKeys.remove(note)
  126. if sendSignal:
  127. self.noteOff.emit(note)
  128. self.update()
  129. if len(self.fEnabledKeys) == 0:
  130. self.notesOff.emit()
  131. def setMode(self, mode):
  132. if mode == self.HORIZONTAL:
  133. self.fMidiMap = kMidiKey2RectMapHorizontal
  134. self.fPixmap.load(":/bitmaps/kbd_h_dark.png")
  135. self.fPixmapMode = self.HORIZONTAL
  136. self.fWidth = self.fPixmap.width()
  137. self.fHeight = self.fPixmap.height() / 2
  138. elif mode == self.VERTICAL:
  139. self.fMidiMap = kMidiKey2RectMapVertical
  140. self.fPixmap.load(":/bitmaps/kbd_v_dark.png")
  141. self.fPixmapMode = self.VERTICAL
  142. self.fWidth = self.fPixmap.width() / 2
  143. self.fHeight = self.fPixmap.height()
  144. else:
  145. qCritical("PixmapKeyboard::setMode(%i) - invalid mode" % mode)
  146. return self.setMode(self.HORIZONTAL)
  147. self.setOctaves(self.fOctaves)
  148. def setOctaves(self, octaves):
  149. if octaves < 1:
  150. octaves = 1
  151. elif octaves > 10:
  152. octaves = 10
  153. self.fOctaves = octaves
  154. if self.fPixmapMode == self.HORIZONTAL:
  155. self.setMinimumSize(self.fWidth * self.fOctaves, self.fHeight)
  156. self.setMaximumSize(self.fWidth * self.fOctaves, self.fHeight)
  157. elif self.fPixmapMode == self.VERTICAL:
  158. self.setMinimumSize(self.fWidth, self.fHeight * self.fOctaves)
  159. self.setMaximumSize(self.fWidth, self.fHeight * self.fOctaves)
  160. self.update()
  161. def handleMousePos(self, pos):
  162. if self.fPixmapMode == self.HORIZONTAL:
  163. if pos.x() < 0 or pos.x() > self.fOctaves * self.fWidth:
  164. return
  165. octave = int(pos.x() / self.fWidth)
  166. keyPos = QPointF(pos.x() % self.fWidth, pos.y())
  167. elif self.fPixmapMode == self.VERTICAL:
  168. if pos.y() < 0 or pos.y() > self.fOctaves * self.fHeight:
  169. return
  170. octave = int(self.fOctaves - pos.y() / self.fHeight)
  171. keyPos = QPointF(pos.x(), (pos.y()-1) % self.fHeight)
  172. else:
  173. return
  174. if self.fMidiMap['1'].contains(keyPos): # C#
  175. note = 1
  176. elif self.fMidiMap['3'].contains(keyPos): # D#
  177. note = 3
  178. elif self.fMidiMap['6'].contains(keyPos): # F#
  179. note = 6
  180. elif self.fMidiMap['8'].contains(keyPos): # G#
  181. note = 8
  182. elif self.fMidiMap['10'].contains(keyPos):# A#
  183. note = 10
  184. elif self.fMidiMap['0'].contains(keyPos): # C
  185. note = 0
  186. elif self.fMidiMap['2'].contains(keyPos): # D
  187. note = 2
  188. elif self.fMidiMap['4'].contains(keyPos): # E
  189. note = 4
  190. elif self.fMidiMap['5'].contains(keyPos): # F
  191. note = 5
  192. elif self.fMidiMap['7'].contains(keyPos): # G
  193. note = 7
  194. elif self.fMidiMap['9'].contains(keyPos): # A
  195. note = 9
  196. elif self.fMidiMap['11'].contains(keyPos):# B
  197. note = 11
  198. else:
  199. note = -1
  200. if note != -1:
  201. note += octave * 12
  202. if self.fLastMouseNote != note:
  203. self.sendNoteOff(self.fLastMouseNote)
  204. self.sendNoteOn(note)
  205. elif self.fLastMouseNote != -1:
  206. self.sendNoteOff(self.fLastMouseNote)
  207. self.fLastMouseNote = note
  208. def minimumSizeHint(self):
  209. return QSize(self.fWidth, self.fHeight)
  210. def sizeHint(self):
  211. if self.fPixmapMode == self.HORIZONTAL:
  212. return QSize(self.fWidth * self.fOctaves, self.fHeight)
  213. elif self.fPixmapMode == self.VERTICAL:
  214. return QSize(self.fWidth, self.fHeight * self.fOctaves)
  215. else:
  216. return QSize(self.fWidth, self.fHeight)
  217. def keyPressEvent(self, event):
  218. if not event.isAutoRepeat():
  219. qKey = str(event.key())
  220. if qKey in kMidiKeyboard2KeyMap.keys():
  221. self.sendNoteOn(kMidiKeyboard2KeyMap.get(qKey))
  222. QWidget.keyPressEvent(self, event)
  223. def keyReleaseEvent(self, event):
  224. if not event.isAutoRepeat():
  225. qKey = str(event.key())
  226. if qKey in kMidiKeyboard2KeyMap.keys():
  227. self.sendNoteOff(kMidiKeyboard2KeyMap.get(qKey))
  228. QWidget.keyReleaseEvent(self, event)
  229. def mousePressEvent(self, event):
  230. self.fLastMouseNote = -1
  231. self.handleMousePos(event.pos())
  232. self.setFocus()
  233. QWidget.mousePressEvent(self, event)
  234. def mouseMoveEvent(self, event):
  235. self.handleMousePos(event.pos())
  236. QWidget.mouseMoveEvent(self, event)
  237. def mouseReleaseEvent(self, event):
  238. if self.fLastMouseNote != -1:
  239. self.sendNoteOff(self.fLastMouseNote)
  240. self.fLastMouseNote = -1
  241. QWidget.mouseReleaseEvent(self, event)
  242. def paintEvent(self, event):
  243. painter = QPainter(self)
  244. event.accept()
  245. # -------------------------------------------------------------
  246. # Paint clean keys (as background)
  247. for octave in range(self.fOctaves):
  248. if self.fPixmapMode == self.HORIZONTAL:
  249. target = QRectF(self.fWidth * octave, 0, self.fWidth, self.fHeight)
  250. elif self.fPixmapMode == self.VERTICAL:
  251. target = QRectF(0, self.fHeight * octave, self.fWidth, self.fHeight)
  252. else:
  253. return
  254. source = QRectF(0, 0, self.fWidth, self.fHeight)
  255. painter.drawPixmap(target, self.fPixmap, source)
  256. if not self.isEnabled():
  257. painter.setBrush(QColor(0, 0, 0, 150))
  258. painter.setPen(QColor(0, 0, 0, 150))
  259. painter.drawRect(0, 0, self.width(), self.height())
  260. return
  261. # -------------------------------------------------------------
  262. # Paint (white) pressed keys
  263. paintedWhite = False
  264. for note in self.fEnabledKeys:
  265. pos = self._getRectFromMidiNote(note)
  266. if self._isNoteBlack(note):
  267. continue
  268. if note < 12:
  269. octave = 0
  270. elif note < 24:
  271. octave = 1
  272. elif note < 36:
  273. octave = 2
  274. elif note < 48:
  275. octave = 3
  276. elif note < 60:
  277. octave = 4
  278. elif note < 72:
  279. octave = 5
  280. elif note < 84:
  281. octave = 6
  282. elif note < 96:
  283. octave = 7
  284. elif note < 108:
  285. octave = 8
  286. elif note < 120:
  287. octave = 9
  288. elif note < 132:
  289. octave = 10
  290. else:
  291. # cannot paint this note
  292. continue
  293. if self.fPixmapMode == self.VERTICAL:
  294. octave = self.fOctaves - octave - 1
  295. if self.fPixmapMode == self.HORIZONTAL:
  296. target = QRectF(pos.x() + (self.fWidth * octave), 0, pos.width(), pos.height())
  297. source = QRectF(pos.x(), self.fHeight, pos.width(), pos.height())
  298. elif self.fPixmapMode == self.VERTICAL:
  299. target = QRectF(pos.x(), pos.y() + (self.fHeight * octave), pos.width(), pos.height())
  300. source = QRectF(self.fWidth, pos.y(), pos.width(), pos.height())
  301. else:
  302. return
  303. paintedWhite = True
  304. painter.drawPixmap(target, self.fPixmap, source)
  305. # -------------------------------------------------------------
  306. # Clear white keys border
  307. if paintedWhite:
  308. for octave in range(self.fOctaves):
  309. for note in kBlackNotes:
  310. pos = self._getRectFromMidiNote(note)
  311. if self.fPixmapMode == self.HORIZONTAL:
  312. target = QRectF(pos.x() + (self.fWidth * octave), 0, pos.width(), pos.height())
  313. source = QRectF(pos.x(), 0, pos.width(), pos.height())
  314. elif self.fPixmapMode == self.VERTICAL:
  315. target = QRectF(pos.x(), pos.y() + (self.fHeight * octave), pos.width(), pos.height())
  316. source = QRectF(0, pos.y(), pos.width(), pos.height())
  317. else:
  318. return
  319. painter.drawPixmap(target, self.fPixmap, source)
  320. # -------------------------------------------------------------
  321. # Paint (black) pressed keys
  322. for note in self.fEnabledKeys:
  323. pos = self._getRectFromMidiNote(note)
  324. if not self._isNoteBlack(note):
  325. continue
  326. if note < 12:
  327. octave = 0
  328. elif note < 24:
  329. octave = 1
  330. elif note < 36:
  331. octave = 2
  332. elif note < 48:
  333. octave = 3
  334. elif note < 60:
  335. octave = 4
  336. elif note < 72:
  337. octave = 5
  338. elif note < 84:
  339. octave = 6
  340. elif note < 96:
  341. octave = 7
  342. elif note < 108:
  343. octave = 8
  344. elif note < 120:
  345. octave = 9
  346. elif note < 132:
  347. octave = 10
  348. else:
  349. # cannot paint this note
  350. continue
  351. if self.fPixmapMode == self.VERTICAL:
  352. octave = self.fOctaves - octave - 1
  353. if self.fPixmapMode == self.HORIZONTAL:
  354. target = QRectF(pos.x() + (self.fWidth * octave), 0, pos.width(), pos.height())
  355. source = QRectF(pos.x(), self.fHeight, pos.width(), pos.height())
  356. elif self.fPixmapMode == self.VERTICAL:
  357. target = QRectF(pos.x(), pos.y() + (self.fHeight * octave), pos.width(), pos.height())
  358. source = QRectF(self.fWidth, pos.y(), pos.width(), pos.height())
  359. else:
  360. return
  361. painter.drawPixmap(target, self.fPixmap, source)
  362. # Paint C-number note info
  363. painter.setFont(self.fFont)
  364. painter.setPen(Qt.black)
  365. for i in range(self.fOctaves):
  366. if self.fPixmapMode == self.HORIZONTAL:
  367. painter.drawText(i * 168 + (4 if i == 0 else 3), 35, 20, 20, Qt.AlignCenter, "C%i" % (i-1))
  368. elif self.fPixmapMode == self.VERTICAL:
  369. painter.drawText(33, (self.fOctaves * 168) - (i * 168) - 20, 20, 20, Qt.AlignCenter, "C%i" % (i-1))
  370. def _isNoteBlack(self, note):
  371. baseNote = note % 12
  372. return bool(baseNote in kBlackNotes)
  373. def _getRectFromMidiNote(self, note):
  374. baseNote = note % 12
  375. return self.fMidiMap.get(str(baseNote))
  376. # ------------------------------------------------------------------------------------------------------------
  377. # Horizontal scroll area for keyboard
  378. class PixmapKeyboardHArea(QScrollArea):
  379. def __init__(self, parent):
  380. QScrollArea.__init__(self, parent)
  381. self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
  382. self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
  383. self.keyboard = PixmapKeyboard(self)
  384. self.keyboard.setOctaves(10)
  385. self.setWidget(self.keyboard)
  386. self.setEnabled(False)
  387. self.setFixedHeight(self.keyboard.height() + self.horizontalScrollBar().height()/2 + 2)
  388. QTimer.singleShot(0, self.slot_initScrollbarValue)
  389. def setEnabled(self, yesNo):
  390. self.keyboard.setEnabled(yesNo)
  391. QScrollArea.setEnabled(self, yesNo)
  392. @pyqtSlot()
  393. def slot_initScrollbarValue(self):
  394. self.horizontalScrollBar().setValue(self.horizontalScrollBar().maximum()/2)
  395. # ------------------------------------------------------------------------------------------------------------
  396. # Main Testing
  397. if __name__ == '__main__':
  398. import sys
  399. from PyQt5.QtWidgets import QApplication
  400. import resources_rc
  401. app = QApplication(sys.argv)
  402. gui = PixmapKeyboard(None)
  403. gui.setMode(gui.HORIZONTAL)
  404. #gui.setMode(gui.VERTICAL)
  405. gui.setEnabled(True)
  406. gui.show()
  407. sys.exit(app.exec_())