Adding first code drop
[demorecorder] / src / TrackBin.vala
diff --git a/src/TrackBin.vala b/src/TrackBin.vala
new file mode 100644 (file)
index 0000000..9720df0
--- /dev/null
@@ -0,0 +1,268 @@
+/*  Demo Recorder for MAEMO 5
+*   Copyright (C) 2010 Dru Moore <usr@dru-id.co.uk>
+*   This program is free software; you can redistribute it and/or modify
+*   it under the terms of the GNU General Public License version 2,
+*   or (at your option) any later version, as published by the Free
+*   Software Foundation
+*
+*   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, write to the
+*   Free Software Foundation, Inc.,
+*   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+*/
+namespace IdWorks {
+
+public class TrackBin: Gst.Bin {
+
+  Gst.Element decoder;
+  Gst.Element converter;
+  Gst.Element dynamics;
+  Gst.Element equalizer;
+  Gst.Element echo;
+  Gst.Element volume;
+  Gst.Element panorama;
+  Gst.GhostPad sink_pad;
+  Gst.GhostPad src_pad;
+  
+  public TrackBin(string name) {
+    this.set_name(name);
+    
+    decoder = Gst.ElementFactory.make("uridecodebin", "decoder-" + name);
+    decoder.connect("swapped-object-signal::autoplug-continue", autoplug_continue, this);
+    decoder.connect("swapped-object-signal::drained", drained, this);
+    this.add(decoder);
+    
+    converter = Gst.ElementFactory.make("audioconvert", "converter-" + name);
+    this.add(converter);
+    
+    dynamics = Gst.ElementFactory.make("audiodynamic", "dynamics");
+    dynamics.set_property("mode", "compressor"); // compression
+    dynamics.set_property("characteristics", "soft-knee"); // compression
+    dynamics.set_property("threshold", 1); // shouldn't kick in
+    dynamics.set_property("ratio", 100); // shouldn't be applied
+    this.add(dynamics);
+    
+    equalizer = Gst.ElementFactory.make("equalizer-10bands", "equalizer-" + name);
+    this.add(equalizer);
+    
+    echo = Gst.ElementFactory.make("audioecho", "echo");
+    echo.set_property("delay", 0);
+    echo.set_property("feedback", 0);
+    echo.set_property("intensity", 0);
+    echo.set_property("max-delay", 1 * Time.Nanoseconds.SECOND);
+    this.add(echo);
+    
+    volume = Gst.ElementFactory.make("volume", "volume-" + name);
+    volume.set_property("volume", 1);
+    this.add(volume);
+    
+    panorama = Gst.ElementFactory.make("audiopanorama", "panorama-" + name);
+    panorama.set_property("panorama", 0);
+    this.add(panorama);
+    
+    converter.link(dynamics);
+    dynamics.link(equalizer);
+    equalizer.link(echo);
+    echo.link(panorama);
+    panorama.link(volume);
+    
+    sink_pad = new Gst.GhostPad("sink", decoder.get_static_pad("sink"));
+    this.add_pad(sink_pad);
+    src_pad = new Gst.GhostPad("src", volume.get_static_pad("src"));
+    this.add_pad(src_pad);
+    
+  }
+  /*
+  public void volume_updated_callback(TrackTransport sender, double volume) {
+    this.set_volume(volume);
+  }
+  
+  public void panorama_updated_callback(TrackTransport sender, double panorama) {
+    this.set_panorama(panorama);
+  }
+  
+  public void eq_updated_callback(TrackTransport sender, int band, double val) {
+    this.set_eq(band, val);
+  }
+  */
+  private bool autoplug_continue(Gst.Pad decodebin, Gst.Caps arg1, void* data) {
+    Gst.PadLinkReturn ret = decodebin.link(converter.get_static_pad("sink"));
+    if (Gst.PadLinkReturn.OK != ret) {
+      //stdout.printf("link failed: %s %s\n", this.get_name(), ret.to_string());
+      //stdout.flush();
+    }
+    return true;
+  }
+  
+  private void drained(Gst.Pad decodebin, void* data) {
+  }
+  
+  public new Gst.PadTemplate get_pad_template(string name) {
+    switch (name) {
+      case "src":
+        return this.volume.get_pad_template("src");
+      case "sink":
+        return this.decoder.get_pad_template("sink");
+      default:
+        return this.get_pad_template(name);
+    }
+  }
+  
+  public void play() {
+    this.set_state(Gst.State.PLAYING);
+  }
+  
+  public void pause() {
+    this.set_state(Gst.State.PAUSED);
+  }
+  
+  public void stop() {
+    this.set_state(Gst.State.READY);
+  }
+  
+  public void set_uri(string uri) {
+    this.set_state(Gst.State.NULL);
+    decoder.set_property("uri", uri);
+  }
+  public string get_uri() {
+    GLib.Value ret = 0.0;
+    decoder.get_property("uri", ref ret);
+    return ret.get_string();
+  }
+  
+  public void set_volume(double val)
+  requires (val >= 0.0 && val <= 10.0) {
+    volume.set_property("volume", val);
+  }
+  public double get_volume() {
+    GLib.Value ret = 0.0;
+    volume.get_property("volume", ref ret);
+    return ret.get_double();
+  }
+  
+  public void set_panorama(double val) 
+  requires (val >= -1 && val <= 1) {
+    panorama.set_property("panorama", val);
+  }
+  public double get_panorama() {
+    GLib.Value ret = 0.0;
+    panorama.get_property("panorama", ref ret);
+    return ret.get_double();
+  }
+  
+  public void set_eq(int band, double val)
+  requires (band > -1 && band < 10)
+  requires (val >= -24 && val <= 12) {
+    equalizer.set_property("band" + band.to_string(), val);
+  }
+  public double get_eq(int band) {
+    GLib.Value ret = 0.0;
+    equalizer.get_property("band" + band.to_string(), ref ret);
+    return ret.get_double();
+  }
+  
+  public void set_echo_delay(uint64 val) {
+    echo.set_property("delay", val);
+  }
+  public uint64 get_echo_delay() {
+    GLib.Value ret = 0.0;
+    echo.get_property("delay", ref ret);
+    return ret.get_uint64();
+  }
+  
+  public void set_echo_max_delay(uint64 val) {
+    echo.set_property("max-delay", val);
+  }
+  public uint64 get_echo_max_delay() {
+    GLib.Value ret = 0.0;
+    echo.get_property("max-delay", ref ret);
+    return ret.get_uint64();
+  }
+  
+  public void set_echo_feedback(double val) {
+    echo.set_property("feedback", val);
+  }
+  public double get_echo_feedback() {
+    GLib.Value ret = 0.0;
+    echo.get_property("feedback", ref ret);
+    return ret.get_double();
+  }
+  
+  public void set_echo_intensity(double val) {
+    echo.set_property("intensity", val);
+  }
+  public double get_echo_intensity() {
+    GLib.Value ret = 0.0;
+    echo.get_property("intensity", ref ret);
+    return ret.get_double();
+  }
+  
+  public void set_echo_active(bool val) {
+    if (val) {}
+    else {
+      set_echo_delay(0);
+      set_echo_feedback(0);
+      set_echo_intensity(0);
+    }
+  }
+  public bool get_echo_active() {
+    return true;
+  }
+  
+  public void set_dynamics_mode(string val) {
+    dynamics.set_property("mode", val);
+  }
+  public string get_dynamics_mode() {
+    GLib.Value ret = 0.0;
+    dynamics.get_property("mode", ref ret);
+    return ret.get_string();
+  }
+  
+  public void set_dynamics_characteristics(string val) {
+    dynamics.set_property("characteristics", val);
+  }
+  public string get_dynamics_characteristics() {
+    GLib.Value ret = 0.0;
+    dynamics.get_property("characteristics", ref ret);
+    return ret.get_string();
+  }
+  
+  public void set_dynamics_threshold(double val) {
+    dynamics.set_property("threshold", val);
+  }
+  public double get_dynamics_threshold() {
+    GLib.Value ret = 0.0;
+    dynamics.get_property("threshold", ref ret);
+    return ret.get_double();
+  }
+  
+  public void set_dynamics_ratio(double val) {
+    dynamics.set_property("ratio", val);
+  }
+  public double get_dynamics_ratio() {
+    GLib.Value ret = 0.0;
+    dynamics.get_property("ratio", ref ret);
+    return ret.get_double();
+  }
+  
+  public void set_dynamics_active(bool val) {
+    if (val) {}
+    else {
+      set_dynamics_mode("compressor");
+      set_dynamics_characteristics("soft-knee");
+      set_dynamics_ratio(1);
+      set_dynamics_threshold(0);
+    }
+  }
+  public bool get_dynamics_active() {
+    return true;
+  }
+  
+}
+
+}
\ No newline at end of file