summaryrefslogtreecommitdiff
path: root/voctogui/lib/audioleveldisplay.py
blob: ed62313795f2c5efde3e6c0c5f44cb3432a13253 (plain)
  1. import logging, math
  2. from gi.repository import Gst, Gtk
  3. class AudioLevelDisplay(object):
  4. """ Displays a Level-Meter of another VideoDisplay into a GtkWidget """
  5. def __init__(self, drawing_area):
  6. self.log = logging.getLogger('AudioLevelDisplay[%s]' % drawing_area.get_name())
  7. self.drawing_area = drawing_area
  8. self.levelrms = []
  9. self.levelpeak = []
  10. self.leveldecay = []
  11. self.drawing_area.connect('draw', self.on_draw)
  12. def on_draw(self, widget, cr):
  13. channels = len(self.levelrms)
  14. if channels == 0:
  15. return
  16. width = self.drawing_area.get_allocated_width()
  17. height = self.drawing_area.get_allocated_height()
  18. margin = 2 # px
  19. channel_width = int((width - (margin * (channels - 1))) / channels)
  20. # self.log.debug(
  21. # 'width: %upx filled with %u channels of each %upx '
  22. # 'and %ux margin of %upx',
  23. # width, channels, channel_width, channels-1, margin)
  24. rms_px = [ self.normalize_db(db) * height for db in self.levelrms ]
  25. peak_px = [ self.normalize_db(db) * height for db in self.levelpeak ]
  26. decay_px = [ self.normalize_db(db) * height for db in self.leveldecay ]
  27. cr.set_line_width(channel_width)
  28. for y in range(0, height):
  29. pct = y / height
  30. for channel in range(0, channels):
  31. x = (channel * channel_width) + (channel * margin)
  32. bright = 0.25
  33. if y < rms_px[channel]:
  34. bright = 1
  35. # elif abs(y - peak_px[channel]) < 3:
  36. # bright = 1.5
  37. elif y < decay_px[channel]:
  38. bright = 0.75
  39. cr.set_source_rgb(pct * bright, (1-pct) * bright, 0 * bright)
  40. cr.move_to(x, height-y)
  41. cr.line_to(x + channel_width, height-y)
  42. cr.stroke()
  43. return True
  44. def normalize_db(self, db):
  45. # -60db -> 1.00 (very quiet)
  46. # -30db -> 0.75
  47. # -15db -> 0.50
  48. # -5db -> 0.25
  49. # -0db -> 0.00 (very loud)
  50. logscale = 1 - math.log10(-0.15 * db + 1)
  51. normalized = self.clamp(logscale, 0, 1)
  52. return normalized
  53. def clamp(self, value, min_value, max_value):
  54. return max(min(value, max_value), min_value)
  55. def level_callback(self, rms, peak, decay):
  56. self.levelrms = rms
  57. self.levelpeak = peak
  58. self.leveldecay = decay
  59. self.drawing_area.queue_draw()