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 10KB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Digital Peak Meter, a custom Qt4 widget
  4. # Copyright (C) 2011-2014 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 (Config)
  19. from carla_config import *
  20. # ------------------------------------------------------------------------------------------------------------
  21. # Imports (Global)
  22. if config_UseQt5:
  23. from PyQt5.QtCore import qCritical, Qt, QTimer, QSize
  24. from PyQt5.QtGui import QColor, QLinearGradient, QPainter
  25. from PyQt5.QtWidgets import QWidget
  26. else:
  27. from PyQt4.QtCore import qCritical, Qt, QTimer, QSize
  28. from PyQt4.QtGui import QColor, QLinearGradient, QPainter, QWidget
  29. # ------------------------------------------------------------------------------------------------------------
  30. # Widget Class
  31. class DigitalPeakMeter(QWidget):
  32. # enum Orientation
  33. HORIZONTAL = 1
  34. VERTICAL = 2
  35. # enum Color
  36. GREEN = 1
  37. BLUE = 2
  38. # enum Style
  39. STYLE_DEFAULT = 0
  40. STYLE_OPENAV = 1
  41. def __init__(self, parent):
  42. QWidget.__init__(self, parent)
  43. self.fChannels = 0
  44. self.fDrawLines = True
  45. self.fOrientation = self.VERTICAL
  46. self.fSmoothMultiplier = 1
  47. self.fColorBackground = QColor("#111111")
  48. self.fGradientMeter = QLinearGradient(0, 0, 1, 1)
  49. self.fMeterStyle = self.STYLE_DEFAULT
  50. self.setChannels(0)
  51. self.setColor(self.GREEN)
  52. def displayMeter(self, meter, level, forced = False):
  53. if meter <= 0 or meter > self.fChannels:
  54. return qCritical("DigitalPeakMeter::displayMeter(%i, %f) - invalid meter number" % (meter, level))
  55. if not isinstance(level, float):
  56. return qCritical("DigitalPeakMeter::displayMeter(%i, %f) - meter value must be float" % (meter, level))
  57. i = meter - 1
  58. if self.fSmoothMultiplier > 0 and not forced:
  59. level = (self.fLastValueData[i] * self.fSmoothMultiplier + level) / float(self.fSmoothMultiplier + 1)
  60. if level < 0.001:
  61. level = 0.0
  62. elif level > 0.999:
  63. level = 1.0
  64. if self.fChannelsData[i] != level:
  65. self.fChannelsData[i] = level
  66. self.update()
  67. self.fLastValueData[i] = level
  68. def setChannels(self, channels):
  69. if channels < 0:
  70. return qCritical("DigitalPeakMeter::setChannels(%i) - 'channels' must be a positive integer" % channels)
  71. self.fChannels = channels
  72. self.fChannelsData = []
  73. self.fLastValueData = []
  74. for x in range(channels):
  75. self.fChannelsData.append(0.0)
  76. self.fLastValueData.append(0.0)
  77. def setColor(self, color):
  78. if color == self.GREEN:
  79. self.fColorBase = QColor(93, 231, 61)
  80. self.fColorBaseAlt = QColor(15, 110, 15, 100)
  81. elif color == self.BLUE:
  82. self.fColorBase = QColor(82, 238, 248)
  83. self.fColorBaseAlt = QColor(15, 15, 110, 100)
  84. else:
  85. return qCritical("DigitalPeakMeter::setColor(%i) - invalid color" % color)
  86. self.setOrientation(self.fOrientation)
  87. def setMeterStyle(self, style):
  88. self.fMeterStyle = style
  89. self.setOrientation(self.fOrientation)
  90. def setLinesEnabled(self, yesNo):
  91. self.fDrawLines = yesNo
  92. def setOrientation(self, orientation):
  93. self.fOrientation = orientation
  94. self.fGradientMeter = QLinearGradient(0, 0, 1, 1)
  95. if self.fMeterStyle == self.STYLE_OPENAV:
  96. self.fGradientMeter.setColorAt(0.0, self.fColorBase)
  97. self.fGradientMeter.setColorAt(1.0, self.fColorBase)
  98. elif self.fOrientation == self.HORIZONTAL:
  99. self.fGradientMeter.setColorAt(0.0, self.fColorBase)
  100. self.fGradientMeter.setColorAt(0.2, self.fColorBase)
  101. self.fGradientMeter.setColorAt(0.4, self.fColorBase)
  102. self.fGradientMeter.setColorAt(0.6, self.fColorBase)
  103. self.fGradientMeter.setColorAt(0.8, Qt.yellow)
  104. self.fGradientMeter.setColorAt(1.0, Qt.red)
  105. elif self.fOrientation == self.VERTICAL:
  106. self.fGradientMeter.setColorAt(0.0, Qt.red)
  107. self.fGradientMeter.setColorAt(0.2, Qt.yellow)
  108. self.fGradientMeter.setColorAt(0.4, self.fColorBase)
  109. self.fGradientMeter.setColorAt(0.6, self.fColorBase)
  110. self.fGradientMeter.setColorAt(0.8, self.fColorBase)
  111. self.fGradientMeter.setColorAt(1.0, self.fColorBase)
  112. else:
  113. return qCritical("DigitalPeakMeter::setOrientation(%i) - invalid orientation" % orientation)
  114. self.updateSizes()
  115. def setSmoothRelease(self, value):
  116. if value < 0:
  117. value = 0
  118. elif value > 5:
  119. value = 5
  120. self.fSmoothMultiplier = value
  121. def minimumSizeHint(self):
  122. return QSize(10, 10)
  123. def sizeHint(self):
  124. return QSize(self.fWidth, self.fHeight)
  125. def updateSizes(self):
  126. self.fWidth = self.width()
  127. self.fHeight = self.height()
  128. self.fSizeMeter = 0
  129. if self.fOrientation == self.HORIZONTAL:
  130. self.fGradientMeter.setFinalStop(self.fWidth, 0)
  131. if self.fChannels > 0:
  132. self.fSizeMeter = self.fHeight / self.fChannels
  133. elif self.fOrientation == self.VERTICAL:
  134. self.fGradientMeter.setFinalStop(0, self.fHeight)
  135. if self.fChannels > 0:
  136. self.fSizeMeter = self.fWidth / self.fChannels
  137. def paintEvent(self, event):
  138. painter = QPainter(self)
  139. event.accept()
  140. if self.fMeterStyle == self.STYLE_OPENAV:
  141. painter.setPen(QColor("#1A1A1A"))
  142. painter.setBrush(QColor("#1A1A1A"))
  143. else:
  144. painter.setPen(Qt.black)
  145. painter.setBrush(Qt.black)
  146. painter.drawRect(0, 0, self.fWidth, self.fHeight)
  147. meterX = 0
  148. startX = -1 if self.fMeterStyle == self.STYLE_OPENAV else 0
  149. padding = 2 if self.fMeterStyle == self.STYLE_OPENAV else 0
  150. painter.setPen(self.fColorBackground)
  151. painter.setBrush(self.fGradientMeter)
  152. if self.fMeterStyle == self.STYLE_OPENAV:
  153. color = self.fGradientMeter.stops()[0][1]
  154. painter.setPen(color)
  155. color.setAlphaF(0.5)
  156. painter.setBrush(color)
  157. del color
  158. for i in range(self.fChannels):
  159. level = self.fChannelsData[i]
  160. if self.fOrientation == self.HORIZONTAL:
  161. value = level * float(self.fWidth)
  162. elif self.fOrientation == self.VERTICAL:
  163. value = float(self.fHeight) - (level * float(self.fHeight))
  164. else:
  165. value = 0.0
  166. if self.fOrientation == self.HORIZONTAL:
  167. painter.drawRect(startX, meterX+padding, int(value), self.fSizeMeter-padding*(1 if self.fChannels > 1 else 2))
  168. elif self.fOrientation == self.VERTICAL:
  169. painter.drawRect(meterX, int(value), self.fSizeMeter, self.fHeight)
  170. meterX += self.fSizeMeter
  171. if not self.fDrawLines:
  172. return
  173. painter.setBrush(Qt.black)
  174. if self.fOrientation == self.HORIZONTAL:
  175. # Variables
  176. lsmall = float(self.fWidth)
  177. lfull = float(self.fHeight - 1)
  178. if self.fMeterStyle == self.STYLE_OPENAV:
  179. painter.setPen(QColor(37, 37, 37, 100))
  180. painter.drawLine(lsmall * 0.25, 2, lsmall * 0.25, lfull-2.0)
  181. painter.drawLine(lsmall * 0.50, 2, lsmall * 0.50, lfull-2.0)
  182. painter.drawLine(lsmall * 0.75, 2, lsmall * 0.75, lfull-2.0)
  183. if self.fChannels > 1:
  184. painter.drawLine(1, lfull/2, lsmall-1, lfull/2)
  185. else:
  186. # Base
  187. painter.setPen(self.fColorBaseAlt)
  188. painter.drawLine(lsmall * 0.25, 2, lsmall * 0.25, lfull-2.0)
  189. painter.drawLine(lsmall * 0.50, 2, lsmall * 0.50, lfull-2.0)
  190. # Yellow
  191. painter.setPen(QColor(110, 110, 15, 100))
  192. painter.drawLine(lsmall * 0.70, 2, lsmall * 0.70, lfull-2.0)
  193. painter.drawLine(lsmall * 0.83, 2, lsmall * 0.83, lfull-2.0)
  194. # Orange
  195. painter.setPen(QColor(180, 110, 15, 100))
  196. painter.drawLine(lsmall * 0.90, 2, lsmall * 0.90, lfull-2.0)
  197. # Red
  198. painter.setPen(QColor(110, 15, 15, 100))
  199. painter.drawLine(lsmall * 0.96, 2, lsmall * 0.96, lfull-2.0)
  200. elif self.fOrientation == self.VERTICAL:
  201. # Variables
  202. lsmall = float(self.fHeight)
  203. lfull = float(self.fWidth - 1)
  204. if self.fMeterStyle == self.STYLE_OPENAV:
  205. # TODO
  206. pass
  207. else:
  208. # Base
  209. painter.setPen(self.fColorBaseAlt)
  210. painter.drawLine(2, lsmall - (lsmall * 0.25), lfull-2.0, lsmall - (lsmall * 0.25))
  211. painter.drawLine(2, lsmall - (lsmall * 0.50), lfull-2.0, lsmall - (lsmall * 0.50))
  212. # Yellow
  213. painter.setPen(QColor(110, 110, 15, 100))
  214. painter.drawLine(2, lsmall - (lsmall * 0.70), lfull-2.0, lsmall - (lsmall * 0.70))
  215. painter.drawLine(2, lsmall - (lsmall * 0.82), lfull-2.0, lsmall - (lsmall * 0.82))
  216. # Orange
  217. painter.setPen(QColor(180, 110, 15, 100))
  218. painter.drawLine(2, lsmall - (lsmall * 0.90), lfull-2.0, lsmall - (lsmall * 0.90))
  219. # Red
  220. painter.setPen(QColor(110, 15, 15, 100))
  221. painter.drawLine(2, lsmall - (lsmall * 0.96), lfull-2.0, lsmall - (lsmall * 0.96))
  222. def resizeEvent(self, event):
  223. self.updateSizes()
  224. QWidget.resizeEvent(self, event)