Initial project commit
[decibelmeter] / src / InputBin.vala
1 /*  Decibel Meter for MAEMO 5
2 *   Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
3 *   This program is free software; you can redistribute it and/or modify
4 *   it under the terms of the GNU General Public License version 2,
5 *   or (at your option) any later version, as published by the Free
6 *   Software Foundation
7 *
8 *   This program is distributed in the hope that it will be useful,
9 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *   GNU General Public License for more details
12 *
13 *   You should have received a copy of the GNU General Public
14 *   License along with this program; if not, write to the
15 *   Free Software Foundation, Inc.,
16 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17 */
18 namespace IdWorks {
19
20 public class InputBin : Gst.Bin {
21   
22   Gst.Element src;
23   Gst.Element converter;
24   Gst.Element volume;
25   Gst.GhostPad src_pad;
26   Gst.GhostPad sink_pad;
27   
28   public InputBin(string name) {
29     this.set_name(name);
30     construct_pipeline("Microphone", 1.0);
31   }  
32   public InputBin.with_settings(string name, string source, double volume) {
33     this.name = name;
34     construct_pipeline(source, volume);
35   }
36   
37   private string get_source_device(string name) {
38     switch (name) {
39       case "Microphone":
40         return "source.hw0";
41       case "Monitor":
42         return "sink.hw0.monitor";
43       case "Bluetooth":
44         return "source.hw1";
45       default:
46         return "source.hw0";
47     }
48   }
49   private string get_source_name(string device) {
50     switch (device) {
51       case "source.hw0":
52         return "Microphone";
53       case "sink.hw0.monitor":
54         return "Monitor";
55       case "source.hw1":
56         return "Bluetooth";
57       default:
58         return "Microphone";
59     }
60   }
61   
62   private void construct_pipeline(string source, double volume) {
63     
64     this.src = Gst.ElementFactory.make("pulsesrc", "capture");
65     this.src.set_property("device", get_source_device(source)); //"source.hw0"); /// TODO
66     this.add(this.src);
67     
68     this.converter = Gst.ElementFactory.make("audioconvert", "converter");
69     this.add(this.converter);
70     
71     this.volume = Gst.ElementFactory.make("volume", "volume");
72     this.volume.set_property("volume", volume);
73     this.add(this.volume);
74     
75     this.src.link(converter);
76     this.converter.link(this.volume);
77     
78     this.sink_pad = new Gst.GhostPad("sink", this.src.get_static_pad("sink"));
79     this.add_pad(sink_pad);
80     this.src_pad = new Gst.GhostPad("src", this.volume.get_static_pad("src"));
81     this.add_pad(src_pad);
82   }
83   
84   public new Gst.PadTemplate get_pad_template(string name) {
85     switch (name) {
86       case "src":
87         return this.volume.get_pad_template("src");
88       case "sink":
89         return this.src.get_pad_template("sink");
90       default:
91         return base.get_pad_template(name);
92     }
93   }
94   
95   public void set_source(string name) {
96     Gst.State state;
97     Gst.ClockTime time = Gst.util_get_timestamp ();
98     this.get_state (out state, null, time);
99     this.set_state(Gst.State.NULL);
100     this.src.set_property("device", get_source_device(name));
101     this.set_state(state);
102   }
103   public string get_source() {
104     GLib.Value val = 0;
105     this.src.get_property("device", ref val);
106     return get_source_name(val.get_string());
107   }
108   
109   public void set_volume(double val)
110   requires (val >= 0.0 && val <= 10.0) {
111     this.volume.set_property("volume", val);
112   }
113   public double get_volume()
114   ensures (result >= 0.0 && result <= 10.0) {
115     GLib.Value val = 0;
116     this.volume.get_property("volume", ref val);
117     return val.get_double();
118   }
119   
120   public void set_mute(bool val) {
121     this.volume.set_property("mute", val);
122   }
123   public bool get_mute() {
124     GLib.Value val = 0;
125     this.volume.get_property("mute", ref val);
126     return val.get_boolean();
127   }
128   
129
130 }
131
132 }