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.

429 lines
15KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Pixmap Keyboard, a custom Qt4 widget
  4. # Copyright (C) 2011-2013 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 GPL.txt file
  17. # ------------------------------------------------------------------------------------------------------------
  18. # Imports (Global)
  19. from PyQt4.QtCore import pyqtSlot, qCritical, Qt, QPointF, QRectF, QTimer, SIGNAL, SLOT
  20. from PyQt4.QtGui import QFont, QPainter, QPixmap, QWidget
  21. # ------------------------------------------------------------------------------------------------------------
  22. midi_key2rect_map_horizontal = {
  23. '0': QRectF(0, 0, 18, 64), # C
  24. '1': QRectF(13, 0, 11, 42), # C#
  25. '2': QRectF(18, 0, 25, 64), # D
  26. '3': QRectF(37, 0, 11, 42), # D#
  27. '4': QRectF(42, 0, 18, 64), # E
  28. '5': QRectF(60, 0, 18, 64), # F
  29. '6': QRectF(73, 0, 11, 42), # F#
  30. '7': QRectF(78, 0, 25, 64), # G
  31. '8': QRectF(97, 0, 11, 42), # G#
  32. '9': QRectF(102, 0, 25, 64), # A
  33. '10': QRectF(121, 0, 11, 42), # A#
  34. '11': QRectF(126, 0, 18, 64) # B
  35. }
  36. midi_key2rect_map_vertical = {
  37. '11': QRectF(0, 0, 64, 18), # B
  38. '10': QRectF(0, 14, 42, 7), # A#
  39. '9': QRectF(0, 18, 64, 24), # A
  40. '8': QRectF(0, 38, 42, 7), # G#
  41. '7': QRectF(0, 42, 64, 24), # G
  42. '6': QRectF(0, 62, 42, 7), # F#
  43. '5': QRectF(0, 66, 64, 18), # F
  44. '4': QRectF(0, 84, 64, 18), # E
  45. '3': QRectF(0, 98, 42, 7), # D#
  46. '2': QRectF(0, 102, 64, 24), # D
  47. '1': QRectF(0, 122, 42, 7), # C#
  48. '0': QRectF(0, 126, 64, 18) # C
  49. }
  50. midi_keyboard2key_map = {
  51. # 3th octave
  52. '%i' % Qt.Key_Z: 48,
  53. '%i' % Qt.Key_S: 49,
  54. '%i' % Qt.Key_X: 50,
  55. '%i' % Qt.Key_D: 51,
  56. '%i' % Qt.Key_C: 52,
  57. '%i' % Qt.Key_V: 53,
  58. '%i' % Qt.Key_G: 54,
  59. '%i' % Qt.Key_B: 55,
  60. '%i' % Qt.Key_H: 56,
  61. '%i' % Qt.Key_N: 57,
  62. '%i' % Qt.Key_J: 58,
  63. '%i' % Qt.Key_M: 59,
  64. # 4th octave
  65. '%i' % Qt.Key_Q: 60,
  66. '%i' % Qt.Key_2: 61,
  67. '%i' % Qt.Key_W: 62,
  68. '%i' % Qt.Key_3: 63,
  69. '%i' % Qt.Key_E: 64,
  70. '%i' % Qt.Key_R: 65,
  71. '%i' % Qt.Key_5: 66,
  72. '%i' % Qt.Key_T: 67,
  73. '%i' % Qt.Key_6: 68,
  74. '%i' % Qt.Key_Y: 69,
  75. '%i' % Qt.Key_7: 70,
  76. '%i' % Qt.Key_U: 71,
  77. }
  78. # ------------------------------------------------------------------------------------------------------------
  79. # MIDI Keyboard, using a pixmap for painting
  80. class PixmapKeyboard(QWidget):
  81. # enum Color
  82. COLOR_CLASSIC = 0
  83. COLOR_ORANGE = 1
  84. # enum Orientation
  85. HORIZONTAL = 0
  86. VERTICAL = 1
  87. def __init__(self, parent):
  88. QWidget.__init__(self, parent)
  89. self.m_octaves = 6
  90. self.m_lastMouseNote = -1
  91. self.m_needsUpdate = False
  92. self.m_enabledKeys = []
  93. self.m_font = QFont("Monospace", 8, QFont.Normal)
  94. self.m_pixmap = QPixmap("")
  95. self.setCursor(Qt.PointingHandCursor)
  96. self.setMode(self.HORIZONTAL)
  97. def allNotesOff(self):
  98. self.m_enabledKeys = []
  99. self.m_needsUpdate = True
  100. QTimer.singleShot(0, self, SLOT("slot_updateOnce()"))
  101. self.emit(SIGNAL("notesOff()"))
  102. def sendNoteOn(self, note, sendSignal=True):
  103. if 0 <= note <= 127 and note not in self.m_enabledKeys:
  104. self.m_enabledKeys.append(note)
  105. if sendSignal:
  106. self.emit(SIGNAL("noteOn(int)"), note)
  107. self.m_needsUpdate = True
  108. QTimer.singleShot(0, self, SLOT("slot_updateOnce()"))
  109. if len(self.m_enabledKeys) == 1:
  110. self.emit(SIGNAL("notesOn()"))
  111. def sendNoteOff(self, note, sendSignal=True):
  112. if 0 <= note <= 127 and note in self.m_enabledKeys:
  113. self.m_enabledKeys.remove(note)
  114. if sendSignal:
  115. self.emit(SIGNAL("noteOff(int)"), note)
  116. self.m_needsUpdate = True
  117. QTimer.singleShot(0, self, SLOT("slot_updateOnce()"))
  118. if len(self.m_enabledKeys) == 0:
  119. self.emit(SIGNAL("notesOff()"))
  120. def setMode(self, mode, color=COLOR_ORANGE):
  121. if color == self.COLOR_CLASSIC:
  122. self.m_colorStr = "classic"
  123. elif color == self.COLOR_ORANGE:
  124. self.m_colorStr = "orange"
  125. else:
  126. qCritical("PixmapKeyboard::setMode(%i, %i) - invalid color" % (mode, color))
  127. return self.setMode(mode)
  128. if mode == self.HORIZONTAL:
  129. self.m_midiMap = midi_key2rect_map_horizontal
  130. self.m_pixmap.load(":/bitmaps/kbd_h_%s.png" % self.m_colorStr)
  131. self.m_pixmapMode = self.HORIZONTAL
  132. self.p_width = self.m_pixmap.width()
  133. self.p_height = self.m_pixmap.height() / 2
  134. elif mode == self.VERTICAL:
  135. self.m_midiMap = midi_key2rect_map_vertical
  136. self.m_pixmap.load(":/bitmaps/kbd_v_%s.png" % self.m_colorStr)
  137. self.m_pixmapMode = self.VERTICAL
  138. self.p_width = self.m_pixmap.width() / 2
  139. self.p_height = self.m_pixmap.height()
  140. else:
  141. qCritical("PixmapKeyboard::setMode(%i, %i) - invalid mode" % (mode, color))
  142. return self.setMode(self.HORIZONTAL)
  143. self.setOctaves(self.m_octaves)
  144. def setOctaves(self, octaves):
  145. if octaves < 1:
  146. octaves = 1
  147. elif octaves > 8:
  148. octaves = 8
  149. self.m_octaves = octaves
  150. if self.m_pixmapMode == self.HORIZONTAL:
  151. self.setMinimumSize(self.p_width * self.m_octaves, self.p_height)
  152. self.setMaximumSize(self.p_width * self.m_octaves, self.p_height)
  153. elif self.m_pixmapMode == self.VERTICAL:
  154. self.setMinimumSize(self.p_width, self.p_height * self.m_octaves)
  155. self.setMaximumSize(self.p_width, self.p_height * self.m_octaves)
  156. self.update()
  157. def handleMousePos(self, pos):
  158. if self.m_pixmapMode == self.HORIZONTAL:
  159. if pos.x() < 0 or pos.x() > self.m_octaves * 144:
  160. return
  161. posX = pos.x() - 1
  162. octave = int(posX / self.p_width)
  163. n_pos = QPointF(posX % self.p_width, pos.y())
  164. elif self.m_pixmapMode == self.VERTICAL:
  165. if pos.y() < 0 or pos.y() > self.m_octaves * 144:
  166. return
  167. posY = pos.y() - 1
  168. octave = int(self.m_octaves - posY / self.p_height)
  169. n_pos = QPointF(pos.x(), posY % self.p_height)
  170. else:
  171. return
  172. octave += 3
  173. if self.m_midiMap['1'].contains(n_pos): # C#
  174. note = 1
  175. elif self.m_midiMap['3'].contains(n_pos): # D#
  176. note = 3
  177. elif self.m_midiMap['6'].contains(n_pos): # F#
  178. note = 6
  179. elif self.m_midiMap['8'].contains(n_pos): # G#
  180. note = 8
  181. elif self.m_midiMap['10'].contains(n_pos):# A#
  182. note = 10
  183. elif self.m_midiMap['0'].contains(n_pos): # C
  184. note = 0
  185. elif self.m_midiMap['2'].contains(n_pos): # D
  186. note = 2
  187. elif self.m_midiMap['4'].contains(n_pos): # E
  188. note = 4
  189. elif self.m_midiMap['5'].contains(n_pos): # F
  190. note = 5
  191. elif self.m_midiMap['7'].contains(n_pos): # G
  192. note = 7
  193. elif self.m_midiMap['9'].contains(n_pos): # A
  194. note = 9
  195. elif self.m_midiMap['11'].contains(n_pos):# B
  196. note = 11
  197. else:
  198. note = -1
  199. if note != -1:
  200. note += octave * 12
  201. if self.m_lastMouseNote != note:
  202. self.sendNoteOff(self.m_lastMouseNote)
  203. self.sendNoteOn(note)
  204. else:
  205. self.sendNoteOff(self.m_lastMouseNote)
  206. self.m_lastMouseNote = note
  207. def keyPressEvent(self, event):
  208. if not event.isAutoRepeat():
  209. qKey = str(event.key())
  210. if qKey in midi_keyboard2key_map.keys():
  211. self.sendNoteOn(midi_keyboard2key_map.get(qKey))
  212. QWidget.keyPressEvent(self, event)
  213. def keyReleaseEvent(self, event):
  214. if not event.isAutoRepeat():
  215. qKey = str(event.key())
  216. if qKey in midi_keyboard2key_map.keys():
  217. self.sendNoteOff(midi_keyboard2key_map.get(qKey))
  218. QWidget.keyReleaseEvent(self, event)
  219. def mousePressEvent(self, event):
  220. self.m_lastMouseNote = -1
  221. self.handleMousePos(event.pos())
  222. self.setFocus()
  223. QWidget.mousePressEvent(self, event)
  224. def mouseMoveEvent(self, event):
  225. self.handleMousePos(event.pos())
  226. QWidget.mousePressEvent(self, event)
  227. def mouseReleaseEvent(self, event):
  228. if self.m_lastMouseNote != -1:
  229. self.sendNoteOff(self.m_lastMouseNote)
  230. self.m_lastMouseNote = -1
  231. QWidget.mouseReleaseEvent(self, event)
  232. def paintEvent(self, event):
  233. painter = QPainter(self)
  234. # -------------------------------------------------------------
  235. # Paint clean keys (as background)
  236. for octave in range(self.m_octaves):
  237. if self.m_pixmapMode == self.HORIZONTAL:
  238. target = QRectF(self.p_width * octave, 0, self.p_width, self.p_height)
  239. elif self.m_pixmapMode == self.VERTICAL:
  240. target = QRectF(0, self.p_height * octave, self.p_width, self.p_height)
  241. else:
  242. return
  243. source = QRectF(0, 0, self.p_width, self.p_height)
  244. painter.drawPixmap(target, self.m_pixmap, source)
  245. # -------------------------------------------------------------
  246. # Paint (white) pressed keys
  247. paintedWhite = False
  248. for i in range(len(self.m_enabledKeys)):
  249. note = self.m_enabledKeys[i]
  250. pos = self._getRectFromMidiNote(note)
  251. if self._isNoteBlack(note):
  252. continue
  253. if note < 36:
  254. # cannot paint this note
  255. continue
  256. elif note < 48:
  257. octave = 0
  258. elif note < 60:
  259. octave = 1
  260. elif note < 72:
  261. octave = 2
  262. elif note < 84:
  263. octave = 3
  264. elif note < 96:
  265. octave = 4
  266. elif note < 108:
  267. octave = 5
  268. elif note < 120:
  269. octave = 6
  270. elif note < 132:
  271. octave = 7
  272. else:
  273. # cannot paint this note either
  274. continue
  275. if self.m_pixmapMode == self.VERTICAL:
  276. octave = self.m_octaves - octave - 1
  277. if self.m_pixmapMode == self.HORIZONTAL:
  278. target = QRectF(pos.x() + (self.p_width * octave), 0, pos.width(), pos.height())
  279. source = QRectF(pos.x(), self.p_height, pos.width(), pos.height())
  280. elif self.m_pixmapMode == self.VERTICAL:
  281. target = QRectF(pos.x(), pos.y() + (self.p_height * octave), pos.width(), pos.height())
  282. source = QRectF(self.p_width, pos.y(), pos.width(), pos.height())
  283. else:
  284. return
  285. paintedWhite = True
  286. painter.drawPixmap(target, self.m_pixmap, source)
  287. # -------------------------------------------------------------
  288. # Clear white keys border
  289. if paintedWhite:
  290. for octave in range(self.m_octaves):
  291. for note in (1, 3, 6, 8, 10):
  292. pos = self._getRectFromMidiNote(note)
  293. if self.m_pixmapMode == self.HORIZONTAL:
  294. target = QRectF(pos.x() + (self.p_width * octave), 0, pos.width(), pos.height())
  295. source = QRectF(pos.x(), 0, pos.width(), pos.height())
  296. elif self.m_pixmapMode == self.VERTICAL:
  297. target = QRectF(pos.x(), pos.y() + (self.p_height * octave), pos.width(), pos.height())
  298. source = QRectF(0, pos.y(), pos.width(), pos.height())
  299. else:
  300. return
  301. painter.drawPixmap(target, self.m_pixmap, source)
  302. # -------------------------------------------------------------
  303. # Paint (black) pressed keys
  304. for i in range(len(self.m_enabledKeys)):
  305. note = self.m_enabledKeys[i]
  306. pos = self._getRectFromMidiNote(note)
  307. if not self._isNoteBlack(note):
  308. continue
  309. if note < 36:
  310. # cannot paint this note
  311. continue
  312. elif note < 48:
  313. octave = 0
  314. elif note < 60:
  315. octave = 1
  316. elif note < 72:
  317. octave = 2
  318. elif note < 84:
  319. octave = 3
  320. elif note < 96:
  321. octave = 4
  322. elif note < 108:
  323. octave = 5
  324. elif note < 120:
  325. octave = 6
  326. elif note < 132:
  327. octave = 7
  328. else:
  329. # cannot paint this note either
  330. continue
  331. if self.m_pixmapMode == self.VERTICAL:
  332. octave = self.m_octaves - octave - 1
  333. if self.m_pixmapMode == self.HORIZONTAL:
  334. target = QRectF(pos.x() + (self.p_width * octave), 0, pos.width(), pos.height())
  335. source = QRectF(pos.x(), self.p_height, pos.width(), pos.height())
  336. elif self.m_pixmapMode == self.VERTICAL:
  337. target = QRectF(pos.x(), pos.y() + (self.p_height * octave), pos.width(), pos.height())
  338. source = QRectF(self.p_width, pos.y(), pos.width(), pos.height())
  339. else:
  340. return
  341. painter.drawPixmap(target, self.m_pixmap, source)
  342. # Paint C-number note info
  343. painter.setFont(self.m_font)
  344. painter.setPen(Qt.black)
  345. for i in range(self.m_octaves):
  346. if self.m_pixmapMode == self.HORIZONTAL:
  347. painter.drawText(i * 144, 48, 18, 18, Qt.AlignCenter, "C%i" % int(i + 2))
  348. elif self.m_pixmapMode == self.VERTICAL:
  349. painter.drawText(45, (self.m_octaves * 144) - (i * 144) - 16, 18, 18, Qt.AlignCenter, "C%i" % int(i + 2))
  350. event.accept()
  351. @pyqtSlot()
  352. def slot_updateOnce(self):
  353. if self.m_needsUpdate:
  354. self.update()
  355. self.m_needsUpdate = False
  356. def _isNoteBlack(self, note):
  357. baseNote = note % 12
  358. return bool(baseNote in (1, 3, 6, 8, 10))
  359. def _getRectFromMidiNote(self, note):
  360. return self.m_midiMap.get(str(note % 12))