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.

234 lines
8.0KB

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Digital Peak Meter, a custom Qt4 widget
  4. # Copyright (C) 2011-2012 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. # Imports (Global)
  18. from PyQt4.QtCore import qCritical, Qt, QTimer, QSize
  19. from PyQt4.QtGui import QColor, QLinearGradient, QPainter, QWidget
  20. # Widget Class
  21. class DigitalPeakMeter(QWidget):
  22. # enum Orientation
  23. HORIZONTAL = 1
  24. VERTICAL = 2
  25. # enum Color
  26. GREEN = 1
  27. BLUE = 2
  28. def __init__(self, parent):
  29. QWidget.__init__(self, parent)
  30. self.m_channels = 0
  31. self.m_orientation = self.VERTICAL
  32. self.m_smoothMultiplier = 1
  33. self.m_colorBackground = QColor("#111111")
  34. self.m_gradientMeter = QLinearGradient(0, 0, 1, 1)
  35. self.setChannels(0)
  36. self.setColor(self.GREEN)
  37. self.m_paintTimer = QTimer(self)
  38. self.m_paintTimer.setInterval(60)
  39. self.m_paintTimer.timeout.connect(self.update)
  40. self.m_paintTimer.start()
  41. def displayMeter(self, meter, level):
  42. if meter <= 0 or meter > self.m_channels:
  43. return qCritical("DigitalPeakMeter::displayMeter(%i, %f) - invalid meter number" % (meter, level))
  44. if level < 0.0:
  45. level = -level
  46. elif level > 1.0:
  47. level = 1.0
  48. self.m_channelsData[meter-1] = level
  49. def setChannels(self, channels):
  50. if (channels < 0):
  51. return qCritical("DigitalPeakMeter::setChannels(%i) - 'channels' must be a positive integer" % channels)
  52. self.m_channels = channels
  53. self.m_channelsData = []
  54. self.m_lastValueData = []
  55. for x in range(channels):
  56. self.m_channelsData.append(0.0)
  57. self.m_lastValueData.append(0.0)
  58. def setColor(self, color):
  59. if color == self.GREEN:
  60. self.m_colorBase = QColor(93, 231, 61)
  61. self.m_colorBaseT = QColor(15, 110, 15, 100)
  62. elif color == self.BLUE:
  63. self.m_colorBase = QColor(82, 238, 248)
  64. self.m_colorBaseT = QColor(15, 15, 110, 100)
  65. else:
  66. return qCritical("DigitalPeakMeter::setColor(%i) - invalid color" % color)
  67. self.setOrientation(self.m_orientation)
  68. def setOrientation(self, orientation):
  69. self.m_orientation = orientation
  70. if self.m_orientation == self.HORIZONTAL:
  71. self.m_gradientMeter.setColorAt(0.0, self.m_colorBase)
  72. self.m_gradientMeter.setColorAt(0.2, self.m_colorBase)
  73. self.m_gradientMeter.setColorAt(0.4, self.m_colorBase)
  74. self.m_gradientMeter.setColorAt(0.6, self.m_colorBase)
  75. self.m_gradientMeter.setColorAt(0.8, Qt.yellow)
  76. self.m_gradientMeter.setColorAt(1.0, Qt.red)
  77. elif self.m_orientation == self.VERTICAL:
  78. self.m_gradientMeter.setColorAt(0.0, Qt.red)
  79. self.m_gradientMeter.setColorAt(0.2, Qt.yellow)
  80. self.m_gradientMeter.setColorAt(0.4, self.m_colorBase)
  81. self.m_gradientMeter.setColorAt(0.6, self.m_colorBase)
  82. self.m_gradientMeter.setColorAt(0.8, self.m_colorBase)
  83. self.m_gradientMeter.setColorAt(1.0, self.m_colorBase)
  84. else:
  85. return qCritical("DigitalPeakMeter::setOrientation(%i) - invalid orientation" % orientation)
  86. self.updateSizes()
  87. def setRefreshRate(self, rate):
  88. self.m_paintTimer.stop()
  89. self.m_paintTimer.setInterval(rate)
  90. self.m_paintTimer.start()
  91. def setSmoothRelease(self, value):
  92. if value < 0:
  93. value = 0
  94. elif value > 5:
  95. value = 5
  96. self.m_smoothMultiplier = value
  97. def minimumSizeHint(self):
  98. return QSize(30, 30)
  99. def sizeHint(self):
  100. return QSize(self.m_width, self.m_height)
  101. def updateSizes(self):
  102. self.m_width = self.width()
  103. self.m_height = self.height()
  104. self.m_sizeMeter = 0
  105. if self.m_orientation == self.HORIZONTAL:
  106. self.m_gradientMeter.setFinalStop(self.m_width, 0)
  107. if self.m_channels > 0:
  108. self.m_sizeMeter = self.m_height / self.m_channels
  109. elif self.m_orientation == self.VERTICAL:
  110. self.m_gradientMeter.setFinalStop(0, self.m_height)
  111. if self.m_channels > 0:
  112. self.m_sizeMeter = self.m_width / self.m_channels
  113. def paintEvent(self, event):
  114. painter = QPainter(self)
  115. painter.setPen(Qt.black)
  116. painter.setBrush(Qt.black)
  117. painter.drawRect(0, 0, self.m_width, self.m_height)
  118. meterX = 0
  119. painter.setPen(self.m_colorBackground)
  120. painter.setBrush(self.m_gradientMeter)
  121. for i in range(self.m_channels):
  122. level = self.m_channelsData[i]
  123. if level == self.m_lastValueData[i]:
  124. continue
  125. if self.m_orientation == self.HORIZONTAL:
  126. value = level * self.m_width
  127. elif self.m_orientation == self.VERTICAL:
  128. value = float(self.m_height) - (level * self.m_height)
  129. else:
  130. value = 0.0
  131. if value < 0.0:
  132. value = 0.0
  133. elif self.m_smoothMultiplier > 0:
  134. value = (self.m_lastValueData[i] * self.m_smoothMultiplier + value) / (self.m_smoothMultiplier + 1)
  135. if self.m_orientation == self.HORIZONTAL:
  136. painter.drawRect(0, meterX, value, self.m_sizeMeter)
  137. elif self.m_orientation == self.VERTICAL:
  138. painter.drawRect(meterX, value, self.m_sizeMeter, self.m_height)
  139. meterX += self.m_sizeMeter
  140. self.m_lastValueData[i] = value
  141. painter.setBrush(QColor(0, 0, 0, 0))
  142. if self.m_orientation == self.HORIZONTAL:
  143. # Variables
  144. lsmall = self.m_width
  145. lfull = self.m_height - 1
  146. # Base
  147. painter.setPen(self.m_colorBaseT)
  148. painter.drawLine(lsmall * 0.25, 2, lsmall * 0.25, lfull-2)
  149. painter.drawLine(lsmall * 0.50, 2, lsmall * 0.50, lfull-2)
  150. # Yellow
  151. painter.setPen(QColor(110, 110, 15, 100))
  152. painter.drawLine(lsmall * 0.70, 2, lsmall * 0.70, lfull-2)
  153. painter.drawLine(lsmall * 0.83, 2, lsmall * 0.83, lfull-2)
  154. # Orange
  155. painter.setPen(QColor(180, 110, 15, 100))
  156. painter.drawLine(lsmall * 0.90, 2, lsmall * 0.90, lfull-2)
  157. # Red
  158. painter.setPen(QColor(110, 15, 15, 100))
  159. painter.drawLine(lsmall * 0.96, 2, lsmall * 0.96, lfull-2)
  160. elif self.m_orientation == self.VERTICAL:
  161. # Variables
  162. lsmall = self.m_height
  163. lfull = self.m_width - 1
  164. # Base
  165. painter.setPen(self.m_colorBaseT)
  166. painter.drawLine(2, lsmall - (lsmall * 0.25), lfull-2, lsmall - (lsmall * 0.25))
  167. painter.drawLine(2, lsmall - (lsmall * 0.50), lfull-2, lsmall - (lsmall * 0.50))
  168. # Yellow
  169. painter.setPen(QColor(110, 110, 15, 100))
  170. painter.drawLine(2, lsmall - (lsmall * 0.70), lfull-2, lsmall - (lsmall * 0.70))
  171. painter.drawLine(2, lsmall - (lsmall * 0.82), lfull-2, lsmall - (lsmall * 0.82))
  172. # Orange
  173. painter.setPen(QColor(180, 110, 15, 100))
  174. painter.drawLine(2, lsmall - (lsmall * 0.90), lfull-2, lsmall - (lsmall * 0.90))
  175. # Red
  176. painter.setPen(QColor(110, 15, 15, 100))
  177. painter.drawLine(2, lsmall - (lsmall * 0.96), lfull-2, lsmall - (lsmall * 0.96))
  178. def resizeEvent(self, event):
  179. self.updateSizes()
  180. QWidget.resizeEvent(self, event)