UI fixes regarding button states.
[demorecorder] / src / InputBin.vala
1 /*  Demo Recorder 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 pregain;
25   Gst.Element dynamics;
26   Gst.Element volume;
27   Gst.GhostPad src_pad;
28   Gst.GhostPad sink_pad;
29   
30   public InputBin(string name) {
31     this.set_name(name);
32     construct_pipeline(SettingsStructures.RecordingSettings());
33   }  
34   public InputBin.with_settings(string name, SettingsStructures.RecordingSettings settings) {
35     this.name = name;
36     construct_pipeline(settings);
37   }
38   
39   private string get_source_device(string name) {
40     switch (name) {
41       case "Microphone":
42         return "source.hw0";
43       case "Monitor":
44         return "sink.hw0.monitor";
45       case "Bluetooth":
46         return "source.hw1";
47       default:
48         return "source.hw0";
49     }
50   }
51   private string get_source_name(string device) {
52     switch (device) {
53       case "source.hw0":
54         return "Microphone";
55       case "sink.hw0.monitor":
56         return "Monitor";
57       case "source.hw1":
58         return "Bluetooth";
59       default:
60         return "Microphone";
61     }
62   }
63   
64   private void construct_pipeline(SettingsStructures.RecordingSettings settings) {
65     
66     this.src = Gst.ElementFactory.make("pulsesrc", "capture");
67     this.src.set_property("device", get_source_device(settings.source)); //"source.hw0"); /// TODO
68     this.add(this.src);
69     
70     this.converter = Gst.ElementFactory.make("audioconvert", "converter");
71     this.add(this.converter);
72     
73     this.pregain = Gst.ElementFactory.make("volume", "pregain");
74     this.pregain.set_property("volume", settings.pregain);
75     this.add(this.pregain);
76     
77     this.dynamics = Gst.ElementFactory.make("audiodynamic", "dynamics");
78     this.add(this.dynamics);
79     this.dynamics.set_property("mode", settings.dynamics.mode); // compressor or expander
80     this.dynamics.set_property("characteristics", settings.dynamics.characteristics); // hard-knee or soft-knee
81     this.dynamics.set_property("threshold", settings.dynamics.threshold); // [0, 1]
82     this.dynamics.set_property("ratio", settings.dynamics.ratio); // >= 0
83     
84     this.volume = Gst.ElementFactory.make("volume", "volume");
85     this.volume.set_property("volume", settings.volume);
86     this.add(this.volume);
87     
88     this.src.link(converter);
89     this.converter.link(pregain);
90     this.pregain.link(dynamics);
91     this.dynamics.link(volume);
92     
93     sink_pad = new Gst.GhostPad("sink", src.get_static_pad("sink"));
94     this.add_pad(sink_pad);
95     src_pad = new Gst.GhostPad("src", volume.get_static_pad("src"));
96     this.add_pad(src_pad);
97   }
98   
99   public new Gst.PadTemplate get_pad_template(string name) {
100     switch (name) {
101       case "src":
102         return this.volume.get_pad_template("src");
103       case "sink":
104         return this.src.get_pad_template("sink");
105       default:
106         return base.get_pad_template(name);
107     }
108   }
109   
110   public void set_dynamics(string mode, string characteristics, double threshold, double ratio) {
111     this.dynamics.set_property("mode", mode); // compressor or expander
112     this.dynamics.set_property("characteristics", characteristics); // hard-knee or soft-knee
113     this.dynamics.set_property("threshold", threshold); // [0, 1]
114     this.dynamics.set_property("ratio", ratio); // >= 0
115   }
116   public void set_dynamics_mode(string mode) {
117     this.dynamics.set_property("mode", mode); // compressor or expander
118   }
119   public void set_dynamics_characteristics(string characteristics) {
120     this.dynamics.set_property("characteristics", characteristics); // hard-knee or soft-knee
121   }
122   public void set_dynamics_threshold(double threshold) {
123     this.dynamics.set_property("threshold", threshold); // [0, 1]
124   }
125   public void set_dynamics_ratio(double ratio) {
126     this.dynamics.set_property("ratio", ratio); // >= 0
127   }
128   
129   public void set_source(string name) {
130     Gst.State state;
131     Gst.ClockTime time = Gst.util_get_timestamp ();
132     this.get_state (out state, null, time);
133     this.set_state(Gst.State.NULL);
134     this.src.set_property("device", get_source_device(name));
135     this.set_state(state);
136   }
137   public string get_source() {
138     GLib.Value val = 0;
139     this.src.get_property("device", ref val);
140     return get_source_name(val.get_string());
141   }
142   
143   public void set_pregain(double val)
144   requires (val >= 0.0 && val <= 10.0) {
145     this.pregain.set_property("volume", val);
146   }
147   public double get_pregain()
148   ensures (result >= 0.0 && result <= 10.0) {
149     GLib.Value val = 0;
150     this.pregain.get_property("volume", ref val);
151     return val.get_double();
152   }
153   
154   public void set_volume(double val)
155   requires (val >= 0.0 && val <= 10.0) {
156     this.volume.set_property("volume", val);
157   }
158   public double get_volume()
159   ensures (result >= 0.0 && result <= 10.0) {
160     GLib.Value val = 0;
161     this.volume.get_property("volume", ref val);
162     return val.get_double();
163   }
164   
165   public void set_mute(bool val) {
166     this.volume.set_property("mute", val);
167   }
168   public bool get_mute() {
169     GLib.Value val = 0;
170     this.volume.get_property("mute", ref val);
171     return val.get_boolean();
172   }
173   
174
175 }
176
177 }