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.

239 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. self.fPaintTimer = QTimer(self)
  40. self.fPaintTimer.setInterval(60)
  41. self.fPaintTimer.timeout.connect(self.update)
  42. self.fPaintTimer.start()
  43. def displayMeter(self, meter, level):
  44. if meter <= 0 or meter > self.fChannels:
  45. return qCritical("DigitalPeakMeter::displayMeter(%i, %f) - invalid meter number" % (meter, level))
  46. if level < 0.0:
  47. level = -level
  48. elif level > 1.0:
  49. level = 1.0
  50. self.fChannelsData[meter-1] = float(level)
  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 setRefreshRate(self, rate):
  90. self.fPaintTimer.stop()
  91. self.fPaintTimer.setInterval(rate)
  92. self.fPaintTimer.start()
  93. def setSmoothRelease(self, value):
  94. if value < 0:
  95. value = 0
  96. elif value > 5:
  97. value = 5
  98. self.fSmoothMultiplier = value
  99. def minimumSizeHint(self):
  100. return QSize(10, 10)
  101. def sizeHint(self):
  102. return QSize(self.fWidth, self.fHeight)
  103. def updateSizes(self):
  104. self.fWidth = self.width()
  105. self.fHeight = self.height()
  106. self.fSizeMeter = 0
  107. if self.fOrientation == self.HORIZONTAL:
  108. self.fGradientMeter.setFinalStop(self.fWidth, 0)
  109. if self.fChannels > 0:
  110. self.fSizeMeter = self.fHeight / self.fChannels
  111. elif self.fOrientation == self.VERTICAL:
  112. self.fGradientMeter.setFinalStop(0, self.fHeight)
  113. if self.fChannels > 0:
  114. self.fSizeMeter = self.fWidth / self.fChannels
  115. def paintEvent(self, event):
  116. painter = QPainter(self)
  117. event.accept()
  118. painter.setPen(Qt.black)
  119. painter.setBrush(Qt.black)
  120. painter.drawRect(0, 0, self.fWidth, self.fHeight)
  121. meterX = 0
  122. painter.setPen(self.fColorBackground)
  123. painter.setBrush(self.fGradientMeter)
  124. for i in range(self.fChannels):
  125. level = self.fChannelsData[i]
  126. if level == self.fLastValueData[i]:
  127. continue
  128. if self.fOrientation == self.HORIZONTAL:
  129. value = level * float(self.fWidth)
  130. elif self.fOrientation == self.VERTICAL:
  131. value = float(self.fHeight) - (level * float(self.fHeight))
  132. else:
  133. value = 0.0
  134. if value < 0.0:
  135. value = 0.0
  136. elif self.fSmoothMultiplier > 0:
  137. value = (self.fLastValueData[i] * self.fSmoothMultiplier + value) / float(self.fSmoothMultiplier + 1)
  138. if self.fOrientation == self.HORIZONTAL:
  139. painter.drawRect(0, meterX, int(value), self.fSizeMeter)
  140. elif self.fOrientation == self.VERTICAL:
  141. painter.drawRect(meterX, int(value), self.fSizeMeter, self.fHeight)
  142. meterX += self.fSizeMeter
  143. self.fLastValueData[i] = value
  144. painter.setBrush(Qt.black)
  145. if self.fOrientation == self.HORIZONTAL:
  146. # Variables
  147. lsmall = float(self.fWidth)
  148. lfull = float(self.fHeight - 1)
  149. # Base
  150. painter.setPen(self.fColorBaseAlt)
  151. painter.drawLine(lsmall * 0.25, 2, lsmall * 0.25, lfull-2.0)
  152. painter.drawLine(lsmall * 0.50, 2, lsmall * 0.50, lfull-2.0)
  153. # Yellow
  154. painter.setPen(QColor(110, 110, 15, 100))
  155. painter.drawLine(lsmall * 0.70, 2, lsmall * 0.70, lfull-2.0)
  156. painter.drawLine(lsmall * 0.83, 2, lsmall * 0.83, lfull-2.0)
  157. # Orange
  158. painter.setPen(QColor(180, 110, 15, 100))
  159. painter.drawLine(lsmall * 0.90, 2, lsmall * 0.90, lfull-2.0)
  160. # Red
  161. painter.setPen(QColor(110, 15, 15, 100))
  162. painter.drawLine(lsmall * 0.96, 2, lsmall * 0.96, lfull-2.0)
  163. elif self.fOrientation == self.VERTICAL:
  164. # Variables
  165. lsmall = float(self.fHeight)
  166. lfull = float(self.fWidth - 1)
  167. # Base
  168. painter.setPen(self.fColorBaseAlt)
  169. painter.drawLine(2, lsmall - (lsmall * 0.25), lfull-2.0, lsmall - (lsmall * 0.25))
  170. painter.drawLine(2, lsmall - (lsmall * 0.50), lfull-2.0, lsmall - (lsmall * 0.50))
  171. # Yellow
  172. painter.setPen(QColor(110, 110, 15, 100))
  173. painter.drawLine(2, lsmall - (lsmall * 0.70), lfull-2.0, lsmall - (lsmall * 0.70))
  174. painter.drawLine(2, lsmall - (lsmall * 0.82), lfull-2.0, lsmall - (lsmall * 0.82))
  175. # Orange
  176. painter.setPen(QColor(180, 110, 15, 100))
  177. painter.drawLine(2, lsmall - (lsmall * 0.90), lfull-2.0, lsmall - (lsmall * 0.90))
  178. # Red
  179. painter.setPen(QColor(110, 15, 15, 100))
  180. painter.drawLine(2, lsmall - (lsmall * 0.96), lfull-2.0, lsmall - (lsmall * 0.96))
  181. def resizeEvent(self, event):
  182. self.updateSizes()
  183. QWidget.resizeEvent(self, event)