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.

digitalpeakmeter.py 8.2KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 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.fDrawLines = True
  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, forced = False):
  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 and not forced:
  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 setLinesEnabled(self, yesNo):
  76. self.fDrawLines = yesNo
  77. def setOrientation(self, orientation):
  78. self.fOrientation = orientation
  79. if self.fOrientation == self.HORIZONTAL:
  80. self.fGradientMeter.setColorAt(0.0, self.fColorBase)
  81. self.fGradientMeter.setColorAt(0.2, self.fColorBase)
  82. self.fGradientMeter.setColorAt(0.4, self.fColorBase)
  83. self.fGradientMeter.setColorAt(0.6, self.fColorBase)
  84. self.fGradientMeter.setColorAt(0.8, Qt.yellow)
  85. self.fGradientMeter.setColorAt(1.0, Qt.red)
  86. elif self.fOrientation == self.VERTICAL:
  87. self.fGradientMeter.setColorAt(0.0, Qt.red)
  88. self.fGradientMeter.setColorAt(0.2, Qt.yellow)
  89. self.fGradientMeter.setColorAt(0.4, self.fColorBase)
  90. self.fGradientMeter.setColorAt(0.6, self.fColorBase)
  91. self.fGradientMeter.setColorAt(0.8, self.fColorBase)
  92. self.fGradientMeter.setColorAt(1.0, self.fColorBase)
  93. else:
  94. return qCritical("DigitalPeakMeter::setOrientation(%i) - invalid orientation" % orientation)
  95. self.updateSizes()
  96. def setSmoothRelease(self, value):
  97. if value < 0:
  98. value = 0
  99. elif value > 5:
  100. value = 5
  101. self.fSmoothMultiplier = value
  102. def minimumSizeHint(self):
  103. return QSize(10, 10)
  104. def sizeHint(self):
  105. return QSize(self.fWidth, self.fHeight)
  106. def updateSizes(self):
  107. self.fWidth = self.width()
  108. self.fHeight = self.height()
  109. self.fSizeMeter = 0
  110. if self.fOrientation == self.HORIZONTAL:
  111. self.fGradientMeter.setFinalStop(self.fWidth, 0)
  112. if self.fChannels > 0:
  113. self.fSizeMeter = self.fHeight / self.fChannels
  114. elif self.fOrientation == self.VERTICAL:
  115. self.fGradientMeter.setFinalStop(0, self.fHeight)
  116. if self.fChannels > 0:
  117. self.fSizeMeter = self.fWidth / self.fChannels
  118. def paintEvent(self, event):
  119. painter = QPainter(self)
  120. event.accept()
  121. painter.setPen(Qt.black)
  122. painter.setBrush(Qt.black)
  123. painter.drawRect(0, 0, self.fWidth, self.fHeight)
  124. meterX = 0
  125. painter.setPen(self.fColorBackground)
  126. painter.setBrush(self.fGradientMeter)
  127. for i in range(self.fChannels):
  128. level = self.fChannelsData[i]
  129. if self.fOrientation == self.HORIZONTAL:
  130. value = level * float(self.fWidth)
  131. elif self.fOrientation == self.VERTICAL:
  132. value = float(self.fHeight) - (level * float(self.fHeight))
  133. else:
  134. value = 0.0
  135. if self.fOrientation == self.HORIZONTAL:
  136. painter.drawRect(0, meterX, int(value), self.fSizeMeter)
  137. elif self.fOrientation == self.VERTICAL:
  138. painter.drawRect(meterX, int(value), self.fSizeMeter, self.fHeight)
  139. meterX += self.fSizeMeter
  140. if not self.fDrawLines:
  141. return
  142. painter.setBrush(Qt.black)
  143. if self.fOrientation == self.HORIZONTAL:
  144. # Variables
  145. lsmall = float(self.fWidth)
  146. lfull = float(self.fHeight - 1)
  147. # Base
  148. painter.setPen(self.fColorBaseAlt)
  149. painter.drawLine(lsmall * 0.25, 2, lsmall * 0.25, lfull-2.0)
  150. painter.drawLine(lsmall * 0.50, 2, lsmall * 0.50, lfull-2.0)
  151. # Yellow
  152. painter.setPen(QColor(110, 110, 15, 100))
  153. painter.drawLine(lsmall * 0.70, 2, lsmall * 0.70, lfull-2.0)
  154. painter.drawLine(lsmall * 0.83, 2, lsmall * 0.83, lfull-2.0)
  155. # Orange
  156. painter.setPen(QColor(180, 110, 15, 100))
  157. painter.drawLine(lsmall * 0.90, 2, lsmall * 0.90, lfull-2.0)
  158. # Red
  159. painter.setPen(QColor(110, 15, 15, 100))
  160. painter.drawLine(lsmall * 0.96, 2, lsmall * 0.96, lfull-2.0)
  161. elif self.fOrientation == self.VERTICAL:
  162. # Variables
  163. lsmall = float(self.fHeight)
  164. lfull = float(self.fWidth - 1)
  165. # Base
  166. painter.setPen(self.fColorBaseAlt)
  167. painter.drawLine(2, lsmall - (lsmall * 0.25), lfull-2.0, lsmall - (lsmall * 0.25))
  168. painter.drawLine(2, lsmall - (lsmall * 0.50), lfull-2.0, lsmall - (lsmall * 0.50))
  169. # Yellow
  170. painter.setPen(QColor(110, 110, 15, 100))
  171. painter.drawLine(2, lsmall - (lsmall * 0.70), lfull-2.0, lsmall - (lsmall * 0.70))
  172. painter.drawLine(2, lsmall - (lsmall * 0.82), lfull-2.0, lsmall - (lsmall * 0.82))
  173. # Orange
  174. painter.setPen(QColor(180, 110, 15, 100))
  175. painter.drawLine(2, lsmall - (lsmall * 0.90), lfull-2.0, lsmall - (lsmall * 0.90))
  176. # Red
  177. painter.setPen(QColor(110, 15, 15, 100))
  178. painter.drawLine(2, lsmall - (lsmall * 0.96), lfull-2.0, lsmall - (lsmall * 0.96))
  179. def resizeEvent(self, event):
  180. self.updateSizes()
  181. QWidget.resizeEvent(self, event)