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.

677 lines
28KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # A piano roll viewer/editor
  4. # Copyright (C) 2012-2015 Filipe Coelho <falktx@falktx.com>
  5. # Copyright (C) 2014-2015 tatch
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License as
  9. # published by the Free Software Foundation; either version 2 of
  10. # the License, or any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # For a full copy of the GNU General Public License see the doc/GPL.txt file.
  18. # ------------------------------------------------------------------------------------------------------------
  19. # Imports (Config)
  20. from carla_config import *
  21. # ------------------------------------------------------------------------------------------------------------
  22. # Imports (Global)
  23. if config_UseQt5:
  24. from PyQt5.QtCore import Qt, QRectF
  25. from PyQt5.QtGui import QColor, QFont, QPen
  26. from PyQt5.QtWidgets import QGraphicsItem, QGraphicsLineItem, QGraphicsOpacityEffect, QGraphicsRectItem, QGraphicsSimpleTextItem
  27. from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView
  28. else:
  29. from PyQt4.QtCore import Qt, QRectF
  30. from PyQt4.QtGui import QColor, QFont, QPen
  31. from PyQt4.QtGui import QGraphicsItem, QGraphicsLineItem, QGraphicsOpacityEffect, QGraphicsRectItem, QGraphicsSimpleTextItem
  32. from PyQt4.QtGui import QGraphicsScene, QGraphicsView
  33. # ------------------------------------------------------------------------------------------------------------
  34. # Imports (Custom)
  35. from carla_shared import *
  36. from carla_utils import *
  37. # ------------------------------------------------------------------------------------------------------------
  38. # Imports (ExternalUI)
  39. from carla_app import CarlaApplication
  40. from externalui import ExternalUI
  41. # ------------------------------------------------------------------------------------------------------------
  42. # MIDI definitions, copied from CarlaMIDI.h
  43. MAX_MIDI_CHANNELS = 16
  44. MAX_MIDI_NOTE = 128
  45. MAX_MIDI_VALUE = 128
  46. MAX_MIDI_CONTROL = 120 # 0x77
  47. MIDI_STATUS_BIT = 0xF0
  48. MIDI_CHANNEL_BIT = 0x0F
  49. # MIDI Messages List
  50. MIDI_STATUS_NOTE_OFF = 0x80 # note (0-127), velocity (0-127)
  51. MIDI_STATUS_NOTE_ON = 0x90 # note (0-127), velocity (0-127)
  52. MIDI_STATUS_POLYPHONIC_AFTERTOUCH = 0xA0 # note (0-127), pressure (0-127)
  53. MIDI_STATUS_CONTROL_CHANGE = 0xB0 # see 'Control Change Messages List'
  54. MIDI_STATUS_PROGRAM_CHANGE = 0xC0 # program (0-127), none
  55. MIDI_STATUS_CHANNEL_PRESSURE = 0xD0 # pressure (0-127), none
  56. MIDI_STATUS_PITCH_WHEEL_CONTROL = 0xE0 # LSB (0-127), MSB (0-127)
  57. # MIDI Message type
  58. def MIDI_IS_CHANNEL_MESSAGE(status): return status >= MIDI_STATUS_NOTE_OFF and status < MIDI_STATUS_BIT
  59. def MIDI_IS_SYSTEM_MESSAGE(status): return status >= MIDI_STATUS_BIT and status <= 0xFF
  60. def MIDI_IS_OSC_MESSAGE(status): return status == '/' or status == '#'
  61. # MIDI Channel message type
  62. def MIDI_IS_STATUS_NOTE_OFF(status): return MIDI_IS_CHANNEL_MESSAGE(status) and (status & MIDI_STATUS_BIT) == MIDI_STATUS_NOTE_OFF
  63. def MIDI_IS_STATUS_NOTE_ON(status): return MIDI_IS_CHANNEL_MESSAGE(status) and (status & MIDI_STATUS_BIT) == MIDI_STATUS_NOTE_ON
  64. def MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status): return MIDI_IS_CHANNEL_MESSAGE(status) and (status & MIDI_STATUS_BIT) == MIDI_STATUS_POLYPHONIC_AFTERTOUCH
  65. def MIDI_IS_STATUS_CONTROL_CHANGE(status): return MIDI_IS_CHANNEL_MESSAGE(status) and (status & MIDI_STATUS_BIT) == MIDI_STATUS_CONTROL_CHANGE
  66. def MIDI_IS_STATUS_PROGRAM_CHANGE(status): return MIDI_IS_CHANNEL_MESSAGE(status) and (status & MIDI_STATUS_BIT) == MIDI_STATUS_PROGRAM_CHANGE
  67. def MIDI_IS_STATUS_CHANNEL_PRESSURE(status): return MIDI_IS_CHANNEL_MESSAGE(status) and (status & MIDI_STATUS_BIT) == MIDI_STATUS_CHANNEL_PRESSURE
  68. def MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status): return MIDI_IS_CHANNEL_MESSAGE(status) and (status & MIDI_STATUS_BIT) == MIDI_STATUS_PITCH_WHEEL_CONTROL
  69. # MIDI Utils
  70. def MIDI_GET_STATUS_FROM_DATA(data): return data[0] & MIDI_STATUS_BIT if MIDI_IS_CHANNEL_MESSAGE(data[0]) else data[0]
  71. def MIDI_GET_CHANNEL_FROM_DATA(data): return data[0] & MIDI_CHANNEL_BIT if MIDI_IS_CHANNEL_MESSAGE(data[0]) else 0
  72. # ------------------------------------------------------------------------------------------------------------
  73. # Global variables and functions
  74. global_piano_roll_snap = True
  75. global_piano_roll_grid_width = 1000.0
  76. global_piano_roll_grid_max_start_time = 999.0
  77. global_piano_roll_note_height = 10
  78. global_piano_roll_snap_value = global_piano_roll_grid_width / 64.0
  79. global_piano_roll_note_count = 120
  80. global_piano_keys_width = 34
  81. global_piano_roll_header_height = 20
  82. global_piano_roll_total_height = 1000 #this gets changed
  83. global_piano_roll_quantize_choices = ['None','1/4','1/3','1/2','1']
  84. def piano_roll_quantize(a_index):
  85. global global_piano_roll_snap_value
  86. global global_piano_roll_snap
  87. if a_index == 0:
  88. global_piano_roll_snap = False
  89. elif a_index == 1:
  90. global_piano_roll_snap_value = global_piano_roll_grid_width / 16.0
  91. global_piano_roll_snap = True
  92. elif a_index == 2:
  93. global_piano_roll_snap_value = global_piano_roll_grid_width / 12.0
  94. global_piano_roll_snap = True
  95. elif a_index == 3:
  96. global_piano_roll_snap_value = global_piano_roll_grid_width / 8.0
  97. global_piano_roll_snap = True
  98. elif a_index == 4:
  99. global_piano_roll_snap_value = global_piano_roll_grid_width / 4.0
  100. global_piano_roll_snap = True
  101. def snap(a_pos_x, a_pos_y = None):
  102. if global_piano_roll_snap:
  103. f_pos_x = int((a_pos_x - global_piano_keys_width) / global_piano_roll_snap_value) * global_piano_roll_snap_value + global_piano_keys_width
  104. if a_pos_y:
  105. f_pos_y = int((a_pos_y - global_piano_roll_header_height) / global_piano_roll_note_height) * global_piano_roll_note_height + global_piano_roll_header_height
  106. return (f_pos_x, f_pos_y)
  107. else:
  108. return f_pos_x
  109. # ------------------------------------------------------------------------------------------------------------
  110. # Graphics Items
  111. class note_item(QGraphicsRectItem):
  112. '''a note on the pianoroll sequencer'''
  113. def __init__(self, a_length, a_note_height, a_note_num):
  114. QGraphicsRectItem.__init__(self, 0, 0, a_length, a_note_height)
  115. self.setFlag(QGraphicsItem.ItemIsMovable)
  116. self.setFlag(QGraphicsItem.ItemIsSelectable)
  117. self.setFlag(QGraphicsItem.ItemSendsGeometryChanges)
  118. self.setAcceptHoverEvents(True)
  119. self.o_brush = QColor(100, 0, 0)
  120. self.hover_brush = QColor(200, 200, 100)
  121. self.s_brush = QColor(200, 100, 100)
  122. self.setBrush(self.o_brush)
  123. self.note_height = a_note_height
  124. self.note_num = a_note_num
  125. self.pressed = False
  126. self.selected = False
  127. def select(self):
  128. self.setSelected(True)
  129. self.selected = True
  130. self.setBrush(self.s_brush)
  131. def deselect(self):
  132. self.setSelected(False)
  133. self.selected = False
  134. self.setBrush(self.o_brush)
  135. def hoverEnterEvent(self, a_event):
  136. QGraphicsRectItem.hoverEnterEvent(self, a_event)
  137. if not self.selected:
  138. self.setBrush(self.hover_brush)
  139. def hoverLeaveEvent(self, a_event):
  140. QGraphicsRectItem.hoverLeaveEvent(self, a_event)
  141. if not self.selected:
  142. self.setBrush(self.o_brush)
  143. elif self.selected:
  144. self.setBrush(self.s_brush)
  145. def mousePressEvent(self, a_event):
  146. a_event.setAccepted(True)
  147. QGraphicsRectItem.mousePressEvent(self, a_event)
  148. self.select()
  149. self.pressed = True
  150. def mouseMoveEvent(self, a_event):
  151. QGraphicsRectItem.mouseMoveEvent(self, a_event)
  152. f_pos_x = self.pos().x()
  153. f_pos_y = self.pos().y()
  154. if f_pos_x < global_piano_keys_width:
  155. f_pos_x = global_piano_keys_width
  156. elif f_pos_x > global_piano_roll_grid_max_start_time:
  157. f_pos_x = global_piano_roll_grid_max_start_time
  158. if f_pos_y < global_piano_roll_header_height:
  159. f_pos_y = global_piano_roll_header_height
  160. elif f_pos_y > global_piano_roll_total_height:
  161. f_pos_y = global_piano_roll_total_height
  162. (f_pos_x, f_pos_y,) = snap(f_pos_x, f_pos_y)
  163. self.setPos(f_pos_x, f_pos_y)
  164. def mouseReleaseEvent(self, a_event):
  165. a_event.setAccepted(True)
  166. QGraphicsRectItem.mouseReleaseEvent(self, a_event)
  167. self.pressed = False
  168. if a_event.button() == Qt.LeftButton:
  169. (f_pos_x, f_pos_y,) = snap(self.pos().x(), self.pos().y())
  170. self.setPos(f_pos_x, f_pos_y)
  171. f_new_note_start = (f_pos_x - global_piano_keys_width) * 0.001 * 4.0
  172. f_new_note_num = int(global_piano_roll_note_count - (f_pos_y - global_piano_roll_header_height) / global_piano_roll_note_height)
  173. #print("note start: ", str(f_new_note_start))
  174. print("MIDI number: ", str(f_new_note_num))
  175. class piano_key_item(QGraphicsRectItem):
  176. def __init__(self, width, height, parent):
  177. QGraphicsRectItem.__init__(self, 0, 0, width, height, parent)
  178. self.width = width
  179. self.height = height
  180. self.setFlag(QGraphicsItem.ItemSendsGeometryChanges)
  181. self.setAcceptHoverEvents(True)
  182. self.hover_brush = QColor(200, 0, 0)
  183. self.click_brush = QColor(255, 100, 100)
  184. self.pressed = False
  185. def hoverEnterEvent(self, a_event):
  186. QGraphicsRectItem.hoverEnterEvent(self, a_event)
  187. self.o_brush = self.brush()
  188. self.setBrush(self.hover_brush)
  189. def hoverLeaveEvent(self, a_event):
  190. if self.pressed:
  191. self.pressed = False
  192. self.setBrush(self.hover_brush)
  193. QGraphicsRectItem.hoverLeaveEvent(self, a_event)
  194. self.setBrush(self.o_brush)
  195. def mousePressEvent(self, a_event):
  196. self.pressed = True
  197. self.setBrush(self.click_brush)
  198. def mouseMoveEvent(self, a_event):
  199. """this may eventually do something"""
  200. pass
  201. def mouseReleaseEvent(self, a_event):
  202. self.pressed = False
  203. QGraphicsRectItem.mouseReleaseEvent(self, a_event)
  204. self.setBrush(self.hover_brush)
  205. # ------------------------------------------------------------------------------------------------------------
  206. # External UI
  207. class piano_roll(ExternalUI, QGraphicsView):
  208. '''the piano roll'''
  209. def __init__(self, a_item_length = 4, a_grid_div = 4):
  210. ExternalUI.__init__(self)
  211. QGraphicsView.__init__(self)
  212. global global_piano_roll_total_height
  213. self.item_length = float(a_item_length)
  214. self.viewer_width = 1000
  215. self.grid_div = a_grid_div
  216. self.end_octave = 8
  217. self.start_octave = -2
  218. self.notes_in_octave = 12
  219. self.total_notes = (self.end_octave - self.start_octave) * self.notes_in_octave + 1
  220. self.note_height = global_piano_roll_note_height
  221. self.octave_height = self.notes_in_octave * self.note_height
  222. self.header_height = global_piano_roll_header_height
  223. self.piano_height = self.note_height * self.total_notes
  224. self.padding = 2
  225. self.piano_width = global_piano_keys_width - self.padding
  226. self.piano_height = self.note_height * self.total_notes
  227. global_piano_roll_total_height = self.piano_height - self.note_height + global_piano_roll_header_height
  228. self.scene = QGraphicsScene(self)
  229. self.scene.mousePressEvent = self.sceneMousePressEvent
  230. self.scene.mouseReleaseEvent = self.sceneMouseReleaseEvent
  231. self.scene.mouseMoveEvent = self.sceneMouseMoveEvent
  232. self.scene.setBackgroundBrush(QColor(100, 100, 100))
  233. self.setScene(self.scene)
  234. self.setAlignment(Qt.AlignLeft)
  235. self.notes = []
  236. self.selected_notes = []
  237. self.piano_keys = []
  238. self.ghost_vel = 100
  239. self.show_ghost = True
  240. self.marquee_select = False
  241. self.insert_mode = False
  242. self.place_ghost = False
  243. self.draw_piano()
  244. self.draw_header()
  245. self.draw_grid()
  246. # to be filled with note-on events, while waiting for their matching note-off
  247. self.fPendingNoteOns = [] # (channel, note, velocity, time)
  248. self.fIdleTimer = self.startTimer(30)
  249. self.setWindowTitle(self.fUiName)
  250. self.ready()
  251. # -------------------------------------------------------------------
  252. # DSP Callbacks
  253. def dspParameterChanged(self, index, value):
  254. pass
  255. def dspStateChanged(self, key, value):
  256. pass
  257. # -------------------------------------------------------------------
  258. # ExternalUI Callbacks
  259. def uiShow(self):
  260. self.show()
  261. def uiFocus(self):
  262. self.setWindowState((self.windowState() & ~Qt.WindowMinimized) | Qt.WindowActive)
  263. self.show()
  264. self.raise_()
  265. self.activateWindow()
  266. def uiHide(self):
  267. self.hide()
  268. def uiQuit(self):
  269. self.closeExternalUI()
  270. self.close()
  271. app.quit()
  272. def uiTitleChanged(self, uiTitle):
  273. self.setWindowTitle(uiTitle)
  274. # -------------------------------------------------------------------
  275. # Qt events
  276. def timerEvent(self, event):
  277. if event.timerId() == self.fIdleTimer:
  278. self.idleExternalUI()
  279. QGraphicsView.timerEvent(self, event)
  280. def closeEvent(self, event):
  281. self.closeExternalUI()
  282. QGraphicsView.closeEvent(self, event)
  283. # -------------------------------------------------------------------
  284. # Custom callback
  285. def msgCallback(self, msg):
  286. #try:
  287. self.msgCallback2(msg)
  288. #except:
  289. #print("Custom msgCallback error, skipped for", msg)
  290. def msgCallback2(self, msg):
  291. msg = charPtrToString(msg)
  292. if msg == "midi-clear-all":
  293. # clear all notes
  294. self.clear_drawn_items()
  295. elif msg == "midievent-add":
  296. # adds single midi event
  297. time = int(self.readlineblock())
  298. size = int(self.readlineblock())
  299. data = []
  300. for x in range(size):
  301. data.append(int(self.readlineblock()))
  302. self.handleMidiEvent(time, size, data)
  303. elif msg == "show":
  304. self.uiShow()
  305. elif msg == "focus":
  306. self.uiFocus()
  307. elif msg == "hide":
  308. self.uiHide()
  309. elif msg == "quit":
  310. self.fQuitReceived = True
  311. self.uiQuit()
  312. elif msg == "uiTitle":
  313. uiTitle = self.readlineblock()
  314. self.uiTitleChanged(uiTitle)
  315. else:
  316. print("unknown message: \"" + msg + "\"")
  317. # -------------------------------------------------------------------
  318. # Internal stuff
  319. def handleMidiEvent(self, time, size, data):
  320. print("Got MIDI Event on UI", time, size, data)
  321. # NOTE: for now time comes in frames, which might not be desirable
  322. # we'll convert it to a smaller value for now (seconds)
  323. # later on we can have time as PPQ or similar
  324. time /= self.getSampleRate()
  325. status = MIDI_GET_STATUS_FROM_DATA(data)
  326. channel = MIDI_GET_CHANNEL_FROM_DATA(data)
  327. if status == MIDI_STATUS_NOTE_ON:
  328. note = data[1]
  329. velo = data[2]
  330. # append (channel, note, velo, time) for later
  331. self.fPendingNoteOns.append((channel, note, velo, time))
  332. elif status == MIDI_STATUS_NOTE_OFF:
  333. note = data[1]
  334. velo = data[2]
  335. # find previous note-on that matches this note and channel
  336. for noteOnMsg in self.fPendingNoteOns:
  337. channel_, note_, velo_, time_ = noteOnMsg
  338. if channel_ != channel:
  339. continue
  340. if note_ != note:
  341. continue
  342. # found it
  343. self.draw_note(note, time_, time - time_, velo_)
  344. # remove from list
  345. self.fPendingNoteOns.remove(noteOnMsg)
  346. break
  347. def make_ghostnote(self, pos_x, pos_y):
  348. """creates the ghostnote that is placed on the scene before the real one is."""
  349. f_length = self.beat_width / 4
  350. (f_start, f_note,) = snap(pos_x, pos_y)
  351. self.ghost_rect_orig = QRectF(f_start, f_note, f_length, self.note_height)
  352. self.ghost_rect = QRectF(self.ghost_rect_orig)
  353. self.ghost_note = QGraphicsRectItem(self.ghost_rect)
  354. self.ghost_note.setBrush(QColor(230, 221, 45, 100))
  355. self.scene.addItem(self.ghost_note)
  356. def keyPressEvent(self, a_event):
  357. QGraphicsView.keyPressEvent(self, a_event)
  358. if a_event.key() == Qt.Key_B:
  359. if not self.insert_mode:
  360. self.insert_mode = True
  361. if self.show_ghost:
  362. self.make_ghostnote(self.piano_width, self.header_height)
  363. elif self.insert_mode:
  364. self.insert_mode = False
  365. if self.place_ghost:
  366. self.place_ghost = False
  367. self.scene.removeItem(self.ghost_note)
  368. elif self.show_ghost:
  369. self.scene.removeItem(self.ghost_note)
  370. if a_event.key() == Qt.Key_Delete or a_event.key() == Qt.Key_Backspace:
  371. for note in self.selected_notes:
  372. note.deselect()
  373. self.scene.removeItem(note)
  374. def sceneMousePressEvent(self, a_event):
  375. QGraphicsScene.mousePressEvent(self.scene, a_event)
  376. if not (any(key.pressed for key in self.piano_keys) or any(note.pressed for note in self.notes)):
  377. #if not self.scene.mouseGrabberItem():
  378. for note in self.selected_notes:
  379. note.deselect()
  380. self.selected_notes = []
  381. self.f_pos = a_event.scenePos()
  382. if a_event.button() == Qt.LeftButton:
  383. if self.insert_mode:
  384. self.place_ghost = True
  385. else:
  386. self.marquee_select = True
  387. self.marquee_rect = QRectF(self.f_pos.x(), self.f_pos.y(), 1, 1)
  388. self.marquee = QGraphicsRectItem(self.marquee_rect)
  389. self.marquee.setBrush(QColor(255, 255, 255, 100))
  390. self.scene.addItem(self.marquee)
  391. else:
  392. out = False
  393. for note in self.notes:
  394. if note.pressed and note in self.selected_notes:
  395. break
  396. elif note.pressed and note not in self.selected_notes:
  397. s_note = note
  398. out = True
  399. break
  400. if out == True:
  401. for note in self.selected_notes:
  402. note.deselect()
  403. self.selected_notes = [s_note]
  404. elif out == False:
  405. for note in self.selected_notes:
  406. note.mousePressEvent(a_event)
  407. def sceneMouseMoveEvent(self, a_event):
  408. QGraphicsScene.mouseMoveEvent(self.scene, a_event)
  409. #if not (any((key.pressed for key in self.piano_keys)) or any((note.pressed for note in self.notes))):
  410. if not (any((key.pressed for key in self.piano_keys))):
  411. #if not self.scene.mouseGrabberItem():
  412. m_pos = a_event.scenePos()
  413. if self.insert_mode and self.place_ghost: #placing a note
  414. #bind velocity to vertical mouse movement
  415. self.ghost_vel = self.ghost_vel + (a_event.lastScenePos().y() - m_pos.y())/10
  416. if self.ghost_vel < 0:
  417. self.ghost_vel = 0
  418. elif self.ghost_vel > 127:
  419. self.ghost_vel = 127
  420. #print "velocity:", self.ghost_vel
  421. m_width = self.ghost_rect.x() + self.ghost_rect_orig.width()
  422. if m_pos.x() < m_width:
  423. m_pos.setX(m_width)
  424. m_new_x = snap(m_pos.x())
  425. self.ghost_rect.setRight(m_new_x)
  426. self.ghost_note.setRect(self.ghost_rect)
  427. else:
  428. if m_pos.x() < self.piano_width:
  429. m_pos.setX(self.piano_width)
  430. elif m_pos.x() > self.viewer_width:
  431. m_pos.setX(self.viewer_width)
  432. if m_pos.y() < self.header_height:
  433. m_pos.setY(self.header_height)
  434. if self.insert_mode and self.show_ghost: #ghostnote follows mouse around
  435. (m_new_x, m_new_y,) = snap(m_pos.x(), m_pos.y())
  436. self.ghost_rect.moveTo(m_new_x, m_new_y)
  437. self.ghost_note.setRect(self.ghost_rect)
  438. elif self.marquee_select:
  439. if self.f_pos.x() < m_pos.x() and self.f_pos.y() < m_pos.y():
  440. self.marquee_rect.setBottomRight(m_pos)
  441. elif self.f_pos.x() < m_pos.x() and self.f_pos.y() > m_pos.y():
  442. self.marquee_rect.setTopRight(m_pos)
  443. elif self.f_pos.x() > m_pos.x() and self.f_pos.y() < m_pos.y():
  444. self.marquee_rect.setBottomLeft(m_pos)
  445. elif self.f_pos.x() > m_pos.x() and self.f_pos.y() > m_pos.y():
  446. self.marquee_rect.setTopLeft(m_pos)
  447. self.marquee.setRect(self.marquee_rect)
  448. self.selected_notes = []
  449. for item in self.scene.collidingItems(self.marquee):
  450. if item in self.notes:
  451. self.selected_notes.append(item)
  452. for note in self.notes:
  453. if note in self.selected_notes:
  454. note.select()
  455. else:
  456. note.deselect()
  457. elif not self.marquee_select:
  458. for note in self.selected_notes:
  459. note.mouseMoveEvent(a_event)
  460. def sceneMouseReleaseEvent(self, a_event):
  461. QGraphicsScene.mouseReleaseEvent(self.scene, a_event)
  462. if not (any((key.pressed for key in self.piano_keys)) or any((note.pressed for note in self.notes))):
  463. #if not self.scene.mouseGrabberItem():
  464. if a_event.button() == Qt.LeftButton:
  465. if self.place_ghost and self.insert_mode:
  466. self.place_ghost = False
  467. f_final_scenePos = a_event.scenePos().x()
  468. f_final_scenePos = snap(f_final_scenePos)
  469. f_delta = f_final_scenePos - self.ghost_rect.x()
  470. if f_delta < self.beat_width / 8:
  471. f_delta = self.beat_width / 4
  472. f_length = f_delta / self.viewer_width * 4
  473. f_start = (self.ghost_rect.x() - self.piano_width - self.padding) / self.beat_width
  474. f_note = self.total_notes - (self.ghost_rect.y() - self.header_height) / self.note_height - 1
  475. self.draw_note(f_note, f_start, f_length, self.ghost_vel)
  476. self.ghost_rect = QRectF(self.ghost_rect_orig)
  477. self.ghost_note.setRect(self.ghost_rect)
  478. elif self.marquee_select:
  479. self.marquee_select = False
  480. self.scene.removeItem(self.marquee)
  481. elif not self.marquee_select:
  482. for n in self.selected_notes:
  483. n.mouseReleaseEvent(a_event)
  484. def draw_header(self):
  485. self.header = QGraphicsRectItem(0, 0, self.viewer_width, self.header_height)
  486. #self.header.setZValue(1.0)
  487. self.header.setPos(self.piano_width + self.padding, 0)
  488. self.scene.addItem(self.header)
  489. self.beat_width = self.viewer_width / self.item_length
  490. self.value_width = self.beat_width / self.grid_div
  491. def draw_piano(self):
  492. f_labels = ['B','Bb','A','Ab','G','Gb','F','E','Eb','D','Db','C']
  493. f_black_notes = [2,4,6,9,11]
  494. f_piano_label = QFont()
  495. f_piano_label.setPointSize(8)
  496. self.piano = QGraphicsRectItem(0, 0, self.piano_width, self.piano_height)
  497. self.piano.setPos(0, self.header_height)
  498. self.scene.addItem(self.piano)
  499. f_key = piano_key_item(self.piano_width, self.note_height, self.piano)
  500. f_label = QGraphicsSimpleTextItem('C8', f_key)
  501. f_label.setPos(4, 0)
  502. f_label.setFont(f_piano_label)
  503. f_key.setBrush(QColor(255, 255, 255))
  504. for i in range(self.end_octave - self.start_octave, self.start_octave - self.start_octave, -1):
  505. for j in range(self.notes_in_octave, 0, -1):
  506. f_key = piano_key_item(self.piano_width, self.note_height, self.piano)
  507. f_key.setPos(0, self.note_height * j + self.octave_height * (i - 1))
  508. if j == 12:
  509. f_label = QGraphicsSimpleTextItem('%s%d' % (f_labels[(j - 1)], self.end_octave - i), f_key)
  510. f_label.setPos(4, 0)
  511. f_label.setFont(f_piano_label)
  512. if j in f_black_notes:
  513. f_key.setBrush(QColor(0, 0, 0))
  514. else:
  515. f_key.setBrush(QColor(255, 255, 255))
  516. self.piano_keys.append(f_key)
  517. def draw_grid(self):
  518. f_black_notes = [2,4,6,9,11]
  519. for i in range(self.end_octave - self.start_octave, self.start_octave - self.start_octave, -1):
  520. for j in range(self.notes_in_octave, 0, -1):
  521. f_scale_bar = QGraphicsRectItem(0, 0, self.viewer_width, self.note_height, self.piano)
  522. f_scale_bar.setPos(self.piano_width + self.padding, self.note_height * j + self.octave_height * (i - 1))
  523. if j not in f_black_notes:
  524. f_scale_bar.setBrush(QColor(230, 230, 230, 100))
  525. f_beat_pen = QPen()
  526. f_beat_pen.setWidth(2)
  527. f_line_pen = QPen()
  528. f_line_pen.setColor(QColor(0, 0, 0, 40))
  529. for i in range(0, int(self.item_length) + 1):
  530. f_beat = QGraphicsLineItem(0, 0, 0, self.piano_height + self.header_height - f_beat_pen.width(), self.header)
  531. f_beat.setPos(self.beat_width * i, 0.5 * f_beat_pen.width())
  532. f_beat.setPen(f_beat_pen)
  533. if i < self.item_length:
  534. f_number = QGraphicsSimpleTextItem('%d' % (i + 1), self.header)
  535. f_number.setPos(self.beat_width * i + 5, 2)
  536. f_number.setBrush(Qt.white)
  537. for j in range(0, self.grid_div):
  538. f_line = QGraphicsLineItem(0, 0, 0, self.piano_height, self.header)
  539. f_line.setZValue(1.0)
  540. if float(j) == self.grid_div / 2.0:
  541. f_line.setLine(0, 0, 0, self.piano_height)
  542. f_line.setPos(self.beat_width * i + self.value_width * j, self.header_height)
  543. else:
  544. f_line.setPos(self.beat_width * i + self.value_width * j, self.header_height)
  545. f_line.setPen(f_line_pen)
  546. def set_zoom(self, scale_x, scale_y):
  547. self.scale(scale_x, scale_y)
  548. def clear_drawn_items(self):
  549. self.scene.clear()
  550. self.draw_header()
  551. self.draw_piano()
  552. self.draw_grid()
  553. def draw_item(self, a_item):
  554. """ Draw all notes in an instance of the item class"""
  555. for f_notes in a_item.notes:
  556. self.draw_note(f_note)
  557. def draw_note(self, a_note, a_start=None, a_length=None, a_velocity=None):
  558. """a_note is the midi number, a_start is 1-4.99, a_length is in beats, a_velocity is 0-127"""
  559. f_start = self.piano_width + self.padding + self.beat_width * a_start
  560. f_length = self.beat_width * (a_length * 0.25 * self.item_length)
  561. f_note = self.header_height + self.note_height * (self.total_notes - a_note - 1)
  562. f_note_item = note_item(f_length, self.note_height, a_note)
  563. f_note_item.setPos(f_start, f_note)
  564. f_vel_opacity = QGraphicsOpacityEffect()
  565. f_vel_opacity.setOpacity(a_velocity * 0.007874016 * 0.6 + 0.3)
  566. f_note_item.setGraphicsEffect(f_vel_opacity)
  567. self.scene.addItem(f_note_item)
  568. self.notes.append(f_note_item)
  569. #--------------- main ------------------
  570. if __name__ == '__main__':
  571. import resources_rc
  572. pathBinaries, pathResources = getPaths()
  573. gCarla.utils = CarlaUtils(os.path.join(pathBinaries, "libcarla_utils." + DLL_EXTENSION))
  574. gCarla.utils.set_process_name("MidiSequencer")
  575. app = CarlaApplication("MidiSequencer")
  576. gui = piano_roll()
  577. app.exit_exec()