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.

249 lines
8.6KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Digital Peak Meter, a custom Qt4 widget
  4. # Copyright (C) 2011-2014 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 Orientation
  33. HORIZONTAL = 1
  34. VERTICAL = 2
  35. # enum Color
  36. GREEN = 1
  37. BLUE = 2
  38. def __init__(self, parent):
  39. QWidget.__init__(self, parent)
  40. self.fChannels = 0
  41. self.fDrawLines = True
  42. self.fOrientation = self.VERTICAL
  43. self.fSmoothMultiplier = 1
  44. self.fColorBackground = QColor("#111111")
  45. self.fGradientMeter = QLinearGradient(0, 0, 1, 1)
  46. self.setChannels(0)
  47. self.setColor(self.GREEN)
  48. def displayMeter(self, meter, level, forced = False):
  49. if meter <= 0 or meter > self.fChannels:
  50. return qCritical("DigitalPeakMeter::displayMeter(%i, %f) - invalid meter number" % (meter, level))
  51. if not isinstance(level, float):
  52. return qCritical("DigitalPeakMeter::displayMeter(%i, %f) - meter value must be float" % (meter, level))
  53. i = meter - 1
  54. if self.fSmoothMultiplier > 0 and not forced:
  55. level = (self.fLastValueData[i] * self.fSmoothMultiplier + level) / float(self.fSmoothMultiplier + 1)
  56. if level < 0.001:
  57. level = 0.0
  58. elif level > 0.999:
  59. level = 1.0
  60. if self.fChannelsData[i] != level:
  61. self.fChannelsData[i] = level
  62. self.update()
  63. self.fLastValueData[i] = level
  64. def setChannels(self, channels):
  65. if channels < 0:
  66. return qCritical("DigitalPeakMeter::setChannels(%i) - 'channels' must be a positive integer" % channels)
  67. self.fChannels = channels
  68. self.fChannelsData = []
  69. self.fLastValueData = []
  70. for x in range(channels):
  71. self.fChannelsData.append(0.0)
  72. self.fLastValueData.append(0.0)
  73. def setColor(self, color):
  74. if color == self.GREEN:
  75. self.fColorBase = QColor(93, 231, 61)
  76. self.fColorBaseAlt = QColor(15, 110, 15, 100)
  77. elif color == self.BLUE:
  78. self.fColorBase = QColor(82, 238, 248)
  79. self.fColorBaseAlt = QColor(15, 15, 110, 100)
  80. else:
  81. return qCritical("DigitalPeakMeter::setColor(%i) - invalid color" % color)
  82. self.setOrientation(self.fOrientation)
  83. def setLinesEnabled(self, yesNo):
  84. self.fDrawLines = yesNo
  85. def setOrientation(self, orientation):
  86. self.fOrientation = orientation
  87. if self.fOrientation == self.HORIZONTAL:
  88. self.fGradientMeter.setColorAt(0.0, self.fColorBase)
  89. self.fGradientMeter.setColorAt(0.2, self.fColorBase)
  90. self.fGradientMeter.setColorAt(0.4, self.fColorBase)
  91. self.fGradientMeter.setColorAt(0.6, self.fColorBase)
  92. self.fGradientMeter.setColorAt(0.8, Qt.yellow)
  93. self.fGradientMeter.setColorAt(1.0, Qt.red)
  94. elif self.fOrientation == self.VERTICAL:
  95. self.fGradientMeter.setColorAt(0.0, Qt.red)
  96. self.fGradientMeter.setColorAt(0.2, Qt.yellow)
  97. self.fGradientMeter.setColorAt(0.4, self.fColorBase)
  98. self.fGradientMeter.setColorAt(0.6, self.fColorBase)
  99. self.fGradientMeter.setColorAt(0.8, self.fColorBase)
  100. self.fGradientMeter.setColorAt(1.0, self.fColorBase)
  101. else:
  102. return qCritical("DigitalPeakMeter::setOrientation(%i) - invalid orientation" % orientation)
  103. self.updateSizes()
  104. def setSmoothRelease(self, value):
  105. if value < 0:
  106. value = 0
  107. elif value > 5:
  108. value = 5
  109. self.fSmoothMultiplier = value
  110. def minimumSizeHint(self):
  111. return QSize(10, 10)
  112. def sizeHint(self):
  113. return QSize(self.fWidth, self.fHeight)
  114. def updateSizes(self):
  115. self.fWidth = self.width()
  116. self.fHeight = self.height()
  117. self.fSizeMeter = 0
  118. if self.fOrientation == self.HORIZONTAL:
  119. self.fGradientMeter.setFinalStop(self.fWidth, 0)
  120. if self.fChannels > 0:
  121. self.fSizeMeter = self.fHeight / self.fChannels
  122. elif self.fOrientation == self.VERTICAL:
  123. self.fGradientMeter.setFinalStop(0, self.fHeight)
  124. if self.fChannels > 0:
  125. self.fSizeMeter = self.fWidth / self.fChannels
  126. def paintEvent(self, event):
  127. painter = QPainter(self)
  128. event.accept()
  129. painter.setPen(Qt.black)
  130. painter.setBrush(Qt.black)
  131. painter.drawRect(0, 0, self.fWidth, self.fHeight)
  132. meterX = 0
  133. painter.setPen(self.fColorBackground)
  134. painter.setBrush(self.fGradientMeter)
  135. for i in range(self.fChannels):
  136. level = self.fChannelsData[i]
  137. if self.fOrientation == self.HORIZONTAL:
  138. value = level * float(self.fWidth)
  139. elif self.fOrientation == self.VERTICAL:
  140. value = float(self.fHeight) - (level * float(self.fHeight))
  141. else:
  142. value = 0.0
  143. if self.fOrientation == self.HORIZONTAL:
  144. painter.drawRect(0, meterX, int(value), self.fSizeMeter)
  145. elif self.fOrientation == self.VERTICAL:
  146. painter.drawRect(meterX, int(value), self.fSizeMeter, self.fHeight)
  147. meterX += self.fSizeMeter
  148. if not self.fDrawLines:
  149. return
  150. painter.setBrush(Qt.black)
  151. if self.fOrientation == self.HORIZONTAL:
  152. # Variables
  153. lsmall = float(self.fWidth)
  154. lfull = float(self.fHeight - 1)
  155. # Base
  156. painter.setPen(self.fColorBaseAlt)
  157. painter.drawLine(lsmall * 0.25, 2, lsmall * 0.25, lfull-2.0)
  158. painter.drawLine(lsmall * 0.50, 2, lsmall * 0.50, lfull-2.0)
  159. # Yellow
  160. painter.setPen(QColor(110, 110, 15, 100))
  161. painter.drawLine(lsmall * 0.70, 2, lsmall * 0.70, lfull-2.0)
  162. painter.drawLine(lsmall * 0.83, 2, lsmall * 0.83, lfull-2.0)
  163. # Orange
  164. painter.setPen(QColor(180, 110, 15, 100))
  165. painter.drawLine(lsmall * 0.90, 2, lsmall * 0.90, lfull-2.0)
  166. # Red
  167. painter.setPen(QColor(110, 15, 15, 100))
  168. painter.drawLine(lsmall * 0.96, 2, lsmall * 0.96, lfull-2.0)
  169. elif self.fOrientation == self.VERTICAL:
  170. # Variables
  171. lsmall = float(self.fHeight)
  172. lfull = float(self.fWidth - 1)
  173. # Base
  174. painter.setPen(self.fColorBaseAlt)
  175. painter.drawLine(2, lsmall - (lsmall * 0.25), lfull-2.0, lsmall - (lsmall * 0.25))
  176. painter.drawLine(2, lsmall - (lsmall * 0.50), lfull-2.0, lsmall - (lsmall * 0.50))
  177. # Yellow
  178. painter.setPen(QColor(110, 110, 15, 100))
  179. painter.drawLine(2, lsmall - (lsmall * 0.70), lfull-2.0, lsmall - (lsmall * 0.70))
  180. painter.drawLine(2, lsmall - (lsmall * 0.82), lfull-2.0, lsmall - (lsmall * 0.82))
  181. # Orange
  182. painter.setPen(QColor(180, 110, 15, 100))
  183. painter.drawLine(2, lsmall - (lsmall * 0.90), lfull-2.0, lsmall - (lsmall * 0.90))
  184. # Red
  185. painter.setPen(QColor(110, 15, 15, 100))
  186. painter.drawLine(2, lsmall - (lsmall * 0.96), lfull-2.0, lsmall - (lsmall * 0.96))
  187. def resizeEvent(self, event):
  188. self.updateSizes()
  189. QWidget.resizeEvent(self, event)