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
14KB

  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", 7, 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. self.update()
  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.update()
  109. if len(self.fEnabledKeys) == 1:
  110. self.emit(SIGNAL("notesOn()"))
  111. def sendNoteOff(self, note, sendSignal=True):
  112. if 0 <= note <= 127 and note in self.fEnabledKeys:
  113. self.fEnabledKeys.remove(note)
  114. if sendSignal:
  115. self.emit(SIGNAL("noteOff(int)"), note)
  116. self.update()
  117. if len(self.fEnabledKeys) == 0:
  118. self.emit(SIGNAL("notesOff()"))
  119. def setMode(self, mode, color=COLOR_ORANGE):
  120. if color == self.COLOR_CLASSIC:
  121. self.fColorStr = "classic"
  122. elif color == self.COLOR_ORANGE:
  123. self.fColorStr = "orange"
  124. else:
  125. qCritical("PixmapKeyboard::setMode(%i, %i) - invalid color" % (mode, color))
  126. return self.setMode(mode)
  127. if mode == self.HORIZONTAL:
  128. self.fMidiMap = kMidiKey2RectMapHorizontal
  129. self.fPixmap.load(":/bitmaps/kbd_h_%s.png" % self.fColorStr)
  130. self.fPixmapMode = self.HORIZONTAL
  131. self.fWidth = self.fPixmap.width()
  132. self.fHeight = self.fPixmap.height() / 2
  133. elif mode == self.VERTICAL:
  134. self.fMidiMap = kMidiKey2RectMapVertical
  135. self.fPixmap.load(":/bitmaps/kbd_v_%s.png" % self.fColorStr)
  136. self.fPixmapMode = self.VERTICAL
  137. self.fWidth = self.fPixmap.width() / 2
  138. self.fHeight = self.fPixmap.height()
  139. else:
  140. qCritical("PixmapKeyboard::setMode(%i, %i) - invalid mode" % (mode, color))
  141. return self.setMode(self.HORIZONTAL)
  142. self.setOctaves(self.fOctaves)
  143. def setOctaves(self, octaves):
  144. if octaves < 1:
  145. octaves = 1
  146. elif octaves > 10:
  147. octaves = 10
  148. self.fOctaves = octaves
  149. if self.fPixmapMode == self.HORIZONTAL:
  150. self.setMinimumSize(self.fWidth * self.fOctaves, self.fHeight)
  151. self.setMaximumSize(self.fWidth * self.fOctaves, self.fHeight)
  152. elif self.fPixmapMode == self.VERTICAL:
  153. self.setMinimumSize(self.fWidth, self.fHeight * self.fOctaves)
  154. self.setMaximumSize(self.fWidth, self.fHeight * self.fOctaves)
  155. self.update()
  156. def handleMousePos(self, pos):
  157. if self.fPixmapMode == self.HORIZONTAL:
  158. if pos.x() < 0 or pos.x() > self.fOctaves * 144:
  159. return
  160. posX = pos.x() - 1
  161. octave = int(posX / self.fWidth)
  162. keyPos = QPointF(posX % self.fWidth, pos.y())
  163. elif self.fPixmapMode == self.VERTICAL:
  164. if pos.y() < 0 or pos.y() > self.fOctaves * 144:
  165. return
  166. posY = pos.y() - 1
  167. octave = int(self.fOctaves - posY / self.fHeight)
  168. keyPos = QPointF(pos.x(), posY % self.fHeight)
  169. else:
  170. return
  171. if self.fMidiMap['1'].contains(keyPos): # C#
  172. note = 1
  173. elif self.fMidiMap['3'].contains(keyPos): # D#
  174. note = 3
  175. elif self.fMidiMap['6'].contains(keyPos): # F#
  176. note = 6
  177. elif self.fMidiMap['8'].contains(keyPos): # G#
  178. note = 8
  179. elif self.fMidiMap['10'].contains(keyPos):# A#
  180. note = 10
  181. elif self.fMidiMap['0'].contains(keyPos): # C
  182. note = 0
  183. elif self.fMidiMap['2'].contains(keyPos): # D
  184. note = 2
  185. elif self.fMidiMap['4'].contains(keyPos): # E
  186. note = 4
  187. elif self.fMidiMap['5'].contains(keyPos): # F
  188. note = 5
  189. elif self.fMidiMap['7'].contains(keyPos): # G
  190. note = 7
  191. elif self.fMidiMap['9'].contains(keyPos): # A
  192. note = 9
  193. elif self.fMidiMap['11'].contains(keyPos):# B
  194. note = 11
  195. else:
  196. note = -1
  197. if note != -1:
  198. note += octave * 12
  199. if self.fLastMouseNote != note:
  200. self.sendNoteOff(self.fLastMouseNote)
  201. self.sendNoteOn(note)
  202. elif self.fLastMouseNote != -1:
  203. self.sendNoteOff(self.fLastMouseNote)
  204. self.fLastMouseNote = note
  205. def keyPressEvent(self, event):
  206. if not event.isAutoRepeat():
  207. qKey = str(event.key())
  208. if qKey in kMidiKeyboard2KeyMap.keys():
  209. self.sendNoteOn(kMidiKeyboard2KeyMap.get(qKey))
  210. QWidget.keyPressEvent(self, event)
  211. def keyReleaseEvent(self, event):
  212. if not event.isAutoRepeat():
  213. qKey = str(event.key())
  214. if qKey in kMidiKeyboard2KeyMap.keys():
  215. self.sendNoteOff(kMidiKeyboard2KeyMap.get(qKey))
  216. QWidget.keyReleaseEvent(self, event)
  217. def mousePressEvent(self, event):
  218. self.fLastMouseNote = -1
  219. self.handleMousePos(event.pos())
  220. self.setFocus()
  221. QWidget.mousePressEvent(self, event)
  222. def mouseMoveEvent(self, event):
  223. self.handleMousePos(event.pos())
  224. QWidget.mousePressEvent(self, event)
  225. def mouseReleaseEvent(self, event):
  226. if self.fLastMouseNote != -1:
  227. self.sendNoteOff(self.fLastMouseNote)
  228. self.fLastMouseNote = -1
  229. QWidget.mouseReleaseEvent(self, event)
  230. def paintEvent(self, event):
  231. painter = QPainter(self)
  232. event.accept()
  233. # -------------------------------------------------------------
  234. # Paint clean keys (as background)
  235. for octave in range(self.fOctaves):
  236. if self.fPixmapMode == self.HORIZONTAL:
  237. target = QRectF(self.fWidth * octave, 0, self.fWidth, self.fHeight)
  238. elif self.fPixmapMode == self.VERTICAL:
  239. target = QRectF(0, self.fHeight * octave, self.fWidth, self.fHeight)
  240. else:
  241. return
  242. source = QRectF(0, 0, self.fWidth, self.fHeight)
  243. painter.drawPixmap(target, self.fPixmap, source)
  244. # -------------------------------------------------------------
  245. # Paint (white) pressed keys
  246. paintedWhite = False
  247. for note in self.fEnabledKeys:
  248. pos = self._getRectFromMidiNote(note)
  249. if self._isNoteBlack(note):
  250. continue
  251. if note < 12:
  252. octave = 0
  253. elif note < 24:
  254. octave = 1
  255. elif note < 36:
  256. octave = 2
  257. elif note < 48:
  258. octave = 3
  259. elif note < 60:
  260. octave = 4
  261. elif note < 72:
  262. octave = 5
  263. elif note < 84:
  264. octave = 6
  265. elif note < 96:
  266. octave = 7
  267. elif note < 108:
  268. octave = 8
  269. elif note < 120:
  270. octave = 9
  271. elif note < 132:
  272. octave = 10
  273. else:
  274. # cannot paint this note either
  275. continue
  276. if self.fPixmapMode == self.VERTICAL:
  277. octave = self.fOctaves - octave - 1
  278. if self.fPixmapMode == self.HORIZONTAL:
  279. target = QRectF(pos.x() + (self.fWidth * octave), 0, pos.width(), pos.height())
  280. source = QRectF(pos.x(), self.fHeight, pos.width(), pos.height())
  281. elif self.fPixmapMode == self.VERTICAL:
  282. target = QRectF(pos.x(), pos.y() + (self.fHeight * octave), pos.width(), pos.height())
  283. source = QRectF(self.fWidth, pos.y(), pos.width(), pos.height())
  284. else:
  285. return
  286. paintedWhite = True
  287. painter.drawPixmap(target, self.fPixmap, source)
  288. # -------------------------------------------------------------
  289. # Clear white keys border
  290. if paintedWhite:
  291. for octave in range(self.fOctaves):
  292. for note in kBlackNotes:
  293. pos = self._getRectFromMidiNote(note)
  294. if self.fPixmapMode == self.HORIZONTAL:
  295. target = QRectF(pos.x() + (self.fWidth * octave), 0, pos.width(), pos.height())
  296. source = QRectF(pos.x(), 0, pos.width(), pos.height())
  297. elif self.fPixmapMode == self.VERTICAL:
  298. target = QRectF(pos.x(), pos.y() + (self.fHeight * octave), pos.width(), pos.height())
  299. source = QRectF(0, pos.y(), pos.width(), pos.height())
  300. else:
  301. return
  302. painter.drawPixmap(target, self.fPixmap, source)
  303. # -------------------------------------------------------------
  304. # Paint (black) pressed keys
  305. for note in self.fEnabledKeys:
  306. pos = self._getRectFromMidiNote(note)
  307. if not self._isNoteBlack(note):
  308. continue
  309. if note < 12:
  310. octave = 0
  311. elif note < 24:
  312. octave = 1
  313. elif note < 36:
  314. octave = 2
  315. elif note < 48:
  316. octave = 3
  317. elif note < 60:
  318. octave = 4
  319. elif note < 72:
  320. octave = 5
  321. elif note < 84:
  322. octave = 6
  323. elif note < 96:
  324. octave = 7
  325. elif note < 108:
  326. octave = 8
  327. elif note < 120:
  328. octave = 9
  329. elif note < 132:
  330. octave = 10
  331. else:
  332. # cannot paint this note either
  333. continue
  334. if self.fPixmapMode == self.VERTICAL:
  335. octave = self.fOctaves - octave - 1
  336. if self.fPixmapMode == self.HORIZONTAL:
  337. target = QRectF(pos.x() + (self.fWidth * octave), 0, pos.width(), pos.height())
  338. source = QRectF(pos.x(), self.fHeight, pos.width(), pos.height())
  339. elif self.fPixmapMode == self.VERTICAL:
  340. target = QRectF(pos.x(), pos.y() + (self.fHeight * octave), pos.width(), pos.height())
  341. source = QRectF(self.fWidth, pos.y(), pos.width(), pos.height())
  342. else:
  343. return
  344. painter.drawPixmap(target, self.fPixmap, source)
  345. # Paint C-number note info
  346. painter.setFont(self.fFont)
  347. painter.setPen(Qt.black)
  348. for i in range(self.fOctaves):
  349. if self.fPixmapMode == self.HORIZONTAL:
  350. painter.drawText(i * 144, 48, 18, 18, Qt.AlignCenter, "C%i" % (i-1))
  351. elif self.fPixmapMode == self.VERTICAL:
  352. painter.drawText(45, (self.fOctaves * 144) - (i * 144) - 16, 18, 18, Qt.AlignCenter, "C%i" % (i-1))
  353. def _isNoteBlack(self, note):
  354. baseNote = note % 12
  355. return bool(baseNote in kBlackNotes)
  356. def _getRectFromMidiNote(self, note):
  357. return self.fMidiMap.get(str(note % 12))