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.

233 lines
8.2KB

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