Collection of tools useful for audio production
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.

232 lines
7.5KB

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