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.

397 lines
14KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Digital Peak Meter, a custom Qt4 widget
  4. # Copyright (C) 2011-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 qCritical, Qt, QTimer, QSize
  24. from PyQt5.QtGui import QColor, QLinearGradient, QPainter
  25. from PyQt5.QtWidgets import QWidget
  26. else:
  27. from PyQt4.QtCore import qCritical, Qt, QTimer, QSize
  28. from PyQt4.QtGui import QColor, QLinearGradient, QPainter, QWidget
  29. # ------------------------------------------------------------------------------------------------------------
  30. # Widget Class
  31. class DigitalPeakMeter(QWidget):
  32. # enum Color
  33. COLOR_GREEN = 1
  34. COLOR_BLUE = 2
  35. # enum Orientation
  36. HORIZONTAL = 1
  37. VERTICAL = 2
  38. # enum Style
  39. STYLE_DEFAULT = 1
  40. STYLE_OPENAV = 2
  41. STYLE_RNCBC = 3
  42. def __init__(self, parent):
  43. QWidget.__init__(self, parent)
  44. # defaults are VERTICAL, COLOR_GREEN, STYLE_DEFAULT
  45. self.fChannelCount = 0
  46. self.fChannelData = []
  47. self.fLastChannelData = []
  48. self.fMeterColor = self.COLOR_GREEN
  49. self.fMeterColorBase = QColor(93, 231, 61)
  50. self.fMeterColorBaseAlt = QColor(15, 110, 15, 100)
  51. self.fMeterLinesEnabled = True
  52. self.fMeterOrientation = self.VERTICAL
  53. self.fMeterStyle = self.STYLE_DEFAULT
  54. self.fMeterBackground = QColor("#111111")
  55. self.fMeterGradient = QLinearGradient(0, 0, 0, 0)
  56. self.fSmoothMultiplier = 1
  57. self.updateGrandient()
  58. # --------------------------------------------------------------------------------------------------------
  59. def channelCount(self):
  60. return self.fChannelCount
  61. def setChannelCount(self, count):
  62. if self.fChannelCount == count:
  63. return
  64. if count < 0:
  65. return qCritical("DigitalPeakMeter::setChannelCount(%i) - channel count must be a positive integer or zero" % count)
  66. self.fChannelCount = count
  67. self.fChannelData = []
  68. self.fLastChannelData = []
  69. for x in range(count):
  70. self.fChannelData.append(0.0)
  71. self.fLastChannelData.append(0.0)
  72. # --------------------------------------------------------------------------------------------------------
  73. def meterColor(self):
  74. return self.fMeterColor
  75. def setMeterColor(self, color):
  76. if self.fMeterColor == color:
  77. return
  78. if color not in (self.COLOR_GREEN, self.COLOR_BLUE):
  79. return qCritical("DigitalPeakMeter::setMeterColor(%i) - invalid color" % color)
  80. self.fMeterColor = color
  81. if color == self.COLOR_GREEN:
  82. self.fMeterColorBase = QColor(93, 231, 61)
  83. self.fMeterColorBaseAlt = QColor(15, 110, 15, 100)
  84. elif color == self.COLOR_BLUE:
  85. self.fMeterColorBase = QColor(82, 238, 248)
  86. self.fMeterColorBaseAlt = QColor(15, 15, 110, 100)
  87. self.updateGrandient()
  88. # --------------------------------------------------------------------------------------------------------
  89. def meterLinesEnabled(self):
  90. return self.fMeterLinesEnabled
  91. def setMeterLinesEnabled(self, yesNo):
  92. if self.fMeterLinesEnabled == yesNo:
  93. return
  94. self.fMeterLinesEnabled = yesNo
  95. # --------------------------------------------------------------------------------------------------------
  96. def meterOrientation(self):
  97. return self.fMeterOrientation
  98. def setMeterOrientation(self, orientation):
  99. if self.fMeterOrientation == orientation:
  100. return
  101. if orientation not in (self.HORIZONTAL, self.VERTICAL):
  102. return qCritical("DigitalPeakMeter::setMeterOrientation(%i) - invalid orientation" % orientation)
  103. self.fMeterOrientation = orientation
  104. self.updateGrandient()
  105. # --------------------------------------------------------------------------------------------------------
  106. def meterStyle(self):
  107. return self.fMeterStyle
  108. def setMeterStyle(self, style):
  109. if self.fMeterStyle == style:
  110. return
  111. if style not in (self.STYLE_DEFAULT, self.STYLE_OPENAV, self.STYLE_RNCBC):
  112. return qCritical("DigitalPeakMeter::setMeterStyle(%i) - invalid style" % style)
  113. self.fMeterStyle = style
  114. if style == self.STYLE_DEFAULT:
  115. self.fMeterBackground = QColor("#111111")
  116. elif style == self.STYLE_OPENAV:
  117. self.fMeterBackground = QColor("#1A1A1A")
  118. elif style == self.STYLE_RNCBC:
  119. self.fMeterBackground = QColor("#111111")
  120. self.updateGrandient()
  121. # --------------------------------------------------------------------------------------------------------
  122. def smoothMultiplier(self):
  123. return self.fSmoothMultiplier
  124. def setSmoothMultiplier(self, value):
  125. if self.fSmoothMultiplier == value:
  126. return
  127. if not isinstance(value, int):
  128. return qCritical("DigitalPeakMeter::setSmoothMultiplier() - value must be an integer")
  129. if value < 0:
  130. return qCritical("DigitalPeakMeter::setSmoothMultiplier(%i) - value must be >= 0" % value)
  131. if value > 5:
  132. return qCritical("DigitalPeakMeter::setSmoothMultiplier(%i) - value must be < 5" % value)
  133. self.fSmoothMultiplier = value
  134. # --------------------------------------------------------------------------------------------------------
  135. def displayMeter(self, meter, level, forced = False):
  136. if not isinstance(meter, int):
  137. return qCritical("DigitalPeakMeter::displayMeter(,) - meter value must be an integer")
  138. if not isinstance(level, float):
  139. return qCritical("DigitalPeakMeter::displayMeter(%i,) - level value must be a float" % (meter,))
  140. if meter <= 0 or meter > self.fChannelCount:
  141. return qCritical("DigitalPeakMeter::displayMeter(%i, %f) - invalid meter number" % (meter, level))
  142. i = meter - 1
  143. if self.fSmoothMultiplier > 0 and not forced:
  144. level = (self.fLastChannelData[i] * float(self.fSmoothMultiplier) + level) / float(self.fSmoothMultiplier + 1)
  145. if level < 0.001:
  146. level = 0.0
  147. elif level > 0.999:
  148. level = 1.0
  149. if self.fChannelData[i] != level:
  150. self.fChannelData[i] = level
  151. self.update()
  152. self.fLastChannelData[i] = level
  153. # --------------------------------------------------------------------------------------------------------
  154. def updateGrandient(self):
  155. self.fMeterGradient = QLinearGradient(0, 0, 1, 1)
  156. if self.fMeterStyle == self.STYLE_OPENAV:
  157. self.fMeterGradient.setColorAt(0.0, self.fMeterColorBase)
  158. self.fMeterGradient.setColorAt(1.0, self.fMeterColorBase)
  159. elif self.fMeterOrientation == self.HORIZONTAL:
  160. self.fMeterGradient.setColorAt(0.0, self.fMeterColorBase)
  161. self.fMeterGradient.setColorAt(0.2, self.fMeterColorBase)
  162. self.fMeterGradient.setColorAt(0.4, self.fMeterColorBase)
  163. self.fMeterGradient.setColorAt(0.6, self.fMeterColorBase)
  164. self.fMeterGradient.setColorAt(0.8, Qt.yellow)
  165. self.fMeterGradient.setColorAt(1.0, Qt.red)
  166. elif self.fMeterOrientation == self.VERTICAL:
  167. self.fMeterGradient.setColorAt(0.0, Qt.red)
  168. self.fMeterGradient.setColorAt(0.2, Qt.yellow)
  169. self.fMeterGradient.setColorAt(0.4, self.fMeterColorBase)
  170. self.fMeterGradient.setColorAt(0.6, self.fMeterColorBase)
  171. self.fMeterGradient.setColorAt(0.8, self.fMeterColorBase)
  172. self.fMeterGradient.setColorAt(1.0, self.fMeterColorBase)
  173. self.updateGrandientFinalStop()
  174. def updateGrandientFinalStop(self):
  175. if self.fMeterOrientation == self.HORIZONTAL:
  176. self.fMeterGradient.setFinalStop(self.width(), 0)
  177. elif self.fMeterOrientation == self.VERTICAL:
  178. self.fMeterGradient.setFinalStop(0, self.height())
  179. # --------------------------------------------------------------------------------------------------------
  180. def minimumSizeHint(self):
  181. return QSize(10, 10)
  182. def sizeHint(self):
  183. return QSize(self.width(), self.height())
  184. # --------------------------------------------------------------------------------------------------------
  185. def paintEvent(self, event):
  186. painter = QPainter(self)
  187. event.accept()
  188. width = self.width()
  189. height = self.height()
  190. if self.fMeterStyle == self.STYLE_OPENAV:
  191. painter.setPen(QColor("#1A1A1A"))
  192. painter.setBrush(QColor("#1A1A1A"))
  193. else:
  194. painter.setPen(Qt.black)
  195. painter.setBrush(Qt.black)
  196. painter.drawRect(0, 0, width, height)
  197. meterX = 0
  198. startX = -1 if self.fMeterStyle == self.STYLE_OPENAV else 0
  199. padding = 2 if self.fMeterStyle == self.STYLE_OPENAV else 0
  200. painter.setPen(self.fMeterBackground) # FIXME ?
  201. painter.setBrush(self.fMeterGradient)
  202. #if self.fMeterStyle == self.STYLE_OPENAV:
  203. #color = self.fMeterGradient.stops()[0][1]
  204. #painter.setPen(color)
  205. #color.setAlphaF(0.5)
  206. #painter.setBrush(color)
  207. #del color
  208. for i in range(self.fChannelCount):
  209. level = self.fChannelsData[i]
  210. if self.fMeterOrientation == self.HORIZONTAL:
  211. value = level * float(width)
  212. elif self.fMeterOrientation == self.VERTICAL:
  213. value = float(height) - (level * float(height))
  214. else:
  215. value = 0.0
  216. if self.fMeterOrientation == self.HORIZONTAL:
  217. painter.drawRect(startX, meterX+padding, int(value), self.fSizeMeter-padding*(1 if self.fChannels > 1 else 2))
  218. elif self.fMeterOrientation == self.VERTICAL:
  219. painter.drawRect(meterX, int(value), self.fSizeMeter, self.fHeight)
  220. meterX += self.fSizeMeter
  221. if not self.fDrawLines:
  222. return
  223. painter.setBrush(Qt.black)
  224. if self.fOrientation == self.HORIZONTAL:
  225. # Variables
  226. lsmall = float(self.fWidth)
  227. lfull = float(self.fHeight - 1)
  228. if self.fMeterStyle == self.STYLE_OPENAV:
  229. painter.setPen(QColor(37, 37, 37, 100))
  230. painter.drawLine(lsmall * 0.25, 2, lsmall * 0.25, lfull-2.0)
  231. painter.drawLine(lsmall * 0.50, 2, lsmall * 0.50, lfull-2.0)
  232. painter.drawLine(lsmall * 0.75, 2, lsmall * 0.75, lfull-2.0)
  233. if self.fChannels > 1:
  234. painter.drawLine(1, lfull/2, lsmall-1, lfull/2)
  235. else:
  236. # Base
  237. painter.setPen(self.fColorBaseAlt)
  238. painter.drawLine(lsmall * 0.25, 2, lsmall * 0.25, lfull-2.0)
  239. painter.drawLine(lsmall * 0.50, 2, lsmall * 0.50, lfull-2.0)
  240. # Yellow
  241. painter.setPen(QColor(110, 110, 15, 100))
  242. painter.drawLine(lsmall * 0.70, 2, lsmall * 0.70, lfull-2.0)
  243. painter.drawLine(lsmall * 0.83, 2, lsmall * 0.83, lfull-2.0)
  244. # Orange
  245. painter.setPen(QColor(180, 110, 15, 100))
  246. painter.drawLine(lsmall * 0.90, 2, lsmall * 0.90, lfull-2.0)
  247. # Red
  248. painter.setPen(QColor(110, 15, 15, 100))
  249. painter.drawLine(lsmall * 0.96, 2, lsmall * 0.96, lfull-2.0)
  250. elif self.fOrientation == self.VERTICAL:
  251. # Variables
  252. lsmall = float(self.fHeight)
  253. lfull = float(self.fWidth - 1)
  254. if self.fMeterStyle == self.STYLE_OPENAV:
  255. # TODO
  256. pass
  257. else:
  258. # Base
  259. painter.setPen(self.fColorBaseAlt)
  260. painter.drawLine(2, lsmall - (lsmall * 0.25), lfull-2.0, lsmall - (lsmall * 0.25))
  261. painter.drawLine(2, lsmall - (lsmall * 0.50), lfull-2.0, lsmall - (lsmall * 0.50))
  262. # Yellow
  263. painter.setPen(QColor(110, 110, 15, 100))
  264. painter.drawLine(2, lsmall - (lsmall * 0.70), lfull-2.0, lsmall - (lsmall * 0.70))
  265. painter.drawLine(2, lsmall - (lsmall * 0.82), lfull-2.0, lsmall - (lsmall * 0.82))
  266. # Orange
  267. painter.setPen(QColor(180, 110, 15, 100))
  268. painter.drawLine(2, lsmall - (lsmall * 0.90), lfull-2.0, lsmall - (lsmall * 0.90))
  269. # Red
  270. painter.setPen(QColor(110, 15, 15, 100))
  271. painter.drawLine(2, lsmall - (lsmall * 0.96), lfull-2.0, lsmall - (lsmall * 0.96))
  272. # --------------------------------------------------------------------------------------------------------
  273. def resizeEvent(self, event):
  274. self.updateGrandientFinalStop()
  275. QWidget.resizeEvent(self, event)
  276. # ------------------------------------------------------------------------------------------------------------
  277. # Main Testing
  278. if __name__ == '__main__':
  279. import sys
  280. import resources_rc
  281. if config_UseQt5:
  282. from PyQt5.QtWidgets import QApplication
  283. else:
  284. from PyQt4.QtGui import QApplication
  285. app = QApplication(sys.argv)
  286. gui = DigitalPeakMeter(None)
  287. gui.setChannelCount(2)
  288. gui.displayMeter(1, 0.5)
  289. gui.displayMeter(2, 0.8)
  290. gui.show()
  291. sys.exit(app.exec_())