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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. try:
  20. from PyQt5.QtCore import qCritical, Qt, QTimer, QSize
  21. from PyQt5.QtGui import QColor, QLinearGradient, QPainter
  22. from PyQt5.QtWidgets import QWidget
  23. except:
  24. from PyQt4.QtCore import qCritical, Qt, QTimer, QSize
  25. from PyQt4.QtGui import QColor, QLinearGradient, QPainter, QWidget
  26. # ------------------------------------------------------------------------------------------------------------
  27. # Widget Class
  28. class DigitalPeakMeter(QWidget):
  29. # enum Orientation
  30. HORIZONTAL = 1
  31. VERTICAL = 2
  32. # enum Color
  33. GREEN = 1
  34. BLUE = 2
  35. def __init__(self, parent):
  36. QWidget.__init__(self, parent)
  37. self.fChannels = 0
  38. self.fOrientation = self.VERTICAL
  39. self.fSmoothMultiplier = 1
  40. self.fColorBackground = QColor("#111111")
  41. self.fGradientMeter = QLinearGradient(0, 0, 1, 1)
  42. self.setChannels(0)
  43. self.setColor(self.GREEN)
  44. def displayMeter(self, meter, level):
  45. if meter <= 0 or meter > self.fChannels:
  46. return qCritical("DigitalPeakMeter::displayMeter(%i, %f) - invalid meter number" % (meter, level))
  47. if not isinstance(level, float):
  48. return qCritical("DigitalPeakMeter::displayMeter(%i, %f) - meter value must be float" % (meter, level))
  49. i = meter - 1
  50. if self.fSmoothMultiplier > 0:
  51. level = (self.fLastValueData[i] * self.fSmoothMultiplier + level) / float(self.fSmoothMultiplier + 1)
  52. if level < 0.001:
  53. level = 0.0
  54. elif level > 0.999:
  55. level = 1.0
  56. if self.fChannelsData[i] != level:
  57. self.fChannelsData[i] = level
  58. self.update()
  59. self.fLastValueData[i] = level
  60. def setChannels(self, channels):
  61. if channels < 0:
  62. return qCritical("DigitalPeakMeter::setChannels(%i) - 'channels' must be a positive integer" % channels)
  63. self.fChannels = channels
  64. self.fChannelsData = []
  65. self.fLastValueData = []
  66. for x in range(channels):
  67. self.fChannelsData.append(0.0)
  68. self.fLastValueData.append(0.0)
  69. def setColor(self, color):
  70. if color == self.GREEN:
  71. self.fColorBase = QColor(93, 231, 61)
  72. self.fColorBaseAlt = QColor(15, 110, 15, 100)
  73. elif color == self.BLUE:
  74. self.fColorBase = QColor(82, 238, 248)
  75. self.fColorBaseAlt = QColor(15, 15, 110, 100)
  76. else:
  77. return qCritical("DigitalPeakMeter::setColor(%i) - invalid color" % color)
  78. self.setOrientation(self.fOrientation)
  79. def setOrientation(self, orientation):
  80. self.fOrientation = orientation
  81. if self.fOrientation == self.HORIZONTAL:
  82. self.fGradientMeter.setColorAt(0.0, self.fColorBase)
  83. self.fGradientMeter.setColorAt(0.2, self.fColorBase)
  84. self.fGradientMeter.setColorAt(0.4, self.fColorBase)
  85. self.fGradientMeter.setColorAt(0.6, self.fColorBase)
  86. self.fGradientMeter.setColorAt(0.8, Qt.yellow)
  87. self.fGradientMeter.setColorAt(1.0, Qt.red)
  88. elif self.fOrientation == self.VERTICAL:
  89. self.fGradientMeter.setColorAt(0.0, Qt.red)
  90. self.fGradientMeter.setColorAt(0.2, Qt.yellow)
  91. self.fGradientMeter.setColorAt(0.4, self.fColorBase)
  92. self.fGradientMeter.setColorAt(0.6, self.fColorBase)
  93. self.fGradientMeter.setColorAt(0.8, self.fColorBase)
  94. self.fGradientMeter.setColorAt(1.0, self.fColorBase)
  95. else:
  96. return qCritical("DigitalPeakMeter::setOrientation(%i) - invalid orientation" % orientation)
  97. self.updateSizes()
  98. def setSmoothRelease(self, value):
  99. if value < 0:
  100. value = 0
  101. elif value > 5:
  102. value = 5
  103. self.fSmoothMultiplier = value
  104. def minimumSizeHint(self):
  105. return QSize(10, 10)
  106. def sizeHint(self):
  107. return QSize(self.fWidth, self.fHeight)
  108. def updateSizes(self):
  109. self.fWidth = self.width()
  110. self.fHeight = self.height()
  111. self.fSizeMeter = 0
  112. if self.fOrientation == self.HORIZONTAL:
  113. self.fGradientMeter.setFinalStop(self.fWidth, 0)
  114. if self.fChannels > 0:
  115. self.fSizeMeter = self.fHeight / self.fChannels
  116. elif self.fOrientation == self.VERTICAL:
  117. self.fGradientMeter.setFinalStop(0, self.fHeight)
  118. if self.fChannels > 0:
  119. self.fSizeMeter = self.fWidth / self.fChannels
  120. def paintEvent(self, event):
  121. painter = QPainter(self)
  122. event.accept()
  123. painter.setPen(Qt.black)
  124. painter.setBrush(Qt.black)
  125. painter.drawRect(0, 0, self.fWidth, self.fHeight)
  126. meterX = 0
  127. painter.setPen(self.fColorBackground)
  128. painter.setBrush(self.fGradientMeter)
  129. for i in range(self.fChannels):
  130. level = self.fChannelsData[i]
  131. if self.fOrientation == self.HORIZONTAL:
  132. value = level * float(self.fWidth)
  133. elif self.fOrientation == self.VERTICAL:
  134. value = float(self.fHeight) - (level * float(self.fHeight))
  135. else:
  136. value = 0.0
  137. if self.fOrientation == self.HORIZONTAL:
  138. painter.drawRect(0, meterX, int(value), self.fSizeMeter)
  139. elif self.fOrientation == self.VERTICAL:
  140. painter.drawRect(meterX, int(value), self.fSizeMeter, self.fHeight)
  141. meterX += self.fSizeMeter
  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)