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.

435 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. kMidiKey2RectMapHorizontal = {
  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. kMidiKey2RectMapVertical = {
  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. kMidiKeyboard2KeyMap = {
  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. kBlackNotes = (1, 3, 6, 8, 10)
  79. # ------------------------------------------------------------------------------------------------------------
  80. # MIDI Keyboard, using a pixmap for painting
  81. class PixmapKeyboard(QWidget):
  82. # enum Color
  83. COLOR_CLASSIC = 0
  84. COLOR_ORANGE = 1
  85. # enum Orientation
  86. HORIZONTAL = 0
  87. VERTICAL = 1
  88. def __init__(self, parent):
  89. QWidget.__init__(self, parent)
  90. self.fOctaves = 6
  91. self.fLastMouseNote = -1
  92. self.fNeedsUpdate = False
  93. self.fEnabledKeys = []
  94. self.fFont = QFont("Monospace", 8, QFont.Normal)
  95. self.fPixmap = QPixmap("")
  96. self.setCursor(Qt.PointingHandCursor)
  97. self.setMode(self.HORIZONTAL)
  98. def allNotesOff(self):
  99. self.fEnabledKeys = []
  100. self.fNeedsUpdate = True
  101. self.emit(SIGNAL("notesOff()"))
  102. QTimer.singleShot(0, self, SLOT("slot_updateOnce()"))
  103. def sendNoteOn(self, note, sendSignal=True):
  104. if 0 <= note <= 127 and note not in self.fEnabledKeys:
  105. self.fEnabledKeys.append(note)
  106. if sendSignal:
  107. self.emit(SIGNAL("noteOn(int)"), note)
  108. self.fNeedsUpdate = True
  109. QTimer.singleShot(0, self, SLOT("slot_updateOnce()"))
  110. if len(self.fEnabledKeys) == 1:
  111. self.emit(SIGNAL("notesOn()"))
  112. def sendNoteOff(self, note, sendSignal=True):
  113. if 0 <= note <= 127 and note in self.fEnabledKeys:
  114. self.fEnabledKeys.remove(note)
  115. if sendSignal:
  116. self.emit(SIGNAL("noteOff(int)"), note)
  117. self.fNeedsUpdate = True
  118. QTimer.singleShot(0, self, SLOT("slot_updateOnce()"))
  119. if len(self.fEnabledKeys) == 0:
  120. self.emit(SIGNAL("notesOff()"))
  121. def setMode(self, mode, color=COLOR_ORANGE):
  122. if color == self.COLOR_CLASSIC:
  123. self.fColorStr = "classic"
  124. elif color == self.COLOR_ORANGE:
  125. self.fColorStr = "orange"
  126. else:
  127. qCritical("PixmapKeyboard::setMode(%i, %i) - invalid color" % (mode, color))
  128. return self.setMode(mode)
  129. if mode == self.HORIZONTAL:
  130. self.fMidiMap = kMidiKey2RectMapHorizontal
  131. self.fPixmap.load(":/bitmaps/kbd_h_%s.png" % self.fColorStr)
  132. self.fPixmapMode = self.HORIZONTAL
  133. self.fWidth = self.fPixmap.width()
  134. self.fHeight = self.fPixmap.height() / 2
  135. elif mode == self.VERTICAL:
  136. self.fMidiMap = kMidiKey2RectMapVertical
  137. self.fPixmap.load(":/bitmaps/kbd_v_%s.png" % self.fColorStr)
  138. self.fPixmapMode = self.VERTICAL
  139. self.fWidth = self.fPixmap.width() / 2
  140. self.fHeight = self.fPixmap.height()
  141. else:
  142. qCritical("PixmapKeyboard::setMode(%i, %i) - invalid mode" % (mode, color))
  143. return self.setMode(self.HORIZONTAL)
  144. self.setOctaves(self.fOctaves)
  145. def setOctaves(self, octaves):
  146. if octaves < 1:
  147. octaves = 1
  148. elif octaves > 8:
  149. octaves = 8
  150. self.fOctaves = octaves
  151. if self.fPixmapMode == self.HORIZONTAL:
  152. self.setMinimumSize(self.fWidth * self.fOctaves, self.fHeight)
  153. self.setMaximumSize(self.fWidth * self.fOctaves, self.fHeight)
  154. elif self.fPixmapMode == self.VERTICAL:
  155. self.setMinimumSize(self.fWidth, self.fHeight * self.fOctaves)
  156. self.setMaximumSize(self.fWidth, self.fHeight * self.fOctaves)
  157. self.update()
  158. def handleMousePos(self, pos):
  159. if self.fPixmapMode == self.HORIZONTAL:
  160. if pos.x() < 0 or pos.x() > self.fOctaves * 144:
  161. return
  162. posX = pos.x() - 1
  163. octave = int(posX / self.fWidth)
  164. keyPos = QPointF(posX % self.fWidth, pos.y())
  165. elif self.fPixmapMode == self.VERTICAL:
  166. if pos.y() < 0 or pos.y() > self.fOctaves * 144:
  167. return
  168. posY = pos.y() - 1
  169. octave = int(self.fOctaves - posY / self.fHeight)
  170. keyPos = QPointF(pos.x(), posY % self.fHeight)
  171. else:
  172. return
  173. octave += 3
  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 keyPressEvent(self, event):
  209. if not event.isAutoRepeat():
  210. qKey = str(event.key())
  211. if qKey in kMidiKeyboard2KeyMap.keys():
  212. self.sendNoteOn(kMidiKeyboard2KeyMap.get(qKey))
  213. QWidget.keyPressEvent(self, event)
  214. def keyReleaseEvent(self, event):
  215. if not event.isAutoRepeat():
  216. qKey = str(event.key())
  217. if qKey in kMidiKeyboard2KeyMap.keys():
  218. self.sendNoteOff(kMidiKeyboard2KeyMap.get(qKey))
  219. QWidget.keyReleaseEvent(self, event)
  220. def mousePressEvent(self, event):
  221. self.fLastMouseNote = -1
  222. self.handleMousePos(event.pos())
  223. self.setFocus()
  224. QWidget.mousePressEvent(self, event)
  225. def mouseMoveEvent(self, event):
  226. self.handleMousePos(event.pos())
  227. QWidget.mousePressEvent(self, event)
  228. def mouseReleaseEvent(self, event):
  229. if self.fLastMouseNote != -1:
  230. self.sendNoteOff(self.fLastMouseNote)
  231. self.fLastMouseNote = -1
  232. QWidget.mouseReleaseEvent(self, event)
  233. def paintEvent(self, event):
  234. painter = QPainter(self)
  235. event.accept()
  236. # -------------------------------------------------------------
  237. # Paint clean keys (as background)
  238. for octave in range(self.fOctaves):
  239. if self.fPixmapMode == self.HORIZONTAL:
  240. target = QRectF(self.fWidth * octave, 0, self.fWidth, self.fHeight)
  241. elif self.fPixmapMode == self.VERTICAL:
  242. target = QRectF(0, self.fHeight * octave, self.fWidth, self.fHeight)
  243. else:
  244. return
  245. source = QRectF(0, 0, self.fWidth, self.fHeight)
  246. painter.drawPixmap(target, self.fPixmap, source)
  247. # -------------------------------------------------------------
  248. # Paint (white) pressed keys
  249. paintedWhite = False
  250. for i in range(len(self.fEnabledKeys)):
  251. note = self.fEnabledKeys[i]
  252. pos = self._getRectFromMidiNote(note)
  253. if self._isNoteBlack(note):
  254. continue
  255. if note < 36:
  256. # cannot paint this note
  257. continue
  258. elif note < 48:
  259. octave = 0
  260. elif note < 60:
  261. octave = 1
  262. elif note < 72:
  263. octave = 2
  264. elif note < 84:
  265. octave = 3
  266. elif note < 96:
  267. octave = 4
  268. elif note < 108:
  269. octave = 5
  270. elif note < 120:
  271. octave = 6
  272. elif note < 132:
  273. octave = 7
  274. else:
  275. # cannot paint this note either
  276. continue
  277. if self.fPixmapMode == self.VERTICAL:
  278. octave = self.fOctaves - octave - 1
  279. if self.fPixmapMode == self.HORIZONTAL:
  280. target = QRectF(pos.x() + (self.fWidth * octave), 0, pos.width(), pos.height())
  281. source = QRectF(pos.x(), self.fHeight, pos.width(), pos.height())
  282. elif self.fPixmapMode == self.VERTICAL:
  283. target = QRectF(pos.x(), pos.y() + (self.fHeight * octave), pos.width(), pos.height())
  284. source = QRectF(self.fWidth, pos.y(), pos.width(), pos.height())
  285. else:
  286. return
  287. paintedWhite = True
  288. painter.drawPixmap(target, self.fPixmap, source)
  289. # -------------------------------------------------------------
  290. # Clear white keys border
  291. if paintedWhite:
  292. for octave in range(self.fOctaves):
  293. for note in kBlackNotes:
  294. pos = self._getRectFromMidiNote(note)
  295. if self.fPixmapMode == self.HORIZONTAL:
  296. target = QRectF(pos.x() + (self.fWidth * octave), 0, pos.width(), pos.height())
  297. source = QRectF(pos.x(), 0, 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(0, pos.y(), pos.width(), pos.height())
  301. else:
  302. return
  303. painter.drawPixmap(target, self.fPixmap, source)
  304. # -------------------------------------------------------------
  305. # Paint (black) pressed keys
  306. for i in range(len(self.fEnabledKeys)):
  307. note = self.fEnabledKeys[i]
  308. pos = self._getRectFromMidiNote(note)
  309. if not self._isNoteBlack(note):
  310. continue
  311. if note < 36:
  312. # cannot paint this note
  313. continue
  314. elif note < 48:
  315. octave = 0
  316. elif note < 60:
  317. octave = 1
  318. elif note < 72:
  319. octave = 2
  320. elif note < 84:
  321. octave = 3
  322. elif note < 96:
  323. octave = 4
  324. elif note < 108:
  325. octave = 5
  326. elif note < 120:
  327. octave = 6
  328. elif note < 132:
  329. octave = 7
  330. else:
  331. # cannot paint this note either
  332. continue
  333. if self.fPixmapMode == self.VERTICAL:
  334. octave = self.fOctaves - octave - 1
  335. if self.fPixmapMode == self.HORIZONTAL:
  336. target = QRectF(pos.x() + (self.fWidth * octave), 0, pos.width(), pos.height())
  337. source = QRectF(pos.x(), self.fHeight, pos.width(), pos.height())
  338. elif self.fPixmapMode == self.VERTICAL:
  339. target = QRectF(pos.x(), pos.y() + (self.fHeight * octave), pos.width(), pos.height())
  340. source = QRectF(self.fWidth, pos.y(), pos.width(), pos.height())
  341. else:
  342. return
  343. painter.drawPixmap(target, self.fPixmap, source)
  344. # Paint C-number note info
  345. painter.setFont(self.fFont)
  346. painter.setPen(Qt.black)
  347. for i in range(self.fOctaves):
  348. if self.fPixmapMode == self.HORIZONTAL:
  349. painter.drawText(i * 144, 48, 18, 18, Qt.AlignCenter, "C%i" % int(i + 2))
  350. elif self.fPixmapMode == self.VERTICAL:
  351. painter.drawText(45, (self.fOctaves * 144) - (i * 144) - 16, 18, 18, Qt.AlignCenter, "C%i" % int(i + 2))
  352. @pyqtSlot()
  353. def slot_updateOnce(self):
  354. if self.fNeedsUpdate:
  355. self.update()
  356. self.fNeedsUpdate = False
  357. def _isNoteBlack(self, note):
  358. baseNote = note % 12
  359. return bool(baseNote in kBlackNotes)
  360. def _getRectFromMidiNote(self, note):
  361. return self.fMidiMap.get(str(note % 12))