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.

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