summaryrefslogtreecommitdiffstats
path: root/pulsecaster/source.py
blob: 94558350d66f817237786207eeb13e51234b1026 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Copyright (C) 2011 Paul W. Frields and others.
# -*- coding: utf-8 -*-
#
# This program 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.
#
# This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# Author: Paul W. Frields <stickster@gmail.com>

from config import *
from gi.repository import Gtk, GObject, Gst
from pulseaudio.PulseObj import PulseObj

class PulseCasterSource:
    '''A source object that provides sound data for PulseCaster'''
    def __init__(self):
        '''Construct the source object'''
        # Should include a PA source, a GtkCombBox, and a GtkProgressBar
        self.store = Gtk.ListStore(GObject.TYPE_STRING,
                                   GObject.TYPE_STRING,
                                   GObject.TYPE_PYOBJECT)
        self.bus = None
        self.cbox = Gtk.ComboBox.new_with_model(self.store)
        self.cell = Gtk.CellRendererText()
        self.cbox.pack_start(self.cell, True)
        self.cbox.add_attribute(self.cell, 'text', True)
        self.cbox.connect('changed', self.set_meters)
        self.pbar = Gtk.ProgressBar()
        self.pipeline = None
        print 'out of __init__'
        
    def repopulate(self, pa, use_source=True, use_monitor=True):
        '''Repopulate the ComboBox for this object'''
        print 'in repopulate'
        sources = pa.pulse_source_list()
        self.store.clear()
        for source in sources:
            if source.monitor_of_sink_name == None:
                if use_source == True:
                    self.store.append([source.name,
                                       source.description,
                                       source])
            else:
                if use_monitor == True:
                    self.store.append([source.name,
                                       source.description,
                                       source])
        # Don't leave without resetting a source
        self.cbox.set_active(0)
        print 'out of repopulate'

    def create_level_pipeline(self, *args):
        '''Make a GStreamer pipeline that allows level checking'''
        print 'in create_level_pipeline'
        self.pipeline = Gst.Pipeline()
        self.src = Gst.ElementFactory.make('pulsesrc', None)
        self.lvl = Gst.ElementFactory.make('level', None)
        self.lvl.set_property('message', True)
        self.snk = Gst.ElementFactory.make('fakesink', None)
        for e in (self.src, self.lvl, self.snk):
            self.pipeline.add(e)
        self.bus = self.pipeline.get_bus()
        self.bus.add_signal_watch()
        self.conn = self.bus.connect('message::element', self.update_level)
        self.pipeline.set_state(Gst.State.PLAYING)
        print 'out of create_level_pipeline'
    
    def remove_level_pipeline(self, *args):
        '''Tear down the GStreamer pipeline attached to this object'''
        print 'in remove_level_pipeline'
        self.pipeline.set_state(Gst.State.NULL)
        self.bus.remove_signal_watch()
        self.bus.disconnect(self.conn)
        self.conn = None
        self.pipeline = None
        print 'out of remove_level_pipeline'
    
    def set_meters(self, *args):
        print 'in set_meters'
        self.cbox.set_sensitive(False)
        if self.pipeline is not None:
            self.remove_level_pipeline()
        i = self.cbox.get_active_iter()
        if i is not None:
            self.pulsesrc = self.cbox.get_model().get_value(i, 0)
            self.create_level_pipeline()
        self.cbox.set_sensitive(True)
        print 'out of set_meters'
        
    def update_level(self, bus, message, *args):
        '''Update this object's GtkProgressBar to reflect current level'''
        structure = message.get_structure()
        if structure.get_name() == 'level':
            # stick with left channel in stereo setups
            peak = structure.get_value('peak')[0]
            self.pbar.set_fraction(self.iec_scale(peak)/100)
            self.pbar.queue_draw()
        return True
    
    def iec_scale(self, db):
        '''For a given dB value, return the iEC-268-18 standard value''' 
        pct = 0.0
        if db < -70.0:
            pct = 0.0
        elif db < -60.0:
            pct = (db + 70.0) * 0.25
        elif db < -50.0:
            pct = (db + 60.0) * 0.5 + 2.5
        elif db < -40.0:
            pct = (db + 50.0) * 0.75 + 7.5
        elif db < -30.0:
            pct = (db + 40.0) * 1.5 + 15.0
        elif db < -20.0:
            pct = (db + 30.0) * 2.0 + 30.0
        elif db < 0.0:
            pct = (db + 20.0) * 2.5 + 50.0
        else:
            pct = 100.0
        return pct