File: db_meter.py

package info (click to toggle)
linux-show-player 0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,896 kB
  • sloc: python: 12,408; sh: 154; makefile: 17; xml: 8
file content (76 lines) | stat: -rw-r--r-- 2,843 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
#
# This file is part of Linux Show Player
#
# Copyright 2012-2016 Francesco Ceruti <ceppofrancy@gmail.com>
#
# Linux Show Player is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Linux Show Player is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Linux Show Player.  If not, see <http://www.gnu.org/licenses/>.

from PyQt5.QtCore import QT_TRANSLATE_NOOP

from lisp.backend.media_element import ElementType, MediaType
from lisp.core.signal import Signal
from lisp.modules.gst_backend.gi_repository import Gst
from lisp.modules.gst_backend.gst_element import GstMediaElement, GstProperty


class DbMeter(GstMediaElement):
    ElementType = ElementType.Plugin
    MediaType = MediaType.Audio
    Name = QT_TRANSLATE_NOOP('MediaElementName', 'dB Meter')

    interval = GstProperty('level', default=50 * Gst.MSECOND)
    peak_ttl = GstProperty('level', default=Gst.SECOND, gst_name='peak-ttl')
    peak_falloff = GstProperty('level', default=20, gst_name='peak-falloff')

    def __init__(self, pipeline):
        super().__init__()

        self.level_ready = Signal()

        self.pipeline = pipeline
        self.level = Gst.ElementFactory.make('level', None)
        self.level.set_property('post-messages', True)
        self.level.set_property('interval', 50 * Gst.MSECOND)
        self.level.set_property('peak-ttl', Gst.SECOND)
        self.level.set_property('peak-falloff', 20)
        self.audio_convert = Gst.ElementFactory.make('audioconvert', None)

        self.pipeline.add(self.level)
        self.pipeline.add(self.audio_convert)

        self.level.link(self.audio_convert)

        bus = self.pipeline.get_bus()
        bus.add_signal_watch()
        self._handler = bus.connect('message::element', self.__on_message)

    def dispose(self):
        bus = self.pipeline.get_bus()
        bus.remove_signal_watch()
        bus.disconnect(self._handler)

    def sink(self):
        return self.level

    def src(self):
        return self.audio_convert

    def __on_message(self, bus, message):
        if message.src == self.level:
            structure = message.get_structure()
            if structure is not None and structure.has_name('level'):
                self.level_ready.emit(structure.get_value('peak'),
                                      structure.get_value('rms'),
                                      structure.get_value('decay'))