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.

240 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 modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # 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 COPYING 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.m_channels = 0
  33. self.m_orientation = self.VERTICAL
  34. self.m_smoothMultiplier = 1
  35. self.m_colorBackground = QColor("#111111")
  36. self.m_gradientMeter = QLinearGradient(0, 0, 1, 1)
  37. self.setChannels(0)
  38. self.setColor(self.GREEN)
  39. self.m_paintTimer = QTimer(self)
  40. self.m_paintTimer.setInterval(60)
  41. self.m_paintTimer.timeout.connect(self.update)
  42. self.m_paintTimer.start()
  43. def displayMeter(self, meter, level):
  44. if meter <= 0 or meter > self.m_channels:
  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.m_channelsData[meter-1] = 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.m_channels = channels
  55. self.m_channelsData = []
  56. self.m_lastValueData = []
  57. for x in range(channels):
  58. self.m_channelsData.append(0.0)
  59. self.m_lastValueData.append(0.0)
  60. def setColor(self, color):
  61. if color == self.GREEN:
  62. self.m_colorBase = QColor(93, 231, 61)
  63. self.m_colorBaseT = QColor(15, 110, 15, 100)
  64. elif color == self.BLUE:
  65. self.m_colorBase = QColor(82, 238, 248)
  66. self.m_colorBaseT = QColor(15, 15, 110, 100)
  67. else:
  68. return qCritical("DigitalPeakMeter::setColor(%i) - invalid color" % color)
  69. self.setOrientation(self.m_orientation)
  70. def setOrientation(self, orientation):
  71. self.m_orientation = orientation
  72. if self.m_orientation == self.HORIZONTAL:
  73. self.m_gradientMeter.setColorAt(0.0, self.m_colorBase)
  74. self.m_gradientMeter.setColorAt(0.2, self.m_colorBase)
  75. self.m_gradientMeter.setColorAt(0.4, self.m_colorBase)
  76. self.m_gradientMeter.setColorAt(0.6, self.m_colorBase)
  77. self.m_gradientMeter.setColorAt(0.8, Qt.yellow)
  78. self.m_gradientMeter.setColorAt(1.0, Qt.red)
  79. elif self.m_orientation == self.VERTICAL:
  80. self.m_gradientMeter.setColorAt(0.0, Qt.red)
  81. self.m_gradientMeter.setColorAt(0.2, Qt.yellow)
  82. self.m_gradientMeter.setColorAt(0.4, self.m_colorBase)
  83. self.m_gradientMeter.setColorAt(0.6, self.m_colorBase)
  84. self.m_gradientMeter.setColorAt(0.8, self.m_colorBase)
  85. self.m_gradientMeter.setColorAt(1.0, self.m_colorBase)
  86. else:
  87. return qCritical("DigitalPeakMeter::setOrientation(%i) - invalid orientation" % orientation)
  88. self.updateSizes()
  89. def setRefreshRate(self, rate):
  90. self.m_paintTimer.stop()
  91. self.m_paintTimer.setInterval(rate)
  92. self.m_paintTimer.start()
  93. def setSmoothRelease(self, value):
  94. if value < 0:
  95. value = 0
  96. elif value > 5:
  97. value = 5
  98. self.m_smoothMultiplier = value
  99. def minimumSizeHint(self):
  100. return QSize(30, 30)
  101. def sizeHint(self):
  102. return QSize(self.m_width, self.m_height)
  103. def updateSizes(self):
  104. self.m_width = self.width()
  105. self.m_height = self.height()
  106. self.m_sizeMeter = 0
  107. if self.m_orientation == self.HORIZONTAL:
  108. self.m_gradientMeter.setFinalStop(self.m_width, 0)
  109. if self.m_channels > 0:
  110. self.m_sizeMeter = self.m_height / self.m_channels
  111. elif self.m_orientation == self.VERTICAL:
  112. self.m_gradientMeter.setFinalStop(0, self.m_height)
  113. if self.m_channels > 0:
  114. self.m_sizeMeter = self.m_width / self.m_channels
  115. def paintEvent(self, event):
  116. painter = QPainter(self)
  117. painter.setPen(Qt.black)
  118. painter.setBrush(Qt.black)
  119. painter.drawRect(0, 0, self.m_width, self.m_height)
  120. meterX = 0
  121. painter.setPen(self.m_colorBackground)
  122. painter.setBrush(self.m_gradientMeter)
  123. for i in range(self.m_channels):
  124. level = self.m_channelsData[i]
  125. if level == self.m_lastValueData[i]:
  126. continue
  127. if self.m_orientation == self.HORIZONTAL:
  128. value = level * self.m_width
  129. elif self.m_orientation == self.VERTICAL:
  130. value = float(self.m_height) - (level * self.m_height)
  131. else:
  132. value = 0.0
  133. if value < 0.0:
  134. value = 0.0
  135. elif self.m_smoothMultiplier > 0:
  136. value = (self.m_lastValueData[i] * self.m_smoothMultiplier + value) / (self.m_smoothMultiplier + 1)
  137. if self.m_orientation == self.HORIZONTAL:
  138. painter.drawRect(0, meterX, value, self.m_sizeMeter)
  139. elif self.m_orientation == self.VERTICAL:
  140. painter.drawRect(meterX, value, self.m_sizeMeter, self.m_height)
  141. meterX += self.m_sizeMeter
  142. self.m_lastValueData[i] = value
  143. painter.setBrush(QColor(0, 0, 0, 0))
  144. if self.m_orientation == self.HORIZONTAL:
  145. # Variables
  146. lsmall = self.m_width
  147. lfull = self.m_height - 1
  148. # Base
  149. painter.setPen(self.m_colorBaseT)
  150. painter.drawLine(lsmall * 0.25, 2, lsmall * 0.25, lfull-2)
  151. painter.drawLine(lsmall * 0.50, 2, lsmall * 0.50, lfull-2)
  152. # Yellow
  153. painter.setPen(QColor(110, 110, 15, 100))
  154. painter.drawLine(lsmall * 0.70, 2, lsmall * 0.70, lfull-2)
  155. painter.drawLine(lsmall * 0.83, 2, lsmall * 0.83, lfull-2)
  156. # Orange
  157. painter.setPen(QColor(180, 110, 15, 100))
  158. painter.drawLine(lsmall * 0.90, 2, lsmall * 0.90, lfull-2)
  159. # Red
  160. painter.setPen(QColor(110, 15, 15, 100))
  161. painter.drawLine(lsmall * 0.96, 2, lsmall * 0.96, lfull-2)
  162. elif self.m_orientation == self.VERTICAL:
  163. # Variables
  164. lsmall = self.m_height
  165. lfull = self.m_width - 1
  166. # Base
  167. painter.setPen(self.m_colorBaseT)
  168. painter.drawLine(2, lsmall - (lsmall * 0.25), lfull-2, lsmall - (lsmall * 0.25))
  169. painter.drawLine(2, lsmall - (lsmall * 0.50), lfull-2, lsmall - (lsmall * 0.50))
  170. # Yellow
  171. painter.setPen(QColor(110, 110, 15, 100))
  172. painter.drawLine(2, lsmall - (lsmall * 0.70), lfull-2, lsmall - (lsmall * 0.70))
  173. painter.drawLine(2, lsmall - (lsmall * 0.82), lfull-2, lsmall - (lsmall * 0.82))
  174. # Orange
  175. painter.setPen(QColor(180, 110, 15, 100))
  176. painter.drawLine(2, lsmall - (lsmall * 0.90), lfull-2, lsmall - (lsmall * 0.90))
  177. # Red
  178. painter.setPen(QColor(110, 15, 15, 100))
  179. painter.drawLine(2, lsmall - (lsmall * 0.96), lfull-2, lsmall - (lsmall * 0.96))
  180. event.accept()
  181. def resizeEvent(self, event):
  182. self.updateSizes()
  183. QWidget.resizeEvent(self, event)