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.1KB

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