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

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. i = meter - 1
  45. if self.fSmoothMultiplier > 0:
  46. level = (self.fLastValueData[i] * self.fSmoothMultiplier + level) / float(self.fSmoothMultiplier + 1)
  47. if level < 0.001:
  48. level = 0.0
  49. elif level > 0.999:
  50. level = 1.0
  51. if self.fChannelsData[i] != level:
  52. self.fChannelsData[i] = level
  53. self.update()
  54. self.fLastValueData[i] = level
  55. def setChannels(self, channels):
  56. if channels < 0:
  57. return qCritical("DigitalPeakMeter::setChannels(%i) - 'channels' must be a positive integer" % channels)
  58. self.fChannels = channels
  59. self.fChannelsData = []
  60. self.fLastValueData = []
  61. for x in range(channels):
  62. self.fChannelsData.append(0.0)
  63. self.fLastValueData.append(0.0)
  64. def setColor(self, color):
  65. if color == self.GREEN:
  66. self.fColorBase = QColor(93, 231, 61)
  67. self.fColorBaseAlt = QColor(15, 110, 15, 100)
  68. elif color == self.BLUE:
  69. self.fColorBase = QColor(82, 238, 248)
  70. self.fColorBaseAlt = QColor(15, 15, 110, 100)
  71. else:
  72. return qCritical("DigitalPeakMeter::setColor(%i) - invalid color" % color)
  73. self.setOrientation(self.fOrientation)
  74. def setOrientation(self, orientation):
  75. self.fOrientation = orientation
  76. if self.fOrientation == self.HORIZONTAL:
  77. self.fGradientMeter.setColorAt(0.0, self.fColorBase)
  78. self.fGradientMeter.setColorAt(0.2, self.fColorBase)
  79. self.fGradientMeter.setColorAt(0.4, self.fColorBase)
  80. self.fGradientMeter.setColorAt(0.6, self.fColorBase)
  81. self.fGradientMeter.setColorAt(0.8, Qt.yellow)
  82. self.fGradientMeter.setColorAt(1.0, Qt.red)
  83. elif self.fOrientation == self.VERTICAL:
  84. self.fGradientMeter.setColorAt(0.0, Qt.red)
  85. self.fGradientMeter.setColorAt(0.2, Qt.yellow)
  86. self.fGradientMeter.setColorAt(0.4, self.fColorBase)
  87. self.fGradientMeter.setColorAt(0.6, self.fColorBase)
  88. self.fGradientMeter.setColorAt(0.8, self.fColorBase)
  89. self.fGradientMeter.setColorAt(1.0, self.fColorBase)
  90. else:
  91. return qCritical("DigitalPeakMeter::setOrientation(%i) - invalid orientation" % orientation)
  92. self.updateSizes()
  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 self.fOrientation == self.HORIZONTAL:
  127. value = level * float(self.fWidth)
  128. elif self.fOrientation == self.VERTICAL:
  129. value = float(self.fHeight) - (level * float(self.fHeight))
  130. else:
  131. value = 0.0
  132. if self.fOrientation == self.HORIZONTAL:
  133. painter.drawRect(0, meterX, int(value), self.fSizeMeter)
  134. elif self.fOrientation == self.VERTICAL:
  135. painter.drawRect(meterX, int(value), self.fSizeMeter, self.fHeight)
  136. meterX += self.fSizeMeter
  137. painter.setBrush(Qt.black)
  138. if self.fOrientation == self.HORIZONTAL:
  139. # Variables
  140. lsmall = float(self.fWidth)
  141. lfull = float(self.fHeight - 1)
  142. # Base
  143. painter.setPen(self.fColorBaseAlt)
  144. painter.drawLine(lsmall * 0.25, 2, lsmall * 0.25, lfull-2.0)
  145. painter.drawLine(lsmall * 0.50, 2, lsmall * 0.50, lfull-2.0)
  146. # Yellow
  147. painter.setPen(QColor(110, 110, 15, 100))
  148. painter.drawLine(lsmall * 0.70, 2, lsmall * 0.70, lfull-2.0)
  149. painter.drawLine(lsmall * 0.83, 2, lsmall * 0.83, lfull-2.0)
  150. # Orange
  151. painter.setPen(QColor(180, 110, 15, 100))
  152. painter.drawLine(lsmall * 0.90, 2, lsmall * 0.90, lfull-2.0)
  153. # Red
  154. painter.setPen(QColor(110, 15, 15, 100))
  155. painter.drawLine(lsmall * 0.96, 2, lsmall * 0.96, lfull-2.0)
  156. elif self.fOrientation == self.VERTICAL:
  157. # Variables
  158. lsmall = float(self.fHeight)
  159. lfull = float(self.fWidth - 1)
  160. # Base
  161. painter.setPen(self.fColorBaseAlt)
  162. painter.drawLine(2, lsmall - (lsmall * 0.25), lfull-2.0, lsmall - (lsmall * 0.25))
  163. painter.drawLine(2, lsmall - (lsmall * 0.50), lfull-2.0, lsmall - (lsmall * 0.50))
  164. # Yellow
  165. painter.setPen(QColor(110, 110, 15, 100))
  166. painter.drawLine(2, lsmall - (lsmall * 0.70), lfull-2.0, lsmall - (lsmall * 0.70))
  167. painter.drawLine(2, lsmall - (lsmall * 0.82), lfull-2.0, lsmall - (lsmall * 0.82))
  168. # Orange
  169. painter.setPen(QColor(180, 110, 15, 100))
  170. painter.drawLine(2, lsmall - (lsmall * 0.90), lfull-2.0, lsmall - (lsmall * 0.90))
  171. # Red
  172. painter.setPen(QColor(110, 15, 15, 100))
  173. painter.drawLine(2, lsmall - (lsmall * 0.96), lfull-2.0, lsmall - (lsmall * 0.96))
  174. def resizeEvent(self, event):
  175. self.updateSizes()
  176. QWidget.resizeEvent(self, event)