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.

676 lines
28KB

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